Exemple #1
0
 def test_invert_dict_list(self):
     expected = {
         'a': 1,
         'b': 1,
         'c': 1,
         'd': 2,
         'e': 3,
         'f': 3,
     }
     original = {1: ['a', 'b', 'c'], 2: ['d'], 3: ['e', 'f']}
     assert_equal(dicts.invert_dict_list(original), expected)
Exemple #2
0
 def test_invert_dict_list(self):
     expected = {
         'a': 1,
         'b': 1,
         'c': 1,
         'd': 2,
         'e': 3,
         'f': 3,
     }
     original = {
         1: ['a', 'b', 'c'],
         2: ['d'],
         3: ['e', 'f']
     }
     assert_equal(dicts.invert_dict_list(original), expected)
Exemple #3
0
    valid_string(value, config_context)
    for format in ['%H:%M', '%H:%M:%S']:
        try:
            return datetime.datetime.strptime(value, format)
        except ValueError, exc:
            pass

    msg = 'Value at %s is not a valid time: %s'
    raise ConfigError(msg % (config_context.path, exc))


# Translations from possible configuration units to the argument to
# datetime.timedelta
TIME_INTERVAL_UNITS = dicts.invert_dict_list({
    'days':     ['d', 'day', 'days'],
    'hours':    ['h', 'hr', 'hrs', 'hour', 'hours'],
    'minutes':  ['m', 'min', 'mins', 'minute', 'minutes'],
    'seconds':  ['s', 'sec', 'secs', 'second', 'seconds']
})

TIME_INTERVAL_RE = re.compile(r"^\s*(?P<value>\d+)\s*(?P<units>[a-zA-Z]+)\s*$")

def valid_time_delta(value, config_context):
    error_msg = "Value at %s is not a valid time delta: %s"
    matches = TIME_INTERVAL_RE.match(value)
    if not matches:
        raise ConfigError(error_msg % (config_context.path, value))

    units = matches.group('units')
    if units not in TIME_INTERVAL_UNITS:
        raise ConfigError(error_msg % (config_context.path, value))