def fromFile(path, filename, filterer=None): logging.debug('Looking for file: %s'%(os.path.join(path, filename))) o = None if _caching: o = _cache.getCachedFile(path, filename) if not o: o = [] mod_name = os.path.splitext(filename)[0] new_mod = None try: fp, pathname, desc = imp.find_module(mod_name,[path]) try: logging.debug('Found, attempting to load...') new_mod = imp.load_module(mod_name,fp,pathname,desc) logging.debug('Loaded: %s'%(new_mod)) finally: if fp: fp.close() except ImportError, err: logging.debug('Failed to import %s, %s'%(mod_name, err)) if not new_mod: return [] o = fromModule(new_mod) if _caching: _cache.addCachedFile(path, filename, o) if filterer: o = filter(filterer, o)
def fromPackage(package, filterer = None): logging.debug("Attempting to load from package: %s"%(package)) if _caching: o = _cache.getCachedPackage(package) if not o: o = [] new_mod = __import__(package, None, None, "*") if not hasattr(new_mod, "__path__"): raise Exception("Not a package") for p in new_mod.__path__: o = o + fromPath(p) if _caching: _cache.addCachedPackage(package, o) if filterer: o = filter(filterer, o) return o
def fromModule(mod, filterer=None): if _caching: o = _cache.getCachedModule(mod) if not o: o = [] if type(mod) is type(""): mod = _import(mod) if hasattr(mod, "EXPORTS"): s = getattr(mod, "EXPORTS") else: s = [getattr(mod, x) for x in dir(mod)] s = filter(lambda _: (type(_) is type(object)), s) o = filter(ISprinkle.implementedBy, s) logging.debug("Found sprinkles: %s"%(o)) if _caching: _cache.addCachedModule(mod, o) if filterer: logging.debug('Filtering %s with %s'%(o, filterer)) o = filter(filterer, o) return o
def _import(name): logging.debug("Importing: %s" % (name)) mod = import_hook(name) logging.debug("Found module: %s" % (mod)) components = name.split('.') for comp in components[1:]: mod = getattr(mod, comp) logging.debug("Found module: %s" % (mod)) return mod
def fromPath(path, filterer=None): logging.debug('Looking for sprinkles in path: %s...'%(path)) o = None if _caching: o = _cache.getCachedPath(path) if not o: o = [] files = set([os.path.splitext(x)[0] for x in os.listdir(path)]) for f in files: logging.debug('Found file: %s'%(f)) loaded = fromFile(path, f) o = o + loaded if _caching: _cache.addCachedPath(path, o) if filterer: logging.debug('Filtering %s with %s'%(o, filterer)) o = filter(filterer, o) logging.debug("Passed: %s"%(o)) return o
def getCachedModule(self, module): if module in self.cachedModules.keys(): logging.debug("Found cached module: %s"%(module)) return self.cachedModules[module] return None
def getCachedPackage(self, package): if package in self.cachedPackages.keys(): logging.debug("Found cached package: %s"%(package)) return self.cachedPackages[package] return None
def getCachedFile(self, path, filename): f = os.path.join(path, filename) if f in self.cachedFiles.keys(): logging.debug("Found cached file: %s"%(f)) return self.cachedFiles[f] return None
def getCachedPath(self, path): if path in self.cachedPaths.keys(): logging.debug("Found cached path: %s"%(path)) return self.cachedPaths[path] return None