Beispiel #1
0
def iter_filters_in(target):
    # type: (Any) -> Generator[Tuple[Text, Type[BaseFilter]]]
    """
    Iterates over all filters in the specified module/class.
    """
    global legacy_warned
    if not legacy_warned:
        # Set the global flag to ``True`` first, in case the user has
        # ``simplefilter('error')`` set.
        legacy_warned = True

        warn(
            'Legacy extension loader is deprecated and will be removed in '
            'Filters v1.4.  '
            'See http://filters.readthedocs.io/en/latest/extensions.html#legacy-extensions-loader '
            'for more information.',
            DeprecationWarning,
        )

    ift_result = is_filter_type(target)

    if ift_result is True:
        logger.debug(
            'Registering extension filter '
            '{cls.__module__}.{cls.__name__}.'.format(cls=target, ), )

        yield target.__name__, target
    elif is_module(target):
        for member_name, member in get_members(target):
            member_ift_result = is_filter_type(member)

            if member_ift_result is True:
                logger.debug(
                    'Registering extension filter '
                    '{cls.__module__}.{cls.__name__}.'.format(cls=member, ), )

                yield member.__name__, member
            else:
                logger.debug(
                    'Ignoring {module}.{name} ({reason})'.format(
                        module=target.__name__,
                        name=member_name,
                        reason=member_ift_result,
                    ), )
    elif is_class(target):
        logger.debug(
            'Ignoring {cls.__module__}.{cls.__name__} ({reason}).'.format(
                cls=target,
                reason=ift_result,
            ), )
    else:
        logger.debug(
            'Ignoring {target!r} ({reason}).'.format(
                reason=ift_result,
                target=target,
            ), )
Beispiel #2
0
def iter_filters_in(
    target: typing.Any,
) -> typing.Generator[typing.Tuple[str, typing.Type[BaseFilter]], None, None]:
    """
    Iterates over all filters in the specified module/class.
    """
    ift_result = is_filter_type(target)

    if ift_result is True:
        logger.debug(
            'Registering extension filter '
            '{cls.__module__}.{cls.__name__}.'.format(cls=target, ), )

        yield target.__name__, target
    elif is_module(target):
        for member_name, member in get_members(target):
            member_ift_result = is_filter_type(member)

            if member_ift_result is True:
                logger.debug(
                    'Registering extension filter '
                    '{cls.__module__}.{cls.__name__}.'.format(cls=member, ), )

                yield member.__name__, member
            else:
                logger.debug(
                    'Ignoring {module}.{name} ({reason})'.format(
                        module=target.__name__,
                        name=member_name,
                        reason=member_ift_result,
                    ), )
    elif is_class(target):
        logger.debug(
            'Ignoring {cls.__module__}.{cls.__name__} ({reason}).'.format(
                cls=target,
                reason=ift_result,
            ), )
    else:
        logger.debug(
            'Ignoring {target!r} ({reason}).'.format(
                reason=ift_result,
                target=target,
            ), )
Beispiel #3
0
def discover_commands(package, recursively=True):
    # type: (Union[ModuleType, Text], bool) -> Dict[Text, 'CommandMeta']
    """
  Automatically discover commands in the specified package.

  :param package:
    Package path or reference.

  :param recursively:
    If True, will descend recursively into sub-packages.

  :return:
    All commands discovered in the specified package, indexed by
    command name (note: not class name).
  """
    # http://stackoverflow.com/a/25562415/
    if isinstance(package, string_types):
        package = import_module(package)  # type: ModuleType

    commands = {}

    for _, name, is_package in walk_packages(package.__path__,
                                             package.__name__ + '.'):
        # Loading the module is good enough; the CommandMeta metaclass will
        # ensure that any commands in the module get registered.

        # Prefix in name module move to function "walk_packages" for fix
        # conflict with names importing packages
        # Bug https://github.com/iotaledger/iota.lib.py/issues/63
        sub_package = import_module(name)

        # Index any command classes that we find.
        for (_, obj) in get_members(sub_package):
            if is_class(obj) and isinstance(obj, CommandMeta):
                command_name = getattr(obj, 'command')
                if command_name:
                    commands[command_name] = obj

        if recursively and is_package:
            commands.update(discover_commands(sub_package))

    return commands
Beispiel #4
0
def discover_commands(package, recursively=True):
  # type: (Union[ModuleType, Text], bool) -> Dict[Text, 'CommandMeta']
  """
  Automatically discover commands in the specified package.

  :param package:
    Package path or reference.

  :param recursively:
    If True, will descend recursively into sub-packages.

  :return:
    All commands discovered in the specified package, indexed by
    command name (note: not class name).
  """
  # http://stackoverflow.com/a/25562415/
  if isinstance(package, string_types):
    package = import_module(package) # type: ModuleType

  commands = {}

  for _, name, is_package in walk_packages(package.__path__, package.__name__ + '.'):
    # Loading the module is good enough; the CommandMeta metaclass will
    # ensure that any commands in the module get registered.

    # Prefix in name module move to function "walk_packages" for fix
    # conflict with names importing packages
    # Bug https://github.com/iotaledger/iota.lib.py/issues/63
    sub_package = import_module(name)

    # Index any command classes that we find.
    for (_, obj) in get_members(sub_package):
      if is_class(obj) and isinstance(obj, CommandMeta):
        command_name = getattr(obj, 'command')
        if command_name:
          commands[command_name] = obj

    if recursively and is_package:
      commands.update(discover_commands(sub_package))

  return commands