mini-pix 0.1.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mini_pix/__init__.py ADDED
File without changes
mini_pix/config.py ADDED
@@ -0,0 +1,107 @@
1
+ """
2
+
3
+ Kengo NAKADA:
4
+ https://github.com/shimane-dev, https://github.com/kengo-nakada
5
+ kengo.nakada@mat.shimane-u.ac.jp, kengo.nakada@gmail.com
6
+ """
7
+
8
+ import os
9
+ import yaml
10
+ from typing import Union
11
+ from pathlib import Path
12
+ import inspect
13
+
14
+
15
+ class Config:
16
+ # --- mini-pix server / client
17
+ SERVER_IP = "10.170.10.38"
18
+ SERVER_PORT = 50051
19
+ TIMEOUT = 30.0
20
+ ASYNC_MODE = True
21
+ CLIENT_LOGGER_NAME = "mini_pix_client"
22
+ SERVER_LOGGER_NAME = "mini_pix_server"
23
+ LOGGER_LEVEL = "debug"
24
+
25
+ def __init__(self, yaml_path=None):
26
+ # インスタンスで使いたいときだけ、ここで個別設定
27
+ if yaml_path:
28
+ self._load_yaml(yaml_path=yaml_path, depth=3)
29
+
30
+ @classmethod
31
+ def load_yaml(
32
+ cls,
33
+ yaml_path,
34
+ ):
35
+ """クラスとして使う"""
36
+ # if yaml_path is None:
37
+ # yaml_path = os.environ.get("MINI_PIX_CONFIG_YAML")
38
+ # if yaml_path is None:
39
+ # return
40
+
41
+ # 新しいパス解決ロジックを使用
42
+ # resolved_path = yaml_path
43
+ resolved_path = cls._resolve_path(yaml_path=yaml_path, depth=2)
44
+ with open(resolved_path, "r") as f:
45
+ data = yaml.safe_load(f)
46
+
47
+ for k, v in data.items():
48
+ if hasattr(cls, k):
49
+ setattr(cls, k, v)
50
+ else:
51
+ print("NO SUCH VAR:", k)
52
+
53
+ def _load_yaml(
54
+ self,
55
+ *,
56
+ yaml_path,
57
+ depth: int,
58
+ ):
59
+ """コンストラクタから呼び出す"""
60
+ # インスタンス用:インスタンスdictにのみ上書き
61
+ print("load_yaml:", yaml_path)
62
+ if yaml_path is None:
63
+ yaml_path = os.environ.get("MINI_PIX_CONFIG_YAML")
64
+ if yaml_path is None:
65
+ return
66
+
67
+ # 新しいパス解決ロジックを使用 (引数で受け取った depth を渡す)
68
+ resolved_path = self._resolve_path(yaml_path=yaml_path, depth=depth)
69
+ # resolved_path = os.path.abspath(yaml_path)
70
+
71
+ print("resolved_path:", resolved_path)
72
+ with open(resolved_path, "r") as f:
73
+ data = yaml.safe_load(f)
74
+ for k, v in data.items():
75
+ setattr(self, k, v)
76
+
77
+ @staticmethod
78
+ def _resolve_path(
79
+ *,
80
+ yaml_path: Union[str, Path],
81
+ depth: int,
82
+ ) -> Path:
83
+ """
84
+ 絶対パスならそのまま、相対パスならこのメソッドの呼び出し元を基準に解決する
85
+ """
86
+ path = Path(yaml_path)
87
+
88
+ # 1. 絶対パス判定(/ や C:\\ など)
89
+ if path.is_absolute():
90
+ return path
91
+ # return path # / や C:\\ から始まれば、そのままのパスを返す
92
+
93
+ # 2. 実行ファイルの場所を特定
94
+ # クラスで呼び出す時
95
+ # stack[0]: _resolve_path
96
+ # stack[1]: load_yaml または _load_yaml
97
+ # stack[2]: Config.load(...) を書いたファイル
98
+ # インスタンス変数(コンストラクタ)から呼び出す時
99
+ # stack[0] = _resolve_path
100
+ # stack[1] = load_yaml または _load_yaml
101
+ # stack[2] = __init__
102
+ # stack[3] = config = Config(...) を書いたファイル
103
+ caller_frame = inspect.stack()[depth]
104
+ caller_file = Path(caller_frame.filename).resolve()
105
+
106
+ # 3. 実行ファイルのディレクトリ + 相対パス を結合
107
+ return (caller_file.parent / path).resolve()