From b4f032b3e0a239d2bcf8342c57c9f951eee22d21 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Wed, 14 Apr 2021 18:31:45 +0300
Subject: [PATCH] netaddr filters.

A bunch of filters that just wrap objects from the netaddr library.
---
 README.rst          |  9 +++++
 setup.py            |  8 ++++-
 template/filters.py | 80 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/README.rst b/README.rst
index 687e044..367be69 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 8df02bf..07e2fa7 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 c18addc..93bd676 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)
-- 
GitLab