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 test_get_invalid_blok(self): try: BlokManager.load() BlokManager.get('invalid blok') self.fail('No exception when get invalid blok') except BlokManagerException: pass
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 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_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 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 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 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 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 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_auto_migration_is_between_pre_and_post_migration_1(self): registry = self.init_registry(None) registry.upgrade(install=('test-blok14', )) blok = BlokManager.get('test-blok14') self.assertFalse(blok.table_exist_before_automigration) self.assertTrue(blok.table_exist_after_automigration) registry.Test.insert() self.assertEqual(registry.Test.query().count(), 1)
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 test_auto_migration_is_between_pre_and_post_migration_1( self, registry_testblok): registry = registry_testblok registry.upgrade(install=('test-blok14', )) blok = BlokManager.get('test-blok14') assert blok.table_exist_before_automigration is False assert blok.table_exist_after_automigration is True registry.Test.insert() assert registry.Test.query().count() == 1
def test_reload(self): import sys module_type = sys.__class__ # is there a simpler way ? def fake_reload(module): self.assertIsInstance(module, module_type) blok = BlokManager.get('wms-quantity') blok.reload_declaration_module(fake_reload)
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 test_update_without_demo(self, registry_blok16_installed): registry = registry_blok16_installed registry.System.Parameter.set("with-demo", False) registry.upgrade(update=('test-blok16', )) blok = BlokManager.get('test-blok16') assert blok.called_methods == [ "pre_migration", "post_migration", "update", ]
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 test_install_with_demo(self, registry_testblok_func): registry = registry_testblok_func registry.System.Parameter.set("with-demo", True) registry.upgrade(install=('test-blok16', )) blok = BlokManager.get('test-blok16') assert blok.called_methods == [ "pre_migration", "post_migration", "update", "update_demo", ]
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_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 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 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 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 get_static(static_type): """ Get in the Blok definition the static data from the client :param static: entry to read: css, js, ... :rtype: list of str """ res = [] for blok_name in BlokManager.ordered_bloks: blok = BlokManager.get(blok_name) if hasattr(blok, static_type): for static_url in getattr(blok, static_type): res.append(format_static(blok_name, static_url)) return res
def get_static(static_type): """ Get in the Blok definition the static data from the client :param static: entry to read: css, js, ... :rtype: list of str """ res = [] for blok_name in BlokManager.ordered_bloks: blok = BlokManager.get(blok_name) if hasattr(blok, static_type): for static_url in getattr(blok, static_type): res.append(format_static(blok_name, static_url)) return res
def get_static(cls, static_type): """ return the list of all static file path :param static_type: entry in the blok :rtype: list of the path """ res = [] Blok = cls.registry.System.Blok for blok in Blok.list_by_state('installed'): b = BlokManager.get(blok) if hasattr(b, static_type): for static_url in getattr(b, static_type): res.append(cls.format_static(blok, static_url)) return res
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 get_static(cls, static_type): """ return the list of all static file path :param static_type: entry in the blok :rtype: list of the path """ res = [] Blok = cls.registry.System.Blok for blok in Blok.list_by_state('installed'): b = BlokManager.get(blok) if hasattr(b, static_type): for static_url in getattr(b, static_type): res.append(cls.format_static(blok, static_url)) return res
def load_config_bloks(self): """ loop on each blok, keep the order of the blok to load the pyramid config. The blok must declare the meth ``pyramid_load_config``:: def pyramid_load_config(config): config.add_route('hello', '/hello/{name}/') ... """ self.commit() for blok_name in BlokManager.ordered_bloks: blok = BlokManager.get(blok_name) if hasattr(blok, 'pyramid_load_config'): logger.debug('Load configuration from: %r' % blok_name) blok.pyramid_load_config(self) self.commit()
def load_config_bloks(self): """ loop on each blok, keep the order of the blok to load the pyramid config. The blok must declare the meth ``pyramid_load_config``:: def pyramid_load_config(config): config.add_route('hello', '/hello/{name}/') ... """ self.commit() for blok_name in BlokManager.ordered_bloks: blok = BlokManager.get(blok_name) if hasattr(blok, 'pyramid_load_config'): logger.debug('Load configuration from: %r' % blok_name) blok.pyramid_load_config(self) self.commit()
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_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 reload(self): """ Reload all the imports for this module :exception: ImportManagerException """ from anyblok.blok import BlokManager from anyblok.registry import RegistryManager from anyblok.environment import EnvironmentManager b = BlokManager.get(self.blok) if not hasattr(b, 'reload_declaration_module'): return try: EnvironmentManager.set('reload', True) RegistryManager.init_blok(self.blok) b.reload_declaration_module(reload_module) finally: EnvironmentManager.set('reload', False)
def reload(self): """ Reload all the imports for this module :exception: ImportManagerException """ from anyblok.blok import BlokManager from anyblok.registry import RegistryManager from anyblok.environment import EnvironmentManager b = BlokManager.get(self.blok) if not hasattr(b, 'reload_declaration_module'): return try: EnvironmentManager.set('reload', True) RegistryManager.init_blok(self.blok) b.reload_declaration_module(reload_wraper) finally: EnvironmentManager.set('reload', False)
def pre_load(cls, lang='en'): logger.info('Preload furet UI component') templates = Template() i18n = {} Blok = cls.anyblok.System.Blok for blok in Blok.list_by_state('installed'): b = BlokManager.get(blok) bpath = BlokManager.getPath(blok) if hasattr(b, 'furetui'): for template in b.furetui.get('templates', []): with open(join(bpath, template), 'r') as fp: templates.load_file(fp) for local, translations in b.furetui.get('i18n', {}).items(): node = i18n.setdefault(local, {}) update_translation(node, translations) cls.import_i18n(lang) templates.compile(lang=lang) cls.anyblok.furetui_templates = templates cls.anyblok.furetui_i18n = i18n
def test_upgrade_contents_local_goods_ids(self): Props = self.Props p1_id = Props.insert(flexible=dict(foo='bar')).id p2_id = Props.insert(flexible=dict( monty='python', contents=[ dict(type='POT1', forward_properties=['foo'], quantity=3, local_goods_ids=[1, 3, 7]), dict(type='POT2', forward_properties=['foo'], quantity=3), ])).id from anyblok.blok import BlokManager wms_core = BlokManager.get('wms-core')(self.registry) # make sure we have no side effect of Property instances in the # session self.registry.session.expire_all() wms_core.update_contents_property_local_goods_id() # and again self.registry.flush() self.registry.session.expire_all() # time for assertions p1 = Props.query().get(p1_id) self.assertEqual(p1.flexible, dict(foo='bar')) p2 = Props.query().get(p2_id) self.assertEqual( p2.flexible, dict(monty='python', contents=[ dict(type='POT1', forward_properties=['foo'], quantity=3, local_physobj_ids=[1, 3, 7]), dict(type='POT2', forward_properties=['foo'], quantity=3) ]))
def pre_load(cls): logger.info('Preload furet UI component') tmpl_views = Template() tmpl_components = Template() js = [] css = [] i18n = {} Blok = cls.registry.System.Blok timestamp = datetime.utcnow().timestamp() for blok in Blok.list_by_state('installed'): b = BlokManager.get(blok) bpath = BlokManager.getPath(blok) if hasattr(b, 'furetui'): for template in b.furetui.get('templates', []): with open(join(bpath, template), 'r') as fp: tmpl_components.load_file(fp) js.extend([ '%s?%s' % (join('furet-ui', blok, 'js', filename), timestamp) for filename in b.furetui.get('js', []) ]) css.extend([ join('furet-ui', blok, 'css', filename) for filename in b.furetui.get('css', []) ]) for local, translations in b.furetui.get('i18n', {}).items(): node = i18n.setdefault(local, {}) update_translation(node, translations) tmpl_views.compile() cls.registry.furetui_views = tmpl_views tmpl_components.compile() cls.registry.furetui_components = tmpl_components cls.registry.furetui_js = js cls.registry.furetui_css = css cls.registry.furetui_i18n = i18n
def clear_called_methods(): BlokManager.get('test-blok16').called_methods = []
def registry_blok16_installed(self, registry_testblok_func): registry_testblok_func.upgrade(install=('test-blok16', )) blok = BlokManager.get('test-blok16') blok.called_methods = [] return registry_testblok_func
def test_marked_as_conflicting(self): blok1 = BlokManager.get('test-blok1') blok13 = BlokManager.get('test-blok13') self.assertEqual(blok13.conflicting, ['test-blok1']) self.assertEqual(blok1.conflicting_by, ['test-blok13'])
def get_inherit_bloks(blok_name): bloks.add(blok_name) blok = BlokManager.get(blok_name) for subblok in (blok.required_by + blok.conditional_by): get_inherit_bloks(subblok)
def test_marked_as_conflicting(self, registry_testblok): blok1 = BlokManager.get('test-blok1') blok13 = BlokManager.get('test-blok13') assert blok13.conflicting == ['test-blok1'] assert blok1.conflicting_by == ['test-blok13']