Skip to content

Added get_ip_networks_from_file() convenience function - #7

Open
Script-Nomad wants to merge 3 commits into
zeroSteiner:masterfrom
Script-Nomad:master
Open

Added get_ip_networks_from_file() convenience function #7
Script-Nomad wants to merge 3 commits into
zeroSteiner:masterfrom
Script-Nomad:master

Conversation

@Script-Nomad

@Script-Nomad Script-Nomad commented Oct 23, 2020

Copy link
Copy Markdown
Contributor

Created a little convenience function that I use quite a bit for parsing files of IP addresses and CIDR networks.

Offers a "strict" flag that defaults to True and bubbles errors up when it encounters an invalid IPv4Network in a file. When the strict flag is set to False, it ignores invalid lines and proceeds silently. It is left up to the user to handle the errors encountered.

Thought it might make a useful addition to the library.

This commit iterates the micro version to 2.0.2

Recommend squashing commits to keep the history clean.

Example output run in IDLE:

>>> get_ip_networks_from_file(test_scope, strict=False)
[IPv4Network('10.33.6.0/24'), IPv4Network('10.32.8.0/24'), IPv4Network('10.35.3.0/24'), IPv4Network('10.34.6.0/24'), IPv4Network('10.31.6.0/24'), IPv4Network('10.37.6.0/24')]
>>> with open(test_scope) as fin:
...    print("Example File: \n", fin.read())
... 
Example File: 
Not an IP Address
10.33.6.0/24
10.32.8.0/24
10.35.3.0/24
10.34.6.0/24
10.31.6.0/24
10.37.6.0/24

>>> get_ip_networks_from_file(test_scope)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 17, in get_ip_networks_from_file
  File "<stdin>", line 14, in get_ip_networks_from_file
  File "/usr/lib/python3.6/ipaddress.py", line 84, in ip_network
    address)
ValueError: 'Not an IP Address' does not appear to be an IPv4 or IPv6 network
>>> get_ip_networks_from_file(test_scope, strict=False)
[IPv4Network('10.33.6.0/24'), IPv4Network('10.32.8.0/24'), IPv4Network('10.35.3.0/24'), IPv4Network('10.34.6.0/24'), IPv4Network('10.31.6.0/24'), IPv4Network('10.37.6.0/24')]

@zeroSteiner zeroSteiner left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you also be able to add a unit test for this? You can write a file to /tmp if you'd like.

Comment thread smoke_zephyr/__init__.py

# Semantic Versioning: http://semver.org/spec/v2.0.0.html
version_info = collections.namedtuple('version_info', ['major', 'minor', 'micro'])(2, 0, 1)
version_info = collections.namedtuple('version_info', ['major', 'minor', 'micro'])(2, 0, 2)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a feature we should be bumping the minor version not the patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants