OLD | NEW |
1 # Copyright (C) 2009 Google Inc. All rights reserved. | 1 # Copyright (C) 2009 Google Inc. All rights reserved. |
2 # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com) | 2 # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@gmail.com) |
3 # Copyright (C) 2010 ProFUSION embedded systems | 3 # Copyright (C) 2010 ProFUSION embedded systems |
4 # | 4 # |
5 # Redistribution and use in source and binary forms, with or without | 5 # Redistribution and use in source and binary forms, with or without |
6 # modification, are permitted provided that the following conditions are | 6 # modification, are permitted provided that the following conditions are |
7 # met: | 7 # met: |
8 # | 8 # |
9 # * Redistributions of source code must retain the above copyright | 9 # * Redistributions of source code must retain the above copyright |
10 # notice, this list of conditions and the following disclaimer. | 10 # notice, this list of conditions and the following disclaimer. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 """Supports reading and processing text files. | 44 """Supports reading and processing text files. |
45 | 45 |
46 Attributes: | 46 Attributes: |
47 file_count: The total number of files passed to this instance | 47 file_count: The total number of files passed to this instance |
48 for processing, including non-text files and files | 48 for processing, including non-text files and files |
49 that should be skipped. | 49 that should be skipped. |
50 delete_only_file_count: The total number of files that are not | 50 delete_only_file_count: The total number of files that are not |
51 processed this instance actually because | 51 processed this instance actually because |
52 the files don't have any modified lines | 52 the files don't have any modified lines |
53 but should be treated as processed. | 53 but should be treated as processed. |
54 | |
55 """ | 54 """ |
56 | 55 |
57 def __init__(self, filesystem, processor): | 56 def __init__(self, filesystem, processor): |
58 """Create an instance. | 57 """Create an instance. |
59 | 58 |
60 Arguments: | 59 Arguments: |
61 processor: A ProcessorBase instance. | 60 processor: A ProcessorBase instance. |
62 | |
63 """ | 61 """ |
64 # FIXME: Although TextFileReader requires a FileSystem it circumvents it
in two places! | 62 # FIXME: Although TextFileReader requires a FileSystem it circumvents it
in two places! |
65 self.filesystem = filesystem | 63 self.filesystem = filesystem |
66 self._processor = processor | 64 self._processor = processor |
67 self.file_count = 0 | 65 self.file_count = 0 |
68 self.delete_only_file_count = 0 | 66 self.delete_only_file_count = 0 |
69 | 67 |
70 def _read_lines(self, file_path): | 68 def _read_lines(self, file_path): |
71 """Read the file at a path, and return its lines. | 69 """Read the file at a path, and return its lines. |
72 | 70 |
73 Raises: | 71 Raises: |
74 IOError: If the file does not exist or cannot be read. | 72 IOError: If the file does not exist or cannot be read. |
75 | |
76 """ | 73 """ |
77 # Support the UNIX convention of using "-" for stdin. | 74 # Support the UNIX convention of using "-" for stdin. |
78 if file_path == '-': | 75 if file_path == '-': |
79 file = codecs.StreamReaderWriter(sys.stdin, | 76 file = codecs.StreamReaderWriter(sys.stdin, |
80 codecs.getreader('utf8'), | 77 codecs.getreader('utf8'), |
81 codecs.getwriter('utf8'), | 78 codecs.getwriter('utf8'), |
82 'replace') | 79 'replace') |
83 else: | 80 else: |
84 # We do not open the file with universal newline support | 81 # We do not open the file with universal newline support |
85 # (codecs does not support it anyway), so the resulting | 82 # (codecs does not support it anyway), so the resulting |
(...skipping 14 matching lines...) Expand all Loading... |
100 """Process the given file by calling the processor's process() method. | 97 """Process the given file by calling the processor's process() method. |
101 | 98 |
102 Args: | 99 Args: |
103 file_path: The path of the file to process. | 100 file_path: The path of the file to process. |
104 **kwargs: Any additional keyword parameters that should be passed | 101 **kwargs: Any additional keyword parameters that should be passed |
105 to the processor's process() method. The process() | 102 to the processor's process() method. The process() |
106 method should support these keyword arguments. | 103 method should support these keyword arguments. |
107 | 104 |
108 Raises: | 105 Raises: |
109 SystemExit: If no file at file_path exists. | 106 SystemExit: If no file at file_path exists. |
110 | |
111 """ | 107 """ |
112 self.file_count += 1 | 108 self.file_count += 1 |
113 | 109 |
114 if not self.filesystem.exists(file_path) and file_path != "-": | 110 if not self.filesystem.exists(file_path) and file_path != "-": |
115 _log.error("File does not exist: '%s'", file_path) | 111 _log.error("File does not exist: '%s'", file_path) |
116 sys.exit(1) # FIXME: This should throw or return instead of exiting
directly. | 112 sys.exit(1) # FIXME: This should throw or return instead of exiting
directly. |
117 | 113 |
118 if not self._processor.should_process(file_path): | 114 if not self._processor.should_process(file_path): |
119 _log.debug("Skipping file: '%s'", file_path) | 115 _log.debug("Skipping file: '%s'", file_path) |
120 return | 116 return |
(...skipping 24 matching lines...) Expand all Loading... |
145 self.process_file(path) | 141 self.process_file(path) |
146 | 142 |
147 def count_delete_only_file(self): | 143 def count_delete_only_file(self): |
148 """Count up files that contains only deleted lines. | 144 """Count up files that contains only deleted lines. |
149 | 145 |
150 Files which has no modified or newly-added lines don't need | 146 Files which has no modified or newly-added lines don't need |
151 to check style, but should be treated as checked. For that | 147 to check style, but should be treated as checked. For that |
152 purpose, we just count up the number of such files. | 148 purpose, we just count up the number of such files. |
153 """ | 149 """ |
154 self.delete_only_file_count += 1 | 150 self.delete_only_file_count += 1 |
OLD | NEW |