-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefactor.py
88 lines (83 loc) · 2.21 KB
/
refactor.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
# Author: Pierce Brooks
import os
import sys
def run(source, target):
descriptor = open(source, "r")
lines = descriptor.readlines()
descriptor.close()
headers = []
objects = {}
object = None
for line in lines:
if (line.startswith("def ")):
break
if (line.startswith("class ")):
if not (object == None):
objects[object[0]] = object
object = []
if (object == None):
headers.append(line)
else:
object.append(line)
for key in objects:
last = -1
temp = ""
name = key.strip().split(" ")[1:][0].split("(")[0].strip()
if (name.upper() == name):
temp += name.lower()
else:
for i in range(len(name)):
if not (last == i-1):
if (name[i:(i+1)].upper() == name[i:(i+1)]):
last = i
temp += "_"
temp += name[i:(i+1)].lower()
if (temp.endswith("port")):
temp += "er"
print("from ."+temp+" import "+name+"\n")
temp += ".py"
path = os.path.join(os.getcwd(), target, temp)
descriptor = open(path, "w")
for header in headers:
descriptor.write(header)
for other in objects:
if (key == other):
continue
last = -1
temp = None
name = other.strip().split(" ")[1:][0].split("(")[0].strip()
for line in objects[key]:
if (name in line):
temp = ""
break
if (temp == None):
continue
if (name.upper() == name):
temp += name.lower()
else:
for i in range(len(name)):
if not (last == i-1):
if (name[i:(i+1)].upper() == name[i:(i+1)]):
last = i
temp += "_"
temp += name[i:(i+1)].lower()
if (temp.endswith("port")):
temp += "er"
descriptor.write("from ."+temp+" import "+name+"\n")
descriptor.write("\nfrom .utilities import *\n\n")
for line in objects[key]:
descriptor.write(line)
descriptor.close()
return 0
def launch(arguments):
if (len(arguments) < 3):
return False
source = arguments[1]
target = arguments[2]
result = run(source, target)
print(str(result))
if not (result == 0):
return False
return True
if (__name__ == "__main__"):
print(str(launch(sys.argv)))