-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_registry.py
More file actions
170 lines (148 loc) · 4.87 KB
/
Copy pathmodule_registry.py
File metadata and controls
170 lines (148 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# module_registry.py
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from typing import Dict, List, Optional
@dataclass
class ModuleInfo:
key: str
display_name: str
filename: str
configured_path: str = ""
resolved_path: Optional[Path] = None
available: bool = False
_KNOWN: List[tuple] = [
# (key, display_name, filename, sibling_dir_hint, extra_candidates)
(
"prosync",
"ProSync",
"ProSyncStart_V3.1.py",
"REL-PUB_ProSync",
["ProSync.exe", "START.bat", "dist/ProSync/ProSync.exe"],
),
(
"sqliteviewer",
"SQLiteViewer",
"SQLiteViewer.py",
"REL-PUB_SQLiteViewer",
[],
),
(
"datenschutzampel",
"Datenschutzampel",
"ProFiler_Datenschutzampel.py",
"REL-PUB_Datenschutzampel",
[],
),
(
"formconstructor",
"FormConstructor",
"FormConstructor_V1_5.py",
"REL-PUB_FormConstructor",
["FormConstructor.py"],
),
(
"pythonbox",
"PythonBox",
"PythonBox.py",
"REL-PUB_PythonBox",
[],
),
]
class ModuleRegistry:
"""Erkennt alle ProFiler-Suite-Begleitmodule anhand von Pfadkandidaten."""
def __init__(
self,
base_dir: Path,
configured_paths: Optional[Dict[str, str]] = None,
):
self._base_dir = Path(base_dir).expanduser().resolve()
self._configured = configured_paths or {}
self._modules: Dict[str, ModuleInfo] = {}
self._detect_all()
# ------------------------------------------------------------------
# Public API
# ------------------------------------------------------------------
def get(self, key: str) -> Optional[ModuleInfo]:
return self._modules.get(key)
def get_by_filename(self, filename: str) -> Optional[ModuleInfo]:
"""Liefert ModuleInfo zum bekannten Hauptdateinamen, oder None."""
for info in self._modules.values():
if info.filename == filename:
return info
return None
def all_modules(self) -> List[ModuleInfo]:
return list(self._modules.values())
def refresh(self) -> None:
self._modules.clear()
self._detect_all()
# ------------------------------------------------------------------
# Detection
# ------------------------------------------------------------------
def _detect_all(self) -> None:
for key, display_name, filename, sibling_hint, extras in _KNOWN:
configured = self._configured.get(key, "")
info = self._detect_one(key, display_name, filename, sibling_hint, extras, configured)
self._modules[key] = info
def _detect_one(
self,
key: str,
display_name: str,
filename: str,
sibling_hint: str,
extras: List[str],
configured_path: str,
) -> ModuleInfo:
candidates: List[Path] = []
# 1) Konfigurierter Pfad hat höchste Priorität
if configured_path:
p = Path(configured_path).expanduser()
if not p.is_absolute():
p = self._base_dir / p
if p.is_dir():
candidates.append(p / filename)
for ex in extras:
candidates.append(p / ex)
else:
candidates.append(p)
# 2) Gleiches Verzeichnis
candidates.append(self._base_dir / filename)
for ex in extras:
candidates.append(self._base_dir / ex)
# 3) Benanntes Geschwister-Verzeichnis
sibling = self._base_dir.parent / sibling_hint
if sibling.exists():
candidates.append(sibling / filename)
for ex in extras:
candidates.append(sibling / ex)
# 4) Alle Geschwister-Verzeichnisse durchsuchen
try:
for entry in self._base_dir.parent.iterdir():
if entry.is_dir() and entry != self._base_dir:
c = entry / filename
if c not in candidates:
candidates.append(c)
except OSError:
pass
seen: set = set()
for c in candidates:
resolved = c.resolve() if c.exists() else c
key_str = str(resolved)
if key_str in seen:
continue
seen.add(key_str)
if resolved.exists():
return ModuleInfo(
key=key,
display_name=display_name,
filename=filename,
configured_path=configured_path,
resolved_path=resolved,
available=True,
)
return ModuleInfo(
key=key,
display_name=display_name,
filename=filename,
configured_path=configured_path,
)