def __init__(self, name, *, loop=None, router=None, middlewares=(), logger=web.web_logger, access_logger=None, handler_factory=web.RequestHandlerFactory, **OPTIONS): """Initialize the application.""" super().__init__(loop=loop, router=router, middlewares=middlewares, logger=logger, handler_factory=handler_factory) self.name = name self._error_handlers = {} self._start_callbacks = [] # Overide options self.defaults['CONFIG'] = OPTIONS.pop('CONFIG', self.defaults['CONFIG']) self.cfg.update(OPTIONS) self._debug = self.cfg.DEBUG # Setup logging ch = logging.StreamHandler() ch.setFormatter( logging.Formatter(self.cfg.LOG_FORMAT, self.cfg.LOG_DATE_FORMAT)) self.logger.addHandler(ch) self.logger.setLevel(self.cfg.LOG_LEVEL) self.logger.name = 'muffin' self.logger.propagate = False self.access_logger = access_logger LOGGING_CFG = self.cfg.get('LOGGING') if LOGGING_CFG and isinstance(LOGGING_CFG, dict): logging.config.dictConfig(LOGGING_CFG) # Setup CLI self.manage = Manager(self) # Setup static files if isinstance(self.cfg.STATIC_FOLDERS, str): self.cfg.STATIC_FOLDERS = [self.cfg.STATIC_FOLDERS] elif not isinstance(self.cfg.STATIC_FOLDERS, list): self.cfg.STATIC_FOLDERS = list(self.cfg.STATIC_FOLDERS) # Setup plugins self.plugins = self.ps = LStruct() for plugin in self.cfg.PLUGINS: try: self.install(plugin) except Exception as exc: # noqa self.logger.error('Plugin is invalid: %s', plugin) self.logger.exception(exc)
def test_struct(): from muffin.utils import Struct, LStruct data = Struct({'test': 42}) assert data.test == 42 data.test = 21 assert data.test == 21 settings = LStruct({'option': 'value'}) assert settings.option == 'value' settings.option2 = 'value2' settings.lock() settings.lock() with pytest.raises(RuntimeError): settings.test = 42
def cfg(self): """ Load the application configuration. """ config = LStruct(self.defaults) module = config['CONFIG'] = os.environ.get( CONFIGURATION_ENVIRON_VARIABLE, config['CONFIG']) if module: try: module = import_module(module) config.update({ name: getattr(module, name) for name in dir(module) if name == name.upper() and not name.startswith('_') }) except ImportError: config.CONFIG = None self.register_on_start( lambda app: app.logger.warn("The configuration hasn't found: %s" % module)) return config
def cfg(self): """Load the application configuration. This method loads configuration from python module. """ config = LStruct(self.defaults) module = config['CONFIG'] = os.environ.get( CONFIGURATION_ENVIRON_VARIABLE, config['CONFIG']) if module: try: module = import_module(module) config.update({ name: getattr(module, name) for name in dir(module) if name == name.upper() and not name.startswith('_') }) except ImportError as exc: config.CONFIG = None message = "Error importing %s: %s" % (module, exc) self.register_on_start(lambda app: app.logger.error(message)) return config
def test_struct(): from muffin.utils import Struct, LStruct data = Struct({'test': 42}) assert data.test == 42 data.test = 21 assert data.test == 21 settings = LStruct({'option': 'value'}) assert settings.option == 'value' settings.option2 = 'value2' settings.freeze() settings.freeze() with pytest.raises(RuntimeError): settings.test = 42
def __init__(self, **options): """Save application and create he plugin's configuration.""" self.app = None self.config = self.cfg = LStruct(options)
def __init__(self, app=None, **options): """Save application and create he plugin's configuration.""" self.config = self.cfg = LStruct(options) self.app = app if app is not None: app.install(self)