OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2012 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 os | |
6 | |
7 | |
8 class Checker(object): | |
M-A Ruel
2012/07/17 14:23:20
Note that an "interface" is not a contractual defi
Iain Merrick
2012/07/17 15:55:08
OK, if you prefer to keep the contract implicit, t
| |
9 """Base class for language-specific checker modules. | |
10 | |
11 Properties: | |
12 _base_directory: root directory against which imports are resolved. | |
13 _verbose: True if CheckFile should print detailed progress information. | |
14 """ | |
15 | |
16 def __init__(self, base_directory, verbose): | |
17 self._base_directory = base_directory | |
18 self._verbose = verbose | |
19 | |
20 def Extensions(self): | |
21 """Returns a list of file extensions that this checker can handle.""" | |
22 raise NotImplementedError(self.__class__.__name__ + ".Extensions") | |
23 | |
24 def CheckFile(self, rules, file_name): | |
25 """Checks the given file with the given rule set. | |
26 | |
27 Args: | |
28 rules: The set of rules that apply to files in this directory. | |
29 file_name: The source file to check. | |
30 | |
31 Returns: Either a string describing the error if there was one, or None if | |
32 the file checked out OK. | |
33 """ | |
34 raise NotImplementedError(self.__class__.__name__ + ".CheckFile") | |
35 | |
36 | |
37 class Factory(object): | |
M-A Ruel
2012/07/17 14:23:20
This is overdesign at that point; this is no Java.
| |
38 """Helper class for locating the correct Checker for each file.""" | |
39 | |
40 def __init__(self, base_directory, verbose): | |
41 """Loads and initializes all known subclasses of Checker. | |
42 | |
43 Args: | |
44 base_directory: root directory against which imports are resolved. | |
45 verbose: True if CheckFile should print detailed progress information. | |
46 """ | |
47 self._checkers = [ | |
48 c(base_directory, verbose) for c in Checker.__subclasses__()] | |
49 | |
50 def GetChecker(self, filename): | |
51 """Returns the Checker for the given file, or None if no check is needed.""" | |
52 basename, extension = os.path.splitext(filename) | |
53 for c in self._checkers: | |
54 if extension in c.Extensions(): | |
55 return c | |
56 return None | |
57 | |
OLD | NEW |