Exemplo n.º 1
0
 def test_routes_plugin_fired(self):
     local_config = appconfig('config:%s' % config['__file__'], relative_to=conf_dir)
     local_config.local_conf['ckan.plugins'] = 'routes_plugin'
     app = make_app(local_config.global_conf, **local_config.local_conf)
     routes_plugin = PluginGlobals.env().plugin_registry['RoutesPlugin'].__instance__
     assert routes_plugin.calls_made == ['before_map', 'after_map'], \
            routes_plugin.calls_made
Exemplo n.º 2
0
def get_service_names(env='microdrop.managed'):
    '''
    Parameters
    ----------
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    list
        List of plugin names (e.g., ``['microdrop.step_label_plugin', ...]``).
    '''
    e = PluginGlobals.env(env)
    service_names = []
    for name in get_plugin_names(env):
        plugin_class = e.plugin_registry[name]
        service = get_service_instance(plugin_class, env=env)
        if service is None:
            _L().warn(
                'Plugin `%s` exists in registry, but instance cannot '
                'be found.', name)
        else:
            service_names.append(service.name)
    return service_names
Exemplo n.º 3
0
def get_service_instance_by_package_name(name, env='microdrop.managed'):
    '''
    Parameters
    ----------
    name : str
        Plugin Python module name (e.g., ``dmf_control_board_plugin``).

        Corresponds to ``package_name`` key in plugin ``properties.yml`` file.
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    object
        Active service instance matching specified plugin module name.
    '''
    e = PluginGlobals.env(env)
    plugins = [
        p for i, p in enumerate(e.services)
        if name == get_plugin_package_name(p.__class__.__module__)
    ]
    if plugins:
        return plugins[0]
    else:
        raise KeyError('No plugin registered with package name: %s' % name)
Exemplo n.º 4
0
def get_service_instance_by_name(name, env='microdrop.managed'):
    e = PluginGlobals.env(env)
    plugins = [p for i, p in enumerate(e.services) if name == p.name]
    if plugins:
        return plugins[0]
    else:
        raise KeyError, 'No plugin registered with name: %s' % name
Exemplo n.º 5
0
def get_service_instance(class_, env='microdrop.managed'):
    e = PluginGlobals.env(env)
    for service in e.services:
        if isinstance(service, class_):
            # A plugin of this type is registered
            return service
    return None
Exemplo n.º 6
0
def get_service_instance_by_name(name, env='microdrop.managed'):
    '''
    Parameters
    ----------
    name : str
        Plugin name (e.g., ``microdrop.zmq_hub_plugin``).

        Corresponds to ``plugin_name`` key in plugin ``properties.yml`` file.
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    object
        Active service instance matching specified plugin name.

    Raises
    ------
    KeyError
        If no plugin is found registered with the specified name.
    '''
    e = PluginGlobals.env(env)
    plugins = [p for i, p in enumerate(e.services) if name == p.name]
    if plugins:
        return plugins[0]
    else:
        raise KeyError('No plugin registered with name: %s' % name)
Exemplo n.º 7
0
def get_service_instance(class_, env='microdrop.managed'):
    e = PluginGlobals.env(env)
    for service in e.services:
        if isinstance(service, class_):
            # A plugin of this type is registered
            return service
    return None
Exemplo n.º 8
0
def get_service_instance_by_name(name, env='microdrop.managed'):
    e = PluginGlobals.env(env)
    plugins = [p for i, p in enumerate(e.services) if name == p.name]
    if plugins:
        return plugins[0]
    else:
        raise KeyError, 'No plugin registered with name: %s' % name
def get_service_instance_by_name(name, env='microdrop.managed'):
    '''
    Parameters
    ----------
    name : str
        Plugin name (e.g., ``microdrop.zmq_hub_plugin``).

        Corresponds to ``plugin_name`` key in plugin ``properties.yml`` file.
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    object
        Active service instance matching specified plugin name.

    Raises
    ------
    KeyError
        If no plugin is found registered with the specified name.
    '''
    e = PluginGlobals.env(env)
    plugins = [p for i, p in enumerate(e.services) if name == p.name]
    if plugins:
        return plugins[0]
    else:
        raise KeyError('No plugin registered with name: %s' % name)
