def load_extensions(names, ep_type): import pyomo.environ plugins = ExtensionPoint(ep_type) active_plugins = [] for this_extension in names: module, _ = load_external_module(this_extension) assert module is not None for name, obj in inspect.getmembers(module, inspect.isclass): # the second condition gets around goofyness related # to issubclass returning True when the obj is the # same as the test class. if issubclass(obj, SingletonPlugin) and \ (name != "SingletonPlugin"): for plugin in plugins(all=True): if isinstance(plugin, obj): active_plugins.append(plugin) return tuple(active_plugins)
def parse_command_line(args, register_options_callback, with_extensions=None, **kwds): import pyomo.pysp.plugins pyomo.pysp.plugins.load() from pyomo.pysp.util.config import _domain_tuple_of_str registered_extensions = {} if with_extensions is not None: for name in with_extensions: plugins = ExtensionPoint(with_extensions[name]) for plugin in plugins(all=True): registered_extensions.setdefault(name,[]).\ append(plugin.__class__.__module__) def _get_argument_parser(options): # if we modify this and don't copy it, # the this output will appear twice the second # time this function gets called _kwds = dict(kwds) if len(registered_extensions) > 0: assert with_extensions is not None epilog = _kwds.pop('epilog',"") if epilog != "": epilog += "\n\n" epilog += "Registered Extensions:\n" for name in registered_extensions: epilog += " - "+str(with_extensions[name].__name__)+": " epilog += str(registered_extensions[name])+"\n" _kwds['epilog'] = epilog ap = argparse.ArgumentParser( add_help=False, formatter_class=argparse.RawDescriptionHelpFormatter, **_kwds) options.initialize_argparse(ap) ap.add_argument("-h", "--help", dest="show_help", action="store_true", default=False, help="show this help message and exit") return ap # # Register options # options = PySPConfigBlock() register_options_callback(options) if with_extensions is not None: for name in with_extensions: configval = options.get(name, None) assert configval is not None assert configval._domain is _domain_tuple_of_str ap = _get_argument_parser(options) # First parse known args, then import any extension plugins # specified by the user, regenerate the options block and # reparse to pick up plugin specific registered options opts, _ = ap.parse_known_args(args=args) options.import_argparse(opts) extensions = {} if with_extensions is None: if opts.show_help: pass else: if all(len(options.get(name).value()) == 0 for name in with_extensions) and \ opts.show_help: ap.print_help() sys.exit(0) for name in with_extensions: extensions[name] = load_extensions( options.get(name).value(), with_extensions[name]) # regenerate the options options = PySPConfigBlock() register_options_callback(options) for name in extensions: for plugin in extensions[name]: if isinstance(plugin, PySPConfiguredObject): plugin.register_options(options) # do a dummy access to option to prevent # a warning about it not being used options.get(name).value() ap = _get_argument_parser(options) opts = ap.parse_args(args=args) options.import_argparse(opts) for name in extensions: for plugin in extensions[name]: if isinstance(plugin, PySPConfiguredObject): plugin.set_options(options) if opts.show_help: ap.print_help() sys.exit(0) if with_extensions: for name in extensions: extensions[name] = sort_extensions_by_precedence(extensions[name]) return options, extensions else: return options