forked from geodynamics/calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert_code_header.py
272 lines (230 loc) · 9.01 KB
/
insert_code_header.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#! /usr/bin/env python3
import argparse
import os
import tempfile
def line_comment(header, token):
return token + (os.linesep + token).join(
header.strip().splitlines()) + os.linesep
def block_comment(header, start_token, end_token):
return os.linesep.join(
[start_token, header.strip(), end_token]) + os.linesep
def check_header_for_block_tokens(header, start_token, end_token):
if end_token in header:
raise ValueError(
"Header string contains a token that end block comments.")
def interactive_interface(header, file):
def print_help():
print("Possible Input:")
print(" ?,h,help - Display this help.")
print(" q,quit - Quit this program.")
print(" y,yes - Remove displayed lines and add header to file.")
print(" n,no - Add header to file without removing any lines.")
print(" %N - Remove %N lines from file and add header.")
print(" s,show %N - Show %N lines from the current file and\n"
" reprompt for input.")
print(" a,all y,yes - Remove displayed lines from this file and\n"
" the same number of lines from all remaining\n"
" files while adding the header.")
print(" a,all n,no - Add header to this file and all remaining\n"
" files without removing any lines.")
print(" a,all %N - Remove %N line from this file and all\n"
" remaining files while adding the header.")
num_lines = len(header.splitlines())
while True:
print(file.name + ":")
for n in range(num_lines):
line = file.readline()
if line:
print(("{:0" + str(len(str(num_lines))) + "} ").format(n + 1)
+ line.strip("\n"))
else:
print()
print("EOF")
num_lines = n
break
else:
if file.readline():
print("...")
file.seek(0)
choice = input("-> ").lower().split()
if len(choice) == 1:
if choice[0] == "y" or choice[0] == "yes":
pass
elif choice[0] == "n" or choice[0] == "no":
num_lines = 0
elif choice[0] == "?" or choice[0] == "h" or choice[0] == "help":
print_help()
continue
elif choice[0] == "q" or choice[0] == "quit":
exit(0)
else:
try:
num_lines = int(choice[0])
except ValueError:
print("ERROR: unrecognized input.")
print_help()
continue
for x in range(num_lines):
file.readline()
return None
elif len(choice) == 2:
if choice[0] == "a" or choice[0] == "all":
if choice[1] == "y" or choice[1] == "yes":
pass
elif choice[1] == "n" or choice[1] == "no":
num_lines = 0
else:
try:
num_lines = int(choice[1])
except ValueError:
print("ERROR: unrecognized input.")
print_help()
continue
for x in range(num_lines):
file.readline()
return num_lines
elif choice[0] == "s" or choice[0] == "show":
try:
num_lines = int(choice[1])
except ValueError:
print("ERROR: unrecognized input.")
print_help()
continue
def create_main_parser():
parser = argparse.ArgumentParser(
description=("Insert header text taken from a file into one or more"
" files."))
parser.add_argument(
type=argparse.FileType("rt"),
metavar="header-file",
dest="header_file",
help=("Path to header text file. - means read from stdin."),
)
previous_header_group = parser.add_argument_group(
title="header replacement",
description=("Choose if a header should replace lines at the top of"
" each file. Defaults to no-removal.")
).add_mutually_exclusive_group()
previous_header_group.add_argument(
"-d",
"--no-removal",
action="store_true",
dest="no_removal",
help=("Don't remove any lines when adding the header."),
)
previous_header_group.add_argument(
"-r",
"--replace-lines",
metavar="N",
type=int,
dest="num_lines",
help=("Remove the first N lines in each file when adding the header."),
)
previous_header_group.add_argument(
"-o",
"--overwrite",
action="store_true",
dest="overwrite",
help=("Remove an amount of lines equal to the amount of lines in the"
" provided header."),
)
previous_header_group.add_argument(
"-i",
"--interactive",
action="store_true",
dest="interactive_removal",
help=("For each file, display the first few lines and ask how many"
" lines should be removed when adding the header."),
)
code_comment_group = parser.add_argument_group(
title="code comment formatting",
description=("Provides very basic support for formatting a header as"
" either single line comments or a block comment."
" Defaults to no-commenting."),
).add_mutually_exclusive_group()
code_comment_group.add_argument(
"-n",
"--no-commenting",
action="store_true",
dest="no_commenting",
help=("Insert the header as is. This choice is for headers that are"
" already formatted for insertion into the provided files."),
)
code_comment_group.add_argument(
"-l",
"--line-comments",
nargs=1,
metavar="SYMBOL",
dest="line_token",
help=("Insert the header as a series of single line comments. SYMBOL"
" is the string token used to start a single line comment."),
)
code_comment_group.add_argument(
"-b",
"--block-comment",
nargs=2,
metavar=("START-SYMBOL", "END-SYMBOL"),
dest="block_tokens",
help=("Insert the header as a single block comment. START-SYMBOL is"
" the string token used to start a block comment. END-SYMBOL is"
" the token used to end a block comment."),
)
parser.add_argument(
"-f",
"--force-block-comment",
action="store_true",
dest="ignore_block_warning",
help=("Ignore warning when formatting a block comment about the header"
" text containing an END-SYMBOL."),
)
parser.add_argument(
nargs="+",
metavar="file",
dest="files",
help=("Files to be modified by adding the header. Be sure not to mix"
" file types if using the code comment formatting."),
)
return parser
def main(args):
with args.header_file as header_file:
header = ""
for line in header_file:
header += line
if args.block_tokens:
try:
check_header_for_block_tokens(header, *args.block_tokens)
except ValueError:
if not args.ignore_block_warning:
print(("ERROR: Header text contains the token, {}, for ending"
" a block comment. Please use the --force-block-comment"
" flag to ignore this error.").format(
args.block_tokens[1]))
exit(1)
for filename in args.files:
with tempfile.NamedTemporaryFile('wt', delete=False) as tmp_file:
with open(filename, "rt") as file:
if args.line_token:
header = line_comment(header, *args.line_token)
elif args.block_tokens:
header = block_comment(header, *args.block_tokens)
else: # args.no_commenting
pass
if args.overwrite:
args.num_lines = len(header.splitlines())
args.overwrite = None
if args.num_lines:
for x in range(args.num_lines):
file.readline()
elif args.interactive_removal:
num_lines = interactive_interface(header, file)
if num_lines is not None:
args.num_lines = num_lines
args.interactive_removal = None
else: # args.no_removal
pass
tmp_file.write(header)
for line in file:
tmp_file.write(line)
os.rename(tmp_file.name, file.name)
if __name__ == "__main__":
main(create_main_parser().parse_args())