-
Notifications
You must be signed in to change notification settings - Fork 0
/
camel2underscore.py
executable file
·248 lines (190 loc) · 6.22 KB
/
camel2underscore.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
#!python
# Copyright 2020, 2021, 2022, 2023, 2024 Mihai Gătejescu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
from re import sub
from sys import argv, path, exit as sys_exit
import os
import json
def is_underscore_notation(value):
return '_' in value.strip('_')
def first_char_is_underscore(output):
return output[0] == '_'
def last_char_is_underscore(output):
return output[-1] == '_'
def remove_first_character(output):
return output[1:]
def remove_last_character(output):
return output[:-1]
def remove_underscores_from_start_and_end(output):
while first_char_is_underscore(output):
output = remove_first_character(output)
while last_char_is_underscore(output):
output = remove_last_character(output)
return output.lower()
def read_input_from_file(input_file='input_file.txt'):
output = []
def remove_newlines_from_list(text):
result = []
for line in text:
result.append(remove_last_character(line))
return result
try:
input_file = open(os.path.join(path[0], input_file))
output = input_file.readlines()
input_file.close()
output = remove_newlines_from_list(output)
except FileNotFoundError:
print('File not found')
sys_exit()
return output
def switch_cases(text):
if text.islower():
return text.upper()
else:
return text.lower()
def convert_text_to_code(text):
result = sub('\n+', '\n', text)
result = "'" + result
result = result.replace('\n', "',\n'")
return result[:-3] + '\n'
def list_to_text(a_list):
result = ''
for i in a_list:
result += i + '\n'
return result
def remove_whitespaces(plain_text):
plain_text = plain_text.replace('\t', '')
plain_text = sub('\n+', '\n', plain_text)
plain_text = sub(' +\n', '\n', plain_text)
plain_text = sub('\n +', '\n', plain_text)
return plain_text.lstrip()
def to_double_quote_list(text):
result = remove_whitespaces(text)
result = result.replace('\n', '", "')
return '["' + result[:-3] + ']'
def to_single_quote_list(text):
result = remove_whitespaces(text)
result = result.replace('\n', '\', \'')
return '[\'' + result[:-3] + ']'
def convert_to_text(data):
def remove(text, characters):
for ch in characters:
text = text.replace(ch, '')
return text
result = data.replace('\'', '"')
result = result.replace('\n', '')
result = result.replace('", ', '\n')
result = result.replace('",', '\n')
result = remove(result, '[]{}"')
return result
def convert_camel_2_underscore(input_values_list):
previous_was_digit = False
previous_was_uppercase = False
inside_acronym = False
output = ''
for value in input_values_list:
if is_underscore_notation(value):
continue
line = ''
for char in value:
if char.isdigit():
if not previous_was_digit:
line += '_'
previous_was_digit = True
elif char.isupper():
if not previous_was_uppercase:
line += '_'
else:
inside_acronym = True
previous_was_uppercase = True
elif char.islower():
if inside_acronym:
line = line[:-1] + '_' + line[-1:]
previous_was_uppercase = False
previous_was_digit = False
inside_acronym = False
line += char
line = remove_underscores_from_start_and_end(line)
output += line + '\n'
return remove_last_character(output)
def list_to_lowercase(columns):
result = []
for i in columns:
result.append(i.lower())
return result
def compare_columns(columns1, columns2):
columns1 = list_to_lowercase(columns1)
columns2 = list_to_lowercase(columns2)
return set(columns1) == set(columns2)
# TODO
# I will never load the columns from files
# This probably has to be removed
def get_and_compare_columns(source1, source2):
def get_columns(source):
with open(source, 'r') as source:
result = json.load(source)
return result
columns1 = get_columns(source1)
columns2 = get_columns(source2)
return compare_columns(columns1, columns2)
def split_into_two_columns(text, items_in_column=2):
text = sub('^ *', '', text)
text = sub(' +', ' ', text)
result = ''
count = 0
for character in text:
if character == ' ':
count += 1
if count == items_in_column:
count = 0
character = '\n'
result += character
return result
def get_first_column(text):
text = remove_whitespaces(text)
return sub(' .*$', '', text, flags=re.MULTILINE)
def order_lines(text):
text = remove_whitespaces(text)
text = text.split('\n')
text.sort()
result = ''
for i in text:
if i != '':
result += i + '\n'
return result
def two_columns(text):
result = ''
text = text.split('\n')
for i in range(len(text)):
if i != 0 and i % 2 == 0:
result = result + '\n'
elif i % 2 != 0:
result = result + ' '
result = result + text[i]
return result
def remove_header(text):
result = sub(r'^.*\n', '', text, count=1)
return result
if __name__ == '__main__':
if len(argv) > 1:
input_values = read_input_from_file(argv[1])
else:
input_values = read_input_from_file()
print(convert_camel_2_underscore(input_values))
# TODO
# Refactor around to single/double quote lists
# Check if the values are not already underscored
# ^ this will need logging
# Include help functionality