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 db_exists(cls, db_name=None): if not db_name: raise RegistryException('db_name is required') urls = [] url = Configuration.get('db_url') if url: urls.append(url) wo_url = Configuration.get('db_wo_url') if wo_url: urls.append(wo_url) for ro_url in Configuration.get('db_ro_urls', []) or []: urls.append(ro_url) gurl = Configuration.get('get_url', get_url) for url in urls: url = gurl(db_name=db_name, url=url) if not database_exists(url): return False else: return database_exists(gurl(db_name=db_name)) return True
def test_db_exists_with_ro_url(self): db_name = Configuration.get('db_name') Registry = Configuration.get('Registry') with DBTestCase.Configuration( db_ro_urls=['postgresql:///'], db_url='', db_wo_url='' ): self.assertTrue(Registry.db_exists(db_name=db_name))
def list_databases(): """ return the name of the databases found in the BDD the result can be filtering by the Configuration entry ``db_filter`` ..warning:: For the moment only the ``prostgresql`` dialect is available :rtype: list of the database's names """ url = Configuration.get('get_url')() db_filter = Configuration.get('db_filter') text = None if url.drivername in ('postgres', 'postgresql'): url = Configuration.get('get_url')(db_name='postgres') text = "SELECT datname FROM pg_database" if db_filter: db_filter = db_filter.replace('%', '%%') text += " where datname like '%s'" % db_filter if text is None: return [] engine = create_engine(url) return [ x[0] for x in engine.execute(text).fetchall() if x[0] not in ('template1', 'template0', 'postgres') ]
def getSessionAuthenticationPolicy(): """Define the authentication policy for a session""" return SessionAuthenticationPolicy( prefix=Configuration.get('pyramid_session_prefix'), callback=Configuration.get('pyramid_authentication_callback'), debug=Configuration.get('pyramid_authentication_debug') )
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 getRemoteUserAuthenticationPolicy(): """Define the authentication policy for remote user server""" return RemoteUserAuthenticationPolicy( environ_key=Configuration.get('pyramid_remoteuser_environ_key'), callback=Configuration.get('pyramid_authentication_callback'), debug=Configuration.get('pyramid_authentication_debug') )
def createdb(cls, keep_existing=False, with_demo=False): """Create the database specified in configuration. :: cls.init_configuration_manager() cls.createdb() :param keep_existing: If false drop the previous db before create it """ url = Configuration.get('get_url')() db_template_name = Configuration.get('db_template_name', None) if database_exists(url): if keep_existing: return False drop_database(url) create_database(url, template=db_template_name) registry = RegistryManager.get(Configuration.get('db_name', None)) if registry is None: return registry.System.Parameter.set( "with-demo", Configuration.get('with_demo', with_demo)) return True
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 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 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 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 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) 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 list_databases(): """ return the name of the databases found in the BDD the result can be filtering by the Configuration entry ``db_filter`` ..warning:: For the moment only the ``prostgresql`` dialect is available :rtype: list of the database's names """ url = Configuration.get('get_url')() db_filter = Configuration.get('db_filter') text = None if url.drivername in ('postgres', 'postgresql'): url = Configuration.get('get_url')(db_name='postgres') text = "SELECT datname FROM pg_database" if db_filter: db_filter = db_filter.replace('%', '%%') text += " where datname like '%s'" % db_filter if text is None: return [] engine = create_engine(url) return [x[0] for x in engine.execute(text).fetchall() if x[0] not in ('template1', 'template0', 'postgres')]
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 get_initialize(cls, authenticated_userid): res = [] locales = {'en'} if not authenticated_userid: locale = Configuration.get('furetui_default_locale', 'en') else: locale = cls.get_authenticated_userid_locale(authenticated_userid) res.extend(cls.get_user_informations(authenticated_userid)) cls.import_i18n(locale) cls.anyblok.furetui_templates.compile(lang=locale) locales.add(locale) res.extend([{ 'type': 'SET_LOCALE', 'locale': locale }, { 'type': 'UPDATE_LOCALES', 'locales': [{ 'locale': locale, 'messages': cls.anyblok.furetui_i18n.get(locale, {}) } for locale in locales] }, { 'type': 'SET_LOGIN_PAGE', 'login_page': Configuration.get('furetui_login_page', 'password') }]) return res
def get_client(): """Method that prepare the oidc client. This method use lru_cache in order call the OIDC provider once per thread Today we only support OIDC providers (ISSUER) that expose a /.well-known/openid-configuration route At the moment to authenticate the RP to the OIDC provider we only support through client_id/secret_ID/rp_callback attributes. You must configure OIDC in AnyBlok configuration: * **oidc-provider-issuer**: The OIDC Provider urls (ie: https://gitlab.com) * **oidc-relying-party-callback**: The Relaying Party callback, once the user is authenticate against the OIDC provider he will be redirect to that uri to the RP service (ie: http://localhost:8080/callback). In general this value is also configured in your OIDC provider to avoid redirection issues. * **oidc-relying-party-client-id**: The client id provide by your OIDC provider * **oidc-relying-party-secret-id**: The secret id provide by your OIDC provider And optionally: * **oidc-scope**: Specify what access privileges are being requested for Access Tokens. `cf Requesting claims using scope values <https://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims`_. a list of claims using coma separator. (default value: `openid,email`) * **oidc-userinfo-field**: Specify which field to use from the response of the OIDC provider `userinfo endpoint <https://openid.net/specs/ openid-connect-core-1_0.html#UserInfoResponse>`_. To make sure it's a known user. (default value: `email`). """ client = Client(client_authn_method=CLIENT_AUTHN_METHOD) for config in [ "oidc_provider_issuer", "oidc_relying_party_client_id", "oidc_relying_party_secret_id", "oidc_relying_party_callback", ]: if Configuration.get(config, None) is None: raise ValueError("You must provide {} parameter".format(config)) provider_info = client.provider_config( Configuration.get("oidc_provider_issuer")) info = { "client_id": Configuration.get("oidc_relying_party_client_id"), "client_secret": Configuration.get("oidc_relying_party_secret_id"), "redirect_uris": [Configuration.get("oidc_relying_party_callback")], } info.update(provider_info._dict) client_reg = RegistrationResponse(**info) client.store_registration_info(client_reg) return client
def furetui_user(): """Update an existing database""" registry = anyblok.start( 'furetui-user', loadwithoutmigration=True) login = Configuration.get('furetui_user_login') password = Configuration.get('furetui_user_password') roles = Configuration.get('furetui_user_roles') or [] registry.FuretUI.user_management(login, password, roles)
def reset_db(): if sgdb_in(['MySQL', 'MariaDB']): url = Configuration.get('get_url')() if database_exists(url): drop_database(url) db_template_name = Configuration.get('db_template_name', None) create_database(url, template=db_template_name)
def filterModel(cls, query): Model = cls.registry.System.Model wanted_models = Configuration.get('doc_wanted_models') if wanted_models: query = query.filter(Model.name.in_(wanted_models)) unwanted_models = Configuration.get('doc_unwanted_models') if unwanted_models: query = query.filter(Model.name.notin_(unwanted_models)) return query
def test_registry_by_default_method(self, registry_blok): webserver = init_web_server(add_route_and_views) res = webserver.get('/test/', status=200) assert Configuration.get('db_name') == res.body.decode('utf8') webserver.get('/test/login/') res = webserver.get('/test/', status=200) assert 'other_db_name' == res.body.decode('utf8') webserver.get('/test/logout/') res = webserver.get('/test/', status=200) assert Configuration.get('db_name') == res.body.decode('utf8')
def test_update_query(self): registry = RegistryManager.get(Configuration.get('db_name')) registry.upgrade(install=('test-me-blok2',)) registry.commit() self.assertTrue(registry.System.Blok.query().all_name()) self.assertTrue(registry.System.Blok.query().all()) registry.close() registry = RegistryManager.get(Configuration.get('db_name')) self.assertTrue(registry.System.Blok.query().all_name()) self.assertTrue(registry.System.Blok.query().all())
def options_from_configuration(self): options = [] if not Configuration.get('wkhtmltopdf_unquiet'): options.append('--quiet') if Configuration.get('wkhtmltopdf_debug_javascript'): options.append('--debug-javascript') else: options.append('--no-debug-javascript') return options
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 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 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 test_add_deprecated_argument(self): parser = self.get_parser() with pytest.warns(DeprecationWarning) as record: parser.add_argument('--value', dest='value', deprecated="test deprecated") assert str(record.list[0].message) == 'test deprecated' with pytest.warns(DeprecationWarning) as record: Configuration.get('value') assert str(record.list[0].message) == 'test deprecated'
def test_registry_by_default_method(self): self.includemes.append(self.add_route_and_views) webserver = self.init_web_server() res = webserver.get('/test/', status=200) self.assertEqual(Configuration.get('db_name'), res.body.decode('utf8')) webserver.get('/test/login/') res = webserver.get('/test/', status=200) self.assertEqual('other_db_name', res.body.decode('utf8')) webserver.get('/test/logout/') res = webserver.get('/test/', status=200) self.assertEqual(Configuration.get('db_name'), res.body.decode('utf8'))
def test_registry_by_default_method(self): self.includemes.append(self.add_route_and_views) webserver = self.init_web_server() res = webserver.get('/test/', status=200) self.assertEqual(Configuration.get('db_name'), res.body.decode('utf8')) webserver.get('/test/login/') res = webserver.get('/test/', status=200) self.assertEqual('other_db_name', res.body.decode('utf8')) webserver.get('/test/logout/') res = webserver.get('/test/', status=200) self.assertEqual(Configuration.get('db_name'), res.body.decode('utf8'))
def init_engine(self, db_name=None): """Overload the initiation of engine to create more than one engine use the Configuration option to create engines: * db_url: read and write engine * db_ro_urls: read only engines (list) * db_wo_url: write only engines .. warning:: All the engines use the same database name not at the same location :param db_name: name of the database for the engines """ kwargs = self.init_engine_options() gurl = Configuration.get('get_url', get_url) self.engines = {'ro': [], 'wo': None} self._engine = None for url in Configuration.get('db_ro_urls', []) or []: url = gurl(db_name=db_name, url=url) engine = create_engine(url, **kwargs) self.engines['ro'].append(engine) wo_url = Configuration.get('db_wo_url') if wo_url: url = gurl(db_name=db_name, url=wo_url) engine = create_engine(url, **kwargs) self.engines['wo'] = engine url = Configuration.get('db_url') if url and wo_url: raise RegistryException( "You have not to use the both Configuration option " "--get-wo-url [%s] and --get-url [%s], chose only one of them " "because only one master can be chose" % (wo_url, url)) elif url: url = gurl(db_name=db_name, url=url) engine = create_engine(url, **kwargs) self.engines['wo'] = engine self.engines['ro'].append(engine) if not self.engines['ro'] and not self.engines['wo']: url = gurl(db_name=db_name) engine = create_engine(url, **kwargs) self.engines['wo'] = engine self.engines['ro'].append(engine) elif not self.engines['wo']: logger.debug('No WRITE engine defined use READ ONLY mode') self.loadwithoutmigration = True
def registry2doc(application, configuration_groups, **kwargs): """Return auto documentation 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, 'doc', 'schema') registry = anyblok.start(application, configuration_groups=configuration_groups, **kwargs) if registry: registry.commit() doc = registry.Documentation() doc.auto_doc() if Configuration.get('doc_format') == 'RST': with open(Configuration.get('doc_output'), 'w') as fp: doc.toRST(fp) elif Configuration.get('doc_format') == 'UML': format_ = Configuration.get('schema_format') name_ = Configuration.get('schema_output') dot = ModelSchema(name_, format=format_) doc.toUML(dot) dot.save() elif Configuration.get('doc_format') == 'SQL': format_ = Configuration.get('schema_format') name_ = Configuration.get('schema_output') dot = SQLSchema(name_, format=format_) doc.toSQL(dot) dot.save()
def create_database(database): """ Create a new database, initialize it and return an AnyBlok registry :param: database's name :rtype: AnyBlok registry instance """ url = Configuration.get('get_url')(db_name=database) if database_exists(url): raise Exception("Database %r already exist") db_template_name = Configuration.get('db_template_name', None) SU_create_database(url, template=db_template_name) registry = RegistryManager.get(database) return registry
def preload_database(loadwithoutmigration: bool = True) -> "Registry": # Methode copied from anyblok_pyramid.common""" dbname = Configuration.get("db_name") Registry = Configuration.get("Registry") logger.info("Preload the database : %r", dbname) if Registry.db_exists(db_name=dbname): registry = get_registry_for(dbname, loadwithoutmigration, log_repeat=True) registry.commit() registry.session.close() logger.info("The database %r is preloaded", dbname) else: raise RuntimeError(f"The database {dbname} does not exist") return registry
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 furetui_custom_view_login(request): return [{ 'type': 'UPDATE_CLIENT', 'viewName': 'Login', 'databases': list_databases(), 'database': Configuration.get('get_db_name')(request) }]
def load_client(request): """ Return the client main page """ database = request.session.get('database') login = request.session.get('login') password = request.session.get('password') state = request.session.get('state') if not(database and login and password and state == "connected"): logout(request) return HTTPFound(location=request.route_url('homepage')) try: registry = RegistryManager.get(database) assert registry.Web.Login.check_authentification(login, password) except: logout(request) return HTTPFound(location=request.route_url('homepage')) css = registry.Web.get_css() js = registry.Web.get_js() js_babel = registry.Web.get_js_babel() # TODO see in system.parmeter if they are no data for title title = Configuration.get('app_name', 'ERPBlok') templates = registry.Web.get_templates() return render_to_response('erpblok:client.mak', {'title': title, 'css': css, 'js': js, 'js_babel': js_babel, 'templates': templates, }, request=request)
def init_registry_with_bloks(bloks, function, **kwargs): if bloks is None: bloks = [] if isinstance(bloks, tuple): bloks = list(bloks) if isinstance(bloks, str): bloks = [bloks] anyblok_test_name = 'anyblok-test' if anyblok_test_name not in bloks: bloks.append(anyblok_test_name) loaded_bloks = deepcopy(RegistryManager.loaded_bloks) if function is not None: EnvironmentManager.set('current_blok', anyblok_test_name) try: function(**kwargs) finally: EnvironmentManager.set('current_blok', None) try: registry = RegistryManager.get(Configuration.get('db_name'), unittest=True) # update required blok registry_bloks = registry.get_bloks_by_states('installed', 'toinstall') toinstall = [x for x in bloks if x not in registry_bloks] toupdate = [x for x in bloks if x in registry_bloks] registry.upgrade(install=toinstall, update=toupdate) finally: RegistryManager.loaded_bloks = loaded_bloks return registry
def test_get_unexisting_queues_ok(self): with get_channel(): bus_profile = Configuration.get('bus_profile') registry = self.init_registry_with_bloks(('bus', ), self.add_in_registry) registry.Bus.Profile.insert(name=bus_profile, url=pika_url) self.assertEqual(registry.Bus.get_unexisting_queues(), [])
def get_global_init(request): # TODO call cached pre_load print('get_global_init', request.query_string) registry = request.anyblok.registry FuretUI = registry.FuretUI if eval(Configuration.get('furetui_debug', 'False'), {}, {}) is True: FuretUI.pre_load() authenticated_userid = request.authenticated_userid default_spaces_menu = FuretUI.get_default_space(authenticated_userid) res = { 'templates': FuretUI.get_templates(), 'lang': 'fr', 'langs': FuretUI.get_i18n(), 'js': FuretUI.get_js_files(), 'css': FuretUI.get_css_files(), 'global': FuretUI.get_global(authenticated_userid), 'menus': { 'user': FuretUI.get_user_menu(authenticated_userid), 'spaces': FuretUI.get_spaces_menu(authenticated_userid, default_spaces_menu), 'spaceMenus': FuretUI.get_space_menus(authenticated_userid, default_spaces_menu), }, } return res
def registry(self): """ Add the property registry :: registry = request.anyblok.registry .. note:: The db_name must be defined """ dbname = Configuration.get('get_db_name')(self.request) if Configuration.get('Registry').db_exists(db_name=dbname): return get_registry_for(dbname) else: return None
def sgdb_in(databases): if not DATABASES_CACHED: load_configuration() url = Configuration.get('get_url')(db_name='') engine = sqlalchemy.create_engine(url) return sgdb_in_(engine, databases)
def test_to_sql(self): doc = self.registry.Documentation() doc.auto_doc() format_ = Configuration.get('schema_format') dot = SQLSchema('test', format=format_) doc.toSQL(dot) dot.save()
def registry(self): """ Add the property registry :: registry = request.anyblok.registry .. note:: The db_name must be defined """ dbname = Configuration.get('get_db_name')(self.request) if Configuration.get('Registry').db_exists(db_name=dbname): return get_registry_for(dbname) else: return None
def format_encrypt_key(self, registry, namespace): """Format and return the encyption key :param registry: the current registry :param namespace: the namespace of the model :return: encrypt key """ encrypt_key = self.encrypt_key if encrypt_key is True: encrypt_key = Configuration.get('default_encrypt_key') if not encrypt_key: raise FieldException("No encrypt_key defined in the " "configuration") def wrapper(): """Return encrypt_key wrapper :return: """ Model = registry.get(namespace) if hasattr(Model, encrypt_key): func = getattr(Model, encrypt_key) if ismethod(func): if encrypt_key not in Model.loaded_columns: if encrypt_key not in Model.loaded_fields: return func() return encrypt_key return wrapper
def get_unexisting_queues(cls): profile_name = Configuration.get('bus_profile') profile = cls.registry.Bus.Profile.query().filter_by( name=profile_name).one_or_none() parameters = pika.URLParameters(profile.url.url) connection = pika.BlockingConnection(parameters) unexisting_queues = [] for processes, definitions in cls.get_consumers(): for queue, Model, consumer in definitions: channel = connection.channel() try: channel.queue_declare(queue, passive=True) channel.close() except ChannelClosed as exc: if exc.args[0] == 404: unexisting_queues.append(queue) logger.warning( "The queue %r consumed by '%s:%s' " "does not exist on %r", queue, Model, consumer, profile) else: raise connection.close() return unexisting_queues
def get_consumers(cls): """Return the list of the consumers""" grouped_consumers = [] consumers = [] queues = [] for Model in cls.registry.loaded_namespaces.values(): for queue, consumer, processes in Model.bus_consumers: if queue in queues: raise TwiceQueueConsumptionException( "The consumation of the queue %r is already defined" % (queue)) queues.append(queue) if processes == 0: grouped_consumers.append( (queue, Model.__registry_name__, consumer)) else: consumers.append((processes, [ (queue, Model.__registry_name__, consumer) ])) if grouped_consumers: consumers.append((Configuration.get('bus_processes', 1), grouped_consumers)) return consumers
def check_allow_database_manager(): """ raise an execption if the database manager is unactive :exception: pyramid.httpexceptions.HTTPNotFound """ allow_database_manager = Configuration.get('allow_database_manager') if not allow_database_manager: raise HTTPNotFound()
def test_1_get_homepage(self): self.init_registry(None) response = self.webserver.get('/') self.assertEqual(response.status, '302 Found') db_name = Configuration.get('db_name') login_location = 'http://localhost/login?database=%s' % db_name self.assertEqual(response.location, login_location)
def test_to_sql(self): doc = self.registry.Documentation() doc.auto_doc() format_ = Configuration.get('schema_format') dot = SQLSchema('test', format=format_) doc.toSQL(dot) dot.save()
def before_process_message(self, broker, message): """Called before process message Invalid the cache, this is mean that if a cache have to be invalidated then it will be invalidated else nothing is done If the message is in the ``Model.Dramatiq.Message`` then the status will be change to **running** :param broker: the broker used :param message: the message send in the broker """ registry = RegistryManager.get(Configuration.get('db_name')) logger.debug("[before_process_message] %s: update message(%s) status ", id(registry.session), message.message_id) # cache may have change, then we do the invalidation in the dramatiq # side registry.System.Cache.clear_invalidate_cache() M = registry.Dramatiq.Message m = M.get_instance_of(message) if m: m.update_status(M.STATUS_RUNNING) registry.commit() EnvironmentManager.set('is_called_by_dramatiq_actor', True)
def test_registry_by_path(self): def __get_db_name(request): return request.matchdict['dbname'] def add_route_and_views(config): config.add_route('dbname', '/test/{dbname}/') config.add_view(_get_db_name, route_name='dbname') Configuration.update(get_db_name=__get_db_name) self.includemes.append(add_route_and_views) webserver = self.init_web_server() res = webserver.get('/test/%s/' % Configuration.get('db_name'), status=200) self.assertEqual(Configuration.get('db_name'), res.body.decode('utf8')) res = webserver.get('/test/wrong_db_name/', status=200) self.assertFalse(res.body)
def test_db_exists_with_wo_url_with_wrong_db_name(self): db_name = 'wrong_db_name' Registry = Configuration.get('Registry') with DBTestCase.Configuration( db_ro_urls=[], db_url='', db_wo_url='postgresql:///' ): self.assertFalse(Registry.db_exists(db_name=db_name))
def test_create_and_drop_databases(self): db_name = Configuration.get('db_name') + '_test' self.assertNotIn(db_name, common.list_databases()) registry = common.create_database(db_name) self.assertIsNotNone(registry) self.assertIn(db_name, common.list_databases()) common.drop_database(db_name) self.assertNotIn(db_name, common.list_databases())
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 filterModel(cls, query): Model = cls.registry.System.Model wanted_models = Configuration.get('doc_wanted_models') if wanted_models: wanted_models = cls.get_all_models(wanted_models) query = query.filter(Model.name.in_(wanted_models)) else: wanted_models = [] unwanted_models = Configuration.get('doc_unwanted_models') if unwanted_models: unwanted_models = cls.get_all_models(unwanted_models) unwanted_models = [x for x in unwanted_models if x not in wanted_models] query = query.filter(Model.name.notin_(unwanted_models)) return query
def init_configuration_manager(cls, **env): """ Initialise the configuration manager with environ variable to launch the test .. warning:: For the moment we not use the environ variable juste constante :param prefix: prefix the database name :param env: add another dict to merge with environ variable """ db_name = Configuration.get('db_name') or 'test_anyblok' db_driver_name = Configuration.get('db_driver_name') or 'postgresql' env.update({ 'db_name': db_name, 'db_driver_name': db_driver_name, }) Configuration.update(**env)
def test_preload_databases(self): db_name = Configuration.get('db_name') with DBTestCase.Configuration(db_names=[db_name]): with LogCapture('anyblok_pyramid.common', level=INFO) as handler: preload_databases() messages = handler.get_info_messages() self.assertTrue(messages) self.assertIn('The database %r is preloaded' % db_name, messages)
def __init__(self, registry): self.withoutautomigration = registry.withoutautomigration self.conn = registry.session.connection() self.metadata = registry.declarativebase.metadata opts = { 'compare_server_default': True, 'render_item': self.render_item, 'compare_type': self.compare_type, } self.context = MigrationContext.configure(self.conn, opts=opts) self.operation = Operations(self.context) self.reinit_all = Configuration.get('reinit_all', False) self.reinit_tables = Configuration.get('reinit_tables', False) self.reinit_columns = Configuration.get('reinit_columns', False) self.reinit_indexes = Configuration.get('reinit_indexes', False) self.reinit_constraints = Configuration.get( 'reinit_constraints', False)
def init(self, parser, opts, args): Configuration.parse_options(opts, ('gunicorn',)) # get the configuration save in AnyBlok configuration in # gunicorn configuration for name in Configuration.configuration.keys(): if name in self.cfg.settings: value = Configuration.get(name) if value: self.cfg.settings[name].set(value)