forked from phorward/viur-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviur-2to3.py
executable file
·116 lines (100 loc) · 2.88 KB
/
viur-2to3.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
#!/usr/bin/python3
"""
Naive ViUR3 project porting script with a simple search & replace mechanism using lookup table.
"""
import os, argparse, difflib
# Naive lookup table. Could be done better later...
lookup = {
"onItemAdded": "onAdded",
"onItemEdited": "onEdited",
"onItemDeleted": "onDeleted",
"addItemSuccess": "addSuccess",
"editItemSuccess": "editSuccess",
"from server import": "from viur.core import"
}
bones = [
"base",
"boolean",
"captcha",
"color",
"credential",
"date",
"email",
"file",
"key",
"numeric",
"password",
"randomslice",
"raw",
"record",
"relational",
"selectcountry",
"select",
"sortindex",
"spatial",
"string",
"text",
"treeleaf",
"treenode",
"user"
]
lookup.update({
f"{name}Bone": f"{name[0].upper()}{name[1:]}Bone" for name in bones
})
if __name__ == "__main__":
# Get arguments
ap = argparse.ArgumentParser(
description="ViUR2-to-ViUR3 porting tool"
)
ap.add_argument(
"project_root",
type=str,
help="ViUR project root"
)
ap.add_argument(
"-d", "--dryrun",
action="store_true",
help="Dry-run for testing, don't modify files"
)
ap.add_argument(
"-x", "--daredevil",
action="store_true",
help="Don't make backups of files, just replace and deal with it"
)
args = ap.parse_args()
# Iterate all files in current folder
for root, dirs, files in os.walk(args.project_root):
# Ignore ViUR library folders
if any(ignore in root for ignore in ["viur", "flare", "html5"]):
continue
for filename in files:
# Ignore anything without a .py-extension
ext = os.path.splitext(filename)[1].lower()[1:]
if ext not in ["py"]:
continue
filename = os.path.join(root, filename)
with open(filename, "r") as f:
original_content = content = f.read()
count = 0
for k, v in lookup.items():
if k in content:
content = content.replace(k, v)
count += 1
if count:
if not args.dryrun:
if not args.daredevil:
os.rename(filename, filename + ".bak")
with open(filename, "w") as f:
f.write(content)
print("Modified %r" % filename)
else:
print(
"\n".join(
difflib.unified_diff(
original_content.splitlines(),
content.splitlines(),
filename,
filename
)
)
)