Skip to content

Commit

Permalink
Add ability to suppress warnings for generate_configuration().
Browse files Browse the repository at this point in the history
- Hook this up to a `--quiet` / `-q` flag for each command line tool.
- Add new tests.
  • Loading branch information
desilinguist committed Mar 13, 2020
1 parent ff642a5 commit cf5cc36
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 35 deletions.
3 changes: 2 additions & 1 deletion rsmtool/rsmcompare.py
Expand Up @@ -225,7 +225,8 @@ def main():
# auto-generate an example configuration and print it to STDOUT
configuration = generate_configuration('rsmcompare',
use_subgroups=args.subgroups,
as_string=True)
as_string=True,
suppress_warnings=args.quiet)
print(configuration)


Expand Down
3 changes: 2 additions & 1 deletion rsmtool/rsmeval.py
Expand Up @@ -270,7 +270,8 @@ def main():
# auto-generate an example configuration and print it to STDOUT
configuration = generate_configuration('rsmeval',
use_subgroups=args.subgroups,
as_string=True)
as_string=True,
suppress_warnings=args.quiet)
print(configuration)


Expand Down
4 changes: 3 additions & 1 deletion rsmtool/rsmpredict.py
Expand Up @@ -289,7 +289,9 @@ def main():
logging.root.addHandler(stderr_handler)

# auto-generate an example configuration and print it to STDOUT
configuration = generate_configuration('rsmpredict', as_string=True)
configuration = generate_configuration('rsmpredict',
as_string=True,
suppress_warnings=args.quiet)
print(configuration)


Expand Down
4 changes: 3 additions & 1 deletion rsmtool/rsmsummarize.py
Expand Up @@ -277,7 +277,9 @@ def main():
logging.root.addHandler(stderr_handler)

# auto-generate an example configuration and print it to STDOUT
configuration = generate_configuration('rsmsummarize', as_string=True)
configuration = generate_configuration('rsmsummarize',
as_string=True,
suppress_warnings=args.quiet)
print(configuration)


Expand Down
3 changes: 2 additions & 1 deletion rsmtool/rsmtool.py
Expand Up @@ -369,7 +369,8 @@ def main():
# auto-generate an example configuration and print it to STDOUT
configuration = generate_configuration('rsmtool',
use_subgroups=args.subgroups,
as_string=True)
as_string=True,
suppress_warnings=args.quiet)
print(configuration)


Expand Down
21 changes: 17 additions & 4 deletions rsmtool/utils/commandline.py
Expand Up @@ -148,6 +148,15 @@ def existing_configuration_file(string):
f"subgroup sections in the general "
f"sections list")

parser_generate.add_argument('-q',
'--quiet',
dest='quiet',
action='store_true',
default=False,
help="if specified, the warning about not "
"using the generated configuration "
"as-is will be suppressed.")

##############################################
# Setting up options for the "run" subparser #
##############################################
Expand Down Expand Up @@ -218,7 +227,8 @@ def existing_configuration_file(string):

def generate_configuration(context,
use_subgroups=False,
as_string=False):
as_string=False,
suppress_warnings=False):
"""
Automatically generate an example configuration file for a given
command-line tool.
Expand All @@ -236,6 +246,8 @@ def generate_configuration(context,
If ``True``, return a formatted and indented string representation
of the configuration, rather than a ``Configuration`` object.
Defaults to ``False``.
suppress_warnings : bool, optional
If ``True``, do not generate any warnings.
Returns
-------
Expand Down Expand Up @@ -316,9 +328,10 @@ def generate_configuration(context,
configuration)

# print out a warning to make it clear that it cannot be used as is
logger.warning("Automatically generated configuration files MUST "
"be edited to add values for required fields and "
"even for optional ones depending on your data.")
if not suppress_warnings:
logger.warning("Automatically generated configuration files MUST "
"be edited to add values for required fields and "
"even for optional ones depending on your data.")

# return either the Configuration object or the string
return configuration
90 changes: 64 additions & 26 deletions tests/test_utils.py
Expand Up @@ -884,15 +884,15 @@ def test_run_subparser_with_extra_options_bad_required_value(self):
uses_output_directory=False,
extra_run_options=extra_options)

def test_generate_subparser_no_args(self):
def test_generate_subparser_help_flag(self):
"""
test generate subparser with no arguments
test generate subparser with --help specified
"""
parser = setup_rsmcmd_parser('test')
# we need to patch sys.exit since --help just exists otherwise
with patch('sys.exit') as exit_mock:
parsed_namespace = parser.parse_args('generate --help'.split())
expected_namespace = argparse.Namespace(subcommand='generate')
expected_namespace = argparse.Namespace(subcommand='generate', quiet=False)
eq_(parsed_namespace, expected_namespace)
assert exit_mock.called

Expand All @@ -902,7 +902,7 @@ def test_generate_subparser(self):
"""
parser = setup_rsmcmd_parser('test')
parsed_namespace = parser.parse_args('generate'.split())
expected_namespace = argparse.Namespace(subcommand='generate')
expected_namespace = argparse.Namespace(subcommand='generate', quiet=False)
eq_(parsed_namespace, expected_namespace)

