-
Notifications
You must be signed in to change notification settings - Fork 1
/
code_refactor_mappage.py
65 lines (50 loc) · 2.21 KB
/
code_refactor_mappage.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
import os
import logging
SDK_REPO = 'c:/github/azure-sdk-for-java/sdk/resourcemanager'
KEYWORD = '.mapPage('
def refactor_java(file_path: str):
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
found = False
locations = []
for location, line in enumerate(lines):
if KEYWORD in line:
found = True
locations.append(location)
if found:
logging.info(f'refactor java at {file_path}')
for location in reversed(locations):
line = lines[location]
if line.strip() == KEYWORD:
lines.pop(location)
lines[location-1] = lines[location-1].rstrip() + ',\n'
elif line.strip().startswith(KEYWORD):
lines[location] = line.replace(KEYWORD, '')
lines[location-1] = lines[location-1].rstrip() + ',\n'
else:
lines[location] = line.replace(KEYWORD, ', ')
for rel in range(0, -15, -1):
line = lines[location+rel]
if line.strip().startswith('return '):
lines[location+rel] = line.replace('return ', 'return PagedConverter.mapPage(')
break
importInsertLocation = -1
for reversed_location, line in enumerate(reversed(lines)):
if line.strip() == 'import com.azure.resourcemanager.resources.fluentcore.utils.PagedConverter;':
importInsertLocation = -1
break
if line.startswith('import ') and importInsertLocation == -1:
importInsertLocation = len(lines) - reversed_location
if importInsertLocation >= 0:
lines.insert(importInsertLocation,
'import com.azure.resourcemanager.resources.fluentcore.utils.PagedConverter;\n')
with open(file_path, 'w', encoding='utf-8') as f:
f.write(''.join(lines))
def main():
logging.basicConfig(level=logging.INFO)
for root, dirs, files in os.walk(SDK_REPO):
for name in files:
file_path = os.path.join(root, name)
if os.path.splitext(file_path)[1] == '.java' and 'PagedConverter' not in file_path:
refactor_java(file_path)
main()