Exemplo n.º 10
0
 def test_mapper_plugin_fired(self):
     config['ckan.plugins'] = 'mapper_plugin'
     plugins.load_all(config)
     CreateTestData.create_arbitrary([{'name':u'testpkg'}])
     mapper_plugin = PluginGlobals.env().plugin_registry['MapperPlugin'].__instance__
     assert len(mapper_plugin.added) == 2 # resource group table added automatically
     assert mapper_plugin.added[0].name == 'testpkg'
Exemplo n.º 11
0
def get_service_instance_by_package_name(name, env='microdrop.managed'):
    e = PluginGlobals.env(env)
    plugins = [p for i, p in enumerate(e.services) \
               if name == get_plugin_package_name(p.__class__.__module__)]
    if plugins:
        return plugins[0]
    else:
        raise KeyError, 'No plugin registered with package name: %s' % name
Exemplo n.º 12
0
def get_service_names(env='microdrop.managed'):
    e = PluginGlobals.env(env)
    service_names = []
    for name in get_plugin_names(env):
        plugin_class = e.plugin_registry[name]
        service = get_service_instance(plugin_class, env=env)
        service_names.append(service.name)
    return service_names
Exemplo n.º 13
0
def get_service_names(env='microdrop.managed'):
    e = PluginGlobals.env(env)
    service_names = []
    for name in get_plugin_names(env):
        plugin_class = e.plugin_registry[name]
        service = get_service_instance(plugin_class, env=env)
        service_names.append(service.name)
    return service_names
Exemplo n.º 14
0
def get_service_instance_by_package_name(name, env='microdrop.managed'):
    e = PluginGlobals.env(env)
    plugins = [p for i, p in enumerate(e.services) \
               if name == get_plugin_package_name(p.__class__.__module__)]
    if plugins:
        return plugins[0]
    else:
        raise KeyError, 'No plugin registered with package name: %s' % name
Exemplo n.º 15
0
    def test_plugins_load(self):

        config['ckan.plugins'] = 'mapper_plugin routes_plugin'
        plugins.load_all(config)

        # Imported after call to plugins.load_all to ensure that we test the
        # plugin loader starting from a blank slate.
        from ckantestplugin import MapperPlugin, MapperPlugin2, RoutesPlugin

        system_plugins = set(plugin() for plugin in find_system_plugins())
        assert PluginGlobals.env().services == set([MapperPlugin(), RoutesPlugin()]) | system_plugins
Exemplo n.º 16
0
    def test_plugins_load(self):

        config['ckan.plugins'] = 'mapper_plugin routes_plugin'
        plugins.load_all(config)

        # Imported after call to plugins.load_all to ensure that we test the
        # plugin loader starting from a blank slate.
        from ckantestplugin import MapperPlugin, MapperPlugin2, RoutesPlugin

        system_plugins = set(plugin() for plugin in find_system_plugins())
        assert PluginGlobals.env().services == set(
            [MapperPlugin(), RoutesPlugin()]) | system_plugins
Exemplo n.º 17
0
    def test_plugins_load(self):

        config_plugins = config['ckan.plugins']
        config['ckan.plugins'] = 'mapper_plugin routes_plugin'
        plugins.load_all(config)

        # synchronous_search automatically gets loaded
        current_plugins = set([plugins.get_plugin(p) for p in ['mapper_plugin', 'routes_plugin', 'synchronous_search'] + find_system_plugins()])
        assert PluginGlobals.env().services == current_plugins
        # cleanup
        config['ckan.plugins'] = config_plugins
        plugins.load_all(config)
def get_plugin_names(env=None):
    '''
    Parameters
    ----------
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    list(str)
        List of plugin names (e.g., ``['StepLabelPlugin', ...]``).
    '''
    if env is None:
        env = 'pca'
    e = PluginGlobals.env(env)
    return list(e.plugin_registry.keys())
Exemplo n.º 19
0
def get_plugin_names(env=None):
    '''
    Parameters
    ----------
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    list(str)
        List of plugin names (e.g., ``['StepLabelPlugin', ...]``).
    '''
    if env is None:
        env = 'pca'
    e = PluginGlobals.env(env)
    return list(e.plugin_registry.keys())
def get_service_class(name, env='microdrop.managed'):
    '''
    Parameters
    ----------
    name : str
        Plugin class name (e.g., ``App``).
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    class
        Class type matching specified plugin class name.

        ..notes::
            Returns actual class type -- **not** an instance of the plugin
            service.
    '''
    e = PluginGlobals.env(env)
    if name not in e.plugin_registry:
        raise KeyError('No plugin registered with name: %s' % name)
    return e.plugin_registry[name]
Exemplo n.º 21
0
def get_service_class(name, env='microdrop.managed'):
    '''
    Parameters
    ----------
    name : str
        Plugin class name (e.g., ``App``).
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    class
        Class type matching specified plugin class name.

        ..notes::
            Returns actual class type -- **not** an instance of the plugin
            service.
    '''
    e = PluginGlobals.env(env)
    if name not in e.plugin_registry:
        raise KeyError('No plugin registered with name: %s' % name)
    return e.plugin_registry[name]
Exemplo n.º 22
0
def load_plugins(plugins_dir='plugins'):
    plugins_dir = path(plugins_dir)
    logging.info('Loading plugins:')
    if plugins_dir.parent.abspath() not in sys.path:
        sys.path.insert(0, plugins_dir.parent.abspath())

    for package in plugins_dir.dirs():
        try:
            logging.info('\t %s' % package.abspath())
            import_statement = 'import %s.%s' % \
                (plugins_dir.name, package.name)
            logging.debug(import_statement)
            exec(import_statement)
        except Exception:
            logging.info(''.join(traceback.format_exc()))
            logging.error('Error loading %s plugin.', package.name,
                          exc_info=True)

    # Create an instance of each of the plugins, but set it to disabled
    e = PluginGlobals.env('microdrop.managed')
    for class_ in e.plugin_registry.values():
        service = class_()
        service.disable()
def get_service_instance(class_, env='microdrop.managed'):
    '''
    Parameters
    ----------
    class_ : class
        Plugin class type.
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    object or None
        Registered service instance for the specified plugin class type.

        Returns ``None`` if no service is registered for the specified plugin
        class type.
    '''
    e = PluginGlobals.env(env)
    for service in e.services:
        if isinstance(service, class_):
            # A plugin of this type is registered
            return service
    return None
def get_service_instance_by_package_name(name, env='microdrop.managed'):
    '''
    Parameters
    ----------
    name : str
        Plugin Python module name (e.g., ``dmf_control_board_plugin``).

        Corresponds to ``package_name`` key in plugin ``properties.yml`` file.
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    object
        Active service instance matching specified plugin module name.
    '''
    e = PluginGlobals.env(env)
    plugins = [p for i, p in enumerate(e.services)
               if name == get_plugin_package_name(p.__class__.__module__)]
    if plugins:
        return plugins[0]
    else:
        raise KeyError('No plugin registered with package name: %s' % name)
Exemplo n.º 25
0
def get_service_instance(class_, env='microdrop.managed'):
    '''
    Parameters
    ----------
    class_ : class
        Plugin class type.
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    object or None
        Registered service instance for the specified plugin class type.

        Returns ``None`` if no service is registered for the specified plugin
        class type.
    '''
    e = PluginGlobals.env(env)
    for service in e.services:
        if isinstance(service, class_):
            # A plugin of this type is registered
            return service
    return None
Exemplo n.º 26
0
def load_plugins(plugins_dir='plugins'):
    plugins_dir = path(plugins_dir)
    logging.info('Loading plugins:')
    if plugins_dir.parent.abspath() not in sys.path:
        sys.path.insert(0, plugins_dir.parent.abspath())

    for package in plugins_dir.dirs():
        try:
            logging.info('\t %s' % package.abspath())
            import_statement = 'import %s.%s' % \
                (plugins_dir.name, package.name)
            logging.debug(import_statement)
            exec(import_statement)
        except Exception:
            logging.info(''.join(traceback.format_exc()))
            logging.error('Error loading %s plugin.',
                          package.name,
                          exc_info=True)

    # Create an instance of each of the plugins, but set it to disabled
    e = PluginGlobals.env('microdrop.managed')
    for class_ in e.plugin_registry.values():
        service = class_()
        service.disable()
def get_service_names(env='microdrop.managed'):
    '''
    Parameters
    ----------
    env : str, optional
        Name of ``pyutilib.component.core`` plugin environment (e.g.,
        ``'microdrop.managed``').

    Returns
    -------
    list
        List of plugin names (e.g., ``['microdrop.step_label_plugin', ...]``).
    '''
    e = PluginGlobals.env(env)
    service_names = []
    for name in get_plugin_names(env):
        plugin_class = e.plugin_registry[name]
        service = get_service_instance(plugin_class, env=env)
        if service is None:
            _L().warn('Plugin `%s` exists in registry, but instance cannot '
                      'be found.', name)
        else:
            service_names.append(service.name)
    return service_names
def load_plugins(plugins_dir='plugins', import_from_parent=True):
    '''
    Import each Python plugin module in the specified directory and create an
    instance of each contained plugin class for which an instance has not yet
    been created.

    Parameters
    ----------
    plugins_dir : str
        Directory containing zero or more Python plugin modules to import.
    import_from_parent : bool
        Add parent of specified directory to system path and import
        ``<parent>.<module>``.

        ..notes::
            **Not recommended**, but kept as default to maintain legacy
            protocol compatibility.

    Returns
    -------
    list
        Newly created plugins (plugins are not recreated if they were
        previously loaded.)


    .. versionchanged:: 2.25
        Do not import hidden directories (i.e., name starts with ``.``).
    '''
    logger = _L()  # use logger with function context
    logger.info('plugins_dir=`%s`', plugins_dir)
    plugins_dir = ph.path(plugins_dir).realpath()
    logger.info('Loading plugins:')
    plugins_root = plugins_dir.parent if import_from_parent else plugins_dir
    if plugins_root not in sys.path:
        sys.path.insert(0, plugins_root)

    # Create an instance of each of the plugins, but set it to disabled
    e = PluginGlobals.env('microdrop.managed')
    initial_plugins = set(e.plugin_registry.values())
    imported_plugins = set()

    for package_i in plugins_dir.dirs():
        if package_i.isjunction() and not package_i.readlink().isdir():
            # Plugin directory is a junction/link to a non-existent target
            # path.
            logger.info('Skip import of `%s` (broken link to `%s`).',
                        package_i.name, package_i.readlink())
            continue
        elif package_i.name in (p.__module__ for p in initial_plugins):
            # Plugin with the same name has already been imported.
            logger.info('Skip import of `%s` (plugin with same name has '
                        'already been imported).', package_i.name)
            continue
        elif package_i.name.startswith('.'):
            logger.info('Skip import of hidden directory `%s`.',
                        package_i.name)
            continue

        try:
            plugin_module = package_i.name
            if import_from_parent:
                plugin_module = '.'.join([plugins_dir.name, plugin_module])
            import_statement = 'import {}'.format(plugin_module)
            logger.debug(import_statement)
            exec(import_statement)
            all_plugins = set(e.plugin_registry.values())
            current_plugin = list(all_plugins - initial_plugins -
                                  imported_plugins)[0]
            logger.info('\t Imported: %s (%s)', current_plugin.__name__,
                        package_i)
            imported_plugins.add(current_plugin)
        except Exception:
            map(logger.info, traceback.format_exc().splitlines())
            logger.error('Error loading %s plugin.', package_i.name,
                         exc_info=True)

    # For each newly imported plugin class, create a service instance
    # initialized to the disabled state.
    new_plugins = []
    for class_ in imported_plugins:
        service = class_()
        service.disable()
        new_plugins.append(service)
    logger.debug('\t Created new plugin services: %s',
                 ','.join([p.__class__.__name__ for p in new_plugins]))
    return new_plugins
