mapres 3a4__tar.gz → 3a5__tar.gz
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.
- {mapres-3a4/src/mapres.egg-info → mapres-3a5}/PKG-INFO +1 -1
- {mapres-3a4 → mapres-3a5}/pyproject.toml +1 -1
- {mapres-3a4 → mapres-3a5}/src/mapres/__init__.py +1 -0
- mapres-3a5/src/mapres/configloader.py +149 -0
- {mapres-3a4 → mapres-3a5/src/mapres.egg-info}/PKG-INFO +1 -1
- {mapres-3a4 → mapres-3a5}/src/mapres.egg-info/SOURCES.txt +1 -0
- {mapres-3a4 → mapres-3a5}/LICENSE +0 -0
- {mapres-3a4 → mapres-3a5}/README.md +0 -0
- {mapres-3a4 → mapres-3a5}/setup.cfg +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/ast.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/cache.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/datamap.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/evaluator.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/exceptions.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/layers.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/maps/__init__.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/maps/color.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/maps/time.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/parser.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/resolver.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres/tokenizer.py +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres.egg-info/dependency_links.txt +0 -0
- {mapres-3a4 → mapres-3a5}/src/mapres.egg-info/top_level.txt +0 -0
|
@@ -10,6 +10,7 @@ except PackageNotFoundError:
|
|
|
10
10
|
from .resolver import MapResolver, res, rprint, setGlobalMaps
|
|
11
11
|
from .datamap import DataMap, datamap, syntax
|
|
12
12
|
from .layers import Layer, LayerStack
|
|
13
|
+
from .configloader import loadConfig
|
|
13
14
|
|
|
14
15
|
# maps
|
|
15
16
|
from .maps.color import ColorMap, ascii_colors, mc_colors, strip_colors
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import yaml
|
|
3
|
+
import importlib
|
|
4
|
+
from dataclasses import fields
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
# yaml helpers
|
|
8
|
+
def load_yaml(path: str) -> dict:
|
|
9
|
+
'''Loads a YAML file and always returns a dict.'''
|
|
10
|
+
with open(path, 'r') as f:
|
|
11
|
+
data = yaml.safe_load(f)
|
|
12
|
+
return data if isinstance(data, dict) else {}
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def deep_get(data: dict, path: str):
|
|
16
|
+
'''Resolve dotted YAML paths like 'server.port'.'''
|
|
17
|
+
cur = data
|
|
18
|
+
for part in path.split('.'):
|
|
19
|
+
if not isinstance(cur, dict):
|
|
20
|
+
return None
|
|
21
|
+
cur = cur.get(part)
|
|
22
|
+
if cur is None:
|
|
23
|
+
return None
|
|
24
|
+
return cur
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def convert_value(value, annotation):
|
|
28
|
+
'''Convert YAML values to the dataclass field type.'''
|
|
29
|
+
try:
|
|
30
|
+
if annotation is int:
|
|
31
|
+
return int(value)
|
|
32
|
+
if annotation is float:
|
|
33
|
+
return float(value)
|
|
34
|
+
if annotation is bool:
|
|
35
|
+
return bool(value)
|
|
36
|
+
except Exception:
|
|
37
|
+
pass
|
|
38
|
+
return value
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
# raw namespace
|
|
42
|
+
class RawNamespace:
|
|
43
|
+
'''
|
|
44
|
+
Provides attribute-style access to raw YAML values.
|
|
45
|
+
Uses the dataclass default (YAML path) instead of guessing.
|
|
46
|
+
'''
|
|
47
|
+
def __init__(self, raw_dict, schema):
|
|
48
|
+
self._raw = raw_dict
|
|
49
|
+
self._schema = schema
|
|
50
|
+
|
|
51
|
+
def __getattr__(self, name):
|
|
52
|
+
# Look up the YAML path from the dataclass default
|
|
53
|
+
field = self._schema.__dataclass_fields__.get(name)
|
|
54
|
+
if not field:
|
|
55
|
+
return None
|
|
56
|
+
|
|
57
|
+
yaml_path = field.default # e.g. 'panel.api_key'
|
|
58
|
+
if not isinstance(yaml_path, str):
|
|
59
|
+
return None
|
|
60
|
+
|
|
61
|
+
return self._deep_get(self._raw, yaml_path)
|
|
62
|
+
|
|
63
|
+
def _deep_get(self, data, path):
|
|
64
|
+
cur = data
|
|
65
|
+
for part in path.split('.'):
|
|
66
|
+
if not isinstance(cur, dict):
|
|
67
|
+
return None
|
|
68
|
+
cur = cur.get(part)
|
|
69
|
+
if cur is None:
|
|
70
|
+
return None
|
|
71
|
+
return cur
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# main loader
|
|
75
|
+
def loadConfig(schema, runtime_path: str | None = None):
|
|
76
|
+
'''
|
|
77
|
+
Loads a config dataclass using:
|
|
78
|
+
- schema.__user_config_path__ for runtime file
|
|
79
|
+
- schema.__default_config_path__ for packaged default
|
|
80
|
+
- dataclass defaults as YAML key paths (mode='config')
|
|
81
|
+
- fallback values from schema.fallbacks.<key>
|
|
82
|
+
'''
|
|
83
|
+
|
|
84
|
+
# 1. Resolve runtime path
|
|
85
|
+
if runtime_path is None:
|
|
86
|
+
runtime_path = getattr(schema, '__user_config_path__', 'config/config.yaml')
|
|
87
|
+
|
|
88
|
+
abs_runtime = os.path.abspath(runtime_path)
|
|
89
|
+
os.makedirs(os.path.dirname(abs_runtime), exist_ok=True)
|
|
90
|
+
|
|
91
|
+
# 2. Resolve default config path inside the package
|
|
92
|
+
default_rel = getattr(schema, '__default_config_path__', None)
|
|
93
|
+
abs_default = None
|
|
94
|
+
|
|
95
|
+
if default_rel:
|
|
96
|
+
module = importlib.import_module(schema.__module__)
|
|
97
|
+
schema_file = os.path.abspath(module.__file__)
|
|
98
|
+
package_dir = os.path.dirname(os.path.dirname(schema_file))
|
|
99
|
+
abs_default = os.path.join(package_dir, default_rel.lstrip('/'))
|
|
100
|
+
|
|
101
|
+
# 3. Ensure runtime config exists
|
|
102
|
+
if not os.path.exists(abs_runtime):
|
|
103
|
+
if abs_default and os.path.exists(abs_default):
|
|
104
|
+
with open(abs_default, 'r') as src, open(abs_runtime, 'w') as dst:
|
|
105
|
+
dst.write(src.read())
|
|
106
|
+
else:
|
|
107
|
+
open(abs_runtime, 'a').close() # create empty file
|
|
108
|
+
|
|
109
|
+
# 4. Load YAML
|
|
110
|
+
raw = load_yaml(abs_runtime)
|
|
111
|
+
values = {}
|
|
112
|
+
|
|
113
|
+
# 5. Load fallback class from the same module
|
|
114
|
+
module = importlib.import_module(schema.__module__)
|
|
115
|
+
fallbacks = getattr(module, 'fallbacks', None)
|
|
116
|
+
|
|
117
|
+
# 6. Map YAML → dataclass fields
|
|
118
|
+
mode = getattr(schema, '__mode__', None)
|
|
119
|
+
|
|
120
|
+
for f in fields(schema):
|
|
121
|
+
if f.name.startswith('__'):
|
|
122
|
+
continue
|
|
123
|
+
|
|
124
|
+
yaml_path = f.default
|
|
125
|
+
yaml_value = None
|
|
126
|
+
|
|
127
|
+
# mode='config': default string = YAML path
|
|
128
|
+
if mode == 'config' and isinstance(yaml_path, str):
|
|
129
|
+
yaml_value = deep_get(raw, yaml_path)
|
|
130
|
+
|
|
131
|
+
if yaml_value is not None:
|
|
132
|
+
values[f.name] = convert_value(yaml_value, f.type)
|
|
133
|
+
else:
|
|
134
|
+
# fallback from fallback class
|
|
135
|
+
if fallbacks and hasattr(fallbacks, f.name):
|
|
136
|
+
values[f.name] = getattr(fallbacks, f.name)
|
|
137
|
+
else:
|
|
138
|
+
values[f.name] = None
|
|
139
|
+
|
|
140
|
+
# 7. Build config instance
|
|
141
|
+
config = schema(**values)
|
|
142
|
+
|
|
143
|
+
# 8. Attach fallback namespace
|
|
144
|
+
config.fallbacks = fallbacks
|
|
145
|
+
|
|
146
|
+
# 9. Attach raw YAML namespace
|
|
147
|
+
config.raw = RawNamespace(raw, schema)
|
|
148
|
+
|
|
149
|
+
return config
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|