def test_load5(self): """Test load method""" PluginGlobals.add_env("testing.config_loading") class TMP2(Plugin): def __init__(self): declare_option("a") declare_option("b", cls=FileOption) declare_option("c") declare_option("xx", section_re='globals.*') config = Configuration() tmp2 = TMP2() config.load(currdir + "config4.ini") #config.pprint() if False and sys.platform == "win32": # # A hack, to ensure cross-platform portability of this test # e = ExtensionPoint(IFileOption) for ep in e.extensions(): ep.set_value("/dev/null", raw=True) #PluginGlobals.pprint() config.save(currdir + "config4.out") #print config self.assertFileEqualsBaseline( currdir + "config4.out", currdir + "config4.txt", filter=filter) pyutilib.misc.setup_redirect(currdir + "log2.out") config.pprint() pyutilib.misc.reset_redirect() self.assertFileEqualsBaseline( currdir + "log2.out", currdir + "log2.txt", filter=filter) PluginGlobals.remove_env( "testing.config_loading", cleanup=True, singleton=False)
def __init__(self): PluginGlobals.push_env('orion') self.__window_managers = ExtensionPoint(IWindowManager) self.__display_servers = ExtensionPoint(IDisplayServerCommunicator) self.__accessibility_manager = None self.__conn = None
def register_executable(name, validate=None): ep = ExtensionPoint(IExternalExecutable) if len(ep(name, all=True)) == 0: PluginGlobals.add_env("pca") PluginGlobals._executables.append( ExternalExecutable(name=name, validate=validate)) PluginGlobals.pop_env() else: # # If the executable is being 'registered', then we search for it # again, since the user environment may have changed. # list(ep(name, all=True))[0].find_executable()
def register_executable(name, validate=None): ep = ExtensionPoint(IExternalExecutable) if len(ep(name, all=True)) == 0: PluginGlobals.add_env("pca") PluginGlobals._executables.append( ExternalExecutable( name=name, validate=validate)) PluginGlobals.pop_env() else: # # If the executable is being 'registered', then we search for it # again, since the user environment may have changed. # list(ep(name, all=True))[0].find_executable()
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
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
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 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'
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)
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)
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 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
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
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
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
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 test_load5(self): """Test load method""" PluginGlobals.add_env("testing.config_loading") class TMP2(Plugin): def __init__(self): declare_option("a") declare_option("b", cls=FileOption) declare_option("c") declare_option("xx", section_re='globals.*') config = Configuration() tmp2 = TMP2() config.load(currdir + "config4.ini") #config.pprint() if False and sys.platform == "win32": # # A hack, to ensure cross-platform portability of this test # e = ExtensionPoint(IFileOption) for ep in e.extensions(): ep.set_value("/dev/null", raw=True) #PluginGlobals.pprint() config.save(currdir + "config4.out") #print config self.assertFileEqualsBaseline(currdir + "config4.out", currdir + "config4.txt", filter=filter) pyutilib.misc.setup_redirect(currdir + "log2.out") config.pprint() pyutilib.misc.reset_redirect() self.assertFileEqualsBaseline(currdir + "log2.out", currdir + "log2.txt", filter=filter) PluginGlobals.remove_env("testing.config_loading", cleanup=True, singleton=False)
def run(self): Logger('orion', verbose=True) self.__accessibility_manager = AccessibilityManager() # load Display Server Communicators displayName = os.environ.get("DISPLAY") if not displayName: raise self.__conn = self.__display_servers()[0] self.__conn.init(displayName) # load window managers manager_count = len(self.__window_managers) logger.debug('found %s orion window managers'%manager_count) manager = self.__window_managers()[0] self.__window_manager = manager manager.init() self.DEBUG_TEST() logger.debug("starting '%s' window manager"%manager.name) manager.run() PluginGlobals.pop_env('orion')
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]
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 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)
_L().info('[PluginManager] Enabled plugin: %s', name) if hasattr(service, "on_plugin_enable"): service.on_plugin_enable() emit_signal('on_plugin_enabled', [env, service]) def disable(name, env='microdrop.managed'): ''' Disable specified plugin. 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``'). ''' service = get_service_instance_by_name(name, env) if service and service.enabled(): service.disable() if hasattr(service, "on_plugin_disable"): service.on_plugin_disable() emit_signal('on_plugin_disabled', [env, service]) logging.info('[PluginManager] Disabled plugin: %s' % name) PluginGlobals.pop_env()
def reset_after_updates(self): """ Configure the pyutilib.component logging facility. This will implicitly configure all of the environment-specific logging objects. """ sys.stdout.flush() logger = logging.getLogger('pyutilib.component.core.' + self.namespace) if not self._hdlr is None: logger.removeHandler(self._hdlr) # # Set logging level # level = self.log_level level = level.upper() if level in ('DEBUG', 'ALL'): logger.setLevel(logging.DEBUG) elif level == 'INFO': logger.setLevel(logging.INFO) elif level == 'ERROR': logger.setLevel(logging.ERROR) elif level == 'CRITICAL': logger.setLevel(logging.CRITICAL) else: logger.setLevel(logging.WARNING) # # Initialize the current path. Is there a rule to use for whic # environment will be used??? In practice, there is likely to be # only one environment. # if self.log_dir is None: path = None for plugin in self.env_plugins: (flag, count) = plugin.matches(self.namespace) tmp = plugin.get_option("path") if flag and not tmp is None: path = tmp break if path is None: path = os.getcwd() else: path = self.log_dir # # Setup the logging file # logtype = self.log_type.lower() if self.log_file is None: logfile = os.path.join(path, 'log') else: logfile = self.log_file if not os.path.isabs(logfile): logfile = os.path.join(path, logfile) # # Define the format # format = self.log_format if format is None: format = '[env=%(env)s where=%(module)s] %(levelname)s - %(message)s' if self.timestamp and logtype in ('file', 'stderr'): format = '%(asctime)s ' + format format = format.replace('$(', '%(') \ .replace('%(env)s', PluginGlobals.get_env().name) datefmt = '' if self.timestamp and self.log_type == 'stderr': datefmt = '%X' formatter = logging.Formatter(format, datefmt) # # Define the handler # if logtype == 'file': hdlr = logging.FileHandler(logfile) elif logtype in ('winlog', 'eventlog', 'nteventlog'): # Requires win32 extensions hdlr = handlers.NTEventLogHandler(logid, logtype='Application') elif logtype in ('syslog', 'unix'): hdlr = handlers.SysLogHandler('/dev/log') elif logtype in ('stderr'): hdlr = logging.StreamHandler(sys.stderr) else: hdlr = handlers.BufferingHandler(0) # Note: this _really_ throws away log events, as a `MemoryHandler` # would keep _all_ records in case there's no target handler (a bug?) self._hdlr = hdlr self._logtype = logtype self._logfile = logfile hdlr.setFormatter(formatter) logger.addHandler(hdlr) self._log = logger
from pyutilib.component.core import Interface, PluginGlobals import threading print '[interfaces] %s' % threading.current_thread() PluginGlobals.push_env('microdrop.managed') PluginGlobals.pop_env() PluginGlobals.push_env('microdrop') if 'IFoo' in PluginGlobals.interface_registry: IFoo = PluginGlobals.interface_registry['IFoo'] else: class IFoo(Interface): pass if 'ILoggingPlugin' in PluginGlobals.interface_registry: ILoggingPlugin = PluginGlobals.interface_registry['ILoggingPlugin'] else: class ILoggingPlugin(Interface): def on_debug(self, record): pass def on_info(self, record): pass def on_warning(self, record): pass
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
def get_plugin_names(env=None): if env is None: env = 'pca' e = PluginGlobals.env(env) return list(e.plugin_registry.keys())
def tearDown(self): PluginGlobals.remove_env( "testing.options", cleanup=True, singleton=False)
def setUp(self): PluginGlobals.add_env("testing.config") pyutilib.component.config.plugin_ConfigParser.Configuration_ConfigParser( name="Configuration_ConfigParser") self.tmp = Test.TMP()
# _________________________________________________________________________ # # PyUtilib: A Python utility library. # Copyright (c) 2008 Sandia Corporation. # This software is distributed under the BSD License. # Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, # the U.S. Government retains certain rights in this software. # _________________________________________________________________________ """ The pyutilib.component.config package includes utilities to configure the PyUtilib Component Architecture. This includes facilities for using configuration files, controlling logging, and specifying component options. """ from pyutilib.component.core import PluginGlobals PluginGlobals.add_env("pca") from pyutilib.component.config.env_config import EnvironmentConfig from pyutilib.component.config.options import ExecutableOption, declare_option, Option from pyutilib.component.config.managed_plugin import ManagedPlugin, ManagedSingletonPlugin from pyutilib.component.config.configuration import Configuration, ConfigurationError from pyutilib.component.config.logging_config import LoggingConfig from pyutilib.component.config.tempfiles import ITempfileManager, TempfileManagerPlugin, TempfileManager import pyutilib.component.config.plugin_ConfigParser PluginGlobals.pop_env()
# _________________________________________________________________________ # # PyUtilib: A Python utility library. # Copyright (c) 2008 Sandia Corporation. # This software is distributed under the BSD License. # Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, # the U.S. Government retains certain rights in this software. # _________________________________________________________________________ # from pyutilib.component.core import PluginGlobals PluginGlobals.add_env("pyutilib") from pyutilib.subprocess.processmngr import subprocess, SubprocessMngr, run_command, timer, signal_handler, run, PIPE, STDOUT PluginGlobals.pop_env()
def tearDown(self): PluginGlobals.remove_env("testing.options", cleanup=True, singleton=False)
import os import re import sys from os.path import abspath, dirname currdir = dirname(abspath(__file__)) + os.sep import unittest from pyutilib.component.core import Interface, PluginGlobals, ExtensionPoint, implements, Plugin from pyutilib.component.config.options import Option, OptionError, IOption, declare_option, FileOption, IntOption, FloatOption, DictOption, BoolOption PluginGlobals.add_env("testing.options") class IDummyOption(Interface): """An interface that supports the initialization of the directory for options that specify files. This is needed to correctly initialize relative paths for files.""" class DummyOption1(Option): """A test class that converts option data into float values.""" implements(IDummyOption) def convert(self, value, default): """Conversion routine.""" val = value[-1] if not val: return 0 try: return float(val)
# _________________________________________________________________________ # # PyUtilib: A Python utility library. # Copyright (c) 2008 Sandia Corporation. # This software is distributed under the BSD License. # Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, # the U.S. Government retains certain rights in this software. # _________________________________________________________________________ from pyutilib.component.core import PluginGlobals PluginGlobals.add_env("pca") from pyutilib.component.executables.executable import * PluginGlobals.pop_env()
def _create_environment(self): if Config._environment == None: env_name = "Bumblebee" Config._environment = PluginEnvironment(env_name) PluginGlobals.push_env(Config._environment)
from pyutilib.component.core import Interface, ExtensionPoint, implements, Plugin, PluginGlobals, SingletonPlugin import pyutilib.component.loader import threading print "[interfaces] %s" % threading.current_thread() PluginGlobals.push_env("microdrop.managed") PluginGlobals.pop_env() PluginGlobals.push_env("microdrop") if "IFoo" in PluginGlobals.interface_registry: IFoo = PluginGlobals.interface_registry["IFoo"] else: class IFoo(Interface): pass if "ILoggingPlugin" in PluginGlobals.interface_registry: ILoggingPlugin = PluginGlobals.interface_registry["ILoggingPlugin"] else: class ILoggingPlugin(Interface): def on_debug(self, record): pass def on_info(self, record): pass
>>> from microdrop.interfaces import IPlugin >>> import microdrop.app >>> >>> signals = blinker.Namespace() >>> signal = 'get_schedule_requests' >>> args = ('on_plugin_enable', ) >>> connect_pyutilib_signal(signals, signal, IPlugin) >>> signals.signal(signal).send(*args) [(<bound method DmfDeviceController.get_schedule_requests of <Plugin DmfDeviceController 'microdrop.gui.dmf_device_controller'>>, [ScheduleRequest(before='microdrop.gui.config_controller', after='microdrop.gui.dmf_device_controller'), ScheduleRequest(before='microdrop.gui.main_window_controller', after='microdrop.gui.dmf_device_controller')])] ''' import functools as ft import inspect from microdrop.plugin_manager import ExtensionPoint callbacks = [getattr(p, signal) for p in ExtensionPoint(*args, **kwargs) if hasattr(p, signal)] for callback_i in callbacks: if len(inspect.getargspec(callback_i)[0]) < 2: # Blinker signals require _at least_ one argument (assumed to be sender). # Wrap pyutilib signals without any arguments to make them work with blinker. @ft.wraps(callback_i) def _callback(*args, **kwargs): return callback_i() else: _callback = callback_i signals.signal(signal).connect(_callback, weak=False) PluginGlobals.pop_env()
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]
def tearDown(self): del self.tmp PluginGlobals.remove_env("testing.config", cleanup=True, singleton=False)
def setUp(self): PluginGlobals.add_env("testing.options") PluginGlobals.clear_global_data(keys=['globals', 'a.b'])
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
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