mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-12 14:04:17 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -3,8 +3,10 @@
|
|||
provides the TextFile class, which gives an interface to text files
|
||||
that (optionally) takes care of stripping comments, ignoring blank
|
||||
lines, and joining lines with backslashes."""
|
||||
from __future__ import annotations
|
||||
|
||||
import sys, io
|
||||
import io
|
||||
import sys
|
||||
|
||||
|
||||
class TextFile:
|
||||
|
|
@ -66,14 +68,14 @@ class TextFile:
|
|||
an all-whitespace line), if 'rstrip_ws' is true but 'skip_blanks' is
|
||||
not."""
|
||||
|
||||
default_options = { 'strip_comments': 1,
|
||||
'skip_blanks': 1,
|
||||
'lstrip_ws': 0,
|
||||
'rstrip_ws': 1,
|
||||
'join_lines': 0,
|
||||
'collapse_join': 0,
|
||||
'errors': 'strict',
|
||||
}
|
||||
default_options = {'strip_comments': 1,
|
||||
'skip_blanks': 1,
|
||||
'lstrip_ws': 0,
|
||||
'rstrip_ws': 1,
|
||||
'join_lines': 0,
|
||||
'collapse_join': 0,
|
||||
'errors': 'strict',
|
||||
}
|
||||
|
||||
def __init__(self, filename=None, file=None, **options):
|
||||
"""Construct a new TextFile object. At least one of 'filename'
|
||||
|
|
@ -112,7 +114,7 @@ class TextFile:
|
|||
"""Open a new file named 'filename'. This overrides both the
|
||||
'filename' and 'file' arguments to the constructor."""
|
||||
self.filename = filename
|
||||
self.file = io.open(self.filename, 'r', errors=self.errors)
|
||||
self.file = open(self.filename, errors=self.errors)
|
||||
self.current_line = 0
|
||||
|
||||
def close(self):
|
||||
|
|
@ -128,16 +130,16 @@ class TextFile:
|
|||
outmsg = []
|
||||
if line is None:
|
||||
line = self.current_line
|
||||
outmsg.append(self.filename + ", ")
|
||||
outmsg.append(self.filename + ', ')
|
||||
if isinstance(line, (list, tuple)):
|
||||
outmsg.append("lines %d-%d: " % tuple(line))
|
||||
outmsg.append('lines %d-%d: ' % tuple(line))
|
||||
else:
|
||||
outmsg.append("line %d: " % line)
|
||||
outmsg.append('line %d: ' % line)
|
||||
outmsg.append(str(msg))
|
||||
return "".join(outmsg)
|
||||
return ''.join(outmsg)
|
||||
|
||||
def error(self, msg, line=None):
|
||||
raise ValueError("error: " + self.gen_error(msg, line))
|
||||
raise ValueError('error: ' + self.gen_error(msg, line))
|
||||
|
||||
def warn(self, msg, line=None):
|
||||
"""Print (to stderr) a warning message tied to the current logical
|
||||
|
|
@ -147,7 +149,7 @@ class TextFile:
|
|||
the current line number; it may be a list or tuple to indicate a
|
||||
range of physical lines, or an integer for a single physical
|
||||
line."""
|
||||
sys.stderr.write("warning: " + self.gen_error(msg, line) + "\n")
|
||||
sys.stderr.write('warning: ' + self.gen_error(msg, line) + '\n')
|
||||
|
||||
def readline(self):
|
||||
"""Read and return a single logical line from the current file (or
|
||||
|
|
@ -186,13 +188,13 @@ class TextFile:
|
|||
# unescape it (and any other escaped "#"'s that might be
|
||||
# lurking in there) and otherwise leave the line alone.
|
||||
|
||||
pos = line.find("#")
|
||||
if pos == -1: # no "#" -- no comments
|
||||
pos = line.find('#')
|
||||
if pos == -1: # no "#" -- no comments
|
||||
pass
|
||||
|
||||
# It's definitely a comment -- either "#" is the first
|
||||
# character, or it's elsewhere and unescaped.
|
||||
elif pos == 0 or line[pos-1] != "\\":
|
||||
elif pos == 0 or line[pos - 1] != '\\':
|
||||
# Have to preserve the trailing newline, because it's
|
||||
# the job of a later step (rstrip_ws) to remove it --
|
||||
# and if rstrip_ws is false, we'd better preserve it!
|
||||
|
|
@ -209,17 +211,19 @@ class TextFile:
|
|||
# # comment that should be ignored
|
||||
# there
|
||||
# result in "hello there".
|
||||
if line.strip() == "":
|
||||
if line.strip() == '':
|
||||
continue
|
||||
else: # it's an escaped "#"
|
||||
line = line.replace("\\#", "#")
|
||||
else: # it's an escaped "#"
|
||||
line = line.replace('\\#', '#')
|
||||
|
||||
# did previous line end with a backslash? then accumulate
|
||||
if self.join_lines and buildup_line:
|
||||
# oops: end of file
|
||||
if line is None:
|
||||
self.warn("continuation line immediately precedes "
|
||||
"end-of-file")
|
||||
self.warn(
|
||||
'continuation line immediately precedes '
|
||||
'end-of-file',
|
||||
)
|
||||
return buildup_line
|
||||
|
||||
if self.collapse_join:
|
||||
|
|
@ -230,11 +234,13 @@ class TextFile:
|
|||
if isinstance(self.current_line, list):
|
||||
self.current_line[1] = self.current_line[1] + 1
|
||||
else:
|
||||
self.current_line = [self.current_line,
|
||||
self.current_line + 1]
|
||||
self.current_line = [
|
||||
self.current_line,
|
||||
self.current_line + 1,
|
||||
]
|
||||
# just an ordinary line, read it as usual
|
||||
else:
|
||||
if line is None: # eof
|
||||
if line is None: # eof
|
||||
return None
|
||||
|
||||
# still have to be careful about incrementing the line number!
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue