def test_no_context(self): with pytest.raises(KeyError): settings['COMPONENTS'] assert settings.get('COMPONENTS') is None assert 'COMPONENTS' not in settings assert len(settings) == 0 assert len(list(iter(settings))) == 0
def __init__(self, *args, **kwargs): # Pull server configuration out of configuration. server = settings.get('SERVER', {}) server['host'] = server.get('host', 'localhost') server['port'] = server.get('port', 8000) server['threaded'] = server.get('threaded', True) # Default the server configuration for flask-script. kwargs.update(server) # Continue initialization. super().__init__(*args, **kwargs)
def _package_of(module): """Gets the root package name of the passed `models` package. """ # Get the registered package this model belongs to. package = module.split('.') while package: # Is this name a registered package? test = '.'.join(package) if test in settings.get('PACKAGES', []): # This is the package we are in. return test # Remove the right-most segment. package.pop() if not package: # No package was found to be registered; attempt to guess the # right package name; strip all occurrances of '.models' from the # pacakge name. return module.replace('.models', '')
def _component_of(name): """Get the root package or module of the passed module. """ # Get the registered package this model belongs to. segments = name.split('.') while segments: # Is this name a registered package? test = '.'.join(segments) if test in settings.get('COMPONENTS', []): # This is the component we are in. return test # Remove the right-most segment. segments.pop() if not segments and '.models' in name: # No package was found to be registered; attempt to guess the # right package name; strip all occurrances of '.models' from the # pacakge name. return _component_of(name.replace('.models', ''))
def __getitem__(self, name): if 'DATABASES' not in settings: raise exceptions.ImproperlyConfigured( 'DATABASES not configured in project settings.') if name not in settings['DATABASES']: raise exceptions.ImproperlyConfigured( '%r not present in DATABASES configuration.' % name) config = settings['DATABASES'][name] if isinstance(config, six.string_types): url = make_url(config) options = {} else: config = dict(map(lambda i: (i[0].lower(), i[1]), config.items())) options = config.get('options', {}) url = URL(config['engine'], username=config.get('username', config.get('user')), password=config.get('password', config.get('pass')), host=config.get('hostname', config.get('host')), port=config.get('port'), database=config.get('name', config.get('database'))) # If alchemist is invoked by a test runner we should switch to using # testing databases. if settings['TESTING']: if url.drivername.startswith('sqlite'): # Switch to using an in-memory database for sqlite. url.database = ':memory:' else: # Switch to using a named testing database for other dialects. ident = threading.current_thread().ident url.database = 'test_%s_%s' % (url.database, ident) # Apply MySQL hacks to make MySQL play nice. pool_size = None pool_recycle = None if url.drivername.startswith('mysql'): pool_size = 10 pool_recycle = 7200 # Get "global" options for the database engine. pool_size = settings.get('DATABASE_POOL_SIZE', pool_size) if pool_size: options.setdefault('pool_size', pool_size) pool_recycle = settings.get('DATABASE_POOL_RECYCLE', pool_recycle) if pool_recycle: options.setdefault('pool_recycle', pool_recycle) pool_timeout = settings.get('DATABASE_POOL_TIMEOUT') if pool_timeout: options.setdefault('pool_timeout', pool_timeout) # Forward configuration to sqlalchemy and create the engine. engine = sa.create_engine(url, **options) if settings["DEBUG"]: # Create a no-op listener if we're in debug mode. from sqlalchemy.event import listen listen(engine, "after_cursor_execute", lambda *a, **kw: None) # Return the created engine. return engine
def __getitem__(self, name): if 'DATABASES' not in settings: raise exceptions.ImproperlyConfigured( 'DATABASES not configured in project settings.') if name not in settings['DATABASES']: raise exceptions.ImproperlyConfigured( '%r not present in DATABASES configuration.' % name) config = settings['DATABASES'][name] if isinstance(config, six.string_types): url = make_url(config) options = {} else: config = dict(map(lambda i: (i[0].lower(), i[1]), config.items())) options = config.get('options', {}) url = URL( config['engine'], username=config.get('username', config.get('user')), password=config.get('password', config.get('pass')), host=config.get('hostname', config.get('host')), port=config.get('port'), database=config.get('name', config.get('database'))) # If alchemist is invoked by a test runner we should switch to using # testing databases. if settings['TESTING']: if url.drivername.startswith('sqlite'): # Switch to using an in-memory database for sqlite. url.database = ':memory:' else: # Switch to using a named testing database for other dialects. ident = threading.current_thread().ident url.database = 'test_%s_%s' % (url.database, ident) # Apply MySQL hacks to make MySQL play nice. pool_size = None pool_recycle = None if url.drivername.startswith('mysql'): pool_size = 10 pool_recycle = 7200 # Get "global" options for the database engine. pool_size = settings.get('DATABASE_POOL_SIZE', pool_size) if pool_size: options.setdefault('pool_size', pool_size) pool_recycle = settings.get('DATABASE_POOL_RECYCLE', pool_recycle) if pool_recycle: options.setdefault('pool_recycle', pool_recycle) pool_timeout = settings.get('DATABASE_POOL_TIMEOUT') if pool_timeout: options.setdefault('pool_timeout', pool_timeout) # Forward configuration to sqlalchemy and create the engine. engine = sa.create_engine(url, **options) if settings["DEBUG"]: # Create a no-op listener if we're in debug mode. from sqlalchemy.event import listen listen(engine, "after_cursor_execute", lambda *a, **kw: None) # Return the created engine. return engine