def compile_scenario_tree_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=("The directory in which to store all output files. " "Default is '.'."), doc=None, visibility=0)) safe_register_unique_option( options, "compiled_reference_model_filename", PySPConfigValue( "PySP_CompiledReferenceModel.py", domain=_domain_must_be_str, description=( "The filename to use for the new reference model that uses " "the compiled scenarios. This will be prefixed by the " "output directory name where compiled scenarios are stored. " "Default is 'PySP_CompiledReferenceModel.py'."), doc=None, visibility=0)) safe_register_common_option(options, "scenario_tree_manager") ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "extension_precedence") return options
def scenariotreeserver_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "verbose") safe_register_common_option(options, "pyro_host") safe_register_common_option(options, "pyro_port") safe_register_unique_option( options, "mpi", PySPConfigValue(False, domain=bool, description=("Activate MPI based functionality. " "Requires the mpi4py module."), doc=None, visibility=0)) safe_register_unique_option( options, "import_module", PySPConfigValue( (), domain=_domain_tuple_of_str, description=( "The name of a user-defined python module to import that, " "e.g., registers a user-defined scenario tree worker class."), doc=None, visibility=0)) return options
def convertschuripopt_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "verbose") safe_register_common_option(options, "symbolic_solver_labels") safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=("The directory in which all SchurIpopt files " "will be stored. Default is '.'."), doc=None, visibility=0)) safe_register_unique_option( options, "ignore_bundles", PySPConfigValue( False, domain=bool, description=("Ignore bundles when converting the SP to " "SchurIpopt input files."), doc=None, visibility=0)) safe_register_common_option(options, "scenario_tree_manager") ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
def runadmm_register_options(options=None): if options is None: options = PySPConfigBlock() ADMMSolver.register_options(options) ScenarioTreeManagerFactory.register_options(options) safe_register_common_option(options, "verbose") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "output_solver_log") safe_register_common_option(options, "output_scenario_tree_solution") safe_register_unique_option( options, "default_rho", PySPConfigValue( 1.0, domain=_domain_positive, description=("The default rho value for all non-anticipative " "variables. Default is 1.0."), doc=None, visibility=0), ap_args=("-r", "--default-rho"), ap_group=_admm_group_label) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() # # solve and I/O related # safe_declare_common_option(options, "symbolic_solver_labels") safe_declare_common_option(options, "solver_options") safe_declare_common_option(options, "solver") safe_declare_common_option(options, "solver_io") safe_declare_common_option(options, "solver_manager") safe_declare_common_option(options, "disable_warmstart") safe_declare_common_option(options, "disable_advanced_preprocessing") safe_declare_common_option(options, "output_solver_log") safe_declare_common_option(options, "keep_solver_files") safe_declare_common_option(options, "comparison_tolerance_for_fixed_variables") return options
def runddsip_register_options(options=None): if options is None: options = PySPConfigBlock() DDSIPSolver.register_options(options) ScenarioTreeManagerFactory.register_options(options) safe_register_common_option(options, "verbose") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "output_scenario_tree_solution") safe_register_common_option(options, "keep_solver_files") safe_register_common_option(options, "output_solver_log") safe_register_common_option(options, "symbolic_solver_labels") # used to populate the implicit DDSIP options safe_register_unique_option( options, "solver_options", PySPConfigValue( (), domain=_domain_tuple_of_str_or_dict, description=("Unregistered solver options that will be passed " "to DDSIP via the config file (e.g., NODELIM=4, " "CPLEX_1067=1). This option can be used multiple " "times from the command line to specify more " "than one DDSIP option."), doc=None, visibility=0), ap_kwds={'action': 'append'}, ap_group=_ddsip_group_label) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "input_name") safe_declare_common_option(options, "load_stages") return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "verbose", ap_group=_admm_group_label) ScenarioTreeManagerSolverFactory.register_options( options, options_prefix="subproblem_", setup_argparse=False) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "preprocess_fixed_variables") safe_declare_common_option(options, "symbolic_solver_labels") safe_declare_common_option(options, "output_times") safe_declare_common_option(options, "verbose") return options
def main(args=None): # # Top-level command that executes everything # # # Import plugins # import pyomo.environ # # Parse command-line options. # options = PySPConfigBlock() convertddsip_register_options(options) # # Prevent the compile_scenario_instances option from # appearing on the command line. This script relies on # the original constraints being present on the model # argparse_val = options.get('compile_scenario_instances')._argparse options.get('compile_scenario_instances')._argparse = None try: ap = argparse.ArgumentParser(prog='pysp.convert.ddsip') options.initialize_argparse(ap) # restore the option so the class validation does not # raise an exception options.get('compile_scenario_instances')._argparse = argparse_val options.import_argparse(ap.parse_args(args=args)) except SystemExit as _exc: # the parser throws a system exit if "-h" is specified # - catch it to exit gracefully. return _exc.code return launch_command(run_convertddsip, options, error_label="pysp.convert.ddsip: ", disable_gc=options.disable_gc, profile_count=options.profile, traceback=options.traceback)
def runsd_register_options(options=None): if options is None: options = PySPConfigBlock() SDSolver.register_options(options) ScenarioTreeManagerFactory.register_options(options) safe_register_common_option(options, "verbose") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "output_scenario_tree_solution") safe_register_common_option(options, "keep_solver_files") safe_register_common_option(options, "output_solver_log") safe_register_common_option(options, "symbolic_solver_labels") return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() # options for controlling the solver manager # (not the scenario tree manager) safe_declare_common_option(options, "solver_manager_pyro_host") safe_declare_common_option(options, "solver_manager_pyro_port") safe_declare_common_option(options, "solver_manager_pyro_shutdown") ScenarioTreePreprocessor._declare_options(options) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_common_option(options, "max_iterations", ap_group=_admm_group_label) safe_declare_unique_option( options, "primal_residual_relative_tolerance", PySPConfigValue( 1.0e-4, domain=_domain_positive, description=( "Relative primal-residual tolerance. Default is 1e-4."), doc=None, visibility=0), ap_group=_admm_group_label) safe_declare_unique_option( options, "dual_residual_relative_tolerance", PySPConfigValue( 1.0e-4, domain=_domain_positive, description=( "Relative dual-residual tolerance. Default is 1e-4."), doc=None, visibility=0), ap_group=_admm_group_label) ADMMAlgorithm._declare_options(options) for rstype in RhoStrategyFactory.registered_types.values(): rstype._declare_options(options) assert 'adaptive' in RhoStrategyFactory.registered_types safe_declare_unique_option( options, "rho_strategy", PySPConfigValue( 'adaptive', domain=_domain_must_be_str, description= ("Rho update strategy. Choices are: %s. Default is 'adaptive'." % (str(sorted(RhoStrategyFactory.registered_types.keys())))), doc=None, visibility=0), ap_group=_admm_group_label) return options
def _register_scenario_tree_manager_solver_options(*args, **kwds): if len(args) == 0: options = PySPConfigBlock() else: if len(args) != 1: raise TypeError( "register_options(...) takes at most 1 argument (%s given)" % (len(args))) options = args[0] if not isinstance(options, PySPConfigBlock): raise TypeError( "register_options(...) argument must be of type PySPConfigBlock, " "not %s" % (type(options).__name__)) ScenarioTreeManagerSolverClientSerial.register_options(options, **kwds) ScenarioTreeManagerSolverClientPyro.register_options(options, **kwds) return options
def run_generate_distributed_NL_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "scenario_tree_manager") safe_register_common_option(options, "symbolic_solver_labels") safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=("The directory in which to store all output files. " "Default is '.'."), doc=None, visibility=0)) safe_register_unique_option( options, "linking_suffix_name", PySPConfigValue( "variable_id", domain=_domain_must_be_str, description=("The suffix name used to identify common variables " "across NL files. Default is 'ipopt_blend_id'."), doc=None, visibility=0)) safe_register_unique_option( options, "objective_suffix_name", PySPConfigValue( "objective_weight", domain=_domain_must_be_str, description=("The suffix name used to identify the relative " "objective weight for each NL-file subproblem." "Default is 'ipopt_blend_weight'."), doc=None, visibility=0)) ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
def run_evaluate_xhat_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "scenario_tree_manager") safe_register_common_option(options, "output_scenario_tree_solution") safe_register_common_option(options, "solution_saver_extension") safe_register_common_option(options, "solution_loader_extension") safe_register_unique_option( options, "disable_solution_loader_check", PySPConfigValue( False, domain=bool, description=( "Indicates that no solution loader extension is required to " "run this script, e.g., because the scenario tree manager " "is somehow pre-populated with a solution."), doc=None, visibility=0), ap_group=_extension_options_group_title) safe_register_unique_option( options, "output_scenario_costs", PySPConfigValue( None, domain=_domain_must_be_str, description= ("A file name where individual scenario costs from the solution " "will be stored. The format is determined from the extension used " "in the filename. Recognized extensions: [.csv, .json, .yaml]"), doc=None, visibility=0)) ScenarioTreeManagerFactory.register_options(options) ScenarioTreeManagerSolverFactory.register_options( options, options_prefix="subproblem_") return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() # # scenario instance construction # safe_declare_common_option(options, "objective_sense_stage_based") safe_declare_common_option(options, "output_instance_construction_time") safe_declare_common_option(options, "compile_scenario_instances") # # various # safe_declare_common_option(options, "verbose") safe_declare_common_option(options, "profile_memory") return options
def generate_scenario_tree_image_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_unique_option( options, "output_file", PySPConfigValue( "ScenarioStructure.pdf", domain=_domain_must_be_str, description=( "The name of the file in which to store the scenario " "tree image. Default is ScenarioStructure.pdf." ), doc=None, visibility=0)) safe_register_common_option(options, "verbose") safe_register_common_option(options, "model_location") safe_register_common_option(options, "scenario_tree_location") safe_register_common_option(options, "scenario_tree_random_seed") safe_register_common_option(options, "scenario_tree_downsample_fraction") safe_register_common_option(options, "scenario_bundle_specification") safe_register_common_option(options, "create_random_bundles") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_unique_option( options, "firststage_suffix", PySPConfigValue( "__DDSIP_FIRSTSTAGE", domain=_domain_must_be_str, description=("The suffix used to identity first-stage " "variables to DDSIP. Default is " "'__DDSIP_FIRSTSTAGE'"), doc=None, visibility=0), ap_group=_ddsip_group_label) safe_declare_unique_option( options, "config_file", PySPConfigValue( None, domain=_domain_must_be_str, description=( "The name of a partial DDSIP configuration file " "that contains option specifications unrelated to " "the problem structure. If specified, the contents " "of this file will be appended to the " "configuration created by this solver interface. " "Default is None."), doc=None, visibility=0), ap_group=_ddsip_group_label) safe_declare_common_option(options, "verbose", ap_group=_ddsip_group_label) return options
def test_init(self): PySPConfigBlock()
def runef_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "verbose") safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "output_scenario_tree_solution") safe_register_common_option(options, "solution_saver_extension") safe_register_common_option(options, "solution_loader_extension") safe_register_unique_option( options, "solution_writer", PySPConfigValue( (), domain=_domain_tuple_of_str, description=( "The name of a python module specifying a user-defined " "plugin implementing the ISolutionWriterExtension " "interface. Invoked to save a scenario tree solution. Use " "this option when generating a template configuration file " "or invoking command-line help in order to include any " "plugin-specific options. This option can used multiple " "times from the command line to specify more than one plugin." ), doc=None, visibility=0), ap_kwds={'action': 'append'}, ap_group=_extension_options_group_title) safe_register_unique_option( options, "output_file", PySPConfigValue( "efout", domain=_domain_must_be_str, description=( "The name of the extensive form output file " "(currently LP, MPS, and NL file formats are " "supported). If the option value does not end " "in '.lp', '.mps', or '.nl', then the output format " "will be inferred from the settings for the --solver " "and --solver-io options, and the appropriate suffix " "will be appended to the name. Default is 'efout'." ), doc=None, visibility=0), ap_group=_output_options_group_title) safe_register_unique_option( options, "solve", PySPConfigValue( False, domain=bool, description=( "Solve the extensive form model. Default is " "False, which implies that the EF will be " "saved to a file." ), doc=None, visibility=0)) safe_register_unique_option( options, "output_scenario_costs", PySPConfigValue( None, domain=_domain_must_be_str, description=( "A file name where individual scenario costs from the solution " "will be stored. The format is determined from the extension used " "in the filename. Recognized extensions: [.csv, .json, .yaml]" ), doc=None, visibility=0)) ScenarioTreeManagerClientSerial.register_options(options) ExtensiveFormAlgorithm.register_options(options) # # Deprecated # # this will cause the deprecated "shutdown_pyro" version # to appear safe_register_common_option(options, "pyro_shutdown") # this will cause the deprecated "shutdown_pyro_workers" # version to appear safe_register_common_option(options, "pyro_shutdown_workers") class _DeprecatedActivateJSONIOSolutionSaver(argparse.Action): def __init__(self, option_strings, dest, nargs=None, **kwargs): if nargs is not None: raise ValueError("nargs not allowed") super(_DeprecatedActivateJSONIOSolutionSaver, self).\ __init__(option_strings, dest, nargs=0, **kwargs) def __call__(self, parser, namespace, values, option_string=None): logger.warning( "DEPRECATED: The '--activate-json-io-solution-saver " "command-line option has been deprecated and will " "be removed in the future. Please the following instead: " "'----solution-saver-extension=pysp.plugins.jsonio'") val = getattr(namespace, 'CONFIGBLOCK.solution_saver_extension', []) setattr(namespace, 'CONFIGBLOCK.solution_saver_extension', val + ["pysp.plugins.jsonio"]) def _warn_activate_jsonio_solution_saver(val): # don't use logger here since users might not import # the pyomo logger in a scripting interface sys.stderr.write( "\tWARNING: The 'activate_jsonio_solution_saver' " "config item will be ignored " "unless it is being used as a command-line option " "where it can be redirected to 'solution_saver_extension'. " "Please use 'solution_saver_extension=pysp.plugins.jsonio' " "instead.\n") return _domain_tuple_of_str(val) safe_declare_unique_option( options, "activate_jsonio_solution_saver", PySPConfigValue( None, domain=_warn_activate_jsonio_solution_saver, description=( "Deprecated alias for " "--solution-saver-extension=pysp.plugins.jsonio" ), doc=None, visibility=1), ap_kwds={'action':_DeprecatedActivateJSONIOSolutionSaver}, ap_group=_deprecated_options_group_title, declare_for_argparse=True) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() safe_declare_unique_option( options, "cvar_weight", PySPConfigValue( 1.0, domain=_domain_nonnegative, description=( "The weight associated with the CVaR term in " "the risk-weighted objective " "formulation. If the weight is 0, then " "*only* a non-weighted CVaR cost will appear " "in the EF objective - the expected cost " "component will be dropped. Default is 1.0." ), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_unique_option( options, "generate_weighted_cvar", PySPConfigValue( False, domain=bool, description=( "Add a weighted CVaR term to the " "primary objective. Default is False." ), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_unique_option( options, "risk_alpha", PySPConfigValue( 0.95, domain=_domain_unit_interval, description=( "The probability threshold associated with " "CVaR (or any future) risk-oriented " "performance metrics. Default is 0.95." ), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_unique_option( options, "cc_alpha", PySPConfigValue( 0.0, domain=_domain_unit_interval, description=( "The probability threshold associated with a " "chance constraint. The RHS will be one " "minus this value. Default is 0." ), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_unique_option( options, "cc_indicator_var", PySPConfigValue( None, domain=_domain_must_be_str, description=( "The name of the binary variable to be used " "to construct a chance constraint. Default " "is None, which indicates no chance " "constraint." ), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_unique_option( options, "mipgap", PySPConfigValue( None, domain=_domain_unit_interval, description=( "Specifies the mipgap for the EF solve." ), doc=None, visibility=0), ap_group=_ef_group_label) safe_declare_common_option(options, "solver") safe_declare_common_option(options, "solver_io") safe_declare_common_option(options, "solver_manager") safe_declare_common_option(options, "solver_options") safe_declare_common_option(options, "disable_warmstart") safe_declare_common_option(options, "pyro_host") safe_declare_common_option(options, "pyro_port") safe_declare_common_option(options, "pyro_shutdown") safe_declare_common_option(options, "verbose", ap_group=_ef_group_label) safe_declare_common_option(options, "output_times", ap_group=_ef_group_label) safe_declare_common_option(options, "output_solver_results", ap_group=_ef_group_label) safe_declare_common_option(options, "symbolic_solver_labels", ap_group=_ef_group_label) safe_declare_common_option(options, "output_solver_log", ap_group=_ef_group_label) safe_declare_common_option(options, "verbose", ap_group=_ef_group_label) safe_declare_common_option(options, "output_times", ap_group=_ef_group_label) safe_declare_common_option(options, "keep_solver_files", ap_group=_ef_group_label) safe_declare_common_option(options, "output_solver_results", ap_group=_ef_group_label) return options
def convertddsip_register_options(options=None): if options is None: options = PySPConfigBlock() safe_register_common_option(options, "disable_gc") safe_register_common_option(options, "profile") safe_register_common_option(options, "traceback") safe_register_common_option(options, "verbose") safe_register_common_option(options, "symbolic_solver_labels") safe_register_unique_option( options, "output_directory", PySPConfigValue( ".", domain=_domain_must_be_str, description=( "The directory in which all DDSIP files " "will be stored. Default is '.'." ), doc=None, visibility=0)) safe_register_unique_option( options, "first_stage_suffix", PySPConfigValue( "__DDSIP_FIRSTSTAGE", domain=_domain_must_be_str, description=( "The suffix used to identify first-stage variables. " "Default: '__DDSIP_FIRSTSTAGE'" ), doc=None, visibility=0)) safe_register_unique_option( options, "enforce_derived_nonanticipativity", PySPConfigValue( False, domain=bool, description=( "Adds nonanticipativity constraints for variables flagged " "as derived within their respective time stage (except for " "the final time stage). The default behavior behavior is " "to treat derived variables as belonging to the final " "time stage." ), doc=None, visibility=0)) safe_register_unique_option( options, "disable_consistency_checks", PySPConfigValue( False, domain=bool, description=( "Disables consistency checks that attempt to find issues " "with the DDSIP conversion. By default, these checks are run " "after conversion takes place and leave behind a temporary " "directory with per-scenario output files if the checks fail. " "This option is not recommended, but can be used if the " "consistency checks are prohibitively slow." ), doc=None, visibility=0)) safe_register_unique_option( options, "keep_scenario_files", PySPConfigValue( False, domain=bool, description=( "Keeps around the per-scenario DDSIP files created for testing " "whether a conversion is valid (whether or not the validation " "checks are performed). These files can be useful for " "debugging purposes." ), doc=None, visibility=0)) safe_register_common_option(options, "scenario_tree_manager") ScenarioTreeManagerClientSerial.register_options(options) ScenarioTreeManagerClientPyro.register_options(options) return options
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() return options
def register_options(cls, *args, **kwds): """ Updates and/or returns an options block with options registered for this class. Args: *args: A single argument can be provided which should be an existing PySPConfigBlock to add options to. options_prefix (str): A string to prefix the name of all options added by this class. Returns: a PySPConfigBlock with the registered options \ for this class """ options_prefix = kwds.pop('options_prefix', "") assert isinstance(options_prefix, six.string_types) setup_argparse = kwds.pop('setup_argparse', True) if len(kwds) != 0: raise ValueError("Unexpected keywords: %s" % (str(kwds))) if len(args) == 0: options = PySPConfigBlock() else: if len(args) != 1: raise TypeError( "register_options(...) takes at most 1 argument (%s given)" % (len(args))) options = args[0] if not isinstance(options, PySPConfigBlock): raise TypeError("register_options(...) argument must be of " "type PySPConfigBlock, not %s" % (type(options).__name__)) bases = inspect.getmro(cls) assert bases[-1] is object # # Register options in order of most derived class # first. This allows derived classes to update # option default values. The safe_declare_option # function will simply validate that an existing # option matches when a base class attempts to # register an option that is already registered (in # which cases we tell it to accept differences in # default values). # for base in bases: # # The check here is that PySPConfiguredObject # needs to appear as an immediate base in a # class definition for us to assume that class # is declaring options. This allows declared # options to be distributed across different # containers in the class hierarchy, while # leaving the ability for derived classes to NOT # have to define an empty _declared_options # block or dummy _declare_options classmethod if # they don't have any new options to add but are # derived from some class which does use # PySPConfiguredObject as a base class. By not # declaring PySPConfiguredObject as an immediate # base class, we know not to check for a # _declared_options or _declare options in this # derived class's scope (because otherwise we'd # be getting some base class's definition) # if any(_base is PySPConfiguredObject for _base in base.__bases__): # make sure we've removed this older form assert not hasattr(base, "_declared_options") if "_declare_options" in base.__dict__: _declared_options = base._declare_options() for name in _declared_options: configval = _declared_options.get(name) assert configval._parent is _declared_options configval._parent = None declare_for_argparse = False if (setup_argparse) and \ (configval._argparse is None) and \ (options_prefix+name not in options): declare_for_argparse = True safe_declare_option( options, options_prefix + name, configval, relax_default_check=True, declare_for_argparse=declare_for_argparse) configval._parent = _declared_options return options
def test_implicit(self): b = PySPConfigBlock() b._implicit_declaration = True b.name_a = 1 b.nameb = 2 b.display() out = StringIO() b.display(ostream=out) self.assertEqual(out.getvalue(), "name_a: 1\nnameb: 2\n") with self.assertRaises(ValueError): b.check_usage() with self.assertRaises(ValueError): b.check_usage() self.assertEqual(b.check_usage(error=False), False) b.name_a b.nameb self.assertEqual(b.check_usage(), True) self.assertEqual(b.check_usage(error=False), True) name_a_about = \ """ConfigValue: name_a - type: None - default: 1 - doc: None""" self.assertEqual(b.about("name_a"), name_a_about)
def _declare_options(cls, options=None): if options is None: options = PySPConfigBlock() return ExtensiveFormAlgorithm.register_options(options)
def parse_command_line(args, register_options_callback, with_extensions=None, **kwds): import pysp.plugins pysp.plugins.load() from 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
def test_declared(self): b = PySPConfigBlock() safe_register_common_option(b, "verbose") b.display() b.display() out = StringIO() b.display(ostream=out) self.assertEqual(out.getvalue(), "verbose: false\n") self.assertEqual(b.check_usage(), True) self.assertEqual(b.check_usage(error=False), True) b.verbose = True out = StringIO() b.display(ostream=out) self.assertEqual(out.getvalue(), "verbose: true\n") with self.assertRaises(ValueError): b.check_usage() with self.assertRaises(ValueError): b.check_usage() self.assertEqual(b.check_usage(error=False), False) b.verbose self.assertEqual(b.check_usage(), True) self.assertEqual(b.check_usage(error=False), True) verbose_about = \ """PySPConfigValue: verbose - type: <%s 'bool'> - default: False - doc: Generate verbose output for both initialization and execution.""" % ('class' if six.PY3 else 'type') self.assertEqual(b.about("verbose"), verbose_about)