making davids time string parser work for iosxr too
This commit is contained in:
1 parent
19ee233187
commit
c9d5167fba
2 files changed
+39
-34
No files matched your search
+2
-33
@@ -18,7 +18,7 @@ from pyFG.exceptions import FailedCommit, CommandExecutionException
|
||||
from base import NetworkDriver
|
||||
from exceptions import ReplaceConfigException, MergeConfigException
|
||||
|
||||
from utils.string_parsers import colon_separated_string_to_dict
|
||||
from utils.string_parsers import colon_separated_string_to_dict, convert_uptime_string_seconds
|
||||
|
||||
import re
|
||||
|
||||
@@ -37,37 +37,6 @@ def execute_get(device, cmd, separator=':', auto=False):
|
||||
return colon_separated_string_to_dict('\n'.join(output), separator)
|
||||
|
||||
|
||||
def convert_uptime_string_seconds(uptime):
|
||||
regex_1 = re.compile(r"((?P<days>\d+) days,\s+)?((?P<hours>\d+) hours,\s+)?((?P<minutes>\d+) minutes)")
|
||||
regex_2 = re.compile(r"((?P<hours>\d+)):((?P<minutes>\d+)):((?P<seconds>\d+))")
|
||||
|
||||
regex_list = [regex_1, regex_2]
|
||||
|
||||
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 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
|
||||
|
||||
|
||||
class FortiOSDriver(NetworkDriver):
|
||||
|
||||
def __init__(self, hostname, username, password):
|
||||
@@ -239,4 +208,4 @@ class FortiOSDriver(NetworkDriver):
|
||||
'router_id': bgp_sum[0].split()[3],
|
||||
'peers': peers
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
-1
@@ -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
|
||||
|
||||
Reference in new issue
Block a user