Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1308)

Unified Diff: Tools/Scripts/webkitpy/formatter/main.py

Issue 555283006: Rework format-webkitpy to use single-quoting for chromium, add more args. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | Tools/Scripts/webkitpy/formatter/main_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Tools/Scripts/webkitpy/formatter/main.py
diff --git a/Tools/Scripts/webkitpy/formatter/main.py b/Tools/Scripts/webkitpy/formatter/main.py
index 7c1070aa8d97b8fa596a51d35976cda5fca02edc..92dbfa070061799e935f73a296629e691cc8575a 100644
--- a/Tools/Scripts/webkitpy/formatter/main.py
+++ b/Tools/Scripts/webkitpy/formatter/main.py
@@ -12,72 +12,92 @@ from webkitpy.thirdparty import autopep8
def parse_args(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('--chromium', action='store_const', dest='style', const='chromium', default='blink',
- help="format according to Chromium's Python coding styles instead of Blink's")
+ help="Format according to Chromium's Python coding styles instead of Blink's.")
parser.add_argument('--no-backups', action='store_false', default=True, dest='backup',
- help='do not back up files before overwriting them')
+ help='Do not back up files before overwriting them.')
parser.add_argument('-j', '--jobs', metavar='n', type=int, default=0,
- help='number of parallel jobs; match CPU count if less than 1')
+ help='Number of parallel jobs; match CPU count if less than 1.')
parser.add_argument('files', nargs='*', default=['-'],
help="files to format or '-' for standard in")
+ parser.add_argument('--double-quote-strings', action='store_const', dest='quoting', const='double', default='single',
+ help='Rewrite string literals to use double quotes instead of single quotes.')
+ parser.add_argument('--no-autopep8', action='store_true',
+ help='Skip the autopep8 code-formatting step.')
+ parser.add_argument('--leave-strings-alone', action='store_true',
eseidel 2014/09/10 20:50:36 I might have made this a real enum. --string-quot
Dirk Pranke 2014/09/10 20:52:05 Yeah, I thought about that approach. Kind of a tos
+ help='Do not reformat string literals to use a consistent quote style.')
return parser.parse_args(args=args)
def main(host=None, args=None):
options = parse_args(args)
+ if options.no_autopep8:
+ options.style = None
+
+ if options.leave_strings_alone:
+ options.quoting = None
+
+ autopep8_options = _autopep8_options_for_style(options.style)
+ fixers = _fixers_for_quoting(options.quoting)
if options.files == ['-']:
host = host or SystemHost()
- host.print_(reformat_source(host.stdin.read(), options.style, '<stdin>'), end='')
+ host.print_(reformat_source(host.stdin.read(), autopep8_options, fixers, '<stdin>'), end='')
return
# We create the arglist before checking if we need to create a Host, because a
# real host is non-picklable and can't be passed to host.executive.map().
- arglist = [(host, name, options.style, options.backup) for name in options.files]
+
+ arglist = [(host, name, autopep8_options, fixers, options.backup) for name in options.files]
host = host or SystemHost()
host.executive.map(_reformat_thunk, arglist, processes=options.jobs)
+def _autopep8_options_for_style(style):
+ return {
+ None: [],
+ 'blink': autopep8.parse_args(['--aggressive',
+ '--max-line-length', '132',
+ '--indent-size', '4',
+ '']),
+ 'chromium': autopep8.parse_args(['--aggressive',
+ '--max-line-length', '80',
+ '--indent-size', '2',
+ '']),
+ }.get(style)
+
+
+def _fixers_for_quoting(quoting):
+ return {
+ None: [],
+ 'double': ['webkitpy.formatter.fix_double_quote_strings'],
+ 'single': ['webkitpy.formatter.fix_single_quote_strings'],
+ }.get(quoting)
+
+
def _reformat_thunk(args):
reformat_file(*args)
-def reformat_file(host, name, style, should_backup_file):
+def reformat_file(host, name, autopep8_options, fixers, should_backup_file):
host = host or SystemHost()
source = host.filesystem.read_text_file(name)
- dest = reformat_source(source, style, name)
+ dest = reformat_source(source, autopep8_options, fixers, name)
if dest != source:
if should_backup_file:
host.filesystem.write_text_file(name + '.bak', source)
host.filesystem.write_text_file(name, dest)
-def reformat_source(source, style, name):
- options = _autopep8_options_for_style(style)
- tmp_str = autopep8.fix_code(source, options)
+def reformat_source(source, autopep8_options, fixers, name):
+ tmp_str = source
- fixers = _fixers_for_style(style)
- tool = lib2to3.refactor.RefactoringTool(fixer_names=fixers,
- explicit=fixers)
- return unicode(tool.refactor_string(tmp_str, name=name))
+ if autopep8_options:
+ tmp_str = autopep8.fix_code(tmp_str, autopep8_options)
+ if fixers:
+ tool = lib2to3.refactor.RefactoringTool(fixer_names=fixers,
+ explicit=fixers)
+ tmp_str = unicode(tool.refactor_string(tmp_str, name=name))
-def _autopep8_options_for_style(style):
- if style == 'chromium':
- max_line_length = 80
- indent_size = 2
- else:
- max_line_length = 132
- indent_size = 4
-
- return autopep8.parse_args(['--aggressive',
- '--max-line-length', str(max_line_length),
- '--indent-size', str(indent_size),
- ''])
-
-
-def _fixers_for_style(style):
- if style == 'chromium':
- return ['webkitpy.formatter.fix_double_quote_strings']
- else:
- return ['webkitpy.formatter.fix_single_quote_strings']
+ return tmp_str
« no previous file with comments | « no previous file | Tools/Scripts/webkitpy/formatter/main_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698