def _get_all_imported_modulenames(self, filename): """Compiles an AST for filename and returns the module names for all modules imported by. If no file for filename exists, or it is an unparseable file (pyd, pyc), return an empty list. If the file cannot be parsed, append to self.failed and return an empty list. """ #We can only read py files right now if filename.endswith('.pyd'): return [] if filename.endswith('.pyc') or filename.endswith('.pyo'): filename = filename[:-1] elif not os.path.splitext(filename)[1]: #Has no ext whatsoever filename += '.py' if not os.path.exists(filename): return [] try: astnode = compiler.parseFile(filename) except SyntaxError: self.failed.append(self._extless(filename)) return [] allnodes = utils.flatten(astnode, lambda node: node.getChildNodes()) names = itertools.imap(self._extract_modulename, allnodes) names = itertools.ifilter(None, names) return names
def get_all_importnodes(self, filename): # We can only read py files right now if filename.endswith(".pyd"): return [] if filename.endswith(".pyc"): filename = filename[:-1] if not os.path.exists(filename): return [] try: astnode = compiler.parseFile(filename) except SyntaxError: self.failed.append(self._extless(filename)) return [] importnodes = filter( lambda node: isinstance(node, compiler.ast.Import), utils.flatten(astnode, lambda node: node.getChildNodes()), ) return importnodes
def all_func_nodes(astnode): isclass = lambda x: isinstance(x, compiler.ast.Class) nodes = filter(isclass, utils.flatten(astnode, lambda x: x.getChildNodes())) return nodes