def anyblok_createdb(): """Create a database and install blok from config""" load_init_function_from_entry_points() Configuration.load('createdb') configuration_post_load() BlokManager.load() db_name = get_db_name() db_template_name = Configuration.get('db_template_name', None) url = Configuration.get('get_url')(db_name=db_name) create_database(url, template=db_template_name) registry = RegistryManager.get(db_name) if registry is None: return if Configuration.get('install_all_bloks'): bloks = registry.System.Blok.list_by_state('uninstalled') else: install_bloks = Configuration.get('install_bloks') or [] iou_bloks = Configuration.get('install_or_update_bloks') or [] bloks = list(set(install_bloks + iou_bloks)) registry.upgrade(install=bloks) registry.commit() registry.close()
def createdb(application, configuration_groups, **kwargs): """ Create a database and install blok from config :param application: name of the application :param configuration_groups: list configuration groupe to load :param \**kwargs: ArgumentParser named arguments """ format_configuration(configuration_groups, 'create_db', 'install-bloks', 'install-or-update-bloks') load_init_function_from_entry_points() Configuration.load(application, configuration_groups=configuration_groups, **kwargs) BlokManager.load() db_name = Configuration.get('db_name') db_template_name = Configuration.get('db_template_name', None) url = Configuration.get('get_url')(db_name=db_name) create_database(url, template=db_template_name) registry = RegistryManager.get(db_name) if registry is None: return if Configuration.get('install_all_bloks'): bloks = registry.System.Blok.list_by_state('uninstalled') else: install_bloks = Configuration.get('install_bloks') or [] iou_bloks = Configuration.get('install_or_update_bloks') or [] bloks = list(set(install_bloks + iou_bloks)) registry.upgrade(install=bloks) registry.commit() registry.close()
def anyblok_createdb(): """Create a database and install blok from config""" load_init_function_from_entry_points() Configuration.load('createdb') configuration_post_load() BlokManager.load() db_name = get_db_name() db_template_name = Configuration.get('db_template_name', None) url = Configuration.get('get_url')(db_name=db_name) create_database(url, template=db_template_name) anyblok_registry = RegistryManager.get(db_name) if anyblok_registry is None: return anyblok_registry.System.Parameter.set( "with-demo", Configuration.get('with_demo', False)) if Configuration.get('install_all_bloks'): bloks = anyblok_registry.System.Blok.list_by_state('uninstalled') else: install_bloks = Configuration.get('install_bloks') or [] iou_bloks = Configuration.get('install_or_update_bloks') or [] bloks = list(set(install_bloks + iou_bloks)) anyblok_registry.upgrade(install=bloks) anyblok_registry.commit() anyblok_registry.close()
def wsgi(): """Simple Pyramid wsgi server for development purpose """ load_init_function_from_entry_points() Configuration.load('pyramid') configuration_post_load() BlokManager.load() config = Configurator() config.include_from_entry_point() config.load_config_bloks() wsgi_host = Configuration.get('wsgi_host') wsgi_port = int(Configuration.get('wsgi_port')) app = config.make_wsgi_app() server = make_server(wsgi_host, wsgi_port, app) preload_databases(loadwithoutmigration=False) try: from colorama import Fore, Style start_msg = ("Pyramid development server running at %shttp://%s:%s%s" % (Fore.BLUE, wsgi_host, wsgi_port, Style.RESET_ALL)) except ImportError: logger.info("`pip install colorama` to get colored link") start_msg = "Pyramid development server running at http://%s:%s" % ( wsgi_host, wsgi_port) logger.info(start_msg) print(start_msg) server.serve_forever()
def test_get_invalid_blok(self): try: BlokManager.load() BlokManager.get('invalid blok') self.fail('No exception when get invalid blok') except BlokManagerException: pass
def anyblok_wsgi(application, configuration_groups, **kwargs): """ :param application: name of the application :param configuration_groups: list configuration groupe to load :param \**kwargs: ArgumentParser named arguments """ format_configuration(configuration_groups, 'preload', 'pyramid-debug', 'wsgi') load_init_function_from_entry_points() Configuration.load(application, configuration_groups=configuration_groups, **kwargs) BlokManager.load() config = Configurator() config.include_from_entry_point() config.load_config_bloks() wsgi_host = Configuration.get('wsgi_host') wsgi_port = int(Configuration.get('wsgi_port')) app = config.make_wsgi_app() server = make_server(wsgi_host, wsgi_port, app) preload_databases() logger.info("Serve forever on %r:%r" % (wsgi_host, wsgi_port)) server.serve_forever()
def load(self): BlokManager.load() preload_databases() config = Configurator() config.include_from_entry_point() config.load_config_bloks() return config.make_wsgi_app()
def test_reload(self): BlokManager.load() BlokManager.set('invalid blok', None) BlokManager.get('invalid blok') BlokManager.reload() with pytest.raises(BlokManagerException): BlokManager.get('invalid blok')
def load_registry(self): if self.enabled and self.registryLoaded is False: # Load the registry here not in configuration, # because the configuration are not load in order of score self.registryLoaded = True BlokManager.load() load_init_function_from_entry_points(unittest=True) Configuration.load_config_for_test() Configuration.parse_options(self.AnyBlokOptions, ('bloks',)) db_name = Configuration.get('db_name') if not db_name: raise Exception("No database defined to run Test") registry = RegistryManager.get(db_name) if registry: installed_bloks = registry.System.Blok.list_by_state( "installed") selected_bloks = Configuration.get('selected_bloks') if not selected_bloks: selected_bloks = installed_bloks unwanted_bloks = Configuration.get('unwanted_bloks') if unwanted_bloks is None: unwanted_bloks = [] self.bloks_path = [BlokManager.getPath(x) for x in BlokManager.ordered_bloks] self.authoried_bloks_test_files = [ path for blok in installed_bloks if blok in selected_bloks and blok not in unwanted_bloks for path in [join(BlokManager.getPath(blok), 'tests')] if exists(path)] registry.close() # free the registry to force create it again
def setUp(self): super(TestBlok, self).setUp() self.__class__.init_configuration_manager() self.__class__.createdb(keep_existing=False) BlokManager.load(entry_points=('bloks', 'test_bloks')) registry = RegistryManager.get(Configuration.get('db_name')) registry.commit() registry.close()
def test_load_anyblok(self): BlokManager.load() if not BlokManager.list(): self.fail('No blok load') if not BlokManager.has('anyblok-core'): self.fail("The blok 'anyblok-core' is missing") BlokManager.get('anyblok-core')
def test_reload(self): BlokManager.load() BlokManager.set('invalid blok', None) BlokManager.get('invalid blok') BlokManager.reload() try: BlokManager.get('invalid blok') self.fail("Reload classmethod doesn't reload the bloks") except BlokManagerException: pass
def setUpClass(cls): """ Intialialise the configuration manager """ super(DBTestCase, cls).setUpClass() cls.init_configuration_manager() if cls.createdb(keep_existing=True): BlokManager.load(entry_points=('bloks', 'test_bloks')) registry = cls.getRegistry() registry.commit() registry.close() BlokManager.unload()
def base_loaded(request): url = Configuration.get('get_url')() if not database_exists(url): db_template_name = Configuration.get('db_template_name', None) create_database(url, template=db_template_name) BlokManager.load() registry = init_registry_with_bloks([], None) registry.commit() registry.close() BlokManager.unload()
def setUpClass(cls): """ Initialize the registry. """ super(BlokTestCase, cls).setUpClass() additional_setting = cls.additional_setting() if cls.registry is None: if len(BlokManager.list()) == 0: BlokManager.load() cls.registry = RegistryManager.get(Configuration.get('db_name'), **additional_setting) cls.registry.commit()
def base_loaded(request, configuration_loaded): if sgdb_in(['MySQL', 'MariaDB']): return url = Configuration.get('get_url')() if not database_exists(url): db_template_name = Configuration.get('db_template_name', None) create_database(url, template=db_template_name) BlokManager.load() registry = init_registry_with_bloks([], None) registry.commit() registry.close() BlokManager.unload()
def test_anyblok_core_loaded(self): BlokManager.load() is_exist = 'anyblok-core' in RegistryManager.loaded_bloks self.assertEqual(is_exist, True) anyblokcore = RegistryManager.loaded_bloks['anyblok-core'] self.assertEqual(len(anyblokcore['Core']['Base']), 1) self.assertEqual(len(anyblokcore['Core']['SqlBase']), 1) self.assertEqual(len(anyblokcore['Core']['SqlViewBase']), 1) self.assertEqual(len(anyblokcore['Core']['Session']), 1) self.assertEqual(len(anyblokcore['Core']['Query']), 1) self.assertEqual(len(anyblokcore['Core']['InstrumentedList']), 1) is_exist = 'Model.System' in anyblokcore['Model'] self.assertEqual(is_exist, True) BlokManager.unload()
def test_anyblok_core_loaded(self): BlokManager.load() is_exist = 'anyblok-core' in RegistryManager.loaded_bloks assert is_exist anyblokcore = RegistryManager.loaded_bloks['anyblok-core'] assert len(anyblokcore['Core']['Base']) == 1 assert len(anyblokcore['Core']['SqlBase']) == 1 assert len(anyblokcore['Core']['SqlViewBase']) == 1 assert len(anyblokcore['Core']['Session']) == 1 assert len(anyblokcore['Core']['Query']) == 1 assert len(anyblokcore['Core']['InstrumentedList']) == 1 is_exist = 'Model.System' in anyblokcore['Model'] assert is_exist BlokManager.unload()
def worker_process(worker_id, logging_fd): """consume worker to process messages and execute the actor""" # TODO preload registries db_name = Configuration.get('db_name') try: logging_pipe = os.fdopen(logging_fd, "w") broker = prepare_broker(withmiddleware=True) broker.emit_after("process_boot") BlokManager.load() registry = RegistryManager.get(db_name, loadwithoutmigration=True) if registry is None: logger.critical("No registry found for %s", db_name) return os._exit(4) worker = Worker( broker, worker_threads=Configuration.get('dramatiq_threads', 1)) worker.start() print('worker started') except ImportError as e: logger.critical(e) return os._exit(2) except ConnectionError as e: logger.critical("Broker connection failed. %s", e) return os._exit(3) def termhandler(signum, frame): nonlocal running BlokManager.unload() if running: logger.info("Stopping worker process...") running = False else: logger.warning("Killing worker process...") return os._exit(1) logger.info("Worker process is ready for action.") signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGTERM, termhandler) signal.signal(signal.SIGHUP, termhandler) running = True while running: time.sleep(1) worker.stop() broker.close() logging_pipe.close()
def setUpClass(cls): super(TestMigration, cls).setUpClass() cls.init_configuration_manager() cls.createdb() BlokManager.load() register = Declarations.register Model = Declarations.Model cls.loaded_bloks = deepcopy(RegistryManager.loaded_bloks) EnvironmentManager.set('current_blok', 'anyblok-core') @register(Model) class Test: integer = Int(primary_key=True) other = Str() @register(Model) class TestUnique: integer = Int(primary_key=True) other = Str(unique=True) @register(Model) class TestFKTarget: integer = Int(primary_key=True) @register(Model) class TestFK: integer = Int(primary_key=True) other = Int(foreign_key=Model.TestFKTarget.use('integer')) @register(Model) class TestM2M1: idmodel1 = Int(primary_key=True) @register(Model) class TestM2M2: idmodel2 = Int(primary_key=True) rel_m2m = Many2Many(label="Rel", model=Model.TestM2M1, join_table='reltable', remote_columns='idmodel1', m2m_remote_columns='idmodel1', local_columns='idmodel2', m2m_local_columns='idmodel2', many2many='rel_m2m_inv') EnvironmentManager.set('current_blok', None)
def asgi() -> None: """uvicorn ASGI dev server""" load_init_function_from_entry_points() argv: List[str] = copy(sys.argv) Configuration.load("uvicorn") sys.argv = argv configuration_post_load() BlokManager.load() kwargs = { "app": create_app(preload_database(loadwithoutmigration=False)), "host": Configuration.get("host"), "port": Configuration.get("port"), # "uds": uds, # "fd": fd, # "loop": loop, # "http": http, # "ws": ws, # "lifespan": lifespan, # "env_file": env_file, # "log_config": LOGGING_CONFIG if log_config is None else log_config, # "log_level": log_level, # "access_log": access_log, # "interface": interface, # "debug": debug, # "reload": reload, # "reload_dirs": reload_dirs if reload_dirs else None, # "workers": workers, # "proxy_headers": proxy_headers, # "forwarded_allow_ips": forwarded_allow_ips, # "root_path": root_path, # "limit_concurrency": limit_concurrency, # "backlog": backlog, # "limit_max_requests": limit_max_requests, # "timeout_keep_alive": timeout_keep_alive, # "ssl_keyfile": ssl_keyfile, # "ssl_certfile": ssl_certfile, # "ssl_version": ssl_version, # "ssl_cert_reqs": ssl_cert_reqs, # "ssl_ca_certs": ssl_ca_certs, # "ssl_ciphers": ssl_ciphers, # "headers": list([header.split(":") for header in headers]), # "use_colors": use_colors, } run(**kwargs)
def update_project(): anyblok.load_init_function_from_entry_points() Configuration.load('update-project') anyblok.configuration_post_load() BlokManager.load() db_name = Configuration.get('db_name') logger.info("update project: db_name=%r", db_name) to_install = [] to_update = [] to_uninstall = [] url = get_url() options = {} if not database_exists(url): db_template_name = Configuration.get('db_template_name', None) create_database(url, template=db_template_name) to_install.append('team') version = None else: options.update(dict(loadwithoutmigration=True)) registry = RegistryManager.get(db_name, **options) registry.update_blok_list() # case, new blok added version = registry.System.Blok.query().filter_by( name='team').one().installed_version if version is None: pass else: to_update.append('team') registry.upgrade(install=to_install, update=to_update, uninstall=to_uninstall) if version is None: registry.Pyramid.User.insert(login='******') registry.Pyramid.CredentialStore.insert(login='******', password='******') registry.commit() registry.close() logger.info("Project updated: db_name=%r", db_name)
def load_registry(self): if not self.enabled or self.registryLoaded: return from anyblok.config import Configuration, get_db_name from anyblok import ( load_init_function_from_entry_points, configuration_post_load, ) from anyblok.blok import BlokManager from anyblok.registry import RegistryManager, return_list # Load the registry here not in configuration, # because the configurations are not loaded in order of score self.registryLoaded = True load_init_function_from_entry_points(unittest=True) Configuration.load_config_for_test() Configuration.parse_options(self.AnyBlokOptions) configuration_post_load() BlokManager.load() db_name = get_db_name() registry = RegistryManager.get(db_name) if not registry: return installed_bloks = registry.System.Blok.list_by_state("installed") selected_bloks = return_list( Configuration.get('selected_bloks')) or installed_bloks unwanted_bloks = return_list(Configuration.get('unwanted_bloks')) or [] self.authorized_blok_paths = set( BlokManager.getPath(b) for b in BlokManager.list() if b in selected_bloks and b not in unwanted_bloks) test_dirs = self.authorized_blok_test_dirs = set() for startpath in self.authorized_blok_paths: for root, dirs, _ in walk(startpath): if 'tests' in dirs: test_dirs.add(join(root, 'tests')) registry.close() # free the registry to force create it again
def wsgi(): """Simple wsgi server for dev """ load_init_function_from_entry_points() Configuration.load('pyramid') configuration_post_load() BlokManager.load() config = Configurator() config.include_from_entry_point() config.load_config_bloks() wsgi_host = Configuration.get('wsgi_host') wsgi_port = int(Configuration.get('wsgi_port')) app = config.make_wsgi_app() server = make_server(wsgi_host, wsgi_port, app) preload_databases(loadwithoutmigration=False) logger.info("Serve forever on %r:%r" % (wsgi_host, wsgi_port)) server.serve_forever()
def init_session(request, configuration_loaded): # Init registry additional_setting = {'unittest': True} if len(BlokManager.list()) == 0: BlokManager.load() registry = RegistryManager.get(Configuration.get('db_name'), **additional_setting) registry.commit() # Add savepoint registry.begin_nested() @event.listens_for(registry.session, 'after_transaction_end') def restart_savepoint(session, transaction): if transaction.nested and not transaction._parent.nested: session.expire_all() session.begin_nested() logger.info('Creating registry') yield registry request.addfinalizer(registry.session.close)
def load_registry(self): if self.enabled and self.registryLoaded is False: # Load the registry here not in configuration, # because the configuration are not load in order of score self.registryLoaded = True load_init_function_from_entry_points(unittest=True) Configuration.load_config_for_test() Configuration.parse_options(self.AnyBlokOptions) configuration_post_load() BlokManager.load() db_name = get_db_name() registry = RegistryManager.get(db_name) if registry: installed_bloks = registry.System.Blok.list_by_state( "installed") selected_bloks = return_list( Configuration.get('selected_bloks')) or installed_bloks unwanted_bloks = return_list( Configuration.get('unwanted_bloks')) or [] self.bloks_path = [BlokManager.getPath(x) for x in BlokManager.ordered_bloks] self.authoried_bloks_test_files = [] for blok in installed_bloks: if blok not in selected_bloks or blok in unwanted_bloks: continue startpath = BlokManager.getPath(blok) for root, dirs, _ in walk(startpath): if 'tests' in dirs: self.authoried_bloks_test_files.append( join(root, 'tests')) registry.close() # free the registry to force create it again
def bloks_loaded(request): request.addfinalizer(BlokManager.unload) BlokManager.load()
def setup(app): BlokManager.load() app.add_autodocumenter(AnyBlokDeclarationDocumenter) app.add_autodocumenter(AnyBlokMethodDocumenter) return {'version': version, 'parallel_read_safe': True}
def load(self) -> "FastAPI": BlokManager.load(entry_points=("bloks", )) return create_app(preload_database())
def bloks_loaded(request, configuration_loaded): request.addfinalizer(BlokManager.unload) BlokManager.load()
from os import environ, path from appdirs import AppDirs from anyblok import load_init_function_from_entry_points if BlokManager.bloks: # AnyBlok already load, the state are not sure, better to stop here sys.exit(1) load_init_function_from_entry_points() # load default files ad = AppDirs('AnyBlok') # load the global configuration file Configuration.parse_configfile(path.join(ad.site_config_dir, 'conf.cfg'), ()) # load the user configuration file Configuration.parse_configfile(path.join(ad.user_config_dir, 'conf.cfg'), ()) # load config file in environment variable configfile = environ.get('ANYBLOK_CONFIGFILE') if configfile: Configuration.parse_configfile(configfile, ()) if 'logging_level' in Configuration.configuration: Configuration.initialize_logging() BlokManager.load() preload_databases() config = Configurator() config.include_from_entry_point() config.load_config_bloks() app = config.make_wsgi_app()
def setUp(self): """ Create a database and load the blok manager """ self.registry = None super(DBTestCase, self).setUp() BlokManager.load(entry_points=self.blok_entry_points)
def test_load_without_blok_group(self): try: BlokManager.load(entry_points=()) self.fail('No watchdog to load without blok groups') except BlokManagerException: pass
def test_load_with_invalid_blok_group(self): try: BlokManager.load(entry_points=('Invalid blok group', )) self.fail('Load with invalid blok group') except BlokManagerException: pass
def setUpClass(cls): super(TestMigration, cls).setUpClass() cls.init_configuration_manager() cls.createdb() BlokManager.load() register = Declarations.register Model = Declarations.Model cls.loaded_bloks = deepcopy(RegistryManager.loaded_bloks) EnvironmentManager.set('current_blok', 'anyblok-core') @register(Model) class Test: integer = Int(primary_key=True) other = Str() @register(Model) class TestUnique: integer = Int(primary_key=True) other = Str(unique=True) @register(Model) class TestIndex: integer = Int(primary_key=True) other = Str(index=True) @register(Model) class TestCheck: integer = Int(primary_key=True) @classmethod def define_table_args(cls): table_args = super(TestCheck, cls).define_table_args() return table_args + ( CheckConstraint('integer > 0', name='test'),) @register(Model) class TestCheckLongConstraintName: integer = Int(primary_key=True) @classmethod def define_table_args(cls): table_args = super(TestCheckLongConstraintName, cls).define_table_args() return table_args + ( CheckConstraint('integer > 0', name=( 'long_long_long_long_long_long_long_long_long_long_' 'long_long_long_long_long_long_long_long_test')),) @register(Model) class TestFKTarget: integer = Int(primary_key=True) @register(Model) class TestFK: integer = Int(primary_key=True) other = Int( foreign_key=Model.TestFKTarget.use('integer')) @register(Model) class TestFK2: integer = Int(primary_key=True) other = Int( foreign_key=Model.TestFKTarget.use('integer').options( ondelete='cascade')) @register(Model) class TestM2M1: idmodel1 = Int(primary_key=True) @register(Model) class TestM2M2: idmodel2 = Int(primary_key=True) rel_m2m = Many2Many(label="Rel", model=Model.TestM2M1, join_table='reltable', remote_columns='idmodel1', m2m_remote_columns='idmodel1', local_columns='idmodel2', m2m_local_columns='idmodel2', many2many='rel_m2m_inv') EnvironmentManager.set('current_blok', None)
def test_load_with_invalid_blok_group(self): try: BlokManager.load(entry_points=('Invalid blok group',)) self.fail('Load with invalid blok group') except BlokManagerException: pass
def setUpClass(cls): super(TestMigration, cls).setUpClass() cls.init_configuration_manager() cls.createdb() BlokManager.load() register = Declarations.register Model = Declarations.Model cls.loaded_bloks = deepcopy(RegistryManager.loaded_bloks) EnvironmentManager.set('current_blok', 'anyblok-core') @register(Model) class Test: integer = Int(primary_key=True) other = Str() @register(Model) class TestUnique: integer = Int(primary_key=True) other = Str(unique=True) @register(Model) class TestIndex: integer = Int(primary_key=True) other = Str(index=True) @register(Model) class TestCheck: integer = Int(primary_key=True) @classmethod def define_table_args(cls): table_args = super(TestCheck, cls).define_table_args() return table_args + (CheckConstraint('integer > 0', name='test'), ) @register(Model) class TestCheckLongConstraintName: integer = Int(primary_key=True) @classmethod def define_table_args(cls): table_args = super(TestCheckLongConstraintName, cls).define_table_args() return table_args + (CheckConstraint( 'integer > 0', name=('long_long_long_long_long_long_long_long_long_long_' 'long_long_long_long_long_long_long_long_test')), ) @register(Model) class TestFKTarget: integer = Int(primary_key=True) @register(Model) class TestFK: integer = Int(primary_key=True) other = Int(foreign_key=Model.TestFKTarget.use('integer')) @register(Model) class TestFK2: integer = Int(primary_key=True) other = Int(foreign_key=Model.TestFKTarget.use('integer').options( ondelete='cascade')) @register(Model) class TestM2M1: idmodel1 = Int(primary_key=True) @register(Model) class TestM2M2: idmodel2 = Int(primary_key=True) rel_m2m = Many2Many(label="Rel", model=Model.TestM2M1, join_table='reltable', remote_columns='idmodel1', m2m_remote_columns='idmodel1', local_columns='idmodel2', m2m_local_columns='idmodel2', many2many='rel_m2m_inv') EnvironmentManager.set('current_blok', None)
def test_reload_blok(self): BlokManager.load() try: RegistryManager.reload() finally: BlokManager.unload()