forked from networktocode/netutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes networktocode#288 - added hostname_regex function and jinja fi…
…lter
- Loading branch information
1 parent
d95593b
commit 1f0e72e
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Hostname | ||
|
||
::: netutils.hostname | ||
options: | ||
show_submodules: True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import re | ||
|
||
|
||
def hostname_regex(hostname, match_regex_string): | ||
"""Given a hostname string and regex string, return the regex match object or `None` if no match found | ||
This is useful in two primary use cases: | ||
1. Truthy conditional check that a hostname matches a given regex | ||
2. Returning regex capture groups from the hostname string | ||
Args: | ||
hostname: String representation of the device hostname | ||
match_regex_string: Regex string to match against | ||
Returns: | ||
Regex match object or None if no match found | ||
Extamples: | ||
>>> from netutils.hostname import hostname_regex | ||
>>> print("South Carolina" if hostname_regex("USSCAMS07", ".+SC.+\d\d") else "Not South Carolina") | ||
South Carolina | ||
>>> | ||
>>> match = hostname_regex("USSCAMS07", "([A-Z]{2})([A-Z]{2})([A-Z]{3})(\d*)") | ||
>>> match[1] | ||
'US' | ||
>>> match[2] | ||
'SC' | ||
>>> match[3] | ||
'AMS' | ||
>>> match[4] | ||
'07' | ||
""" | ||
return re.match(match_regex_string, hostname) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Test for the Hostname based functions.""" | ||
|
||
from netutils import hostname | ||
|
||
|
||
def test_truthy_happy_path_case(): | ||
match = hostname.hostname_regex("USSCAMS07", ".+SC.+\d\d") | ||
assert bool(match) is True | ||
|
||
|
||
def test_truthy_sad_path_case(): | ||
match = hostname.hostname_regex("USSCAMS07", "foobar") | ||
assert bool(match) is False | ||
|
||
|
||
def test_capture_group_happy_path_case(): | ||
match = hostname.hostname_regex("USSCAMS07", "([A-Z]{2})([A-Z]{2})([A-Z]{3})(\d*)") | ||
assert match[1] == "US" | ||
|
||
|
||
def test_capture_group_sad_path_case(): | ||
match = hostname.hostname_regex("USSCAMS07", "(foobar)") | ||
assert match is None |