-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
393 lines (319 loc) · 13.3 KB
/
main.py
File metadata and controls
393 lines (319 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
from __future__ import annotations
import argparse
import builtins
import enum
import json
import os
import re
import subprocess
from pathlib import Path
from typing import Optional
from pprint import pprint
from dataclasses import dataclass, field
count = 0
silent = False
verbosity = 0
def silent_print(*args, **kwargs):
if silent:
return
return builtins.print(*args, **kwargs)
def verbosity_print(*args, level, **kwargs):
if silent:
return
if verbosity < level:
return
return builtins.print(*args, **kwargs)
def generate_uuid():
global count
count += 1
return "id_" + hex(count)
connections: list[Connection] = []
def auto_add(collection):
def inner(cls):
def old_post_init(*args, **kwargs):
return None
if hasattr(cls, "__post_init__"):
old_post_init = cls.__post_init__
def wrapper(self, *args, **kwargs):
collection.append(self)
return old_post_init(self, *args, **kwargs)
cls.__post_init__ = wrapper
return cls
return inner
# COLLAPSE SAME DIRECTED CONNECT TO SINGLE DOUBLY
# Allow mult files for everything
# Have Person configs
class Direction(enum.Enum):
UNDIRECTED = enum.auto()
DIRECTED = enum.auto()
BIDIRECTED = enum.auto()
@dataclass
@auto_add(connections)
class Connection:
op: str
origin: Person
target: Person
directed: Optional[Direction] = None
title: Optional[str] = None
color: Optional[str] = None
style: Optional[str] = None
inference: Optional[dict[str, str]] = None
def __hash__(self):
return id(self)
@dataclass
class Person:
fam_id: str
name: Optional[str] = None
incoming: list[Connection] = field(default_factory=list)
outgoing: list[Connection] = field(default_factory=list)
dot_id: str = field(default_factory=generate_uuid)
color: Optional[str] = None
background_color: Optional[str] = None
text_color: Optional[str] = None
image: Optional[str] = None
@dataclass
class Group:
name: Optional[str] = None
members: list[Person] = field(default_factory=list)
dot_id: str = field(default_factory=generate_uuid)
parent: Optional[Group] = None
children: list[Group] = field(default_factory=list)
def generate(self, f):
newline = "\n"
f.write(f"""subgraph cluster_{generate_uuid()} {{
{newline.join(map(lambda p: p.dot_id, self.members))}
label="{self.name}"
""")
for group in self.children:
group.generate(f)
f.write("}")
connection_types = {}
people = {}
groups = []
def add_connection_types(path: str):
with open(path, "r", encoding="UTF-8") as f:
connection_types.update(json.load(f))
def add_people(path: str):
# TODO: Add more Person attributes
with open(path, "r", encoding="UTF-8") as f:
peeps = json.load(f)
for fam_id, data in peeps.items():
person = people.get(fam_id, Person(fam_id))
person.name = data.get("name", person.name)
person.color = data.get("color", person.color)
person.background_color = data.get("background-color", person.background_color)
person.text_color = data.get("text-color", person.text_color)
person.image = data.get("image", person.image)
people[fam_id] = person
def parse(name: str):
current_group = None
file = Path(name).resolve(strict=True)
cwd = os.getcwd()
os.chdir(file.parent)
assert file.is_file(), "You need to Pass a file, not a directory."
with open(file, "r", encoding="UTF-8") as f:
content = f.read()
comment_depth = 0
for row, line in enumerate(re.split("\n|;", content), 1):
line = line.strip()
if comment_depth >= 1:
if line.startswith("/*"):
comment_depth += 1
continue
elif line.endswith("*/"):
comment_depth -= 1
continue
if line.startswith("//") or line.startswith("#"):
continue
elif line.startswith("/*"):
comment_depth += 1
continue
elif current_group:
if line.startswith("}"):
if not current_group.parent:
groups.append(current_group)
else:
current_group.parent.children.append(current_group)
current_group = current_group.parent
continue
elif line.startswith("{"):
group = Group()
group.parent = current_group
current_group = group
continue
elif ":" in line:
attribute, *rest = line.split(":")
rest = ":".join(rest)
if attribute == "name":
current_group.name = rest
continue
current_group.members.append(people.get(line.strip(), Person(line.strip())))
pass
elif line.startswith("include "):
parse(line[8:].replace('"', "") + ".fam")
verbosity_print(f"Parsing include:\t`{line}`", level=2)
continue
elif line.startswith("config "):
add_connection_types(line[7:].replace('"', "") + ".json")
verbosity_print(f"Parsing config:\t`{line}`", level=2)
continue
elif line.startswith("people "):
add_people(line[7:].replace('"', "") + ".json")
verbosity_print(f"Parsing people Statement:\t`{line}`", level=2)
continue
elif line.startswith("{"):
current_group = Group()
elif line == "":
continue
else:
first_person = []
for i, word in enumerate(line.split(" ")):
if word in connection_types:
operator = word
index = i
break
first_person.append(word)
else:
exit(
f"Linie ohne konfigurierte Beziehung, die eine gebraucht hätte: `{name}`:{row} -> `{line}`"
)
first_person = " ".join(first_person)
second_person = " ".join(line.split(" ")[index + 1:])
verbosity_print(
f"Parsing Connection: First Person: `{first_person}` Op: `{operator}` Second Person: `{second_person}`",
level=3)
first_person_object = people.get(first_person, Person(first_person))
second_person_object = people.get(second_person, Person(second_person))
conn = Connection(operator, first_person_object, second_person_object)
first_person_object.outgoing.append(conn)
second_person_object.incoming.append(conn)
people[first_person] = first_person_object
people[second_person] = second_person_object
os.chdir(cwd)
def fixup_connections():
deleted = set()
for connection in connections:
if connection in deleted:
continue
definition = connection_types[connection.op]
connection.title = definition.get("title", None)
connection.color = definition.get("color", None)
connection.style = definition.get("style", None)
connection.inference = definition.get("inference")
if definition["directed"]:
connection.directed = Direction.DIRECTED
else:
connection.directed = Direction.UNDIRECTED
for conn in connection.target.outgoing:
if conn.op != connection.op:
continue
if conn.target is connection.origin and conn.origin is connection.target:
if connection.directed == Direction.DIRECTED:
connection.directed = Direction.BIDIRECTED
deleted.add(conn)
for conn in connection.origin.outgoing:
if conn.op != connection.op:
continue
if conn.target is connection.target and conn.origin is connection.origin and conn is not connection:
deleted.add(conn)
verbosity_print(f"Count of Connections before deletion {len(connections)}", level=1)
for connection in deleted:
try:
connections.remove(connection)
connection.target.incoming.remove(connection)
connection.origin.outgoing.remove(connection)
except Exception as e:
verbosity_print(f"Exception Occured during Connection deletion, this is could be expected but might be a "
f"bug {e}", level=1)
verbosity_print(f"Count of Connections after deletion {len(connections)}", level=1)
verbosity_print(f"Count of Connections deletiod {len(deleted)}", level=1)
def generate_dot_file(name: str):
print(name)
newline = "\n"
with open(name, "w", encoding="UTF-8") as f:
f.write("digraph Tree {\n")
for person in people.values():
color = f'color="{person.color}"' if person.color else ""
label = f'label="{person.name or person.fam_id}"'
bg_color = f'fillcolor="{person.background_color}"' if person.background_color else ""
txt_color = f'fontcolor="{person.text_color}"' if person.text_color else ""
image = f'''image="{person.image}"
imagescale=both''' if person.image else ""
f.write(f"""{person.dot_id} [{newline.join(attr for attr in (color, label, bg_color, txt_color, image,
"style=filled") if attr)}]\n""")
for connection in connections:
"""
directed: Optional[Direction] = None
"""
if connection.directed is Direction.UNDIRECTED:
start, end = "none", "none"
elif connection.directed is Direction.DIRECTED:
start, end = "none", "normal"
elif connection.directed is Direction.BIDIRECTED:
start, end = "normal", "normal"
elif connection.directed is None:
start, end = "none", "none"
else:
assert False, "WTF"
f.write(f"""{connection.origin.dot_id} -> {connection.target.dot_id} [
{('label="' + connection.title + '"') if connection.title is not None else ""}
{('style="' + connection.style + '"') if connection.style is not None else ""}
{('color="' + connection.color + '"') if connection.color is not None else ""}
arrowhead="{end}"
arrowtail="{start}"
]
""")
newline = "\n"
for group in groups:
group.generate(f)
f.write("}")
def infer():
for key, connection_type in connection_types.items():
if not connection_type["inference"]:
continue
elif connection_type["inference"]["type"] == "sib":
for person in people.values():
people_to_add = []
for connection in person.incoming:
if connection.op == connection_type["inference"]["parent_connection"]:
for conn in connection.origin.outgoing:
if conn.op == connection_type["inference"]["parent_connection"]:
people_to_add.append(conn.target)
for peep in people_to_add:
# print("aqs")
conn = Connection(key, person, peep)
person.outgoing.append(conn)
peep.incoming.append(conn)
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("file", help="The file to render")
parser.add_argument(
"--infer", help="Should we try to infer things?", action="store_true"
)
parser.add_argument(
"--no-dot", help="Do not run Graphviz on the generated dot-file", action="store_false"
)
parser.add_argument("--format", help="The output format", default="png")
parser.add_argument("--layout", help="The dot Layout Engine to use", default="dot")
parser.add_argument("--silent", help="Should we just stay fully silent?", action="store_true")
parser.add_argument("-v", "--verbose", help="Should we print various debugging output? Repeat this option more "
"often, to get more Output.", action="count", default=0)
args = parser.parse_args()
global silent, verbosity
silent = args.silent
verbosity = args.verbose
parse(args.file)
fixup_connections()
pprint(connection_types)
if args.infer:
infer()
fixup_connections()
generate_dot_file(args.file + ".dot")
if args.no_dot:
path = Path(args.file + ".dot").resolve()
os.chdir(path.parent)
command = ["dot", f"-T{args.format}", f"-K{args.layout}", "-O", str(path)]
silent_print(f"Calling dot: `{' '.join(command)}`")
subprocess.call(command)
if __name__ == "__main__":
main()