def createInstance(self, module, type, **kargs): for package in reversed(self.packages.keys()): try: imported = __import__(self.packages[package].module+"."+module, __builtin__.globals(), __builtin__.locals(), [type]) instance = getattr(imported, type) except ImportError as importError: self.spam("Importing type "+type+" from "+ self.packages[package].module+"."+module+" failed: "+ str(importError)) self.spam(traceback.format_exc()) except AttributeError as attributeError: self.spam("Importing type "+type+" from "+ self.packages[package].module+"."+module+" failed: "+ str(attributeError)) self.spam(traceback.format_exc()) else: self.spam("Importing type "+type+" from "+ self.packages[package].module+"."+module+" succeeded") return instance(**kargs) error = "Failed to import "+type+" from module "+module+"." if self.debug: error = error+" See debugging output for details." else: error = error+" Enable debugging output for details." self.error(error)
def include(self, package, **kargs): package = package.replace("-", "_") if package in self.packages.keys(): return self.info("Including package: "+package) try: self.packages[package] = Package(package, **kargs) except Exception as includeError: self.error("Failed to include package "+package+": "+str(includeError)) if self.packages[package].requires: for required in self.packages[package].requires: self.include(required) packageModule = __import__(self.packages[package].module) packagePath = os.path.dirname(packageModule.__file__) modules = [name for _, name, _ in pkgutil.iter_modules([packagePath])] if "facade" in modules: facade = __import__(self.packages[package].module+".facade", __builtin__.globals(), __builtin__.locals(), ["*"]) for expression in dir(facade): if not __builtin__.__dict__.has_key(expression): __builtin__.__dict__[expression] = getattr(facade, expression)
def _generate_logger( cls, name, level, buffer, terminator, colored_format, format ): # # ''' Creates a new logger instance by initializing all its components \ with given arguments or default properties saved as class \ properties. Examples: >>> Logger._generate_logger( ... 'test', ('info',), (Buffer(),), ('',), (''), ('',) ... ) # doctest: +ELLIPSIS <logging.Logger object at ...> ''' # TODO check branches. properties = [] for property_name in ( 'level', 'buffer', 'terminator', 'colored_format', 'format' ): properties.append( builtins.locals()[property_name] if builtins.locals()[ property_name ] else builtins.getattr(cls, property_name)) for handler in getLogger(name).handlers: getLogger(name).removeHandler(handler) logger = getLogger(name) logger.propagate = False for _level, _buffer, _terminator, _colored_format, _format in \ builtins.zip(properties[0], properties[1], properties[2], properties[3], properties[4]): handler = LoggingStreamHandler(stream=_buffer) handler.terminator = _terminator handler.setLevel(_level.upper()) # TODO check new branches if _colored_format is None: handler.setFormatter(LoggingFormatter(_format)) else: handler.setFormatter(ColoredLoggingFormatter(_colored_format)) logger.addHandler(handler) ''' Set meta logger level to first given level (level is first \ property). ''' logger.setLevel(builtins.getattr(logging, properties[0][0].upper())) return logger
def import_all_but_wx(name, globals=__builtin__.globals(), locals=__builtin__.locals(), fromlist=[], level=-1, default_import=__builtin__.__import__): if name == "wx" or name.startswith("wx."): raise ImportError("Not allowed to import wx!") return default_import(name, globals, locals, fromlist, level)
def __init__(self, name, homeVar = None, configDir = None, systemDir = None, userDir = None, module = None, requires = None, options = None, arguments = None, **kargs): super(Package, self).__init__() self.name = name self.module = module self.requires = requires self.options = options self.arguments = arguments self.homeVar = homeVar self.configDir = configDir self.systemDir = systemDir self.userDir = userDir if not self.module: self.module = self.name.replace("-", "_") imported = __import__(self.module+".config", __builtin__.globals(), __builtin__.locals(), ["config"]) self.configuration = getattr(imported, "Configuration")() if not self.homeVar: self.homeVar = (self.name.replace("-", "_")+"_HOME").upper() if not self.configDir: self.configDir = self.configuration.configurationPath if not self.systemDir: self.systemDir = self.configuration.filePath if not self.userDir: self.userDir = os.path.join(os.environ["HOME"], "."+self.name) if not self.requires: if hasattr(self.configuration, "requires"): self.requires = self.configuration.requires if not self.options: if hasattr(self.configuration, "options"): self.options = self.configuration.options if not self.arguments: if hasattr(self.configuration, "arguments"): self.arguments = self.configuration.arguments
def _set_properties( cls, level, buffer, terminator, colored_format, format ): # # ''' This method sets the class properties. Examples: >>> Logger._set_properties( ... level=Logger.level, buffer=Logger.buffer, ... terminator=Logger.terminator, ... colored_format=Logger.colored_format, format=Logger.format ... ) # doctest: +ELLIPSIS <class '...Logger'> ''' # TODO check branches. scope = builtins.locals() for name in builtins.filter(lambda name: scope[name], ( 'level', 'buffer', 'terminator', 'colored_format', 'format' )): builtins.setattr(cls, name, scope[name]) return cls