Exemplo n.º 29
0
def get_service_class(name, env='microdrop.managed'):
    e = PluginGlobals.env(env)
    if name not in e.plugin_registry:
        raise KeyError, 'No plugin registered with name: %s' % name
    return e.plugin_registry[name]
Exemplo n.º 30
0
def load_plugins(plugins_dir='plugins', import_from_parent=True):
    '''
    Import each Python plugin module in the specified directory and create an
    instance of each contained plugin class for which an instance has not yet
    been created.

    Parameters
    ----------
    plugins_dir : str
        Directory containing zero or more Python plugin modules to import.
    import_from_parent : bool
        Add parent of specified directory to system path and import
        ``<parent>.<module>``.

        ..notes::
            **Not recommended**, but kept as default to maintain legacy
            protocol compatibility.

    Returns
    -------
    list
        Newly created plugins (plugins are not recreated if they were
        previously loaded.)
    '''
    logger = _L()  # use logger with function context
    logger.info('plugins_dir=`%s`', plugins_dir)
    plugins_dir = ph.path(plugins_dir).realpath()
    logger.info('Loading plugins:')
    plugins_root = plugins_dir.parent if import_from_parent else plugins_dir
    if plugins_root not in sys.path:
        sys.path.insert(0, plugins_root)

    # Create an instance of each of the plugins, but set it to disabled
    e = PluginGlobals.env('microdrop.managed')
    initial_plugins = set(e.plugin_registry.values())
    imported_plugins = set()

    for package_i in plugins_dir.dirs():
        if package_i.isjunction() and not package_i.readlink().isdir():
            # Plugin directory is a junction/link to a non-existent target
            # path.
            logger.info('Skip import of `%s` (broken link to `%s`).',
                        package_i.name, package_i.readlink())
            continue

        try:
            plugin_module = package_i.name
            if import_from_parent:
                plugin_module = '.'.join([plugins_dir.name, plugin_module])
            import_statement = 'import {}'.format(plugin_module)
            logger.debug(import_statement)
            exec(import_statement)
            all_plugins = set(e.plugin_registry.values())
            current_plugin = list(all_plugins - initial_plugins -
                                  imported_plugins)[0]
            logger.info('\t Imported: %s (%s)', current_plugin.__name__,
                        package_i)
            imported_plugins.add(current_plugin)
        except Exception:
            logger.info(''.join(traceback.format_exc()))
            logger.error('Error loading %s plugin.',
                         package_i.name,
                         exc_info=True)

    # For each newly imported plugin class, create a service instance
    # initialized to the disabled state.
    new_plugins = []
    for class_ in imported_plugins:
        service = class_()
        service.disable()
        new_plugins.append(service)
    logger.debug('\t Created new plugin services: %s',
                 ','.join([p.__class__.__name__ for p in new_plugins]))
    return new_plugins
Exemplo n.º 31
0
def get_service_class(name, env='microdrop.managed'):
    e = PluginGlobals.env(env)
    if name not in e.plugin_registry:
        raise KeyError, 'No plugin registered with name: %s' % name
    return e.plugin_registry[name]
Exemplo n.º 32
0
def get_plugin_names(env=None):
    if env is None:
        env = 'pca'
    e = PluginGlobals.env(env)
    return list(e.plugin_registry.keys())
