mapres 2.0__tar.gz → 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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mapres
3
- Version: 2.0
3
+ Version: 2.1
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 = "2.0"
10
+ version = "2.1"
11
11
  description = "A powerfull mapping utility"
12
12
  readme = "README.md"
13
13
  requires-python = ">=3.9"
@@ -13,19 +13,28 @@ from .layers import Layer, LayerStack
13
13
 
14
14
  # maps
15
15
  from .maps.color import ColorMap, ascii_colors, mc_colors, strip_colors
16
- from .maps.time import TimeMap, time_tk
16
+ from .maps.time import TimeMap, time
17
+ from .maps.math import MathMap, math
17
18
 
18
19
  # namespaces
19
20
  maps = SimpleNamespace(
21
+ # color
20
22
  ColorMap = ColorMap,
21
23
  ascii_colors = ascii_colors,
22
24
  mc_colors = mc_colors,
23
25
  strip_colors = strip_colors,
26
+
27
+ # time
24
28
  TimeMap = TimeMap,
25
- time_tk = time_tk,
29
+ time = time,
30
+
31
+ # math
32
+ MathMap = MathMap,
33
+ math = math,
26
34
  )
27
35
 
28
36
  __all__ = [
37
+ # core modules
29
38
  'MapResolver',
30
39
  'res',
31
40
  'DataMap',
@@ -40,7 +49,9 @@ __all__ = [
40
49
  'mc_colors',
41
50
  'strip_colors',
42
51
  'TimeMap',
43
- 'time_tk',
52
+ 'time',
53
+ 'MathMap',
54
+ 'math',
44
55
 
45
56
  # namespaces
46
57
  'maps',
@@ -1,12 +1,19 @@
1
1
  from .color import ColorMap, ascii_colors, mc_colors, strip_colors
2
- from .time import TimeMap, time_tk
2
+ from .time import TimeMap, time
3
3
 
4
4
 
5
5
  __all__ = [
6
+ # color
6
7
  'ColorMap',
7
8
  'ascii_colors',
8
9
  'mc_colors',
9
10
  'strip_colors',
11
+
12
+ # time
10
13
  'TimeMap',
11
- 'time_tk',
14
+ 'time',
15
+
16
+ # math
17
+ 'MathMap',
18
+ 'math'
12
19
  ]
@@ -0,0 +1,58 @@
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
@@ -61,5 +61,6 @@ class TimeMap:
61
61
  'weekday': lambda: now().strftime('%A'),
62
62
  }
63
63
 
64
+
64
65
  # proxy map
65
- time_tk = TimeMap
66
+ time = TimeMap
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mapres
3
- Version: 2.0
3
+ Version: 2.1
4
4
  Summary: A powerfull mapping utility
5
5
  Author: iFamished
6
6
  License: MIT
@@ -14,4 +14,5 @@ src/mapres.egg-info/requires.txt
14
14
  src/mapres.egg-info/top_level.txt
15
15
  src/mapres/maps/__init__.py
16
16
  src/mapres/maps/color.py
17
+ src/mapres/maps/math.py
17
18
  src/mapres/maps/time.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