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 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 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 """ 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 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 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 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 test_get_client_file(self, webserver, rollback_registry): path = Configuration.get('furetui_client_path', '/furet-ui') response = webserver.get(path) blok_path = BlokManager.getPath('furetui') path = join(blok_path, 'static', 'index.html') assert response.status_code == 200 assert response.content_type == 'text/html' with open(path, 'rb') as fp: assert fp.read() == response.body
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 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 import_i18n(cls, lang): reload_at_change = Configuration.get('pyramid.reload_all', False) if Translation.has_lang(lang) and not reload_at_change: return Blok = cls.anyblok.System.Blok for blok in Blok.list_by_state('installed'): bpath = BlokManager.getPath(blok) if path.exists(path.join(bpath, 'locale', f'{lang}.po')): po = polib.pofile(path.join(bpath, 'locale', f'{lang}.po')) for entry in po: Translation.set(lang, entry)
def pyramid_load_config(cls, config): json_data_adapter(config) blok_name, static_path = Configuration.get( 'furetui_client_static', 'furetui:static/furet-ui').split(':') blok_path = BlokManager.getPath(blok_name) path = join(blok_path, *static_path.split('/')) config.add_static_view('furet-ui/js', join(path, 'js')) config.add_static_view('furet-ui/css', join(path, 'css')) config.add_static_view('/js', join(path, 'js')) config.add_static_view('/css', join(path, 'css')) config.add_static_view('/images', join(path, 'images')) config.scan(cls.__module__ + '.views')
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 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_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 get_logo(self): """ Return the logo define in blok description :: class MyBlok(Blok): logo = 'path/to/the/logo/in/blok' """ file_path = super(Blok, self).get_logo() if not file_path: file_path = join(BlokManager.getPath('blok_manager'), 'static/image/none.png') return self.registry.Web.Image.filepath2html(file_path)
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 format_path(path): """Return the absolute path in function of the blok :param path: the path to format :return: the absolute path :exception: PathException """ if '#=#' in path: blokname, path = path.split('#=#') path = os.path.join(BlokManager.getPath(blokname), path) if not os.path.exists(path): raise PathException("%s does not exists", path) return path
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 static_paths(config): """Pyramid includeme, add the static path of the blok :param config: Pyramid configurator instance """ for blok, cls in BlokManager.bloks.items(): if hasattr(cls, 'static_paths'): paths = cls.static_paths if isinstance(paths, str): paths = [paths] else: paths = ['static'] blok_path = BlokManager.getPath(blok) for p in paths: config.add_static_view(join(blok, p), join(blok_path, p))
def static_paths(config): """Pyramid includeme, add the static path of the blok :param config: Pyramid configurator instance """ for blok, cls in BlokManager.bloks.items(): if hasattr(cls, 'static_paths'): paths = cls.static_paths if isinstance(paths, str): paths = [paths] else: paths = ['static'] blok_path = BlokManager.getPath(blok) for p in paths: config.add_static_view(join(blok, p), join(blok_path, p))
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 static_paths(config): """Pyramid includeme, add the static path of the blok :param config: Pyramid configurator instance """ for blok, cls in BlokManager.bloks.items(): if hasattr(cls, 'static_paths'): # pragma: no cover paths = cls.static_paths if isinstance(paths, str): paths = [paths] else: paths = ['static'] blok_path = BlokManager.getPath(blok) cache_max_age = Configuration.get('pyramid_cache_max_age', 3600) for p in paths: config.add_static_view(join(blok, p), join(blok_path, p), cache_max_age=cache_max_age)
def get_static_file(request): blok_name = request.matchdict['blok_name'] file_path = request.matchdict['file_path'] blok_path = BlokManager.getPath(blok_name) path = join(blok_path, file_path) content_type = 'text/html' if request.matchdict['filetype'] == 'js': content_type = 'application/javascript' if eval(Configuration.get('furetui_debug', 'False'), {}, {}) is True: parser = MyPyJsParser() with open(path, 'r') as fp: content = fp.read() parser.parse(blok_name, file_path, content) elif request.matchdict['filetype'] == 'css': content_type = 'text/css' response = FileResponse(path, request=request, content_type=content_type) response.headerlist.append(('Access-Control-Allow-Origin', '*')) return response
def anyblok_nose(): """Run nose unit test after giving it the registry """ warnings.simplefilter('default') warnings.warn( "This script is deprecated and will be removed soon. " "The Nose test machinery has been removed from the framework in order " "to be replaced with Pytest. " "If you need to run your tests with nose, install the Nose package.", DeprecationWarning, stacklevel=2) try: from nose import main except ImportError: logger.error('"Nosetest" is not installed, try: pip install nose') anyblok_registry = anyblok.start('nose', useseparator=True, unittest=True) if anyblok_registry: installed_bloks = anyblok_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 [] unwanted_bloks.extend(['anyblok-core', 'anyblok-test', 'model_authz']) 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')) anyblok_registry.close() # free the registry to force create it again sys.exit(main(defaultTest=defaultTest))
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 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 get_absolute_path(self): return os.path.join(BlokManager.getPath('report'), 'tests', 'test_common.py')
def pyramid_load_config(cls, config): blok_path = BlokManager.getPath('furetui') path = join(blok_path, 'static', 'furet-ui') config.add_static_view('furet-ui/js', join(path, 'js')) config.add_static_view('furet-ui/css', join(path, 'css')) config.scan(cls.__module__ + '.views')
def get_client_file(request): print('get_client_file', request.query_string) blok_path = BlokManager.getPath('furetui') path = join(blok_path, 'static', 'index.html') return FileResponse(path, request=request, content_type='text/html')