示例#1
0
def test_name_generation():
    assert_equal(get_api_name(("some.module", "SomeClass")), 'module')
    assert_equal(
        get_api_name(("some.module", "SomeClass", "cmdline-override")),
        'module')
    assert_equal(
        get_api_name(("some.module", "SomeClass", "cmdline_override",
                      "api_override-dont-touch")), "api_override-dont-touch")
示例#2
0
 def __getattr__(self, attr):
     # Assure that we are not just missing some late binding
     # @datasetmethod . We will use interface definitions.
     # The gotcha could be the mismatch between explicit name
     # provided to @datasetmethod and what is defined in interfaces
     meth = None
     if not attr.startswith('_'):  # do not even consider those
         from datalad.interface.base import (
             get_interface_groups, get_api_name, load_interface
         )
         groups = get_interface_groups(True)
         for group, _, interfaces in groups:
             for intfspec in interfaces:
                 # lgr.log(5, "Considering interface %s", intfspec)
                 name = get_api_name(intfspec)
                 if attr == name:
                     meth_ = load_interface(intfspec)
                     if meth_:
                         lgr.debug("Found matching interface %s for %s",
                                   intfspec, name)
                         if meth:
                             lgr.debug(
                                 "New match %s possibly overloaded previous one %s",
                                 meth_, meth
                             )
                         meth = meth_
         if not meth:
             lgr.debug("Found no match among known interfaces for %r", attr)
     return super(Dataset, self).__getattribute__(attr)
示例#3
0
 def __getattr__(self, attr):
     # Assure that we are not just missing some late binding
     # @datasetmethod . We will use interface definitions.
     # The gotcha could be the mismatch between explicit name
     # provided to @datasetmethod and what is defined in interfaces
     meth = None
     if not attr.startswith('_'):  # do not even consider those
         from datalad.interface.base import (
             get_interface_groups, get_api_name, load_interface
         )
         groups = get_interface_groups(True)
         for group, _, interfaces in groups:
             for intfspec in interfaces:
                 # lgr.log(5, "Considering interface %s", intfspec)
                 name = get_api_name(intfspec)
                 if attr == name:
                     meth_ = load_interface(intfspec)
                     if meth_:
                         lgr.debug("Found matching interface %s for %s",
                                   intfspec, name)
                         if meth:
                             lgr.debug(
                                 "New match %s possibly overloaded previous one %s",
                                 meth_, meth
                             )
                         meth = meth_
         if not meth:
             lgr.debug("Found no match among known interfaces for %r", attr)
     return super(Dataset, self).__getattribute__(attr)
示例#4
0
def _generate_extension_api():
    """Auto detect all available extensions and generate an API from them
    """
    from importlib import import_module
    from pkg_resources import iter_entry_points
    from .interface.base import get_api_name

    from datalad.dochelpers import exc_str
    import logging
    lgr = logging.getLogger('datalad.api')

    for entry_point in iter_entry_points('datalad.extensions'):
        try:
            lgr.debug(
                'Loading entrypoint %s from datalad.extensions for API building',
                entry_point.name)
            grp_descr, interfaces = entry_point.load()
            lgr.debug('Loaded entrypoint %s from datalad.extensions',
                      entry_point.name)
        except Exception as e:
            lgr.warning('Failed to load entrypoint %s: %s', entry_point.name,
                        exc_str(e))
            continue

        for intfspec in interfaces:
            # turn the interface spec into an instance
            mod = import_module(intfspec[0])
            intf = getattr(mod, intfspec[1])
            api_name = get_api_name(intfspec)
            if api_name in globals():
                lgr.debug(
                    'Command %s from extension %s is replacing a previously loaded implementation',
                    api_name, entry_point.name)
            globals()[api_name] = intf.__call__
