91 |
92 |
92 def __addToken(self, toktype, toktext, srow, scol, line): |
93 def __addToken(self, toktype, toktext, srow, scol, line): |
93 """ |
94 """ |
94 Private method used to add a token to our list of tokens. |
95 Private method used to add a token to our list of tokens. |
95 |
96 |
96 @param toktype the type of the token (int) |
97 @param toktype the type of the token |
97 @param toktext the text of the token (string) |
98 @type int |
98 @param srow starting row of the token (int) |
99 @param toktext the text of the token |
99 @param scol starting column of the token (int) |
100 @type str |
100 @param line logical line the token was found (string) |
101 @param srow starting row of the token |
|
102 @type int |
|
103 @param scol starting column of the token |
|
104 @type int |
|
105 @param line logical line the token was found |
|
106 @type str |
101 """ |
107 """ |
102 self.tokenlist.append( |
108 self.tokenlist.append( |
103 Token(type=toktype, text=toktext, row=srow, col=scol, line=line) |
109 Token(type=toktype, text=toktext, row=srow, col=scol, line=line) |
104 ) |
110 ) |
105 |
111 |
129 |
135 |
130 def dedent(self, tok): |
136 def dedent(self, tok): |
131 """ |
137 """ |
132 Public method used to decrement the indentation level. |
138 Public method used to decrement the indentation level. |
133 |
139 |
134 @param tok the token to be processed (Token) |
140 @param tok the token to be processed |
|
141 @type Token |
135 @exception ValueError raised to indicate an invalid indentation level |
142 @exception ValueError raised to indicate an invalid indentation level |
136 """ |
143 """ |
137 self.indent_level -= 1 |
144 self.indent_level -= 1 |
138 if self.indent_level < 0: |
145 if self.indent_level < 0: |
139 raise ValueError("INTERNAL ERROR: Negative indent level") |
146 raise ValueError("INTERNAL ERROR: Negative indent level") |
148 |
155 |
149 def push(self, identifier, row): |
156 def push(self, identifier, row): |
150 """ |
157 """ |
151 Public method used to store an identifier. |
158 Public method used to store an identifier. |
152 |
159 |
153 @param identifier the identifier to be remembered (string) |
160 @param identifier the identifier to be remembered |
154 @param row the row, the identifier is defined in (int) |
161 @type str |
|
162 @param row row, the identifier is defined in |
|
163 @type int |
155 """ |
164 """ |
156 qualified = ( |
165 qualified = ( |
157 self.active[-1][0] + "." + identifier |
166 self.active[-1][0] + "." + identifier |
158 if len(self.active) > 1 and self.indent_level > self.active[-1][1] |
167 if len(self.active) > 1 and self.indent_level > self.active[-1][1] |
159 else identifier |
168 else identifier |
163 |
172 |
164 def inc(self, key, value=1): |
173 def inc(self, key, value=1): |
165 """ |
174 """ |
166 Public method used to increment the value of a key. |
175 Public method used to increment the value of a key. |
167 |
176 |
168 @param key the key to be incremented |
177 @param key key to be incremented |
169 @param value the increment (int) |
178 @type str |
|
179 @param value the increment |
|
180 @type int |
170 """ |
181 """ |
171 for counterId, _level, _row in self.active: |
182 for counterId, _level, _row in self.active: |
172 counters = self.counters.setdefault(counterId, {}) |
183 counters = self.counters.setdefault(counterId, {}) |
173 counters[key] = counters.setdefault(key, 0) + value |
184 counters[key] = counters.setdefault(key, 0) + value |
174 |
185 |
175 def getCounter(self, counterId, key): |
186 def getCounter(self, counterId, key): |
176 """ |
187 """ |
177 Public method used to get a specific counter value. |
188 Public method used to get a specific counter value. |
178 |
189 |
179 @param counterId id of the counter (string) |
190 @param counterId id of the counter |
180 @param key key of the value to be retrieved (string) |
191 @type str |
181 @return the value of the requested counter (int) |
192 @param key key of the value to be retrieved |
|
193 @type str |
|
194 @return the value of the requested counter |
|
195 @rtype int |
182 """ |
196 """ |
183 return self.counters.get(counterId, {}).get(key, 0) |
197 return self.counters.get(counterId, {}).get(key, 0) |
184 |
198 |
185 |
199 |
186 def summarize(total, key, value): |
200 def summarize(total, key, value): |
187 """ |
201 """ |
188 Module function used to collect overall statistics. |
202 Module function used to collect overall statistics. |
189 |
203 |
190 @param total the dictionary for the overall statistics |
204 @param total dictionary of overall statistics |
191 @param key the key to be summarize |
205 @type dict |
192 @param value the value to be added to the overall statistics |
206 @param key key to be summarized |
|
207 @type str |
|
208 @param value value to be added to the overall statistics |
|
209 @type int |
193 @return the value added to the overall statistics |
210 @return the value added to the overall statistics |
|
211 @rtype int |
194 """ |
212 """ |
195 total[key] = total.setdefault(key, 0) + value |
213 total[key] = total.setdefault(key, 0) + value |
196 return value |
214 return value |
197 |
215 |
198 |
216 |
199 def analyze(filename, total): |
217 def analyze(filename, total): |
200 """ |
218 """ |
201 Module function used analyze the source of a Python file. |
219 Module function used analyze the source of a Python file. |
202 |
220 |
203 @param filename name of the Python file to be analyzed (string) |
221 @param filename name of the Python file to be analyzed |
|
222 @type str |
204 @param total dictionary receiving the overall code statistics |
223 @param total dictionary receiving the overall code statistics |
205 @return a statistics object with the collected code statistics (SourceStat) |
224 @type dict |
|
225 @return a statistics object with the collected code statistics |
|
226 @rtype SourceStat |
206 """ |
227 """ |
207 try: |
228 try: |
208 text = Utilities.readEncodedFile(filename)[0] |
229 text = Utilities.readEncodedFile(filename)[0] |
209 except (OSError, UnicodeError): |
230 except (OSError, UnicodeError): |
210 return SourceStat() |
231 return SourceStat() |