pre-commit-hooks/pre_commit_hooks/string_fixer.py

94 lines
2.6 KiB
Python
Raw Permalink Normal View History

from __future__ import annotations
import argparse
2015-02-11 00:14:53 +08:00
import io
import re
import sys
import tokenize
from collections.abc import Sequence
if sys.version_info >= (3, 12): # pragma: >=3.12 cover
FSTRING_START = tokenize.FSTRING_START
FSTRING_END = tokenize.FSTRING_END
else: # pragma: <3.12 cover
FSTRING_START = FSTRING_END = -1
START_QUOTE_RE = re.compile('^[a-zA-Z]*"')
2020-02-06 03:10:42 +08:00
def handle_match(token_text: str) -> str:
2015-02-11 00:14:53 +08:00
if '"""' in token_text or "'''" in token_text:
return token_text
match = START_QUOTE_RE.match(token_text)
if match is not None:
meat = token_text[match.end():-1]
if '"' in meat or "'" in meat:
return token_text
else:
return match.group().replace('"', "'") + meat + "'"
else:
return token_text
2015-02-11 00:14:53 +08:00
def get_line_offsets_by_line_no(src: str) -> list[int]:
2015-02-11 00:14:53 +08:00
# Padded so we can index with line number
offsets = [-1, 0]
for line in src.splitlines(True):
offsets.append(offsets[-1] + len(line))
2015-02-11 00:14:53 +08:00
return offsets
2020-02-06 03:10:42 +08:00
def fix_strings(filename: str) -> int:
with open(filename, encoding='UTF-8', newline='') as f:
2018-06-18 15:00:38 +08:00
contents = f.read()
2015-02-11 00:14:53 +08:00
line_offsets = get_line_offsets_by_line_no(contents)
# Basically a mutable string
splitcontents = list(contents)
fstring_depth = 0
2015-02-11 00:14:53 +08:00
# Iterate in reverse so the offsets are always correct
2020-02-04 00:41:48 +08:00
tokens_l = list(tokenize.generate_tokens(io.StringIO(contents).readline))
tokens = reversed(tokens_l)
2015-02-11 00:14:53 +08:00
for token_type, token_text, (srow, scol), (erow, ecol), _ in tokens:
if token_type == FSTRING_START: # pragma: >=3.12 cover
fstring_depth += 1
elif token_type == FSTRING_END: # pragma: >=3.12 cover
fstring_depth -= 1
elif fstring_depth == 0 and token_type == tokenize.STRING:
2015-02-11 00:14:53 +08:00
new_text = handle_match(token_text)
splitcontents[
line_offsets[srow] + scol:
line_offsets[erow] + ecol
] = new_text
new_contents = ''.join(splitcontents)
if contents != new_contents:
2020-02-06 03:10:42 +08:00
with open(filename, 'w', encoding='UTF-8', newline='') as f:
f.write(new_contents)
2015-02-11 00:14:53 +08:00
return 1
else:
return 0
def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help='Filenames to fix')
args = parser.parse_args(argv)
retv = 0
for filename in args.filenames:
return_value = fix_strings(filename)
if return_value != 0:
2020-02-06 03:10:42 +08:00
print(f'Fixing strings in {filename}')
retv |= return_value
return retv
if __name__ == '__main__':
raise SystemExit(main())