Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_colour_validation_invalid(config):
config.write("color = 'on_weekends_only'\n", 'a')
with patch(
'todoman.configuration.find_config',
return_value=(str(config)),
), pytest.raises(ConfigurationException):
load_config()
def init_config(self):
try:
self.config = load_config()
except ConfigurationException as e:
raise click.ClickException(e.args[0])
def validate_date_format(fmt):
if any(x in fmt for x in ('%H', '%M', '%S', '%X')):
raise ConfigurationException(
'Found time component in `date_format`, please use `time_format` '
'for that.'
)
return fmt
def load_config(custom_path=None):
path = find_config(custom_path)
specpath = os.path.join(os.path.dirname(__file__), 'confspec.ini')
validator = Validator({
'expand_path': expand_path,
'cache_path': validate_cache_path,
'date_format': validate_date_format,
'time_format': validate_time_format,
})
config = ConfigObj(path, configspec=specpath, file_error=True)
validation = config.validate(validator, preserve_errors=True)
for section, key, error in flatten_errors(config, validation):
if not error:
raise ConfigurationException((
'{} is missing from the {} section of the configuration ' +
'file'
).format(key, section))
if isinstance(error, VdtValueError):
raise ConfigurationException(
'Bad {} setting, {}'.format(key, error.args[0])
)
return config
def find_config(custom_path=None):
if custom_path:
if not exists(custom_path):
raise ConfigurationException(
"Configuration file {} does not exist".format(custom_path)
)
return custom_path
for d in xdg.BaseDirectory.xdg_config_dirs:
path = join(d, 'todoman', 'todoman.conf')
if exists(path):
return path
raise ConfigurationException("No configuration file found.\n\n")
def find_config(custom_path=None):
if custom_path:
if not exists(custom_path):
raise ConfigurationException(
"Configuration file {} does not exist".format(custom_path)
)
return custom_path
for d in xdg.BaseDirectory.xdg_config_dirs:
path = join(d, 'todoman', 'todoman.conf')
if exists(path):
return path
raise ConfigurationException("No configuration file found.\n\n")
def validate_time_format(fmt):
if any(x in fmt for x in ('%Y', '%y', '%m', '%d', '%x')):
raise ConfigurationException(
'Found date component in `time_format`, please use `date_format` '
'for that.'
)
return fmt