def test_generate_subparser_with_subgroups_and_flag(self):
Expand All @@ -911,7 +911,9 @@ def test_generate_subparser_with_subgroups_and_flag(self):
"""
parser = setup_rsmcmd_parser('test', uses_subgroups=True)
parsed_namespace = parser.parse_args('generate --subgroups'.split())
expected_namespace = argparse.Namespace(subcommand='generate', subgroups=True)
expected_namespace = argparse.Namespace(subcommand='generate',
quiet=False,
subgroups=True)
eq_(parsed_namespace, expected_namespace)

def test_generate_subparser_with_subgroups_but_no_flag(self):
Expand All @@ -920,7 +922,30 @@ def test_generate_subparser_with_subgroups_but_no_flag(self):
"""
parser = setup_rsmcmd_parser('test', uses_subgroups=True)
parsed_namespace = parser.parse_args('generate'.split())
expected_namespace = argparse.Namespace(subcommand='generate', subgroups=False)
expected_namespace = argparse.Namespace(subcommand='generate',
quiet=False,
subgroups=False)
eq_(parsed_namespace, expected_namespace)

def test_generate_subparser_with_only_quiet_flag(self):
"""
test generate subparser with no arguments
"""
parser = setup_rsmcmd_parser('test')
parsed_namespace = parser.parse_args('generate --quiet'.split())
expected_namespace = argparse.Namespace(subcommand='generate',
quiet=True)
eq_(parsed_namespace, expected_namespace)

def test_generate_subparser_with_subgroups_and_quiet_flags(self):
"""
test generate subparser with no arguments
"""
parser = setup_rsmcmd_parser('test', uses_subgroups=True)
parsed_namespace = parser.parse_args('generate --subgroups -q'.split())
expected_namespace = argparse.Namespace(subcommand='generate',
quiet=True,
subgroups=True)
eq_(parsed_namespace, expected_namespace)


Expand All @@ -932,9 +957,13 @@ def setUpClass(cls):

# a helper method to check that the automatically generated configuration
# matches what we expect for each tool
def check_generated_configuration(self, name, use_subgroups=False, as_string=False):
def check_generated_configuration(self,
context,
use_subgroups=False,
as_string=False,
suppress_warnings=False):

if name == 'rsmtool':
if context == 'rsmtool':

configdict = {'experiment_id': 'ENTER_VALUE_HERE',
'model': 'ENTER_VALUE_HERE',
Expand Down Expand Up @@ -969,7 +998,7 @@ def check_generated_configuration(self, name, use_subgroups=False, as_string=Fal
'intermediate_file_paths',
'sysinfo']

elif name == 'rsmeval':
elif context == 'rsmeval':

configdict = {'experiment_id': 'ENTER_VALUE_HERE',
'predictions_file': 'ENTER_VALUE_HERE',
Expand All @@ -995,7 +1024,7 @@ def check_generated_configuration(self, name, use_subgroups=False, as_string=Fal
'intermediate_file_paths',
'sysinfo']

elif name == "rsmcompare":
elif context == "rsmcompare":

configdict = {'comparison_id': 'ENTER_VALUE_HERE',
'experiment_id_old': 'ENTER_VALUE_HERE',
Expand Down Expand Up @@ -1030,7 +1059,7 @@ def check_generated_configuration(self, name, use_subgroups=False, as_string=Fal
'notes',
'sysinfo']

elif name == "rsmsummarize":
elif context == "rsmsummarize":

configdict = {'summary_id': 'ENTER_VALUE_HERE',
'experiment_dirs': ['ENTER_VALUE_HERE']}
Expand All @@ -1042,48 +1071,57 @@ def check_generated_configuration(self, name, use_subgroups=False, as_string=Fal
'intermediate_file_paths',
'sysinfo']

elif name == "rsmpredict":
elif context == "rsmpredict":

configdict = {'experiment_id': 'ENTER_VALUE_HERE',
'experiment_dir': 'ENTER_VALUE_HERE',
'input_features_file': 'ENTER_VALUE_HERE'}

# get the generated Configuration object
generated_configuration = generate_configuration(name,
generated_configuration = generate_configuration(context,
use_subgroups=use_subgroups,
as_string=as_string)
as_string=as_string,
suppress_warnings=suppress_warnings)

# if we are testing string output, then load the expected json file
# and compare its contents directly to the returned string, otherwise
# compare the `_config` dictionaries of the two Configuration objects
if as_string:
if use_subgroups:
expected_json_file = join(self.expected_json_dir,
f"autogenerated_{name}_config_groups.json")
f"autogenerated_{context}_config_groups.json")
else:
expected_json_file = join(self.expected_json_dir,
f"autogenerated_{name}_config.json")
f"autogenerated_{context}_config.json")
expected_json_string = open(expected_json_file, 'r').read().strip()
eq_(generated_configuration, expected_json_string)
else:
expected_configuration = Configuration(configdict, context=name)
expected_configuration = Configuration(configdict, context=context)
if 'general_sections' in expected_configuration:
expected_configuration['general_sections'] = section_list

assert_dict_equal(expected_configuration._config,
generated_configuration._config)

def test_generate_configuration(self):
for name, use_subgroups, as_string in product(['rsmtool',
'rsmeval',
'rsmcompare',
'rsmsummarize',
'rsmpredict'],
[True, False],
[True, False]):
for (context,
use_subgroups,
as_string,
suppress_warnings) in product(['rsmtool',
'rsmeval',
'rsmcompare',
'rsmsummarize',
'rsmpredict'],
[True, False],
[True, False],
[True, False]):

# rsmpredict and rsmsummarize do not use subgroups
if name in ['rsmpredict', 'rsmsummarize'] and use_subgroups:
if context in ['rsmpredict', 'rsmsummarize'] and use_subgroups:
continue

yield self.check_generated_configuration, name, use_subgroups, as_string
yield (self.check_generated_configuration,
context,
use_subgroups,
as_string,
suppress_warnings)

0 comments on commit cf5cc36

Please sign in to comment.