Skip to content

Commit

Permalink
Support singular form in uptime string (#6)
Browse files Browse the repository at this point in the history
Sometimes the uptime is only "1 something", so we need the regex to
accept those values.
  • Loading branch information
inetAnt authored Sep 9, 2021
1 parent 37ef8c2 commit 830b95b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions napalm_servertech_pro2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
def convert_uptime(uptime):
"""Converts a ServerTech uptime string to a number of seconds."""
time_regex = (
r"^(?P<days>\d+) days (?P<hours>\d+) hours"
r" (?P<minutes>\d+) minutes (?P<seconds>\d+) seconds$"
r"^(?P<days>\d+) days? (?P<hours>\d+) hours?"
r" (?P<minutes>\d+) minutes? (?P<seconds>\d+) seconds?$"
)
m = re.match(
time_regex,
Expand Down
5 changes: 4 additions & 1 deletion tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ def test_convert_uptime():
uptime = "11 days 4 hours 8 minutes 6 seconds"
assert utils.convert_uptime(uptime) == 965286

uptime = "0 days 0 hours 0 minutes 1 seconds"
uptime = "133 days 1 hour 12 minutes 15 seconds"
assert isinstance(utils.convert_uptime(uptime), int)

uptime = "0 days 0 hours 0 minutes 1 second"
assert utils.convert_uptime(uptime) == 1

with pytest.raises(ValueError):
Expand Down

0 comments on commit 830b95b

Please sign in to comment.