model-lang 0.1.0__py3-none-any.whl
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.
- model_lang/__init__.py +9 -0
- model_lang/grammar.peg +57 -0
- model_lang/objects/__init__.py +1 -0
- model_lang/objects/model.py +67 -0
- model_lang/parser.py +41 -0
- model_lang/visitor.py +207 -0
- model_lang-0.1.0.dist-info/METADATA +14 -0
- model_lang-0.1.0.dist-info/RECORD +10 -0
- model_lang-0.1.0.dist-info/WHEEL +4 -0
- model_lang-0.1.0.dist-info/entry_points.txt +3 -0
model_lang/__init__.py
ADDED
model_lang/grammar.peg
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
program = blank_lines (component blank_lines)*
|
|
2
|
+
|
|
3
|
+
component = indent identifier (required_ws component_id)? ws "(" component_body_line* indent ")" line_end?
|
|
4
|
+
component_body_line = direct_property_line
|
|
5
|
+
/ group
|
|
6
|
+
/ child_component
|
|
7
|
+
/ blank_lines
|
|
8
|
+
|
|
9
|
+
direct_property_line = indent property line_end
|
|
10
|
+
|
|
11
|
+
group = indent identifier ws "{" line_end group_property_line* indent "}" line_end
|
|
12
|
+
group_property_line = indent property line_end
|
|
13
|
+
/ blank_lines
|
|
14
|
+
|
|
15
|
+
child_component = component
|
|
16
|
+
/ "DO NOT MATCH"
|
|
17
|
+
|
|
18
|
+
property = identifier ":" ws value
|
|
19
|
+
|
|
20
|
+
component_id = number / identifier / string
|
|
21
|
+
|
|
22
|
+
multiline_string = nl? ws "```" ( any_character / nl )* "```"
|
|
23
|
+
blank_lines = (ws nl)*
|
|
24
|
+
ws = ~r"[ \t]*"
|
|
25
|
+
required_ws = ~r"[ \t]+"
|
|
26
|
+
line_end = nl
|
|
27
|
+
nl = "\n" / "\r\n"
|
|
28
|
+
indent = ~r"[ \t]*"
|
|
29
|
+
value = multiline_string / array / complex / boolean / string / string_like_value
|
|
30
|
+
|
|
31
|
+
array = array_of_number
|
|
32
|
+
/ array_of_string_like_value
|
|
33
|
+
array_separator = required_ws / line_end indent
|
|
34
|
+
|
|
35
|
+
array_of_number = array_of_number_oneline / array_of_number_multiline
|
|
36
|
+
array_of_number_oneline = "[" ws number ( array_separator number )* ws "]"
|
|
37
|
+
array_of_number_multiline = "[" ws line_end ( indent number ws line_end )* indent "]"
|
|
38
|
+
|
|
39
|
+
array_of_string_like_value = array_of_string_like_value_oneline / array_of_string_like_value_multiline
|
|
40
|
+
array_of_string_like_value_oneline = "[" ws word ( array_separator word )* ws "]"
|
|
41
|
+
array_of_string_like_value_multiline = "[" ws line_end ( indent string_like_value ws line_end )* indent "]"
|
|
42
|
+
|
|
43
|
+
complex = "{" line_end key_value_line* indent "}"
|
|
44
|
+
key_value_line = indent key_value line_end
|
|
45
|
+
key_value = identifier ":" ws value
|
|
46
|
+
string_like_value = ~r"[^\[\]\n\r]+"
|
|
47
|
+
reference_character = ~r"[A-Za-z0-9_/#-]*"
|
|
48
|
+
word = ~r"[^\[\]\s\n\r]+"
|
|
49
|
+
string = ~r"\"[^\"]+\""
|
|
50
|
+
any_character = ~r"[^`\n\r]"
|
|
51
|
+
number = "-"? digit+ ( "." digit+ )?
|
|
52
|
+
digit = ~r"[0-9]"
|
|
53
|
+
identifier = identifier_start identifier_rest*
|
|
54
|
+
identifier_start = ~r"[A-Za-z0-9_]"
|
|
55
|
+
identifier_rest = identifier_start / "." / "-"
|
|
56
|
+
boolean = "true" / "false"
|
|
57
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .model import *
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
|
|
2
|
+
class ModelGroup():
|
|
3
|
+
properties = {}
|
|
4
|
+
def __init__(self, initial_data={}):
|
|
5
|
+
self.properties = {}
|
|
6
|
+
self.properties.update(initial_data)
|
|
7
|
+
|
|
8
|
+
def add(self, key, value):
|
|
9
|
+
self.properties[key] = value
|
|
10
|
+
|
|
11
|
+
def get(self, name):
|
|
12
|
+
if name in self.properties:
|
|
13
|
+
return self.properties[name]
|
|
14
|
+
|
|
15
|
+
return None
|
|
16
|
+
|
|
17
|
+
def __getattr__(self, name):
|
|
18
|
+
if name in self.__dict__['properties']:
|
|
19
|
+
return self.__dict__['properties'][name]
|
|
20
|
+
raise AttributeError
|
|
21
|
+
|
|
22
|
+
class ModelComponent():
|
|
23
|
+
def __init__(self, component_type, component_id=None, initial_data={}):
|
|
24
|
+
self.component_type = component_type
|
|
25
|
+
self.component_id = component_id
|
|
26
|
+
self.properties = {}
|
|
27
|
+
self.properties.update(initial_data)
|
|
28
|
+
self.groups = {}
|
|
29
|
+
self.children = {}
|
|
30
|
+
|
|
31
|
+
def add_property(self, key, value):
|
|
32
|
+
self.properties[key] = value
|
|
33
|
+
|
|
34
|
+
def add_group(self, key, value):
|
|
35
|
+
if isinstance(value, ModelGroup):
|
|
36
|
+
self.groups[key] = value
|
|
37
|
+
elif type(value) is dict:
|
|
38
|
+
self.groups[key] = ModelGroup(initial_data=value)
|
|
39
|
+
else:
|
|
40
|
+
raise AttributeError
|
|
41
|
+
|
|
42
|
+
def add_child(self, key, child):
|
|
43
|
+
if key in self.children:
|
|
44
|
+
self.children[key].append(child)
|
|
45
|
+
else:
|
|
46
|
+
self.children[key] = [child]
|
|
47
|
+
|
|
48
|
+
def get(self, name):
|
|
49
|
+
if name in self.properties:
|
|
50
|
+
return self.properties[name]
|
|
51
|
+
|
|
52
|
+
if name in self.groups:
|
|
53
|
+
return self.groups[name]
|
|
54
|
+
|
|
55
|
+
if name in self.children:
|
|
56
|
+
return self.children[name]
|
|
57
|
+
|
|
58
|
+
return None
|
|
59
|
+
|
|
60
|
+
def __getattr__(self, name):
|
|
61
|
+
if name in self.__dict__['properties']:
|
|
62
|
+
return self.__dict__['properties'][name]
|
|
63
|
+
if name in self.__dict__['groups']:
|
|
64
|
+
return self.__dict__['groups'][name]
|
|
65
|
+
if name in self.__dict__['children']:
|
|
66
|
+
return self.__dict__['children'][name]
|
|
67
|
+
raise AttributeError
|
model_lang/parser.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from parsimonious import Grammar
|
|
2
|
+
from .objects import *
|
|
3
|
+
from importlib import resources as impresources
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
import model_lang
|
|
8
|
+
from .visitor import ModelVisitor
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ModelParser:
|
|
12
|
+
def __init__(self, component_object_map={}, group_object_map={}, child_object_map={}):
|
|
13
|
+
inp_file = impresources.files(model_lang) / f'grammar.peg'
|
|
14
|
+
with inp_file.open("rt") as f:
|
|
15
|
+
template = f.read()
|
|
16
|
+
|
|
17
|
+
self.grammar = Grammar(template)
|
|
18
|
+
self.visitor = ModelVisitor(component_object_map, group_object_map, child_object_map)
|
|
19
|
+
|
|
20
|
+
def parse(self, data) -> ModelComponent:
|
|
21
|
+
nodes = self.grammar.parse(data)
|
|
22
|
+
output = self.visitor.visit(nodes)
|
|
23
|
+
return output
|
|
24
|
+
|
|
25
|
+
def parse_file(self, model_file: str | Path):
|
|
26
|
+
if type(model_file) is str:
|
|
27
|
+
fn = Path(model_file)
|
|
28
|
+
else:
|
|
29
|
+
fn = model_file
|
|
30
|
+
with fn.open('r') as f:
|
|
31
|
+
data = f.read()
|
|
32
|
+
|
|
33
|
+
return self.parse(data)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def parse_model_file(model_file: str | Path) -> ModelComponent | list[ModelComponent]:
|
|
37
|
+
return ModelParser().parse_file(model_file)
|
|
38
|
+
|
|
39
|
+
def parse_model(data: str) -> ModelComponent | list[ModelComponent]:
|
|
40
|
+
return ModelParser().parse(data)
|
|
41
|
+
|
model_lang/visitor.py
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
from parsimonious import NodeVisitor
|
|
2
|
+
from .objects import *
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class RuleNotImplemented(BaseException):
|
|
6
|
+
pass
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ModelVisitor(NodeVisitor):
|
|
10
|
+
def __init__(self, component_object_map={}, group_object_map={}, child_object_map={}):
|
|
11
|
+
super().__init__()
|
|
12
|
+
self.component_object_map = component_object_map
|
|
13
|
+
self.group_object_map = group_object_map
|
|
14
|
+
self.child_object_map = child_object_map
|
|
15
|
+
|
|
16
|
+
def visit_program(self, node, visited_children):
|
|
17
|
+
retval = []
|
|
18
|
+
for item in visited_children[1]:
|
|
19
|
+
retval.append(item[0])
|
|
20
|
+
|
|
21
|
+
if len(retval) == 1:
|
|
22
|
+
return retval[0]
|
|
23
|
+
else:
|
|
24
|
+
return retval
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def visit_component(self, node, visited_children):
|
|
28
|
+
component_type = visited_children[1]
|
|
29
|
+
if type(visited_children[2]) is list:
|
|
30
|
+
component_id = visited_children[2][0][1]
|
|
31
|
+
else:
|
|
32
|
+
component_id = None
|
|
33
|
+
body_parts = visited_children[5]
|
|
34
|
+
|
|
35
|
+
component_class = self.component_object_map.get(component_type, ModelComponent)
|
|
36
|
+
component = component_class(component_type=component_type, component_id=component_id)
|
|
37
|
+
|
|
38
|
+
for item in body_parts:
|
|
39
|
+
if item is None:
|
|
40
|
+
continue
|
|
41
|
+
|
|
42
|
+
if type(item) is tuple:
|
|
43
|
+
match item[0]:
|
|
44
|
+
case 'child':
|
|
45
|
+
child_type = item[1]
|
|
46
|
+
child = item[2]
|
|
47
|
+
|
|
48
|
+
if component_type in self.child_object_map:
|
|
49
|
+
if child_type in self.child_object_map[component_type]:
|
|
50
|
+
child.__class__ = self.child_object_map[component_type][child_type]
|
|
51
|
+
elif child_type in self.component_object_map:
|
|
52
|
+
child.__class__ = self.component_object_map[child_type]
|
|
53
|
+
elif child_type in self.component_object_map:
|
|
54
|
+
child.__class__ = self.component_object_map[child_type]
|
|
55
|
+
|
|
56
|
+
component.add_child(item[1], child)
|
|
57
|
+
|
|
58
|
+
case 'group':
|
|
59
|
+
g = ModelGroup(item[2])
|
|
60
|
+
component.add_group(item[1], g)
|
|
61
|
+
# elif isinstance(item[1], dict):
|
|
62
|
+
# component.add_group(*item)
|
|
63
|
+
case _ :
|
|
64
|
+
component.add_property(*item)
|
|
65
|
+
|
|
66
|
+
return component
|
|
67
|
+
|
|
68
|
+
def visit_component_id(self, node, visited_children):
|
|
69
|
+
return node.text
|
|
70
|
+
|
|
71
|
+
def visit_component_body_line(self, node, visited_children):
|
|
72
|
+
return visited_children[0]
|
|
73
|
+
|
|
74
|
+
def visit_direct_property_line(self, node, visited_children):
|
|
75
|
+
return visited_children[1]
|
|
76
|
+
|
|
77
|
+
def visit_group(self, node, visited_children):
|
|
78
|
+
groupname = visited_children[1]
|
|
79
|
+
parts = visited_children[5]
|
|
80
|
+
|
|
81
|
+
group = {}
|
|
82
|
+
for item in parts:
|
|
83
|
+
if item is None:
|
|
84
|
+
continue
|
|
85
|
+
|
|
86
|
+
if type(item) is tuple:
|
|
87
|
+
group[item[0]] = item[1]
|
|
88
|
+
|
|
89
|
+
return ('group', groupname, group)
|
|
90
|
+
|
|
91
|
+
def visit_group_property_line(self, node, visited_children):
|
|
92
|
+
if visited_children[0]:
|
|
93
|
+
return visited_children[0][1]
|
|
94
|
+
return None
|
|
95
|
+
|
|
96
|
+
def visit_child_component(self, node, visited_children):
|
|
97
|
+
return ('child', visited_children[0].component_type, visited_children[0])
|
|
98
|
+
|
|
99
|
+
def visit_property(self, node, visited_children):
|
|
100
|
+
return (visited_children[0], visited_children[3])
|
|
101
|
+
|
|
102
|
+
def visit_value(self, node, visited_children):
|
|
103
|
+
return visited_children[0]
|
|
104
|
+
|
|
105
|
+
def visit_multiline_string(self, node, visited_children):
|
|
106
|
+
return node.text
|
|
107
|
+
|
|
108
|
+
def visit_array(self, node, visited_children):
|
|
109
|
+
return visited_children[0]
|
|
110
|
+
|
|
111
|
+
def visit_array_of_string_like_value(self, node, visited_children):
|
|
112
|
+
return visited_children[0]
|
|
113
|
+
|
|
114
|
+
def visit_array_of_string_like_value_oneline(self, node, visited_children):
|
|
115
|
+
retval = [visited_children[2]]
|
|
116
|
+
for item in visited_children[3]:
|
|
117
|
+
value = item[1]
|
|
118
|
+
retval.append(value)
|
|
119
|
+
return retval
|
|
120
|
+
|
|
121
|
+
def visit_array_of_string_like_value_multiline(self, node, visited_children):
|
|
122
|
+
retval = []
|
|
123
|
+
for item in visited_children[3]:
|
|
124
|
+
value = item[1]
|
|
125
|
+
retval.append(value)
|
|
126
|
+
return retval
|
|
127
|
+
|
|
128
|
+
def visit_array_of_number(self, node, visited_children):
|
|
129
|
+
return visited_children[0]
|
|
130
|
+
|
|
131
|
+
def visit_array_of_number_oneline(self, node, visited_children):
|
|
132
|
+
retval = [visited_children[2]]
|
|
133
|
+
for item in visited_children[3]:
|
|
134
|
+
value = item[1]
|
|
135
|
+
retval.append(value)
|
|
136
|
+
return retval
|
|
137
|
+
|
|
138
|
+
def visit_array_of_number_multiline(self, node, visited_children):
|
|
139
|
+
retval = []
|
|
140
|
+
for item in visited_children[3]:
|
|
141
|
+
value = item[1]
|
|
142
|
+
retval.append(value)
|
|
143
|
+
return retval
|
|
144
|
+
|
|
145
|
+
def visit_complex(self, node, visited_children):
|
|
146
|
+
d = {}
|
|
147
|
+
for k, v in visited_children[2]:
|
|
148
|
+
d[k] = v
|
|
149
|
+
return d
|
|
150
|
+
|
|
151
|
+
def visit_key_value_line(self, node, visited_children):
|
|
152
|
+
return visited_children[1]
|
|
153
|
+
|
|
154
|
+
def visit_key_value(self, node, visited_children):
|
|
155
|
+
value = visited_children[3]
|
|
156
|
+
return (visited_children[0], value)
|
|
157
|
+
|
|
158
|
+
def visit_string_like_value(self, node, visited_children):
|
|
159
|
+
t = node.text
|
|
160
|
+
try:
|
|
161
|
+
if '.' in t:
|
|
162
|
+
return float(t)
|
|
163
|
+
else:
|
|
164
|
+
return int(t)
|
|
165
|
+
except:
|
|
166
|
+
return t
|
|
167
|
+
|
|
168
|
+
def visit_word(self, node, visited_children):
|
|
169
|
+
return node.text
|
|
170
|
+
|
|
171
|
+
def visit_identifier(self, node, visited_children):
|
|
172
|
+
return node.text
|
|
173
|
+
|
|
174
|
+
def visit_identifier_start(self, node, visited_children):
|
|
175
|
+
return node.text
|
|
176
|
+
|
|
177
|
+
def visit_identifier_rest(self, node, visited_children):
|
|
178
|
+
return node.text
|
|
179
|
+
|
|
180
|
+
def visit_string(self, node, visited_children):
|
|
181
|
+
return node.text[1:-1]
|
|
182
|
+
|
|
183
|
+
def visit_boolean(self, node, visited_children):
|
|
184
|
+
return node.text == 'true'
|
|
185
|
+
|
|
186
|
+
def visit_number(self, node, visited_children):
|
|
187
|
+
if '.' in node.text:
|
|
188
|
+
return float(node.text)
|
|
189
|
+
else:
|
|
190
|
+
return int(node.text)
|
|
191
|
+
|
|
192
|
+
def visit_required_ws(self, node, visited_children):
|
|
193
|
+
return None
|
|
194
|
+
|
|
195
|
+
def visit_ws(self, node, visited_children):
|
|
196
|
+
return node.text
|
|
197
|
+
|
|
198
|
+
def visit_indent(self, node, visited_children):
|
|
199
|
+
return node.text
|
|
200
|
+
|
|
201
|
+
def visit_blank_lines(self, node, visited_children):
|
|
202
|
+
return None
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def generic_visit(self, node, visited_children):
|
|
206
|
+
""" The generic visit method. """
|
|
207
|
+
return visited_children or node.text
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: model-lang
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Add your description here
|
|
5
|
+
Author: Maurice Koster
|
|
6
|
+
Author-email: Maurice Koster <maurice@mauricekoster.com>
|
|
7
|
+
Requires-Dist: invoke>=3.0.3
|
|
8
|
+
Requires-Dist: jinja2>=3.1.6
|
|
9
|
+
Requires-Dist: parsimonious>=0.11.0
|
|
10
|
+
Requires-Python: >=3.13
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# model-lang
|
|
14
|
+
A generic web app model description.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
model_lang/__init__.py,sha256=1dFI4MMRUaY92yZvwoBkeuLl3EC74xgNH2y5RoPk7pA,249
|
|
2
|
+
model_lang/grammar.peg,sha256=f9HBl8wSNF2tIdin4_6a9RkAJAhYZcPEaKORT0fl_TE,2861
|
|
3
|
+
model_lang/objects/__init__.py,sha256=tpbB4kJbR6GUnNvuK-kfTYueWrRbHIjRvaE6Yo9D4Fs,20
|
|
4
|
+
model_lang/objects/model.py,sha256=dk9FIieaTXN-2Dqp6WnjTx5owujnY3ghm5i1mvsGENU,2000
|
|
5
|
+
model_lang/parser.py,sha256=qLaylphByZ5i7j4pt4fY6pLBR0MKZQv-0acJIRBZBBw,1226
|
|
6
|
+
model_lang/visitor.py,sha256=RM-60ShUAgxr0VSeBGvWncg1_OMfzgVrEtFHgxeXZWQ,6605
|
|
7
|
+
model_lang-0.1.0.dist-info/WHEEL,sha256=-gI5GRFdiNNn2CFz-ptlExISMINC-k8bFxeojAlHbrM,81
|
|
8
|
+
model_lang-0.1.0.dist-info/entry_points.txt,sha256=p2k-LL0txT6vFqMJLm44vuSY29wAHlbOaKs9j3Aukcs,48
|
|
9
|
+
model_lang-0.1.0.dist-info/METADATA,sha256=p8oD_X64ZpwYkWGqk8SUuEk_22n5DfLGIAH_HsDkGow,379
|
|
10
|
+
model_lang-0.1.0.dist-info/RECORD,,
|