Skip to content

Commit c1ed937

Browse files
run black
1 parent 1bbbe78 commit c1ed937

7 files changed

Lines changed: 1274 additions & 1177 deletions

File tree

umdp3_fixer/ampersands.py

Lines changed: 117 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# For further details please refer to the file COPYRIGHT.txt
55
# which you should have received as part of this distribution.
66
# *****************************COPYRIGHT*******************************
7-
'''
7+
"""
88
## NOTE ##
99
This module is one of several for which the Master copy is in the
1010
UM repository. When making changes, please ensure the changes are made in the UM
@@ -25,7 +25,7 @@
2525
may be some cases which are missed. These lines will be left without applying
2626
the ampersand shifting, and will be flagged, optionally with a message in
2727
stdout.
28-
'''
28+
"""
2929
import sys
3030
import re
3131
import traceback
@@ -37,21 +37,25 @@
3737

3838

3939
class CharError(ParsingError):
40-
'''
40+
"""
4141
Raised when there are an unexpected number of a certain char in a line.
42-
'''
42+
"""
43+
4344
def __init__(self, char, number):
4445
self.number = number
4546
self.char = char
46-
self.msg = "There are {0:d} unquoted, uncommented " \
47-
"\"{1:s}\" in this line.".format(number, char)
47+
self.msg = (
48+
"There are {0:d} unquoted, uncommented "
49+
'"{1:s}" in this line.'.format(number, char)
50+
)
51+
4852
pass
4953

5054

5155
def print_message(errtype, msg, iline=None, line=None, fname=None):
52-
'''
56+
"""
5357
Print a formatted message
54-
'''
58+
"""
5559
if fname is None:
5660
fnamestr = ""
5761
else:
@@ -70,13 +74,15 @@ def print_message(errtype, msg, iline=None, line=None, fname=None):
7074
else:
7175
linestr = ": {0:s}".format(line)
7276

73-
print("{0:s}{1:s}{2:s} - {3:s}{4:s}".format(fnamestr, ilinestr, errtype,
74-
msg, linestr))
77+
print(
78+
"{0:s}{1:s}{2:s} - {3:s}{4:s}".format(fnamestr, ilinestr, errtype, msg, linestr)
79+
)
7580

7681

