def get_transformer(path, ret_members=False): """This function returns a Transformer class with the given path. :param str path: The path points to the custom transformer. :param bool ret_members: If true then return inspect.getmembers(). :return Transformer if not ret_members else inspect.getmembers(). """ file = pathlib.Path(path) if file.suffix != ".py": return load_class(path) if not file.exists(): raise NotebookInvalidPathError(f"The path {path} does not exist.") # Importing a source file directly spec = importlib.util.spec_from_file_location(name=file.name, location=path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) members = inspect.getmembers( module, lambda c: inspect.isclass(c) and hasattr(c, "transform") and hasattr(c, "merge") and callable(c.transform) and callable(c.merge), ) if not members and not ret_members: raise NotebookInvalidTransformError( f"The path {path} was found but it was not a valid transformer." ) return members if ret_members else members[0][-1]
def test_load_class(): with pytest.raises(ImportError): load_class("notimportable") with pytest.raises(ImportError): load_class("notim:por:table") with pytest.raises(ImportError): load_class("notim:portable") with pytest.raises(ImportError): load_class("mozperftest.tests.test_utils:NOEXIST") klass = load_class("mozperftest.tests.test_utils:ImportMe") assert klass is ImportMe
def __init__( self, file_groups, config, prefix, logger, custom_transform=None, sort_files=False, ): """Initializes PerftestETL. :param dict file_groups: A dict of file groupings. The value of each of the dict entries is the name of the data that will be produced. :param str custom_transform: The class name of a custom transformer. """ self.fmt_data = {} self.file_groups = file_groups self.config = config self.sort_files = sort_files self.const = Constant() self.prefix = prefix self.logger = logger # Gather the available transformers tfms_dict = self.const.predefined_transformers # XXX NOTEBOOK_PLUGIN functionality is broken at the moment. # This code block will raise an exception if it detects it in # the environment. plugin_path = os.getenv("NOTEBOOK_PLUGIN") if plugin_path: raise Exception("NOTEBOOK_PLUGIN is currently broken.") # Initialize the requested transformer if custom_transform: # try to load it directly, and fallback to registry try: tfm_cls = load_class(custom_transform) except ImportError: tfm_cls = tfms_dict.get(custom_transform) if tfm_cls: self.transformer = Transformer( files=[], custom_transformer=tfm_cls(), logger=self.logger, prefix=self.prefix, ) self.logger.info(f"Found {custom_transform} transformer", self.prefix) else: raise Exception( f"Could not get a {custom_transform} transformer.") else: self.transformer = Transformer( files=[], custom_transformer=SimplePerfherderTransformer(), logger=self.logger, prefix=self.prefix, )