Commit b4f032b3 authored by nimrod's avatar nimrod
Browse files

netaddr filters.

A bunch of filters that just wrap objects from the netaddr library.
parent 4382b664
Loading
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -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
+7 −1
Original line number Diff line number Diff line
@@ -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"],
    },
+80 −0
Original line number Diff line number Diff line
@@ -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)