77-
def shift_ampersand(line, line_previous, str_continuation, col=DEFAULT_COL,
78-
preclean=False):
79-
'''
82+
def shift_ampersand(
83+
line, line_previous, str_continuation, col=DEFAULT_COL, preclean=False
84+
):
85+
"""
8086
Check if the line contains an ampersand.
8187
If so then set location of ampersand to col so as to be consistent
8288
Sometimes there are comments after the ampersand, in this case keep
@@ -85,7 +91,7 @@ def shift_ampersand(line, line_previous, str_continuation, col=DEFAULT_COL,
8591
comment. If the line is still too long, then reduce whitespace between
8692
the end of the code and the ampersand until the comment fits within the
8793
required line length.
88-
'''
94+
"""
8995

9096
# return earliy if there are no apersands at all.
9197
if "&" not in line:
@@ -106,11 +112,11 @@ def shift_ampersand(line, line_previous, str_continuation, col=DEFAULT_COL,
106112
stripline = workline.strip()
107113

108114
# Pre-processor lines start with #. Ignore them completely.
109-
pre_proc = (stripline[0] == "#")
115+
pre_proc = stripline[0] == "#"
110116

111117
# Lines that are completely commented start with a bang and are also
112118
# ignored completely (except if they are actually OpenMP)
113-
all_comment = (stripline[0] == "!")
119+
all_comment = stripline[0] == "!"
114120
omp_sentinal = all_comment and (stripline[1] == "$")
115121

116122
# Ignore empty lines or pre-processor directives
@@ -164,8 +170,9 @@ def shift_ampersand(line, line_previous, str_continuation, col=DEFAULT_COL,
164170
if omp_loc != -1:
165171
omp_continue = workline.upper().find("!$OMP&")
166172
if omp_continue != -1:
167-
workline = replace_characters(workline, [omp_continue+5], [1],
168-
replchar=" ")
173+
workline = replace_characters(
174+
workline, [omp_continue + 5], [1], replchar=" "
175+
)
169176
workline = replace_characters(workline, [omp_loc], [1], replchar="W")
170177

171178
# Find where there are ampersands or bangs within single or double
@@ -177,35 +184,37 @@ def shift_ampersand(line, line_previous, str_continuation, col=DEFAULT_COL,
177184
# If any ampersands or bangs were found within quotes, replace them
178185
# with "X" and "Y" respectively.
179186
if quoted_amp_locs is not None:
180-
lens = len(quoted_amp_locs) * [1, ]
181-
workline = replace_characters(workline, quoted_amp_locs, lens,
182-
replchar="X")
187+
lens = len(quoted_amp_locs) * [
188+
1,
189+
]
190+
workline = replace_characters(workline, quoted_amp_locs, lens, replchar="X")
183191
if quoted_bang_locs is not None:
184-
lens = len(quoted_bang_locs) * [1, ]
185-
workline = replace_characters(workline, quoted_bang_locs, lens,
186-
replchar="Y")
192+
lens = len(quoted_bang_locs) * [
193+
1,
194+
]
195+
workline = replace_characters(workline, quoted_bang_locs, lens, replchar="Y")
187196

188197
# Find where there are ampersands within comments and replace them with
189198
# "Z" temporarily.
190199
commented_amp_locs = find_commented_char(workline, "&", str_continuation)
191200

192201
if commented_amp_locs is not None:
193-
lens = len(commented_amp_locs) * [1, ]
194-
workline = replace_characters(workline, commented_amp_locs, lens,
195-
replchar="Z")
202+
lens = len(commented_amp_locs) * [
203+
1,
204+
]
205+
workline = replace_characters(workline, commented_amp_locs, lens, replchar="Z")
196206

197207
# Check if there is still more than one ampersand in this line and
198208
# warn if there is.
199-
if (len(re.findall("&", workline)) > 1):
209+
if len(re.findall("&", workline)) > 1:
200210
amp_loc = workline.find("&")
201211

202212
# determin if there is a leading ampersand
203213
beforeline = workline[lp:amp_loc].rstrip()
204214

205215
if len(beforeline) == 0:
206216
# the first ampersand is a leading continuation
207-
workline = replace_characters(workline, [amp_loc], [1],
208-
replchar=" ")
217+
workline = replace_characters(workline, [amp_loc], [1], replchar=" ")
209218
else:
210219
raise CharError("&", len(re.findall("&", workline)))
211220

@@ -220,17 +229,20 @@ def shift_ampersand(line, line_previous, str_continuation, col=DEFAULT_COL,
220229
# Now the locations of the characters that are needed have been found,
221230
# replace ampersands and bangs where they were.
222231
if quoted_amp_locs is not None:
223-
lens = len(quoted_amp_locs) * [1, ]
224-
workline = replace_characters(workline, quoted_amp_locs, lens,
225-
replchar="&")
232+
lens = len(quoted_amp_locs) * [
233+
1,
234+
]
235+
workline = replace_characters(workline, quoted_amp_locs, lens, replchar="&")
226236
if quoted_bang_locs is not None:
227-
lens = len(quoted_bang_locs) * [1, ]
228-
workline = replace_characters(workline, quoted_bang_locs, lens,
229-
replchar="!")
237+
lens = len(quoted_bang_locs) * [
238+
1,
239+
]
240+
workline = replace_characters(workline, quoted_bang_locs, lens, replchar="!")
230241
if commented_amp_locs is not None:
231-
lens = len(commented_amp_locs) * [1, ]
232-
workline = replace_characters(workline, commented_amp_locs, lens,
233-
replchar="&")
242+
lens = len(commented_amp_locs) * [
243+
1,
244+
]
245+
workline = replace_characters(workline, commented_amp_locs, lens, replchar="&")
234246
if omp_loc != -1:
235247
workline = replace_characters(workline, [omp_loc], [1], replchar="!")
236248

@@ -251,8 +263,7 @@ def shift_ampersand(line, line_previous, str_continuation, col=DEFAULT_COL,
251263

252264
if len(beforeline) == 0:
253265
# the ampersand is a leading continuations
254-
workline = replace_characters(workline, [amp_loc], [1],
255-
replchar=" ")
266+
workline = replace_characters(workline, [amp_loc], [1], replchar=" ")
256267
else:
257268
# Keep the part of the input line before the ampersand (without
258269
# white space).
@@ -287,7 +298,7 @@ def shift_ampersand(line, line_previous, str_continuation, col=DEFAULT_COL,
287298
# trailing whitespace.
288299
if len(workline.rstrip()) > col:
289300
comment = workline[comment_loc:].rstrip()
290-
workline = workline[:amp_loc+1]
301+
workline = workline[: amp_loc + 1]
291302
workline = " ".join([workline, comment])
292303

293304
# If the line is still too long, see if ampersand can be moved
@@ -312,10 +323,10 @@ def shift_ampersand(line, line_previous, str_continuation, col=DEFAULT_COL,
312323
# there is only one space left.
313324
for i in range(nloop):
314325

315-
if workline[amp_location-1] == " ":
326+
if workline[amp_location - 1] == " ":
316327
# If there is still whitespace that can be removed,
317328
# remove it and update the ampersand location.
318-
del workline[amp_location-1]
329+
del workline[amp_location - 1]
319330
amp_location -= 1
320331
else:
321332
# Ampersand is now next to no-blank text so place
@@ -330,20 +341,21 @@ def shift_ampersand(line, line_previous, str_continuation, col=DEFAULT_COL,
330341

331342

332343
def check_line_len(line, maxlinelen=DEFAULT_COL):
333-
'''
344+
"""
334345
Check line to see if it violates length requirements. If debugging,
335346
write some information to stdout.
336-
'''
347+
"""
337348

338-
return (len(line) > maxlinelen)
349+
return len(line) > maxlinelen
339350

340351

341-
def apply_ampersand_shift(lines, col=DEFAULT_COL, fname=None, debug=False,
342-
preclean=False):
343-
'''
352+
def apply_ampersand_shift(
353+
lines, col=DEFAULT_COL, fname=None, debug=False, preclean=False
354+
):
355+
"""
344356
For a lot of lines make sure any continuation ampersands are in the
345357
same column and return the result
346-
'''
358+
"""
347359

348360
not_parsed = []
349361
output_lines = []
@@ -354,14 +366,18 @@ def apply_ampersand_shift(lines, col=DEFAULT_COL, fname=None, debug=False,
354366

355367
for iline, line in enumerate(lines):
356368
try:
357-
outline = shift_ampersand(line, line_previous, str_continuation,
358-
col, preclean)
369+
outline = shift_ampersand(
370+
line, line_previous, str_continuation, col, preclean
371+
)
359372
except ParsingError as e:
360373
if debug:
361-
print_message("PARSING ERROR",
362-
"{0:s} Ampersand shifting has not been "
363-
"applied".format(e.msg), iline+1, line=line,
364-
fname=fname)
374+
print_message(
375+
"PARSING ERROR",
376+
"{0:s} Ampersand shifting has not been " "applied".format(e.msg),
377+
iline + 1,
378+
line=line,
379+
fname=fname,
380+
)
365381
outline = line
366382
not_parsed.append(iline)
367383

@@ -397,11 +413,10 @@ def apply_ampersand_shift(lines, col=DEFAULT_COL, fname=None, debug=False,
397413
return output_lines, not_parsed
398414

399415

400-
def apply_check_line_len(lines, fname=None, maxlinelen=DEFAULT_COL,
401-
debug=False):
402-
'''
416+
def apply_check_line_len(lines, fname=None, maxlinelen=DEFAULT_COL, debug=False):
417+
"""
403418
For a lot of lines check if any lines are longer than required
404-
'''
419+
"""
405420

406421
any_too_long = False
407422
ilines_too_long = []
@@ -411,9 +426,13 @@ def apply_check_line_len(lines, fname=None, maxlinelen=DEFAULT_COL,
411426
any_too_long = True
412427
ilines_too_long.append(iline)
413428
if debug:
414-
print_message("VIOLATION",
415-
"Line > {0:d} columns".format(maxlinelen),
416-
iline+1, line=line, fname=fname)
429+
print_message(
430+
"VIOLATION",
431+
"Line > {0:d} columns".format(maxlinelen),
432+
iline + 1,
433+
line=line,
434+
fname=fname,
435+
)
417436

418437
if any_too_long:
419438
return ilines_too_long
@@ -422,10 +441,11 @@ def apply_check_line_len(lines, fname=None, maxlinelen=DEFAULT_COL,
422441

423442

424443
def main():
425-
'''
444+
"""
426445
Main toplevel function for testing
427-
'''
428-
parser = OptionParser(usage="""
446+
"""
447+
parser = OptionParser(
448+
usage="""
429449
%prog [--column col] [--debug] file_1 [file_2 [file_3] ... ]
430450
431451
This script will attempt to manipulate white space to make sure ampersands
@@ -434,11 +454,16 @@ def main():
434454
If the line is still too long, it will minimise its length.
435455
436456
The optional --column tells which column should be used (default=80)
437-
""")
438-
parser.add_option("--column", dest="col", type="int", default=DEFAULT_COL,
439-
help="Column in which ampersands should appear")
440-
parser.add_option("--debug", action="store_true",
441-
help="Report useful information")
457+
"""
458+
)
459+
parser.add_option(
460+
"--column",
461+
dest="col",
462+
type="int",
463+
default=DEFAULT_COL,
464+
help="Column in which ampersands should appear",
465+
)
466+
parser.add_option("--debug", action="store_true", help="Report useful information")
442467

443468
(opts, args) = parser.parse_args()
444469

@@ -449,24 +474,29 @@ def main():
449474

450475
with open(input_file, "r+") as file_in:
451476
lines_in = file_in.read().split("\n")
452-
new_lines, not_parsed = apply_ampersand_shift(lines_in, opts.col,
453-
fname=input_file,
454-
debug=opts.debug)
477+
new_lines, not_parsed = apply_ampersand_shift(
478+
lines_in, opts.col, fname=input_file, debug=opts.debug
479+
)
455480
if opts.debug:
456481
if len(not_parsed) > 0:
457-
print_message("WARNING",
458-
"Ampersand alignment failed for some lines "
459-
"due to parsing errors. Please check lines and "
460-
"make sure they are correct.",
461-
fname=input_file)
462-
ilines_too_long = apply_check_line_len(new_lines, input_file,
463-
maxlinelen=opts.col,
464-
debug=True)
482+
print_message(
483+
"WARNING",
484+
"Ampersand alignment failed for some lines "
485+
"due to parsing errors. Please check lines and "
486+
"make sure they are correct.",
487+
fname=input_file,
488+
)
489+
ilines_too_long = apply_check_line_len(
490+
new_lines, input_file, maxlinelen=opts.col, debug=True
491+
)
465492
if ilines_too_long is not None:
466-
print_message("WARNING",
467-
"Some lines are longer than {0:d} characters. "
468-
"Please check and make them "
469-
"shorter.".format(opts.col), fname=input_file)
493+
print_message(
494+
"WARNING",
495+
"Some lines are longer than {0:d} characters. "
496+
"Please check and make them "
497+
"shorter.".format(opts.col),
498+
fname=input_file,
499+
)
470500

471501
file_in.seek(0)
472502
file_in.write("\n".join(new_lines))

0 commit comments

Comments
 (0)