mapres 2.1__tar.gz → 2.2.1__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-2.1/src/mapres.egg-info → mapres-2.2.1}/PKG-INFO +1 -1
- {mapres-2.1 → mapres-2.2.1}/pyproject.toml +1 -1
- {mapres-2.1 → mapres-2.2.1}/src/mapres/__init__.py +0 -7
- {mapres-2.1 → mapres-2.2.1}/src/mapres/maps/__init__.py +0 -4
- {mapres-2.1 → mapres-2.2.1}/src/mapres/resolver.py +22 -16
- {mapres-2.1 → mapres-2.2.1/src/mapres.egg-info}/PKG-INFO +1 -1
- {mapres-2.1 → mapres-2.2.1}/src/mapres.egg-info/SOURCES.txt +0 -1
- mapres-2.1/src/mapres/maps/math.py +0 -58
- {mapres-2.1 → mapres-2.2.1}/LICENSE +0 -0
- {mapres-2.1 → mapres-2.2.1}/README.md +0 -0
- {mapres-2.1 → mapres-2.2.1}/setup.cfg +0 -0
- {mapres-2.1 → mapres-2.2.1}/src/mapres/cache.py +0 -0
- {mapres-2.1 → mapres-2.2.1}/src/mapres/datamap.py +0 -0
- {mapres-2.1 → mapres-2.2.1}/src/mapres/exceptions.py +0 -0
- {mapres-2.1 → mapres-2.2.1}/src/mapres/layers.py +0 -0
- {mapres-2.1 → mapres-2.2.1}/src/mapres/maps/color.py +0 -0
- {mapres-2.1 → mapres-2.2.1}/src/mapres/maps/time.py +0 -0
- {mapres-2.1 → mapres-2.2.1}/src/mapres.egg-info/dependency_links.txt +0 -0
- {mapres-2.1 → mapres-2.2.1}/src/mapres.egg-info/requires.txt +0 -0
- {mapres-2.1 → mapres-2.2.1}/src/mapres.egg-info/top_level.txt +0 -0
|
@@ -14,7 +14,6 @@ from .layers import Layer, LayerStack
|
|
|
14
14
|
# maps
|
|
15
15
|
from .maps.color import ColorMap, ascii_colors, mc_colors, strip_colors
|
|
16
16
|
from .maps.time import TimeMap, time
|
|
17
|
-
from .maps.math import MathMap, math
|
|
18
17
|
|
|
19
18
|
# namespaces
|
|
20
19
|
maps = SimpleNamespace(
|
|
@@ -27,10 +26,6 @@ maps = SimpleNamespace(
|
|
|
27
26
|
# time
|
|
28
27
|
TimeMap = TimeMap,
|
|
29
28
|
time = time,
|
|
30
|
-
|
|
31
|
-
# math
|
|
32
|
-
MathMap = MathMap,
|
|
33
|
-
math = math,
|
|
34
29
|
)
|
|
35
30
|
|
|
36
31
|
__all__ = [
|
|
@@ -50,8 +45,6 @@ __all__ = [
|
|
|
50
45
|
'strip_colors',
|
|
51
46
|
'TimeMap',
|
|
52
47
|
'time',
|
|
53
|
-
'MathMap',
|
|
54
|
-
'math',
|
|
55
48
|
|
|
56
49
|
# namespaces
|
|
57
50
|
'maps',
|
|
@@ -61,32 +61,38 @@ class MapResolver:
|
|
|
61
61
|
return text
|
|
62
62
|
|
|
63
63
|
# layered maps
|
|
64
|
-
def _apply_maps(self, text: str, ctx: dict, layerstack: LayerStack)
|
|
64
|
+
def _apply_maps(self, text: str, ctx: dict, layerstack: LayerStack):
|
|
65
|
+
syntax_patterns = []
|
|
65
66
|
for m in layerstack:
|
|
66
|
-
|
|
67
|
-
# class-based layer
|
|
68
67
|
if isinstance(m, type) and hasattr(m, "as_map"):
|
|
69
68
|
m = m()
|
|
70
69
|
if hasattr(m, "as_map"):
|
|
71
|
-
|
|
72
|
-
d = m.as_map()
|
|
70
|
+
syntax_patterns.append(m.get_syntax())
|
|
73
71
|
elif isinstance(m, dict):
|
|
74
72
|
pattern = ctx.get("syntax")
|
|
75
|
-
if
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
def repl(match: re.Match) -> str:
|
|
73
|
+
if pattern:
|
|
74
|
+
syntax_patterns.append(pattern)
|
|
75
|
+
syntax_patterns = list(dict.fromkeys(syntax_patterns))
|
|
76
|
+
for pattern in syntax_patterns:
|
|
77
|
+
def repl(match: re.Match):
|
|
82
78
|
k = match.group(1)
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
79
|
+
for m in layerstack:
|
|
80
|
+
layer = m
|
|
81
|
+
if isinstance(layer, type) and hasattr(layer, "as_map"):
|
|
82
|
+
layer = layer()
|
|
83
|
+
if hasattr(layer, "as_map"):
|
|
84
|
+
d = layer.as_map()
|
|
85
|
+
elif isinstance(layer, dict):
|
|
86
|
+
d = layer
|
|
87
|
+
else:
|
|
88
|
+
continue
|
|
89
|
+
if k in d:
|
|
90
|
+
return str(d[k])
|
|
91
|
+
raise MissingKeyError(f"Missing key '{k}' in any layer")
|
|
86
92
|
try:
|
|
87
93
|
text = re.sub(pattern, repl, text)
|
|
88
94
|
except re.error as exc:
|
|
89
|
-
raise MapSyntaxError(f"Regex error in
|
|
95
|
+
raise MapSyntaxError(f"Regex error in syntax pattern {pattern}: {exc}") from exc
|
|
90
96
|
return text
|
|
91
97
|
|
|
92
98
|
# context
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import ast
|
|
2
|
-
from mapres.datamap import datamap, syntax
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def safe_eval(expr: str, ctx: dict):
|
|
6
|
-
"""
|
|
7
|
-
Safely evaluate arithmetic expressions using AST.
|
|
8
|
-
Allowed:
|
|
9
|
-
- numbers
|
|
10
|
-
- names (from ctx)
|
|
11
|
-
- + - * / // % **
|
|
12
|
-
- parentheses
|
|
13
|
-
"""
|
|
14
|
-
node = ast.parse(expr, mode="eval")
|
|
15
|
-
|
|
16
|
-
allowed_nodes = (
|
|
17
|
-
ast.Expression, ast.BinOp, ast.UnaryOp,
|
|
18
|
-
ast.Num, ast.Name, ast.Load,
|
|
19
|
-
ast.Add, ast.Sub, ast.Mult, ast.Div,
|
|
20
|
-
ast.FloorDiv, ast.Mod, ast.Pow,
|
|
21
|
-
ast.USub, ast.UAdd, ast.Constant,
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
for sub in ast.walk(node):
|
|
25
|
-
if not isinstance(sub, allowed_nodes):
|
|
26
|
-
raise ValueError(f"Disallowed expression: {expr}")
|
|
27
|
-
|
|
28
|
-
if isinstance(sub, ast.Name) and sub.id not in ctx:
|
|
29
|
-
raise KeyError(f"Unknown variable '{sub.id}' in expression '{expr}'")
|
|
30
|
-
|
|
31
|
-
return eval(compile(node, "<math>", "eval"), {}, ctx)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
@datamap(syntax=r"\$\{\{(.+?)\}\}", mode="dynamic")
|
|
35
|
-
class MathMap:
|
|
36
|
-
expr: str = None
|
|
37
|
-
|
|
38
|
-
def __init__(self):
|
|
39
|
-
self._ctx = {}
|
|
40
|
-
|
|
41
|
-
@property
|
|
42
|
-
def providers(self):
|
|
43
|
-
return {
|
|
44
|
-
"expr": lambda: None
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
def get_map(self, ctx, resolver):
|
|
48
|
-
# store ctx so safe_eval can use it
|
|
49
|
-
self._ctx = ctx
|
|
50
|
-
|
|
51
|
-
def repl(expr):
|
|
52
|
-
return safe_eval(expr, ctx)
|
|
53
|
-
|
|
54
|
-
return repl
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
# proxy map
|
|
58
|
-
math = MathMap
|
|
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
|