Beispiel #1
0
def get_wrapper(name):
    """Return a wrapper object with name *name*.

    Look for the registered :class:`~.Wrapper` objects and return and instance
    of the one with name equal *name*.

    :param name: Wrapper name
    :type name: :class:`~.str`
    :return: A wrapper instance
    :rtype: :class:`~.Wrapper`
    """

    global _INIT  # pylint: disable=global-statement
    if _INIT:
        _INIT = False
        _import_default_wrappers()

    if MICROPROBE_RC['debugwrapper']:
        name = "DebugBinaryDouble"

    for elem in get_all_subclasses(microprobe.code.wrapper.Wrapper):
        if elem.__name__ == name:
            return elem

    raise MicroprobeValueError(
        "Unknown wrapper '%s'. Available wrappers are: %s. " % (
            name, [
                elem.__name__
                for elem in get_all_subclasses(
                    microprobe.code.wrapper.Wrapper
                )
            ]
        )
    )
Beispiel #2
0
def _import_default_wrappers():
    modules = []
    LOG.debug('Wrapper paths: %s', MICROPROBE_RC['wrapper_paths'])
    for path in MICROPROBE_RC['wrapper_paths']:
        for module in findfiles([path], r"wrappers/.+\.py$", full=True):
            module = os.path.realpath(module)
            if module not in modules:
                modules.append(module)

    lmodules = -1
    while lmodules != len(modules):
        lmodules = len(modules)
        for module_file in modules[:]:

            name = (os.path.basename(module_file).replace(".py", ""))

            if name == "__init__":
                continue

            if name in microprobe.code.wrapper.__dict__:
                raise MicroprobeError(
                    "Wrapper module name '%s' in '%s' already loaded. " %
                    (name, module_file)
                )

            try:
                module = load_source(name, module_file)
            except MicroprobeValueError:
                continue

            microprobe.code.wrapper.__dict__[name] = module
            modules.remove(module_file)

            current_wrappers = \
                [elem.__name__ for elem in
                 get_all_subclasses(microprobe.code.wrapper.Wrapper)
                 ]

            if len(current_wrappers) != len(set(current_wrappers)):
                for elem in set(current_wrappers):
                    current_wrappers.remove(elem)
                overwrapper = list(set(current_wrappers))[0]
                raise MicroprobeError(
                    "Module name '%s' in '%s' overrides an existing wrapper "
                    "with name '%s'" % (name, module_file, overwrapper)
                )