def load(self): BlokManager.load() preload_databases() config = Configurator() config.include_from_entry_point() config.load_config_bloks() return config.make_wsgi_app()
def reload_blokmanager(cls, **kwargs): """Reload all the bloks with their code sources""" RegistryManager.clear() # close all the registry BlokManager.reload() # reload all the blok code # FIXME BlokManager.reload should close all the registry and # forbidden load registry during the reload return [{'type': 'RELOAD'}]
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 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 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) 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 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 export_i18n(cls, blok_name): b = BlokManager.get(blok_name) bpath = BlokManager.getPath(blok_name) po = polib.POFile() po.metadata = { 'Project-Id-Version': b.version, 'POT-Creation-Date': datetime.now().isoformat(), 'MIME-Version': '1.0', 'Content-Type': 'text/plain; charset=utf-8', 'Content-Transfer-Encoding': '8bit', } if hasattr(b, 'furetui'): templates = Template() for template in b.furetui.get('templates', []): with open(join(bpath, template), 'r') as fp: templates.load_file(fp, ignore_missing_extend=True) templates.export_i18n(po) Mapping = cls.anyblok.IO.Mapping for mapping in Mapping.query().filter_by(blokname=blok_name): obj = Mapping.get(mapping.model, mapping.key) if not obj: continue for context, text in obj.get_i18n_to_export(mapping.key): entry = Translation.define(context, text) po.append(entry) cls.export_i18n_bases(blok_name, po) Path(path.join(bpath, 'locale')).mkdir(exist_ok=True) po.save(path.join(bpath, 'locale', f'{blok_name}.pot'))
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_set_two_time(self): blok_name = 'ABlok' BlokManager.set(blok_name, Blok) try: BlokManager.set(blok_name, Blok) self.fail("No watch dog for set two time the same blok") except BlokManagerException: pass
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 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)
def tearDown(self): """ Clear the registry, unload the blok manager and drop the database """ if self.registry: self.registry.close() RegistryManager.clear() BlokManager.unload() super(DBTestCase, self).tearDown()
def tearDown(self): """ Clear the registry, unload the blok manager and drop the database """ registry = RegistryManager.get(Configuration.get('db_name')) registry.close() RegistryManager.clear() BlokManager.unload() clear_mappers() self.__class__.dropdb() super(TestBlok, self).tearDown()
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 tearDown(self): """ Clear the registry, unload the blok manager and drop the database """ if self.registry: self.registry.close() RegistryManager.clear() BlokManager.unload() clear_mappers() super(DBTestCase, self).tearDown()
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 test_start_function(self): BlokManager.unload() db_name = Configuration.get('db_name') or 'test_anyblok' db_driver_name = Configuration.get('db_driver_name') or 'postgresql' testargs = ['default', '--db-name', db_name, '--db-driver-name', db_driver_name] with patch.object(sys, 'argv', testargs): registry = start('default') self.assertIsNotNone(registry)
def test_start_function(self): BlokManager.unload() db_name = Configuration.get('db_name') or 'test_anyblok' db_driver_name = Configuration.get('db_driver_name') or 'postgresql' testargs = ['default', '--db-name', db_name, '--db-driver-name', db_driver_name] with patch.object(sys, 'argv', testargs): registry = start('default') assert registry is not None
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 get_logo(self): """fget of ``logo`` return the path in the blok of the logo :rtype: absolute path or None if unexiste logo """ blok = BlokManager.get(self.name) blok_path = BlokManager.getPath(blok.name) file_path = join(blok_path, blok.logo) if isfile(file_path): return file_path return None
def get_templates_from(attr): tmpl = Template(forclient=True) for blok_name in BlokManager.ordered_bloks: blok = BlokManager.get(blok_name) if hasattr(blok, attr): bpath = BlokManager.getPath(blok_name) for template in getattr(blok, attr): with open(join(bpath, template), 'r') as fp: tmpl.load_file(fp) tmpl.compile() return tmpl.get_all_template()
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 load(self): from os.path import join tmpl = Template() Blok = self.registry.System.Blok for blok in Blok.list_by_state('installed'): b = BlokManager.get(blok) if hasattr(b, 'views'): bpath = BlokManager.getPath(blok) for template in b.views: with open(join(bpath, template), 'r') as fp: tmpl.load_file(fp) tmpl.compile() self.registry.furetui_views = tmpl
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 load(self): from os.path import join tmpl = Template() Blok = self.registry.System.Blok for blok in Blok.list_by_state('installed'): b = BlokManager.get(blok) if hasattr(b, 'views'): bpath = BlokManager.getPath(blok) for template in b.views: with open(join(bpath, template), 'r') as fp: tmpl.load_file(fp) tmpl.compile() self.registry.erpblok_views = tmpl
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 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 get_templates(cls): """ Return the list of the web client template to load """ from os.path import join tmpl = Template(forclient=True) Blok = cls.registry.System.Blok for blok in Blok.list_by_state('installed'): b = BlokManager.get(blok) if hasattr(b, 'client_templates'): bpath = BlokManager.getPath(blok) for template in b.client_templates: with open(join(bpath, template), 'r') as fp: tmpl.load_file(fp) tmpl.compile() return tmpl.get_all_template()
def current_blok(): filename = inspect.stack()[1][1] for blok in BlokManager.ordered_bloks: if filename.startswith(BlokManager.getPath(blok)): return blok raise AnyBlokPyramidException("You are not in a Blok")
def current_blok(): filename = inspect.stack()[1][1] for blok in BlokManager.ordered_bloks: if filename.startswith(BlokManager.getPath(blok)): return blok raise AnyBlokPyramidException("You are not in a Blok") # pragma: no cover
def anyblok_nose(): """Run nose unit test for the registry """ warnings.simplefilter('default') registry = anyblok.start('nose', useseparator=True, unittest=True) defaultTest = [] 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 [] defaultTest = [] 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: defaultTest.append(join(root, 'tests')) registry.close() # free the registry to force create it again sys.exit(main(defaultTest=defaultTest))
def run_exit(application, configuration_groups, **kwargs): """Run nose unit test for the registry :param application: name of the application :param configuration_groups: list configuration groupe to load :param \**kwargs: ArgumentParser named arguments """ format_configuration(configuration_groups, 'unittest') registry = anyblok.start(application, configuration_groups=configuration_groups, useseparator=True, unittest=True, **kwargs) defaultTest = [] 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') or [] defaultTest = [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 sys.exit(main(defaultTest=defaultTest))
def load(self): """Load all the actor defined in all the installed bloks""" Blok = self.registry.System.Blok for blok in Blok.list_by_state('installed'): b = BlokManager.get(blok) if hasattr(b, 'declare_actors'): b.declare_actors(self.registry)
def anyblok_nose(): """Run nose unit test for the registry """ registry = anyblok.start('nose', useseparator=True, unittest=True) defaultTest = [] 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 [] defaultTest = [] 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: defaultTest.append(join(root, 'tests')) registry.close() # free the registry to force create it again sys.exit(main(defaultTest=defaultTest))
def test_update_loadwithoutmigration(self, registry_blok16_installed): registry = registry_blok16_installed registry.loadwithoutmigration = True registry.System.Parameter.set("with-demo", True) registry.upgrade(update=('test-blok16', )) blok = BlokManager.get('test-blok16') assert blok.called_methods == []
def import_file(self, Importer, model, *file_path, **kwargs): """ Import data file :param importer_name: Name of the importer (need installation of the Blok which have the importer) :param model: Model of the data to import :param file_path: relative path of the path in this Blok :param kwargs: Option for the importer :rtype: return dict of result """ blok_path = BlokManager.getPath(self.name) _file = join(blok_path, *file_path) logger.info("import %r file: %r", Importer, _file) file_to_import = None with open(_file, 'rb') as fp: file_to_import = fp.read() importer = Importer.insert(model=model, file_to_import=file_to_import, **kwargs) started_at = datetime.now() res = importer.run(self.name) stoped_at = datetime.now() dt = stoped_at - started_at logger.info("Create %d entries, Update %d entries (%d.%d sec)", len(res['created_entries']), len(res['updated_entries']), dt.seconds, dt.microseconds) if 'error_found' in res and res['error_found']: for error in res['error_found']: logger.error(error) else: importer.delete() return res
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 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 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 imports(self): """ Imports modules and / or packages listed in the blok path""" from anyblok.blok import BlokManager from anyblok.registry import RegistryManager RegistryManager.init_blok(self.blok) b = BlokManager.get(self.blok) b.import_declaration_module()
def test_unistall_without_demo(self, registry_blok16_installed): registry = registry_blok16_installed registry.System.Parameter.set("with-demo", False) registry.upgrade(uninstall=('test-blok16', )) blok = BlokManager.get('test-blok16') assert blok.called_methods == [ "uninstall", ]
def get_long_description(self): """ fget of the ``long_description`` Column.Selection :rtype: the readme file of the blok """ blok = BlokManager.get(self.name) path = BlokManager.getPath(self.name) readme = getattr(blok, 'readme', 'README.rst') if readme == '__doc__': return blok.__doc__ file_path = join(path, readme) description = '' if isfile(file_path): with open(file_path, 'r') as fp: description = fp.read() return description
def get_addons(request): res = [] for blok_name in BlokManager.ordered_bloks: blok = BlokManager.get(blok_name) if hasattr(blok, 'setting_blok_description'): addons = blok.setting_blok_description addons['id'] = blok_name res.append(addons) return res
def get_short_description(self): """ fget of the ``short_description`` Column.Selection :rtype: the docstring of the blok """ blok = BlokManager.get(self.name) if hasattr(blok, '__doc__'): return blok.__doc__ or '' return ''
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 load(self): """ Method to load the blok when the registry is completly loaded """ name = self.name blok_cls = BlokManager.get(name) if blok_cls is None: logger.warning("load(): class of Blok %r not found, " "Blok can't be loaded", name) return logger.info("Loading Blok %r", name) blok_cls(self.registry).load() logger.debug("Succesfully loaded Blok %r", name)
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')