Skip to content

Instantly share code, notes, and snippets.

@samuelcolvin
Last active September 20, 2016 20:56
Show Gist options
  • Save samuelcolvin/68318fa1c849e357463d4eb8d3348162 to your computer and use it in GitHub Desktop.
Save samuelcolvin/68318fa1c849e357463d4eb8d3348162 to your computer and use it in GitHub Desktop.
import re
import sys
from pathlib import Path
REGEXES = [(re.compile(p, f), r) for p, r, f in [
(r'^#(?: -\*-) coding: ?utf-8(?: -\*-)\n', '', 0),
(r"\.(encode|decode)\('utf-?8'\)", r'.\1()', 0),
('(class \w+)\(object\):\n', r'\1:\n', 0),
('super\(\w+, \w+\)\.', 'super().', 0),
('u"""(.*?)"""', r'"""\1"""', re.S),
("u?'(.*?)'", r"'\1'", 0),
('u?"(.*?)"', r'"\1"', 0),
('^from __future__.*?\n', '\n', re.M),
('^import six\n', '\n', re.M),
('^from six\.moves import (range|map|zip|filter|input)\n', '\n', re.M),
('^from six import StringIO\n', '\nfrom io import StringIO\n', re.M),
('^from six.moves.urllib.parse import urlparse', '\nfrom urllib.parse import urlparse', re.M),
('six.(text_type|string_types)', 'str', re.M),
('six.binary_type', 'bytes', 0),
('six.iteritems\((.+?)\)', r'\1.items()', 0),
('def __unicode__\(self\):', 'def __str__(self):', 0),
('\n +def __str__\(self\): *\n +return self.__unicode__\(\)\n', '', 0),
('^\n+', '', 0),
]]
def traverse(p: Path) -> int:
changed = 0
for pp in p.iterdir():
if pp.is_dir():
changed += traverse(pp)
elif pp.is_file() and pp.suffix == '.py':
text = pp.read_text()
b4 = hash(text)
for pattern, repl in REGEXES:
text = pattern.sub(repl, text)
if b4 != hash(text):
pp.write_text(text)
changed += 1
return changed
changed_files = traverse(Path(sys.argv[1]))
print('{} files changed'.format(changed_files))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment