Skip to content

Can it be possible to support multi-level paths for custom input handlers? #272

@wx-ys

Description

@wx-ys

For example, with a handler named galyst.input_handler.ArepoHDFSubfindInputHandler,

def get_named_handler_class(handler):
"""Get a HandlerBase identified by the given name.
The name is of the format submodule.ClassName
:rtype HandlerBase"""
handler = _map_deprecated_handler_name(handler)
try:
output_module = importlib.import_module('.'+handler.split('.')[0],__name__)
except ImportError:
output_module = importlib.import_module(handler.split('.')[0])
output_class = getattr(output_module, handler.split('.')[1])
return output_class

the current implementation attempts to import the module galyst and find a class named input_handler.

Here is the suggested change:

def get_named_handler_class(handler):
    """Get a HandlerBase identified by the given name.

    The name is of the format submodule.ClassName or package.submodule.ClassName

    :rtype HandlerBase"""
    handler = _map_deprecated_handler_name(handler)

    if '.' not in handler:
        raise ValueError("Handler name must be in the format module.ClassName")

    module_name, class_name = handler.rsplit('.', 1)

    try:
        # First, try a relative import within tangos.input_handlers
        output_module = importlib.import_module('.' + module_name, __name__)
    except (ImportError, ModuleNotFoundError):
        # If that fails, try an absolute import
        output_module = importlib.import_module(module_name)

    output_class = getattr(output_module, class_name)
    return output_class

if input galyst.input_handler.ArepoHDFSubfindInputHandler, the output_module will be galyst.input_handler and the class_name will be ArepoHDFSubfindInputHandler.

This update will improve the flexibility of tangos by allowing users to better organize their custom handler modules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions