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

Side by Side Diff: Tools/Scripts/webkitpy/formatter/main.py

Issue 546613003: Add a new 'format-webkitpy' command that will reformat code to the style guide. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: remove hack from test/main.py 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 # found in the LICENSE file.
4
5 import argparse
6 import lib2to3.refactor
7
8 from webkitpy.common.system.systemhost import SystemHost
9 from webkitpy.thirdparty import autopep8
10
11
12 def parse_args(args=None):
13 parser = argparse.ArgumentParser()
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")
16 parser.add_argument('--no-backups', action='store_false', default=True, dest ='backup',
17 help='do not back up files before overwriting them')
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')
20 parser.add_argument('files', nargs='*', default=['-'],
21 help="files to format or '-' for standard in")
22 return parser.parse_args(args=args)
23
24
25 def main(host=None, args=None):
26 options = parse_args(args)
27
28 if options.files == ['-']:
29 host = host or SystemHost()
30 host.print_(reformat_source(host.stdin.read(), options.style, '<stdin>') , end='')
31 return
32
33 # 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().
35 arglist = [(host, name, options.style, options.backup) for name in options.f iles]
36 host = host or SystemHost()
37
38 host.executive.map(_reformat_thunk, arglist, processes=options.jobs)
39
40
41 def _reformat_thunk(args):
42 reformat_file(*args)
43
44
45 def reformat_file(host, name, style, should_backup_file):
46 host = host or SystemHost()
47 source = host.filesystem.read_text_file(name)
48 dest = reformat_source(source, style, name)
49 if dest != source:
50 if should_backup_file:
51 host.filesystem.write_text_file(name + '.bak', source)
52 host.filesystem.write_text_file(name, dest)
53
54
55 def reformat_source(source, style, name):
56 options = _autopep8_options_for_style(style)
57 tmp_str = autopep8.fix_code(source, options)
58
59 fixers = _fixers_for_style(style)
60 tool = lib2to3.refactor.RefactoringTool(fixer_names=fixers,
61 explicit=fixers)
62 return unicode(tool.refactor_string(tmp_str, name=name))
63
64
65 def _autopep8_options_for_style(style):
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']
OLDNEW
« no previous file with comments | « Tools/Scripts/webkitpy/formatter/fix_single_quote_strings.py ('k') | Tools/Scripts/webkitpy/formatter/main_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698