示例#5
0
文件: api.py 项目: datalad/datalad
def _generate_extension_api():
    """Auto detect all available extensions and generate an API from them
    """
    from importlib import import_module
    from pkg_resources import iter_entry_points
    from .interface.base import get_api_name

    from datalad.dochelpers import exc_str
    import logging
    lgr = logging.getLogger('datalad.api')

    for entry_point in iter_entry_points('datalad.extensions'):
        try:
            lgr.debug(
                'Loading entrypoint %s from datalad.extensions for API building',
                entry_point.name)
            grp_descr, interfaces = entry_point.load()
            lgr.debug(
                'Loaded entrypoint %s from datalad.extensions',
                entry_point.name)
        except Exception as e:
            lgr.warning('Failed to load entrypoint %s: %s', entry_point.name, exc_str(e))
            continue

        for intfspec in interfaces:
            # turn the interface spec into an instance
            mod = import_module(intfspec[0])
            intf = getattr(mod, intfspec[1])
            api_name = get_api_name(intfspec)
            if api_name in globals():
                lgr.debug(
                    'Command %s from extension %s is replacing a previously loaded implementation',
                    api_name,
                    entry_point.name)
            globals()[api_name] = intf.__call__
示例#6
0
def test_name_generation():
    assert_equal(get_api_name(("some.module", "SomeClass")), 'module')
    assert_equal(
        get_api_name(("some.module", "SomeClass", "cmdline-override")),
        'module')
    assert_equal(
        get_api_name(("some.module", "SomeClass", "cmdline_override",
                      "api_override-dont-touch")), "api_override-dont-touch")
    assert_equal(
        get_cmdline_command_name(("some.module_something", "SomeClass")),
        "module-something")
    assert_equal(
        get_cmdline_command_name(
            ("some.module_something", "SomeClass", "override")), "override")
    assert_equal(
        get_cmdline_command_name(
            ("some.module_something", "SomeClass", "override", "api_ignore")),
        "override")
示例#7
0
def _command_summary():
    # Import here to avoid polluting the datalad.api namespace.
    from collections import defaultdict
    from datalad.interface.base import alter_interface_docs_for_api
    from datalad.interface.base import get_api_name
    from datalad.interface.base import get_cmd_doc
    from datalad.interface.base import get_cmd_summaries
    from datalad.interface.base import get_interface_groups
    from datalad.interface.base import load_interface

    groups = get_interface_groups()
    grp_short_descriptions = defaultdict(list)
    for group, _, specs in sorted(groups, key=lambda x: x[1]):
        for spec in specs:
            intf = load_interface(spec)
            if intf is None:
                continue
            sdescr = getattr(intf, "short_description", None) or \
                alter_interface_docs_for_api(get_cmd_doc(intf)).split("\n")[0]
            grp_short_descriptions[group].append((get_api_name(spec), sdescr))
    return "\n".join(get_cmd_summaries(grp_short_descriptions, groups))
示例#8
0
文件: api.py 项目: datalad/datalad
def _command_summary():
    # Import here to avoid polluting the datalad.api namespace.
    from collections import defaultdict
    from datalad.interface.base import alter_interface_docs_for_api
    from datalad.interface.base import get_api_name
    from datalad.interface.base import get_cmd_doc
    from datalad.interface.base import get_cmd_summaries
    from datalad.interface.base import get_interface_groups
    from datalad.interface.base import load_interface

    groups = get_interface_groups(include_plugins=True)
    grp_short_descriptions = defaultdict(list)
    for group, _, specs in sorted(groups, key=lambda x: x[1]):
        for spec in specs:
            intf = load_interface(spec)
            if intf is None:
                continue
            sdescr = getattr(intf, "short_description", None) or \
                alter_interface_docs_for_api(get_cmd_doc(intf)).split("\n")[0]
            grp_short_descriptions[group].append(
                (get_api_name(spec), sdescr))
    return "\n".join(get_cmd_summaries(grp_short_descriptions, groups))
示例#9
0
def _generate_extension_api():
    """Auto detect all available extensions and generate an API from them
    """
    from datalad.support.entrypoints import iter_entrypoints
    from datalad.interface.base import (
        get_api_name,
        load_interface,
    )

    import logging
    lgr = logging.getLogger('datalad.api')

    for ename, _, (grp_descr,
                   interfaces) in iter_entrypoints('datalad.extensions',
                                                   load=True):
        for intfspec in interfaces:
            # turn the interface spec into an instance
            intf = load_interface(intfspec[:2])
            api_name = get_api_name(intfspec)
            if api_name in globals():
                lgr.debug(
                    'Command %s from extension %s is replacing a previously loaded implementation',
                    api_name, ename)
            globals()[api_name] = intf.__call__