def get_function_object(self, python_path, name): """ Get the python function for a given function """ fullname = ".".join((python_path, name)) try: loader = _pkgutil.get_loader(fullname.rsplit(".", 1)[0]) module = loader.load_module(python_path) function = getattr(module, name) return function except Exception, e: msg = "Failed to find function %s [%s: %s]" % (name, e.__class__.__name__, e) warnings.warn(msg) return None
def get_function_object(self, python_path, name): """ Get the python function for a given function """ fullname = '.'.join((python_path, name)) try: loader = _pkgutil.get_loader(fullname.rsplit('.', 1)[0]) module = loader.load_module(python_path) function = getattr(module, name) return function except Exception, e: msg = ("Failed to find function %s [%s: %s]" % (name, e.__class__.__name__, e)) warnings.warn(msg) return None
def _ast_mod_source_from_module_and_name(self, module, name): """ Retreive the AST and module source for a function given its module and name. If it fails to find the module or find the function within the module, set the load_error appropriately and return None. Otherwise, return the FuncDef ast for the function. Additionally, return the module source for use with other parsers. """ # Find the source code for the specified module. try: loader = _pkgutil.get_loader(module) except: # A variety of exceptions can be thrown from getting the loader, # mostly dealing with a failure to parse an __init__.py file self.load_error = "failed to find module '%s'" % self.module return None, None if loader is None: self.load_error = "failed to find module '%s'" % self.module return None, None # fixme: Can this fail if the above succeeded? module_source = loader.get_source() # Convert it to an ast, and find all function asts defined in it. try: module_ast = compiler.parse(module_source) except: # This catches parse errors in the python source self.load_error = "failed to parse module '%s'" % self.module return None, None # fixme: find_local_defs is a bit primitive. If we have problems, # with finding the wrong function, this is the place to look. funcs = parse_tools.find_local_defs(module_ast) ast = funcs.get(name, None) if ast is None: # fixme much better error handling here. self.load_error = "failed to find '%s' in module '%s'" % \ (self.name, self.module) return ast, module_source
def get_module_path(module_name): """ Given a module, get an absolute path to it. pkgutil, instead of imp, is used for 2 reasons: * pkgutil works with dotted names * pkgutil works with eggs There are a couple of downsides, the methods we use are not documented and the method was added in python 2.5. For convenience, _pkgutil is used instread, which is copied from Python 2.5 to support Python 2.4. """ imp_loader = _pkgutil.get_loader(module_name) if imp_loader is None: logger.error("Search Path not found for module: %s", module_name) return '' return imp_loader.filename