mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-07 04:26:52 +00:00
Merge pull request #1 from pre-commit/meybs_fixey_fixey
Simplify string-fixer
This commit is contained in:
commit
fed386438e
1 changed files with 9 additions and 25 deletions
|
|
@ -8,7 +8,7 @@ import tokenize
|
||||||
|
|
||||||
|
|
||||||
double_quote_starts = tuple(s for s in tokenize.single_quoted if '"' in s)
|
double_quote_starts = tuple(s for s in tokenize.single_quoted if '"' in s)
|
||||||
compiled_tokenize_string = re.compile(tokenize.String)
|
compiled_tokenize_string = re.compile('(?<!")' + tokenize.String + '(?!")')
|
||||||
|
|
||||||
|
|
||||||
def handle_match(m):
|
def handle_match(m):
|
||||||
|
|
@ -19,34 +19,18 @@ def handle_match(m):
|
||||||
meat = string[len(double_quote_start):-1]
|
meat = string[len(double_quote_start):-1]
|
||||||
if '"' in meat or "'" in meat:
|
if '"' in meat or "'" in meat:
|
||||||
break
|
break
|
||||||
return (
|
return double_quote_start.replace('"', "'") + meat + "'"
|
||||||
double_quote_start.replace('"', "'") +
|
|
||||||
string[len(double_quote_start):-1] +
|
|
||||||
"'"
|
|
||||||
)
|
|
||||||
return string
|
return string
|
||||||
|
|
||||||
|
|
||||||
def fix_strings(filename):
|
def fix_strings(filename):
|
||||||
return_value = 0
|
contents = open(filename).read()
|
||||||
|
new_contents = compiled_tokenize_string.sub(handle_match, contents)
|
||||||
lines = []
|
retval = int(new_contents != contents)
|
||||||
|
if retval:
|
||||||
with open(filename, 'r') as read_handle:
|
|
||||||
for line in read_handle:
|
|
||||||
if '"""' in line:
|
|
||||||
# Docstrings are hard, fuck it
|
|
||||||
lines.append(line)
|
|
||||||
else:
|
|
||||||
result = re.sub(compiled_tokenize_string, handle_match, line)
|
|
||||||
lines.append(result)
|
|
||||||
return_value |= int(result != line)
|
|
||||||
|
|
||||||
with open(filename, 'w') as write_handle:
|
with open(filename, 'w') as write_handle:
|
||||||
for line in lines:
|
write_handle.write(new_contents)
|
||||||
write_handle.write(line)
|
return retval
|
||||||
|
|
||||||
return return_value
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv=None):
|
def main(argv=None):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue