Last active
July 7, 2017 18:44
Revisions
-
charettes revised this gist
Jul 7, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ def add_missing_commas(path, locations): for line, content in enumerate(file_, start=1): char = locations.get(line) if char: content = '%s,%s' % (content[0:char], content[char:]) lines.append(content) with open(path, 'w+') as file_: file_.writelines(lines) -
charettes created this gist
Jul 5, 2017 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ #!/usr/bin/env python """ This script assumes you have both flake8 and flake8-commas packages installed in the Python environment you use to run this script. """ import subprocess import sys from collections import defaultdict MISSING_TRAILING_COMMAS = 'missing trailing comma' def extract_missing_commas(violations): paths_missing_commas = defaultdict(lambda: defaultdict(list)) for violation in out.strip().split('\n'): if violation.endswith(MISSING_TRAILING_COMMAS): path, line, char, _ = violation.split(':') paths_missing_commas[path][int(line)] = int(char) - 1 return sorted(paths_missing_commas.items()) def add_missing_commas(path, locations): with open(path, 'r') as file_: lines = [] for line, content in enumerate(file_, start=1): char = locations.get(line) if char: content = '%s,%s' % (content[0:char], content[char]) lines.append(content) with open(path, 'w+') as file_: file_.writelines(lines) if __name__ == '__main__': sys.stdout.write('Running flake8 to detect missing trailing commas... ') sys.stdout.flush() process = subprocess.Popen( 'flake8 %s' % ' '.join(sys.argv[1:]), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) out, err = process.communicate() sys.stdout.write('done.\n') violations = out.strip().split('\n') for path, locations in extract_missing_commas(violations): sys.stdout.write('Adding missing commas to "%s"... ' % path) sys.stdout.flush() add_missing_commas(path, locations) sys.stdout.write('done.\n')