-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename_fields.py
51 lines (41 loc) · 1.3 KB
/
rename_fields.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
import os
def capitalize(word):
return word[0].upper() + word[1:]
def snake_to_camel(snake):
words = snake.split('_')
return ''.join([words[0], *[capitalize(word) for word in words[1:]]])
BASE_DIR = os.path.join(os.getcwd(), 'src', 'api-client')
SEARCH_DIRS = [
os.path.join(BASE_DIR, 'apis'),
os.path.join(BASE_DIR, 'models'),
]
SNAKE_RENAME_FIELDS = [
'start_time',
'end_time',
'created_at',
'updated_at',
'typical_percentage',
'typical_monthly_amount',
'category_relations',
'total_amount',
'is_percentage',
'first_name',
'last_name'
]
CAMEL_RENAME_FIELDS = [
snake_to_camel(field) for field in SNAKE_RENAME_FIELDS
]
def rename_fields():
for dir in SEARCH_DIRS:
for file in os.listdir(dir):
absolute_path = os.path.join(dir, file)
lines = []
with open(absolute_path, 'r') as input_file:
for line in input_file.readlines():
for snake_field, camel_field in zip(SNAKE_RENAME_FIELDS, CAMEL_RENAME_FIELDS):
line = line.replace(camel_field, snake_field)
lines.append(line)
with open(absolute_path, 'w') as output_file:
output_file.writelines(lines)
if __name__ == '__main__':
rename_fields()