making davids time string parser work for iosxr too

This commit is contained in:
Elisa Jasinska committed 2015-05-23 23:05:53 +02:00
1 parent 19ee233187
commit c9d5167fba
2 files changed
+39 -34

No files matched your search

+37 -1
View File
@@ -39,7 +39,6 @@ def colon_separated_string_to_dict(string, separator=':'):
return dictionary
def hyphen_range(string):
'''
Expands a string of numbers separated by commas and hyphens into a list of integers.
@@ -60,3 +59,40 @@ def hyphen_range(string):
raise Exception('Something went wrong expanding the range'.format(string))
return list_numbers
def convert_uptime_string_seconds(uptime):
'''
Convert uptime strings to seconds. The string can be formatted various ways, eg.
1 hour, 56 minutes
'''
regex_1 = re.compile(r"((?P<days>\d+) day(s)?,\s+)?((?P<hours>\d+) hour(s)?,\s+)?((?P<minutes>\d+) minute(s)?)")
regex_2 = re.compile(r"((?P<hours>\d+)):((?P<minutes>\d+)):((?P<seconds>\d+))")
regex_list = [regex_1, regex_2]
# say whaaaat!?
uptime_dict = dict()
for regex in regex_list:
uptime_dict = regex.match(uptime)
if uptime_dict is not None:
uptime_dict = uptime_dict.groupdict()
break
uptime_dict = dict()
uptime_seconds = 0
for unit, value in uptime_dict.iteritems():
if value != None:
if unit == 'days':
uptime_seconds += int(value) * 86400
elif unit == 'hours':
uptime_seconds += int(value) * 3600
elif unit == 'minutes':
uptime_seconds += int(value) * 60
elif unit == 'seconds':
uptime_seconds += int(value)
else:
raise Exception('Unrecognized unit "{}" in uptime:{}'.format(unit, uptime))
return uptime_seconds