tangos icon indicating copy to clipboard operation
tangos copied to clipboard

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

Open wx-ys opened this issue 6 months ago • 1 comments

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

https://github.com/pynbody/tangos/blob/ddf42fd9081d65ca25f30e61699e69cc76c9d49e/tangos/input_handlers/init.py#L209-L221

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.

wx-ys avatar Jun 29 '25 10:06 wx-ys

Yes I support this change - please propose it in a PR

apontzen avatar Jun 29 '25 11:06 apontzen