-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc2snake.py
executable file
·209 lines (169 loc) · 7.44 KB
/
cc2snake.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
#!/usr/bin/env python
# SPDX-License-Identifier: MIT
# -*- coding: utf-8 -*-
"""Interactive camleCase to snake_case conversion tool"""
import os
import re
import sys
import json
import tempfile
import shutil
from argparse import ArgumentParser, RawTextHelpFormatter
class Main(object):
HIST_FILE = ".cc2snake_history"
FILE_FILTER = r'^\w+$|(\.(sh|py|cpp|hpp|c|h|)$)'
def __init__(self):
self._history = {}
help_text = "By default, every decision you make is recorded and "
help_text += "applied the next time\nyou run the program from the "
help_text += "same directory.\n"
help_text += "The default history file is: ./{}\n"
help_text += "Remove it to start over or edit it as you please!\n"
help_text += "To automatically convert all files in tree run: {} -ya"
help_text = help_text.format(self.HIST_FILE,
os.path.basename(sys.argv[0]))
parser = ArgumentParser(
description=help_text, formatter_class=RawTextHelpFormatter)
parser.add_argument("-l", "--load-hist", dest="load_filenames",
action="append",
help="history file(s) to load")
parser.add_argument("-i", "--ignore-file", dest="ignore_filenames",
action="append",
help="a list of names (one per line) to ignore")
parser.add_argument("-d", "--dump-hist", dest="dump_filename",
help="file to dump history to")
parser.add_argument("-a", "--all-files", dest="all",
action="store_true",
help="process all files in tree")
parser.add_argument("-f", "--files", dest="files",
action="append",
help="files to process")
parser.add_argument("-p", "--python-mode", dest="python_mode",
action="store_true",
help="process Python files according to PEP 8")
parser.add_argument("-y", "--yes", dest="yes",
action="store_true",
help="convert everything")
parser.add_argument("-n", "--no", dest="no",
action="store_true",
help="don't convert anything (create history file "
"for manual editing)")
parser.add_argument("--file-filter", dest="filter",
help="only process files matching (Python) "
"regular expression, default:\n " +
self.FILE_FILTER)
self._args = parser.parse_args()
def run(self):
self._load_history()
paths = []
if self._args.files:
paths += self._args.files
else:
cwd = os.getcwd()
for root, dirnames, filenames in os.walk(cwd):
filenames = [f for f in filenames if not f[0] == '.']
dirnames[:] = [d for d in dirnames if not d[0] == '.']
for name in filenames:
paths.append(os.path.join(root, name))
answ = ''
for path in paths:
file_name = os.path.basename(path)
if re.search(r'^[\.#_]', file_name):
continue
file_filter = self.FILE_FILTER
if self._args.filter is not None:
file_filter = self._args.filter
if re.search(file_filter, file_name) is None:
continue
if not self._args.all:
answ = input("Edit {0} ?\n(y, n, q) [y]: ".format(path))
if not answ:
answ = 'y'
if answ == 'q':
break
elif answ != 'y':
continue
# Determine line endings
with open(path, 'rU') as f:
f.readline()
newline = f.newlines
# Create temp file by copying to preserve permissions
fh_tmp, path_tmp = tempfile.mkstemp()
os.close(fh_tmp)
shutil.copy(path, path_tmp)
new_file = open(path_tmp, 'w', newline=newline)
old_file = open(path, 'rU')
line_number = 1
for line in old_file:
names = filter(None, re.findall('(\w*[a-z0-9][A-Z]\w*)*', line))
for name in names:
if name.startswith("0x"):
continue
if self._args.python_mode and name[0].isupper():
continue
if name in self._history:
new_name = self._history[name]
answ = 'y'
else:
new_name = re.sub(
'([a-z0-9])([A-Z])', r'\1_\2', name).lower()
answ = ''
if self._args.yes:
answ = 'y'
elif self._args.no:
answ = 'no'
while not (answ == 'y' or answ == 'n'):
print("\n{0}: {1}".format(line_number, line))
answ = input(
"Replace {0} with {1} (y, n, e, l) [y]: "
.format(name, new_name))
if answ == 'e':
tmp_name = new_name
tmp_name = input(
"Enter the new name [{0}]: "
.format(new_name))
if tmp_name:
new_name = tmp_name
answ = 'y'
elif not answ:
answ = 'y'
if answ == 'y':
self._history[name] = new_name
line = line.replace(name, new_name)
else:
self._history[name] = name
new_file.write(line)
line_number += 1
new_file.close()
old_file.close()
# Remove original file
os.remove(path)
# Move new file
shutil.move(path_tmp, path)
self._dump_history()
print("done")
def _load_history(self):
filenames = [self.HIST_FILE]
if self._args.load_filenames is not None:
filenames = self._args.load_filenames
for filename in filenames:
if os.path.isfile(filename):
with open(filename, 'r') as hist_file:
self._history.update(json.load(hist_file))
if self._args.ignore_filenames is not None:
for filename in self._args.ignore_filenames:
ignore_file = open(filename, 'r')
for line in ignore_file:
name = line.rstrip('\n')
if name:
self._history[name] = name
ignore_file.close()
def _dump_history(self):
dump_filename = self.HIST_FILE
if self._args.dump_filename is not None:
dump_filename = self._args.dump_filename
with open(dump_filename, 'w') as dump_file:
json.dump(self._history, dump_file, indent=4)
if __name__ == "__main__":
main = Main()
sys.exit(main.run())