Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_optional_interactive_fields_blanks(self, field_name, field_count):
"""
Check that blank user input for an optional field is handled correctly
"""
default_value = DEFAULTS.get(field_name)
blank_return_value = '' if field_count == 'single' else []
with patch('rsmtool.utils.commandline.prompt', return_value=blank_return_value):
ifield = InteractiveField(field_name,
'optional',
{'label': 'optional field label'})
eq_(ifield.get_value(), default_value)
def _get_all_general_section_names(self):
reporter = Reporter()
default_general_sections_value = DEFAULTS.get('general_sections', '')
default_special_sections_value = DEFAULTS.get('special_sections', '')
default_custom_sections_value = DEFAULTS.get('custom_sections', '')
# if we are told ot use subgroups then just make up a dummy subgroup
# value so that the subgroup-based sections will be included in the
# section list. This value is not actually used in configuration file.
subgroups_value = ['GROUP'] if self.use_subgroups else DEFAULTS.get('subgroups', '')
return reporter.determine_chosen_sections(default_general_sections_value,
default_special_sections_value,
default_custom_sections_value,
subgroups_value,
context=self.context)
def _get_all_general_section_names(self):
reporter = Reporter()
default_general_sections_value = DEFAULTS.get('general_sections', '')
default_special_sections_value = DEFAULTS.get('special_sections', '')
default_custom_sections_value = DEFAULTS.get('custom_sections', '')
# if we are told ot use subgroups then just make up a dummy subgroup
# value so that the subgroup-based sections will be included in the
# section list. This value is not actually used in configuration file.
subgroups_value = ['GROUP'] if self.use_subgroups else DEFAULTS.get('subgroups', '')
return reporter.determine_chosen_sections(default_general_sections_value,
default_special_sections_value,
default_custom_sections_value,
subgroups_value,
context=self.context)
def _get_all_general_section_names(self):
reporter = Reporter()
default_general_sections_value = DEFAULTS.get('general_sections', '')
default_special_sections_value = DEFAULTS.get('special_sections', '')
default_custom_sections_value = DEFAULTS.get('custom_sections', '')
# if we are told ot use subgroups then just make up a dummy subgroup
# value so that the subgroup-based sections will be included in the
# section list. This value is not actually used in configuration file.
subgroups_value = ['GROUP'] if self.use_subgroups else DEFAULTS.get('subgroups', '')
return reporter.determine_chosen_sections(default_general_sections_value,
default_special_sections_value,
default_custom_sections_value,
subgroups_value,
context=self.context)
"""
Private method that takes the provided user input
and converts it to the appropriate type.
Parameters
----------
user_input : TYPE
Description
Returns
-------
TYPE
Description
"""
if (user_input == '' or user_input == []) and self.field_type == 'optional':
final_value = DEFAULTS.get(self.field_name)
else:
# boolean fields need to be converted to actual booleans
if self.data_type == 'boolean':
final_value = False if user_input == 'false' else True
# and integer fields to integers/None
elif self.data_type == 'integer':
final_value = int(user_input)
else:
final_value = user_input
return final_value
# iterate over the required fields first, and then the (sorted) optional fields
# keep track of which field type we are currently dealing with
for field_type, field_name in chain(product(['required'], self._required_fields),
product(['optional'], self._optional_fields)):
# skip the subgroups field unless we were told to use subgroups
if field_name == 'subgroups' and not self.use_subgroups:
configdict['subgroups'] = DEFAULTS.get('subgroups')
continue
# if the field is not one that is meant to be filled interactively,
# then just use its default value; for "general_sections", expand it
# so that it is easy for the user to remove sections
if field_name not in INTERACTIVE_MODE_METADATA:
non_interactive_field_value = DEFAULTS.get(field_name, '')
if field_name == 'general_sections':
non_interactive_field_value = self._get_all_general_section_names()
configdict[field_name] = non_interactive_field_value
else:
# instantiate the interactive field first
try:
interactive_field = InteractiveField(field_name,
field_type,
INTERACTIVE_MODE_METADATA[field_name])
configdict[field_name] = interactive_field.get_value()
# if the user pressed Ctrl-D, then exit out of interactive mode
# without generating anything and return an empty string
except EOFError:
sys.stderr.write("\n")
sys.stderr.write("You exited interactive mode without a configuration.")
sys.stderr.write("\n")
# insert the required fields first and give them a dummy value
for required_field in self._required_fields:
configdict[required_field] = 'ENTER_VALUE_HERE'
# insert the optional fields in alphabetical order
for optional_field in self._optional_fields:
# to make it easy for users to add/remove sections, we should
# populate the `general_sections` field with an explicit list
# instead of the default value which is simply ``['all']``. To
# do this, we can use the reporter class.
if optional_field == 'general_sections':
configdict['general_sections'] = self._get_all_general_section_names()
else:
configdict[optional_field] = DEFAULTS.get(optional_field, '')
# create a Configuration object
config_object = Configuration(configdict,
configdir=os.getcwd(),
context=self.context)
# if we were asked for string output, then convert this dictionary to
# a string that will also insert some useful comments
if self.as_string:
configuration = self._convert_to_string(config_object)
# otherwise we just return the dictionary underlying the Configuration object
else:
configuration = config_object._config
# print out a warning to make it clear that it cannot be used as is
if not self.suppress_warnings:
def _get_all_general_section_names(self):
reporter = Reporter()
default_general_sections_value = DEFAULTS.get('general_sections', '')
default_special_sections_value = DEFAULTS.get('special_sections', '')
default_custom_sections_value = DEFAULTS.get('custom_sections', '')
# if we are told ot use subgroups then just make up a dummy subgroup
# value so that the subgroup-based sections will be included in the
# section list. This value is not actually used in configuration file.
subgroups_value = ['GROUP'] if self.use_subgroups else DEFAULTS.get('subgroups', '')
return reporter.determine_chosen_sections(default_general_sections_value,
default_special_sections_value,
default_custom_sections_value,
subgroups_value,
context=self.context)