Exemplo n.º 33
0
def parse_args():
    import argparse

    from coopr.opt import SolverFactory as SF
    from pyutilib.component.core import PluginGlobals

    logger = PluginGlobals.env().log
    logger.disabled = True  # no need for warnings: it's what we're testing!
    available_solvers = set(solver   # name of solver; a string
                            for solver in filter(lambda x: '_' != x[0], SF.services())
                            if SF(solver).available(False)
                            )
    logger.disabled = False

    if available_solvers:
        if 'cplex' in available_solvers:
            default_solver = 'cplex'
        elif 'gurobi' in available_solvers:
            default_solver = 'gurobi'
        elif 'cbc' in available_solvers:
            default_solver = 'cbc'
        elif 'glpk' in available_solvers:
            default_solver = 'glpk'
        else:
            default_solver = available_solvers[0]
    else:
        default_solver = 'NONE'
        SE.write('\nNOTICE: Coopr did not find any suitable solvers.  Temoa will '
                 'not be able to solve any models.  If you need help, ask on the '
                 'Temoa Project forum: http://temoaproject.org/\n\n')

    parser = argparse.ArgumentParser()
    graphviz = parser.add_argument_group('Graphviz Options')
    solver = parser.add_argument_group('Solver Options')

    parser.add_argument('dot_dat',
                        type=str,
                        nargs='+',
                        help='AMPL-format data file(s) with which to create a model instance. '
                        'e.g. "data.dat"'
                        )

    graphviz.add_argument('--graph_format',
                          help='Create a system-wide visual depiction of the model.  The '
                          'available options are the formats available to Graphviz.  To get '
                          'a list of available formats, use the "dot" command: dot -Txxx. '
                          '[Default: None]',
                          action='store',
                          dest='graph_format',
                          default=None)

    graphviz.add_argument('--show_capacity',
                          help='Choose whether or not the capacity shows up in the subgraphs.  '
                          '[Default: not shown]',
                          action='store_true',
                          dest='show_capacity',
                          default=False)

    graphviz.add_argument('--graph_type',
                          help='Choose the type of subgraph depiction desired.  [Default: '
                          'separate_vintages]',
                          action='store',
                          dest='graph_type',
                          choices=('explicit_vintages', 'separate_vintages'),
                          default='separate_vintages')

    graphviz.add_argument('--use_splines',
                          help='Choose whether the subgraph edges needs to be straight or curved.'
                          '  [Default: use straight lines, not splines]',
                          action='store_true',
                          dest='splinevar',
                          default=False)

    solver.add_argument('--solver',
                        help="Which backend solver to use.  See 'pyomo --help-solvers' for a list "
                        'of solvers with which Coopr can interface.  The list shown here is '
                        'what Coopr can currently find on this system.  [Default: {}]'
                        .format(default_solver),
                        action='store',
                        choices=sorted(available_solvers),
                        dest='solver',
                        default=default_solver)

    solver.add_argument('--symbolic_solver_labels',
                        help='When interfacing with the solver, use model-derived symbol names.  '
                        'For example, "V_Capacity(coal_plant,2000)" instead of "x(47)".  '
                        'Mainly used for debugging purposes.  [Default: use x(47) style]',
                        action='store_true',
                        dest='useSymbolLabels',
                        default=False)

    solver.add_argument('--generate_solver_lp_file',
                        help='Request that solver create an LP representation of the optimization '
                        'problem.  Mainly used for model debugging purposes.  The file name '
                        'will have the same base name as the first dot_dat file specified.  '
                        '[Default: do not create solver LP file]',
                        action='store_true',
                        dest='generateSolverLP',
                        default=False)

    solver.add_argument('--keep_coopr_lp_file',
                        help='Save the LP file as written by Pyomo.  This is distinct from the '
                        "solver's generated LP file, but /should/ represent the same model.  "
                        'Mainly used for debugging purposes.  The file name will have the '
                        'same base name as the first dot_dat file specified.  '
                        '[Default: remove Pyomo LP]',
                        action='store_true',
                        dest='keepPyomoLP',
                        default=False)

    options = parser.parse_args()
    return options
Exemplo n.º 34
0
def get_plugin_names(env=None):
    if env is None:
        env = 'pca'
    e = PluginGlobals.env(env)
    return list(e.plugin_registry.keys())