mapres 3a3__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mapres
3
- Version: 3a3
3
+ Version: 3a5
4
4
  Summary: A powerfull mapping utility
5
5
  Author: iFamished
6
6
  License: MIT
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
7
7
 
8
8
  [project]
9
9
  name = "mapres"
10
- version = "3-alpha.3"
10
+ version = "3-alpha.5"
11
11
  description = "A powerfull mapping utility"
12
12
  readme = "README.md"
13
13
  requires-python = ">=3.9"
@@ -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
@@ -18,6 +18,15 @@ class ColorMap:
18
18
  light_purple: str = '\033[95m'
19
19
  yellow: str = '\033[93m'
20
20
  white: str = '\033[97m'
21
+ orange: str = '\033[38;5;208m'
22
+ pink: str = '\033[38;5;213m'
23
+ light_blue: str = '\033[38;5;117m'
24
+ light_green: str = '\033[38;5;120m'
25
+ purple: str = '\033[38;5;129m'
26
+ brown: str = '\033[38;5;130m'
27
+ lime: str = '\033[38;5;154m'
28
+ teal: str = '\033[38;5;37m'
29
+ violet: str = '\033[38;5;141m'
21
30
  reset: str = '\033[0m'
22
31
  bold: str = '\033[1m'
23
32
  italic: str = '\033[3m'
@@ -41,6 +50,19 @@ mc_colors = ColorMap(
41
50
  light_purple='§d',
42
51
  yellow='§e',
43
52
  white='§f',
53
+
54
+ # not supported, fallback to ''
55
+ orange='',
56
+ pink='',
57
+ light_blue='',
58
+ light_green='',
59
+ purple='',
60
+ brown='',
61
+ lime='',
62
+ teal='',
63
+ violet='',
64
+
65
+ # these are supported
44
66
  reset='§r',
45
67
  bold='§l',
46
68
  italic='§o'
@@ -63,6 +85,15 @@ strip_colors = ColorMap(
63
85
  light_purple='',
64
86
  yellow='',
65
87
  white='',
88
+ orange='',
89
+ pink='',
90
+ light_blue='',
91
+ light_green='',
92
+ purple='',
93
+ brown='',
94
+ lime='',
95
+ teal='',
96
+ violet='',
66
97
  reset='',
67
98
  bold='',
68
99
  italic=''
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mapres
3
- Version: 3a3
3
+ Version: 3a5
4
4
  Summary: A powerfull mapping utility
5
5
  Author: iFamished
6
6
  License: MIT
@@ -4,6 +4,7 @@ pyproject.toml
4
4
  src/mapres/__init__.py
5
5
  src/mapres/ast.py
6
6
  src/mapres/cache.py
7
+ src/mapres/configloader.py
7
8
  src/mapres/datamap.py
8
9
  src/mapres/evaluator.py
9
10
  src/mapres/exceptions.py
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