OLD | NEW |
---|---|
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import argparse | 5 import argparse |
6 import lib2to3.refactor | 6 import lib2to3.refactor |
7 | 7 |
8 from webkitpy.common.system.systemhost import SystemHost | 8 from webkitpy.common.system.systemhost import SystemHost |
9 from webkitpy.thirdparty import autopep8 | 9 from webkitpy.thirdparty import autopep8 |
10 | 10 |
11 | 11 |
12 def parse_args(args=None): | 12 def parse_args(args=None): |
13 parser = argparse.ArgumentParser() | 13 parser = argparse.ArgumentParser() |
14 parser.add_argument('--chromium', action='store_const', dest='style', const= 'chromium', default='blink', | 14 parser.add_argument('--chromium', action='store_const', dest='style', const= 'chromium', default='blink', |
15 help="format according to Chromium's Python coding style s instead of Blink's") | 15 help="Format according to Chromium's Python coding style s instead of Blink's.") |
16 parser.add_argument('--no-backups', action='store_false', default=True, dest ='backup', | 16 parser.add_argument('--no-backups', action='store_false', default=True, dest ='backup', |
17 help='do not back up files before overwriting them') | 17 help='Do not back up files before overwriting them.') |
18 parser.add_argument('-j', '--jobs', metavar='n', type=int, default=0, | 18 parser.add_argument('-j', '--jobs', metavar='n', type=int, default=0, |
19 help='number of parallel jobs; match CPU count if less t han 1') | 19 help='Number of parallel jobs; match CPU count if less t han 1.') |
20 parser.add_argument('files', nargs='*', default=['-'], | 20 parser.add_argument('files', nargs='*', default=['-'], |
21 help="files to format or '-' for standard in") | 21 help="files to format or '-' for standard in") |
22 parser.add_argument('--double-quote-strings', action='store_const', dest='qu oting', const='double', default='single', | |
23 help='Rewrite string literals to use double quotes inste ad of single quotes.') | |
24 parser.add_argument('--no-autopep8', action='store_true', | |
25 help='Skip the autopep8 code-formatting step.') | |
26 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
| |
27 help='Do not reformat string literals to use a consisten t quote style.') | |
22 return parser.parse_args(args=args) | 28 return parser.parse_args(args=args) |
23 | 29 |
24 | 30 |
25 def main(host=None, args=None): | 31 def main(host=None, args=None): |
26 options = parse_args(args) | 32 options = parse_args(args) |
33 if options.no_autopep8: | |
34 options.style = None | |
35 | |
36 if options.leave_strings_alone: | |
37 options.quoting = None | |
38 | |
39 autopep8_options = _autopep8_options_for_style(options.style) | |
40 fixers = _fixers_for_quoting(options.quoting) | |
27 | 41 |
28 if options.files == ['-']: | 42 if options.files == ['-']: |
29 host = host or SystemHost() | 43 host = host or SystemHost() |
30 host.print_(reformat_source(host.stdin.read(), options.style, '<stdin>') , end='') | 44 host.print_(reformat_source(host.stdin.read(), autopep8_options, fixers, '<stdin>'), end='') |
31 return | 45 return |
32 | 46 |
33 # We create the arglist before checking if we need to create a Host, because a | 47 # We create the arglist before checking if we need to create a Host, because a |
34 # real host is non-picklable and can't be passed to host.executive.map(). | 48 # real host is non-picklable and can't be passed to host.executive.map(). |
35 arglist = [(host, name, options.style, options.backup) for name in options.f iles] | 49 |
50 arglist = [(host, name, autopep8_options, fixers, options.backup) for name i n options.files] | |
36 host = host or SystemHost() | 51 host = host or SystemHost() |
37 | 52 |
38 host.executive.map(_reformat_thunk, arglist, processes=options.jobs) | 53 host.executive.map(_reformat_thunk, arglist, processes=options.jobs) |
39 | 54 |
40 | 55 |
56 def _autopep8_options_for_style(style): | |
57 return { | |
58 None: [], | |
59 'blink': autopep8.parse_args(['--aggressive', | |
60 '--max-line-length', '132', | |
61 '--indent-size', '4', | |
62 '']), | |
63 'chromium': autopep8.parse_args(['--aggressive', | |
64 '--max-line-length', '80', | |
65 '--indent-size', '2', | |
66 '']), | |
67 }.get(style) | |
68 | |
69 | |
70 def _fixers_for_quoting(quoting): | |
71 return { | |
72 None: [], | |
73 'double': ['webkitpy.formatter.fix_double_quote_strings'], | |
74 'single': ['webkitpy.formatter.fix_single_quote_strings'], | |
75 }.get(quoting) | |
76 | |
77 | |
41 def _reformat_thunk(args): | 78 def _reformat_thunk(args): |
42 reformat_file(*args) | 79 reformat_file(*args) |
43 | 80 |
44 | 81 |
45 def reformat_file(host, name, style, should_backup_file): | 82 def reformat_file(host, name, autopep8_options, fixers, should_backup_file): |
46 host = host or SystemHost() | 83 host = host or SystemHost() |
47 source = host.filesystem.read_text_file(name) | 84 source = host.filesystem.read_text_file(name) |
48 dest = reformat_source(source, style, name) | 85 dest = reformat_source(source, autopep8_options, fixers, name) |
49 if dest != source: | 86 if dest != source: |
50 if should_backup_file: | 87 if should_backup_file: |
51 host.filesystem.write_text_file(name + '.bak', source) | 88 host.filesystem.write_text_file(name + '.bak', source) |
52 host.filesystem.write_text_file(name, dest) | 89 host.filesystem.write_text_file(name, dest) |
53 | 90 |
54 | 91 |
55 def reformat_source(source, style, name): | 92 def reformat_source(source, autopep8_options, fixers, name): |
56 options = _autopep8_options_for_style(style) | 93 tmp_str = source |
57 tmp_str = autopep8.fix_code(source, options) | |
58 | 94 |
59 fixers = _fixers_for_style(style) | 95 if autopep8_options: |
60 tool = lib2to3.refactor.RefactoringTool(fixer_names=fixers, | 96 tmp_str = autopep8.fix_code(tmp_str, autopep8_options) |
61 explicit=fixers) | |
62 return unicode(tool.refactor_string(tmp_str, name=name)) | |
63 | 97 |
98 if fixers: | |
99 tool = lib2to3.refactor.RefactoringTool(fixer_names=fixers, | |
100 explicit=fixers) | |
101 tmp_str = unicode(tool.refactor_string(tmp_str, name=name)) | |
64 | 102 |
65 def _autopep8_options_for_style(style): | 103 return tmp_str |
66 if style == 'chromium': | |
67 max_line_length = 80 | |
68 indent_size = 2 | |
69 else: | |
70 max_line_length = 132 | |
71 indent_size = 4 | |
72 | |
73 return autopep8.parse_args(['--aggressive', | |
74 '--max-line-length', str(max_line_length), | |
75 '--indent-size', str(indent_size), | |
76 '']) | |
77 | |
78 | |
79 def _fixers_for_style(style): | |
80 if style == 'chromium': | |
81 return ['webkitpy.formatter.fix_double_quote_strings'] | |
82 else: | |
83 return ['webkitpy.formatter.fix_single_quote_strings'] | |
OLD | NEW |