forked from robproject/fsjd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frappe_schema_json_diff.py
executable file
·367 lines (320 loc) · 12.8 KB
/
frappe_schema_json_diff.py
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
#!/usr/bin/env python3
import sys
import os
import json
import re
from dataclasses import dataclass
from json_source_map import calculate
from rich.console import Console
from rich.tree import Tree
from rich.table import Table
@dataclass
class FrappeDiff:
old_path: str
new_path: str
print_table: int
def prep(self) -> None:
self.base_obj, self.base_ln = self.get_file(self.old_path)
self.head_obj, self.head_ln = self.get_file(self.new_path)
self.make_label()
if self.print_table:
self.prep_table()
else:
self.table = None
def get_file(self, path: str) -> None:
with open(path) as f:
json_str = f.read()
return json.loads(json_str), LineNos(json_str)
def make_label(self) -> None:
self.label = f"[bold]{new_path.split('/')[-1]}[/bold]"
def prep_table(self) -> None:
table = Table(
title=self.label,
title_justify="left",
show_lines=True,
title_style="bold",
expand=True,
)
table.add_column("+-, L#, Path", ratio=1)
table.add_column("Key or Element", ratio=1)
table.add_column("Value", ratio=1)
self.table = table
def print(self):
self.tree = self.dict_diff(
self.label,
self.base_obj,
self.head_obj,
)
if self.tree.children:
console.log(self.tree)
elif self.table and self.table.rows:
console.log(self.table)
else:
console.log(f"[bold]{self.label}[/bold]")
def dict_diff(
self,
name: str,
base_dict: dict,
head_dict: dict,
b_path: str = "",
h_path: str = "",
) -> Tree:
dict_tree = Tree(name)
for bk, bk_v in base_dict.items():
# concatenated path for passing to table/tree constructor functions: red_kvp, grn_kvp, mod_kvp
self.conc_b_path = f"{b_path}/{bk}"
if bk in head_dict:
hk_v = head_dict[bk]
self.conc_h_path = f"{h_path}/{bk}"
if type(bk_v) is list and (bk_v or hk_v):
# If no differences, None is returned
rtree = self.list_diff(
bk, bk_v, hk_v, self.conc_b_path, self.conc_h_path
)
if rtree.children:
dict_tree.add(rtree)
elif type(bk_v) is dict:
rtree = self.dict_diff(
bk, bk_v, hk_v, self.conc_b_path, self.conc_h_path
)
if rtree.children:
dict_tree.add(rtree)
elif bk_v != hk_v and not (bk == "modified" or bk == "modified_by"):
self.mod_kvp(bk, f"{bk_v}", f"{hk_v}", dict_tree)
else:
self.red_kvp(bk, f"{bk_v}", dict_tree)
for hk in list(set(head_dict) - set(base_dict)):
self.grn_kvp(hk, f"{head_dict[hk]}", dict_tree)
return dict_tree
def red_kvp(self, key: str, value: str, tree: Tree) -> None:
if self.print_table:
self.table.add_row(
f"[bold]- {self.base_ln.key(self.conc_b_path)}[/bold] {self.conc_b_path}",
key,
value,
style="red",
)
else:
tree.add(
f"[red][bold]- {self.base_ln.key(self.conc_b_path)}[/bold] {key} : {value}[/red]"
)
def grn_kvp(self, key: str, value: str, tree: Tree) -> None:
if self.print_table:
self.table.add_row(
f"[bold]+ {self.head_ln.key(self.conc_h_path)}[/bold] {self.conc_h_path}",
key,
value,
style="green",
)
else:
tree.add(
f"[green][bold]+ {self.base_ln.key(self.conc_h_path)}[/bold] {key} : {value}[/green]"
)
def mod_kvp(self, key: str, b_val: str, h_val: str, tree: Tree) -> None:
if self.print_table:
self.table.add_row(
f"[bold]- {self.base_ln.key(self.conc_b_path)}[/bold] {self.conc_b_path}",
f"[default]{key}[/default]",
b_val,
style="red",
)
self.table.add_row(
f"[bold]+ {self.head_ln.key(self.conc_h_path)}[/bold] {self.conc_h_path}",
f"[default]{key}[/default]",
h_val,
style="green",
)
else:
tree.add(
f"[bold red]{self.base_ln.key(self.conc_b_path)}[/bold red] => "
f"[bold green]{self.head_ln.key(self.conc_h_path)}[/bold green]"
f" {key} : [red]{b_val}[/red] => [green]{h_val}[/green]"
)
def list_diff(
self,
name: str,
base_list: list,
head_list: list,
b_path: str,
h_path: str,
) -> Tree:
list_tree = Tree(name)
self.common_key = None
isdict = False
# one (and only one) list may be empty
mt_handler = base_list or head_list
if type(mt_handler[0]) is dict:
isdict = self.get_common_key(name, mt_handler)
if base_list and head_list and isdict:
# Create sets of values of common keys for lists of dicts
head_dicts = {k[self.common_key] for k in head_list}
base_dicts = {k[self.common_key] for k in base_list}
deleted_dict_keys, added_dict_keys = symmetric_diff_sep(
base_dicts, head_dicts
)
diff_dict_keys = head_dicts - added_dict_keys
for i, bd in enumerate(base_list):
bd_kv = bd[self.common_key]
conc_b_path = f"{b_path}/{i}"
if bd_kv in deleted_dict_keys:
self.red_dict(bd, conc_b_path, list_tree)
deleted_dict_keys.remove(bd_kv)
else:
for j, hd in enumerate(head_list):
hd_kv = hd[self.common_key]
conc_h_path = f"{h_path}/{j}"
if bd_kv == hd_kv and bd_kv in diff_dict_keys:
rtree = self.dict_diff(
bd_kv, bd, hd, conc_b_path, conc_h_path
)
diff_dict_keys.remove(bd_kv)
if rtree.children:
list_tree.add(rtree)
elif hd_kv in added_dict_keys:
self.grn_dict(hd, conc_h_path, list_tree)
added_dict_keys.remove(hd_kv)
elif isdict:
for i, d in enumerate(base_list):
self.red_dict(d, f"{b_path}/{i}", list_tree)
for i, d in enumerate(head_list):
self.grn_dict(d, f"{h_path}/{i}", list_tree)
else:
del_set, add_set = symmetric_diff_sep(set(base_list), set(head_list))
for i, e in enumerate(base_list):
if e in del_set:
self.red_elem(e, f"{b_path}/{i}", list_tree)
del_set.remove(e)
for i, e in enumerate(head_list):
if e in add_set:
self.grn_elem(e, f"{h_path}/{i}", list_tree)
add_set.remove(e)
return list_tree
def get_common_key(self, name: str, dict_list: list) -> True:
common_keys = {
"fields": "fieldname",
"permissions": "role",
"actions": "label",
"links": "link_doctype",
"custom_fields": "fieldname",
"property_setters": "field_name",
}
if name in common_keys:
self.common_key = common_keys[name]
else:
# https://stackoverflow.com/a/13985856/14410691
common_keys = set.intersection(*map(set, dict_list))
final_keys = []
for key in self.common_keys:
# find common keys with unique values. If multiple, pick one to use
if len(dict_list) == len({[d[key] for d in dict_list]}):
final_keys.append(key)
try:
self.common_key = final_keys[0]
except IndexError:
print(
"No unique common_key : values - dictionaries can't be diffed based on content"
)
return True
def red_elem(self, elem: str, path: str, tree: Tree) -> None:
if self.print_table:
self.table.add_row(
f"[bold]- {self.base_ln.val(path)}[/bold] {path}", elem, "", style="red"
)
else:
tree.add(f"[red][bold]- {self.base_ln.val(path)}[/bold] {elem}[/red]")
def grn_elem(self, elem: str, path: str, tree: Tree) -> None:
if self.print_table:
self.table.add_row(
f"[bold]+ {self.head_ln.val(path)}[/bold] {path}", elem, "", style="green"
)
else:
tree.add(f"[green][bold]+ {self.head_ln.val(path)}[/bold] {elem}[/green]")
def red_dict(self, bdict: dict, path: str, tree: Tree) -> None:
if self.print_table:
self.table.add_row(
f"[bold]- {self.head_ln.val(path)}[/bold] {path}",
bdict[self.common_key],
"[bold magenta]VALUES BELOW[/bold magenta]",
style="red",
)
for k, v in bdict.items():
self.table.add_row(
f"[bold magenta]In Dict:[/bold magenta] {bdict[self.common_key]}",
k,
f"{v}",
style="red",
)
else:
dict_tree = tree.add(
f"[red][bold]- {self.base_ln.val(path)}[/bold] {bdict[self.common_key]}[/red]"
)
for k, v in bdict.items():
dict_tree.add(f"[red]- {k} : {v}[/red]")
def grn_dict(self, hdict: dict, path: str, tree: Tree) -> None:
if self.print_table:
self.table.add_row(
f"[bold]+ {self.head_ln.val(path)}[/bold] {path}",
hdict[self.common_key],
"[bold magenta]VALUES BELOW[/bold magenta]",
style="green",
)
for k, v in hdict.items():
self.table.add_row(
f"[bold magenta]In Dict:[/bold magenta] {hdict[self.common_key]}",
k,
f"{v}",
style="green",
)
else:
dict_tree = tree.add(
f"[green][bold]+ {self.head_ln.val(path)}[/bold] {hdict[self.common_key]}[/green]"
)
for k, v in hdict.items():
dict_tree.add(f"[green]+ {k} : {v}[/green]")
@dataclass
class LineNos:
json_str: str
def __post_init__(self) -> None:
self.calc_obj = calculate(self.json_str)
def val(self, path: str) -> int:
return self.eval_path(path, "value")
def key(self, path: str) -> int:
return self.eval_path(path, "key")
def eval_path(self, path: str, type: str) -> "dict[int]":
key = type + "_start"
# '/path_parent/path_child': Entry(value_start=Location(line=int, column=int, position=int), value_end=L..., key_start=L..., key_end=L...)
# '/path_parent/path_child': {value_start : {line : int}, value_end: {...}, key_start: {...}, key_end: {...}}
return eval(f"{self.calc_obj[path]}")[key]
def Entry(**kwargs: str) -> dict:
return kwargs
def Location(**kwargs: str) -> int:
return kwargs["line"]
# gets symmetric difference while maintaining original group membership
def symmetric_diff_sep(
set1: "set[str]", set2: "set[str]"
) -> "tuple[set[str], set[str]]":
exclusive_set1 = set1 - set2
exclusive_set2 = set2 - set1
return exclusive_set1, exclusive_set2
def print_custom(path: str) -> None:
if re.search("\/custom\/.*\.json", path):
console.log(f"[bold magenta]Custom File[/bold magenta]")
if __name__ == "__main__":
# https://github.com/azrafe7/test_python_rich_on_gh_pages/blob/0015be3b6b1925c4d4c9acbaed319666ff7cec89/main.py
console = Console(force_terminal=True, width=150, log_time=False, log_path=False)
old_path = sys.argv[2]
old_hex = sys.argv[3]
new_path = sys.argv[5]
new_hex = sys.argv[6]
table_mode = int(os.getenv('TABLE_MODE'))
try:
if old_path.split('.')[-1] == 'json' or new_path.split('.')[-1] == 'json':
if new_hex == '.':
console.log(f"[bold][red]Removed: {old_path.split('/')[-1]}[/red][/bold]")
else:
diff = FrappeDiff(old_path, new_path, table_mode)
diff.prep()
diff.print()
except Exception as e:
print(e)
console.log("\n")