OLD | NEW |
1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) | 1 # Copyright (C) 2010 Chris Jerdonek (cjerdonek@webkit.org) |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions | 4 # modification, are permitted provided that the following conditions |
5 # are met: | 5 # are met: |
6 # 1. Redistributions of source code must retain the above copyright | 6 # 1. Redistributions of source code must retain the above copyright |
7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
8 # 2. Redistributions in binary form must reproduce the above copyright | 8 # 2. Redistributions in binary form must reproduce the above copyright |
9 # notice, this list of conditions and the following disclaimer in the | 9 # notice, this list of conditions and the following disclaimer in the |
10 # documentation and/or other materials provided with the distribution. | 10 # documentation and/or other materials provided with the distribution. |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 | 101 |
102 | 102 |
103 # This class should not have knowledge of the flag key names. | 103 # This class should not have knowledge of the flag key names. |
104 class DefaultCommandOptionValues(object): | 104 class DefaultCommandOptionValues(object): |
105 | 105 |
106 """Stores the default check-webkit-style command-line options. | 106 """Stores the default check-webkit-style command-line options. |
107 | 107 |
108 Attributes: | 108 Attributes: |
109 output_format: A string that is the default output format. | 109 output_format: A string that is the default output format. |
110 min_confidence: An integer that is the default minimum confidence level. | 110 min_confidence: An integer that is the default minimum confidence level. |
111 | |
112 """ | 111 """ |
113 | 112 |
114 def __init__(self, min_confidence, output_format): | 113 def __init__(self, min_confidence, output_format): |
115 self.min_confidence = min_confidence | 114 self.min_confidence = min_confidence |
116 self.output_format = output_format | 115 self.output_format = output_format |
117 | 116 |
118 | 117 |
119 # This class should not have knowledge of the flag key names. | 118 # This class should not have knowledge of the flag key names. |
120 class CommandOptionValues(object): | 119 class CommandOptionValues(object): |
121 | 120 |
(...skipping 10 matching lines...) Expand all Loading... |
132 git_commit: A string representing the git commit to check. | 131 git_commit: A string representing the git commit to check. |
133 The default is None. | 132 The default is None. |
134 | 133 |
135 min_confidence: An integer between 1 and 5 inclusive that is the | 134 min_confidence: An integer between 1 and 5 inclusive that is the |
136 minimum confidence level of style errors to report. | 135 minimum confidence level of style errors to report. |
137 The default is 1, which reports all errors. | 136 The default is 1, which reports all errors. |
138 | 137 |
139 output_format: A string that is the output format. The supported | 138 output_format: A string that is the output format. The supported |
140 output formats are "emacs" which emacs can parse | 139 output formats are "emacs" which emacs can parse |
141 and "vs7" which Microsoft Visual Studio 7 can parse. | 140 and "vs7" which Microsoft Visual Studio 7 can parse. |
142 | |
143 """ | 141 """ |
144 | 142 |
145 def __init__(self, | 143 def __init__(self, |
146 filter_rules=None, | 144 filter_rules=None, |
147 git_commit=None, | 145 git_commit=None, |
148 diff_files=None, | 146 diff_files=None, |
149 is_verbose=False, | 147 is_verbose=False, |
150 min_confidence=1, | 148 min_confidence=1, |
151 output_format="emacs"): | 149 output_format="emacs"): |
152 if filter_rules is None: | 150 if filter_rules is None: |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 def _flag_pair_to_string(self, flag_key, flag_value): | 198 def _flag_pair_to_string(self, flag_key, flag_value): |
201 return '--%(key)s=%(val)s' % {'key': flag_key, 'val': flag_value} | 199 return '--%(key)s=%(val)s' % {'key': flag_key, 'val': flag_value} |
202 | 200 |
203 def to_flag_string(self, options): | 201 def to_flag_string(self, options): |
204 """Return a flag string of the given CommandOptionValues instance. | 202 """Return a flag string of the given CommandOptionValues instance. |
205 | 203 |
206 This method orders the flag values alphabetically by the flag key. | 204 This method orders the flag values alphabetically by the flag key. |
207 | 205 |
208 Args: | 206 Args: |
209 options: A CommandOptionValues instance. | 207 options: A CommandOptionValues instance. |
210 | |
211 """ | 208 """ |
212 flags = {} | 209 flags = {} |
213 flags['min-confidence'] = options.min_confidence | 210 flags['min-confidence'] = options.min_confidence |
214 flags['output'] = options.output_format | 211 flags['output'] = options.output_format |
215 # Only include the filter flag if user-provided rules are present. | 212 # Only include the filter flag if user-provided rules are present. |
216 filter_rules = options.filter_rules | 213 filter_rules = options.filter_rules |
217 if filter_rules: | 214 if filter_rules: |
218 flags['filter'] = ",".join(filter_rules) | 215 flags['filter'] = ",".join(filter_rules) |
219 if options.git_commit: | 216 if options.git_commit: |
220 flags['git-commit'] = options.git_commit | 217 flags['git-commit'] = options.git_commit |
(...skipping 18 matching lines...) Expand all Loading... |
239 create_usage: A function that accepts a DefaultCommandOptionValues | 236 create_usage: A function that accepts a DefaultCommandOptionValues |
240 instance and returns a string of usage instructions. | 237 instance and returns a string of usage instructions. |
241 Defaults to the function that generates the usage | 238 Defaults to the function that generates the usage |
242 string for check-webkit-style. | 239 string for check-webkit-style. |
243 default_options: A DefaultCommandOptionValues instance that provides | 240 default_options: A DefaultCommandOptionValues instance that provides |
244 the default values for options not explicitly | 241 the default values for options not explicitly |
245 provided by the user. | 242 provided by the user. |
246 stderr_write: A function that takes a string as a parameter and | 243 stderr_write: A function that takes a string as a parameter and |
247 serves as stderr.write. Defaults to sys.stderr.write. | 244 serves as stderr.write. Defaults to sys.stderr.write. |
248 This parameter should be specified only for unit tests. | 245 This parameter should be specified only for unit tests. |
249 | |
250 """ | 246 """ |
251 | 247 |
252 def __init__(self, | 248 def __init__(self, |
253 all_categories, | 249 all_categories, |
254 default_options, | 250 default_options, |
255 base_filter_rules=None, | 251 base_filter_rules=None, |
256 mock_stderr=None, | 252 mock_stderr=None, |
257 usage=None): | 253 usage=None): |
258 """Create an ArgumentParser instance. | 254 """Create an ArgumentParser instance. |
259 | 255 |
260 Args: | 256 Args: |
261 all_categories: The set of all available style categories. | 257 all_categories: The set of all available style categories. |
262 default_options: See the corresponding attribute in the class | 258 default_options: See the corresponding attribute in the class |
263 docstring. | 259 docstring. |
264 Keyword Args: | 260 Keyword Args: |
265 base_filter_rules: The list of filter rules at the beginning of | 261 base_filter_rules: The list of filter rules at the beginning of |
266 the list of rules used to check style. This | 262 the list of rules used to check style. This |
267 list has the least precedence when checking | 263 list has the least precedence when checking |
268 style and precedes any user-provided rules. | 264 style and precedes any user-provided rules. |
269 The class uses this parameter only for display | 265 The class uses this parameter only for display |
270 purposes to the user. Defaults to the empty list. | 266 purposes to the user. Defaults to the empty list. |
271 create_usage: See the documentation of the corresponding | 267 create_usage: See the documentation of the corresponding |
272 attribute in the class docstring. | 268 attribute in the class docstring. |
273 stderr_write: See the documentation of the corresponding | 269 stderr_write: See the documentation of the corresponding |
274 attribute in the class docstring. | 270 attribute in the class docstring. |
275 | |
276 """ | 271 """ |
277 if base_filter_rules is None: | 272 if base_filter_rules is None: |
278 base_filter_rules = [] | 273 base_filter_rules = [] |
279 stderr = sys.stderr if mock_stderr is None else mock_stderr | 274 stderr = sys.stderr if mock_stderr is None else mock_stderr |
280 if usage is None: | 275 if usage is None: |
281 usage = _USAGE | 276 usage = _USAGE |
282 | 277 |
283 self._all_categories = all_categories | 278 self._all_categories = all_categories |
284 self._base_filter_rules = base_filter_rules | 279 self._base_filter_rules = base_filter_rules |
285 | 280 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 'and before any --filter flag.\n\n') | 379 'and before any --filter flag.\n\n') |
385 | 380 |
386 sys.exit(0) | 381 sys.exit(0) |
387 | 382 |
388 def _parse_filter_flag(self, flag_value): | 383 def _parse_filter_flag(self, flag_value): |
389 """Parse the --filter flag, and return a list of filter rules. | 384 """Parse the --filter flag, and return a list of filter rules. |
390 | 385 |
391 Args: | 386 Args: |
392 flag_value: A string of comma-separated filter rules, for | 387 flag_value: A string of comma-separated filter rules, for |
393 example "-whitespace,+whitespace/indent". | 388 example "-whitespace,+whitespace/indent". |
394 | |
395 """ | 389 """ |
396 filters = [] | 390 filters = [] |
397 for uncleaned_filter in flag_value.split(','): | 391 for uncleaned_filter in flag_value.split(','): |
398 filter = uncleaned_filter.strip() | 392 filter = uncleaned_filter.strip() |
399 if not filter: | 393 if not filter: |
400 continue | 394 continue |
401 filters.append(filter) | 395 filters.append(filter) |
402 return filters | 396 return filters |
403 | 397 |
404 def parse(self, args): | 398 def parse(self, args): |
405 """Parse the command line arguments to check-webkit-style. | 399 """Parse the command line arguments to check-webkit-style. |
406 | 400 |
407 Args: | 401 Args: |
408 args: A list of command-line arguments as returned by sys.argv[1:]. | 402 args: A list of command-line arguments as returned by sys.argv[1:]. |
409 | 403 |
410 Returns: | 404 Returns: |
411 A tuple of (paths, options) | 405 A tuple of (paths, options) |
412 | 406 |
413 paths: The list of paths to check. | 407 paths: The list of paths to check. |
414 options: A CommandOptionValues instance. | 408 options: A CommandOptionValues instance. |
415 | |
416 """ | 409 """ |
417 (options, paths) = self._parser.parse_args(args=args) | 410 (options, paths) = self._parser.parse_args(args=args) |
418 | 411 |
419 filter_value = options.filter_value | 412 filter_value = options.filter_value |
420 git_commit = options.git_commit | 413 git_commit = options.git_commit |
421 diff_files = options.diff_files | 414 diff_files = options.diff_files |
422 is_verbose = options.is_verbose | 415 is_verbose = options.is_verbose |
423 min_confidence = options.min_confidence | 416 min_confidence = options.min_confidence |
424 output_format = options.output_format | 417 output_format = options.output_format |
425 | 418 |
(...skipping 21 matching lines...) Expand all Loading... |
447 self._parse_error(err) | 440 self._parse_error(err) |
448 | 441 |
449 options = CommandOptionValues(filter_rules=filter_rules, | 442 options = CommandOptionValues(filter_rules=filter_rules, |
450 git_commit=git_commit, | 443 git_commit=git_commit, |
451 diff_files=diff_files, | 444 diff_files=diff_files, |
452 is_verbose=is_verbose, | 445 is_verbose=is_verbose, |
453 min_confidence=min_confidence, | 446 min_confidence=min_confidence, |
454 output_format=output_format) | 447 output_format=output_format) |
455 | 448 |
456 return (paths, options) | 449 return (paths, options) |
OLD | NEW |