Find function calls. A parser which inherits from the basic parser to find function calls. Function calls inside control structures are not found, nesting function calls are.
| 273 | |
| 274 | |
| 275 | class FunctionParser(Parser): |
| 276 | """Find function calls. |
| 277 | |
| 278 | A parser which inherits from the basic parser to find function calls. |
| 279 | Function calls inside control structures are not found, nesting function calls are. |
| 280 | """ |
| 281 | |
| 282 | def __init__(self): |
| 283 | self.gen_name = "" |
| 284 | self.raw_name = "" |
| 285 | self.mappings = {} |
| 286 | self.out = {} |
| 287 | self.call_lookup_active = False |
| 288 | |
| 289 | def visit_BinOp(self, node): |
| 290 | self.visit(node.left) |
| 291 | self.visit(node.right) |
| 292 | |
| 293 | def visit_Assign(self, node): |
| 294 | self.visit(node.value) |
| 295 | |
| 296 | def visit_AugAssign(self, node): |
| 297 | self.visit(node.value) |
| 298 | |
| 299 | def visit_Compare(self, node): |
| 300 | self.visit(node.left) |
| 301 | self.visit_each(node.comparators) |
| 302 | |
| 303 | def visit_UnaryOp(self, node): |
| 304 | self.visit(node.operand) |
| 305 | |
| 306 | def visit_Import(self, node): |
| 307 | for imp in node.names: |
| 308 | if imp.asname is not None: |
| 309 | self.mappings[imp.asname] = imp.name |
| 310 | else: |
| 311 | pass # e.g. numpy import as numpy, so no action needed. |
| 312 | |
| 313 | def visit_ImportFrom(self, node): |
| 314 | for imp in node.names: |
| 315 | self.mappings[imp.asname or imp.name] = node.module + "." + imp.name |
| 316 | |
| 317 | def visit_Expr(self, node): |
| 318 | self.visit(node.value) |
| 319 | |
| 320 | def visit_List(self, node): |
| 321 | [self.visit(el) for el in node.elts] |
| 322 | |
| 323 | def visit_Dict(self, node): |
| 324 | [self.visit(el) for el in node.values] |
| 325 | |
| 326 | def visit_Call(self, node): |
| 327 | if self.call_lookup_active: |
| 328 | self.visit(node.func) |
| 329 | else: |
| 330 | self.call_lookup_active = True |
| 331 | self.visit( |
| 332 | node.func |
no outgoing calls