Beispiel #1
0
def import_classes_from(modules):
    """

    :param modules:

    """

    LOG.info("Start")
    classes = {}

    for module_str in modules:
        for cls in find_subclasses(module_str, Generator):

            name = cls.__name__
            if name in classes:
                raise MicroprobeArchitectureDefinitionError(
                    "Duplicated "
                    "definition"
                    " of Generator '%s' "
                    "in module '%s'", name, module_str)

            LOG.info("%s generator imported", name)
            classes[name] = cls

    if len(classes) == 0:
        LOG.warning("No generators imported.")

    LOG.info("End")
    return list(classes.values())
Beispiel #2
0
def find_env_definitions(paths=None):

    LOG.debug("Start find environment definitions")

    global _INIT  # pylint: disable=global-statement
    global _ENV_DEFINITIONS  # pylint: disable=global-statement

    if not _INIT:
        return _ENV_DEFINITIONS

    _INIT = False

    if paths is None:
        paths = []

    paths = paths + MICROPROBE_RC["environment_paths"] \
        + MICROPROBE_RC["default_paths"]

    results = []
    files = findfiles(paths, "env/.*.py$", full=True)

    if len(files) > 0:
        from microprobe.target import Definition
        LOG.debug("Files found")

    for modfile in files:
        LOG.debug("Processing file: '%s'", modfile)
        try:
            envclses = list(find_subclasses(modfile, GenericEnvironment))
        except (MicroprobeValueError, TypeError) as exc:
            continue

        LOG.debug("Classes find: '%s'", envclses)
        for envcls in envclses:
            LOG.debug("Trying class: '%s'", envcls)
            try:
                env = envcls(None)
                definition = Definition(modfile, env.name, env.description)
                if definition not in results:
                    results.append(definition)
            except TypeError as exc:
                # Skip not complete environments
                LOG.debug("Skipping class '%s'...", envcls)
                LOG.debug(exc)
                continue

    LOG.debug("End find environment definitions")
    _ENV_DEFINITIONS = results
    return results
Beispiel #3
0
def import_env_definition(module, isa, definition_name=None):
    """

    :param module:
    :param isa:
    :param definition_name:  (Default value = None)

    """

    LOG.info("Start Environment import")
    envcls = list(
        find_subclasses(module,
                        GenericEnvironment,
                        extra_import_name=definition_name))

    LOG.debug("Definition name: %s", definition_name)
    LOG.debug("Classes: %s", envcls)

    if definition_name is not None:
        envcls = [cls for cls in envcls if cls.__name__ == definition_name]

    if len(envcls) > 1 and definition_name is None:
        LOG.warning("Multiple environment definitions found and a specific"
                    " name not provided. Taking the first one.")
    elif len(envcls) < 1 and definition_name is None:
        raise MicroprobeImportDefinitionError(
            "No environment definitions found in '%s'" % module)

    elif len(envcls) < 1:
        raise MicroprobeImportDefinitionError(
            "No environment definitions found in '%s' with name"
            " '%s'" % (module, definition_name))

    environment = envcls[0](isa)

    LOG.info("Environment '%s' imported", environment)
    return environment