diff --git a/README.rst b/README.rst index 687e044fc7d08b18e180dc6899364bb89fc48bf3..367be69e46712496d45724aae89aa12b173e7c8a 100644 --- a/README.rst +++ b/README.rst @@ -95,6 +95,14 @@ The following Jinja filters were added: - :code:`run`: Runs a command and returns the stdout, stderr and returncode using `run <https://docs.python.org/3.6/library/subprocess.html?highlight=popen#subprocess.run>`_. +- :code:`ipaddress`: Returns an IPAddress object from the netaddr_ library + (requires the :code:`netaddr` package specifier). +- :code:`ipnetwork`: Returns an IPNetwork object from the netaddr_ library + (requires the :code:`netaddr` package specifier). +- :code:`iprange`: Returns an IPRange object from the netaddr_ library. + (requires the :code:`netaddr` package specifier). +- :code:`ipglob`: Returns an IPGlob object from the netaddr_ library (requires + the :code:`netaddr` package specifier). Example usage can be seen in :code:`tests` and for specific filters in the docstrings in :code:`template/filters.py`. @@ -141,4 +149,5 @@ Nimrod Adar, `contact me <nimrod@shore.co.il>`_ or visit my `website <http://git-scm.com/book/en/v2/Git-Commands-Email>`_. The repository is located at: https://git.shore.co.il/nimrod/. +.. _netaddr: https://netaddr.readthedocs.io/ .. _Pipenv: https://docs.pipenv.org diff --git a/setup.py b/setup.py index 8df02bf65499823d2902abffc738f17fee7598c8..07e2fa75bde978e99bbdc9f81479fa16e3ae71b3 100644 --- a/setup.py +++ b/setup.py @@ -42,9 +42,15 @@ setup( "subprocess32>=3.5.0;python_version<'3.5'", ], extras_require={ - "all": ["PyYAML", "jmespath", "toml"], + "all": [ + "jmespath", + "netaddr", + "PyYAML", + "toml", + ], "dev": ["pipenv"], "jmespath": ["jmespath"], + "netaddr": ["netaddr"], "toml": ["toml"], "yaml": ["PyYAML"], }, diff --git a/template/filters.py b/template/filters.py index c18addca22516f37d0dcf2da7d0e630b091b3992..93bd67629ca876acf34d81872a25328eab5a5f74 100644 --- a/template/filters.py +++ b/template/filters.py @@ -173,3 +173,83 @@ def run(*argv, **kwargs): proc["stdout"] = proc["stdout"].decode() proc["stderr"] = proc["stderr"].decode() return proc + + +def ipaddress(addr, version=None, flags=0): + """ + Returns an IPAddress object from the netaddr library. + + >>> ip = ipaddress('10.0.0.1') + >>> type(ip) + <class 'netaddr.ip.IPAddress'> + >>> ip.is_private() + True + """ + + from netaddr import IPAddress + + return IPAddress(addr, version, flags) + + +def ipglob(glob): + """ + Returns an IPGlob object from the netaddr library. + + >>> glob = ipglob("192.168.32.*") + >>> type(glob) + <class 'netaddr.ip.glob.IPGlob'> + >>> glob.is_private() + True + """ + + from netaddr import IPGlob + + return IPGlob(glob) + + +def ipnetwork(addr, implicit_prefix=False, version=None, flags=0): + """ + Returns an IPNetwork object from the netaddr library. + + >>> net = ipnetwork("172.32.0.0/24") + >>> type(net) + <class 'netaddr.ip.IPNetwork'> + >>> net.is_private() + False + """ + + from netaddr import IPNetwork + + return IPNetwork(addr, implicit_prefix, version, flags) + + +def iprange(start, end, flags=0): + """ + Returns an IPRange object from the netaddr library. + + >>> range = iprange("1.2.3.0", "1.2.3.255") + >>> type(range) + <class 'netaddr.ip.IPRange'> + >>> len(range) + 256 + """ + + from netaddr import IPRange + + return IPRange(start, end, flags) + + +def ipset(iterable=None, flags=0): + """ + Returns an IPSet object from the netaddr library. + + >>> ipset = ipset(["10.0.0.0/16"]) + >>> type(ipset) + <class 'netaddr.ip.sets.IPSet'> + >>> ipaddress('10.0.0.1') in ipset + True + """ + + from netaddr import IPSet + + return IPSet(iterable, flags)