Skip to content

Commit b77e11a

Browse files
committed
Revamp documentation.
1 parent 063a0b7 commit b77e11a

6 files changed

Lines changed: 248 additions & 202 deletions

File tree

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@
2525
html_theme = 'nature'
2626
html_static_path = ['_static']
2727
htmlhelp_basename = 'iptoolsdoc'
28+
29+
autodoc_member_order = 'groupwise'

docs/index.rst

Lines changed: 106 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,120 @@
1-
==============================
1+
##############################
22
iptools - IP Address Utilities
3-
==============================
4-
5-
Utilities for dealing with IPv4 addresses.
6-
7-
:Functions:
8-
- :func:`cidr2block`: Convert a CIDR notation ip address into a tuple
9-
containing network block start and end addresses.
10-
- :func:`hex2ip`: Convert a hex encoded network byte order 32-bit
11-
integer to a dotted-quad ip address.
12-
- :func:`ip2hex`: Convert a dotted-quad ip address to a hex encoded
13-
network byte order 32-bit integer.
14-
- :func:`ip2long`: Convert a dotted-quad ip address to a network byte
15-
order 32-bit integer.
16-
- :func:`ip2network`: Convert a dotted-quad ip to base network number.
17-
- :func:`long2ip`: Convert a network byte order 32-bit integer to
18-
a dotted quad ip address.
19-
- :func:`netmask2prefix`: Convert a dotted-quad netmask into a CIDR
20-
prefix.
21-
- :func:`subnet2block`: Convert a dotted-quad ip address including
22-
a netmask into a tuple containing the network block start and end
23-
addresses.
24-
- :func:`validate_cidr`: Validate a CIDR notation ip address.
25-
- :func:`validate_ip`: Validate a dotted-quad ip address.
26-
- :func:`validate_netmask`: Validate that a dotted-quad ip address is
27-
a valid netmask.
28-
- :func:`validate_subnet`: Validate a dotted-quad ip address including
29-
a netmask.
30-
31-
:Objects:
32-
- :class:`IpRange`: Range of ip addresses supporting ``in`` and iteration.
33-
- :class:`IpRangeList`: List of IpRange objects supporting ``in`` and
34-
iteration.
35-
36-
:Constants:
37-
- :mod:`iptools.constants`: Common and special use IPv4 address blocks.
38-
39-
The :class:`IpRangeList` object can be used in a django settings file to
40-
allow CIDR notation and/or (start, end) ranges to be used in the
41-
INTERNAL_IPS list.
42-
43-
**Example**::
44-
45-
INTERNAL_IPS = IpRangeList(
46-
'127.0.0.1',
47-
'192.168/16',
48-
('10.0.0.1', '10.0.0.19'),
49-
)
50-
51-
.. automodule:: iptools
52-
:members:
53-
:exclude-members: IpRange, IpRangeList
3+
##############################
4+
The iptools_ package is a collection of utilities for dealing with IP
5+
addresses.
6+
7+
The project was inspired by a desire to be able to use CIDR_ address notation
8+
to designate ``INTERNAL_IPS`` in a Django_ project's settings file.
9+
10+
11+
************
12+
Installation
13+
************
14+
Install the latest stable version from PyPi using pip_:
15+
16+
.. code-block:: bash
17+
18+
pip install iptools
19+
20+
or setuptools_:
21+
22+
.. code-block:: bash
23+
24+
easy_install iptools
25+
26+
Install the latest development version:
27+
28+
.. code-block:: bash
29+
30+
git clone https://github.com/bd808/python-iptools.git
31+
cd python-iptools
32+
python setup.py install
33+
34+
35+
******************************
36+
Using with Django INTERNAL_IPS
37+
******************************
38+
An :class:`iptools.IpRangeList` object can be used in a Django_ settings file
39+
to allow CIDR_ and/or ``(start, end)`` ranges to be used in the
40+
``INTERNAL_IPS`` list.
41+
42+
There are many internal and add-on components for Django_ that use the
43+
INTERNAL_IPS_ configuration setting to alter application behavior or make
44+
debugging easier. When you are developing and testing an application by
45+
yourself it's easy to add the ip address that your web browser will be coming
46+
from to this list. When you are developing in a group or testing from many ips
47+
it can become cumbersome to add more and more ip addresses to the setting
48+
individually.
49+
50+
The :class:`iptools.IpRangeList` object can help by replacing the standard
51+
tuple of addresses recommended by the Django docs with an intelligent object
52+
that responds to the membership test operator in. This object can be
53+
configured with dotted quad IP addresses like the default ``INTERNAL_IPS``
54+
tuple (eg. '127.0.0.1'), CIDR_ block notation (eg. '127/8', '192.168/16') for
55+
entire network blocks, and/or (start, end) tuples describing an arbitrary
56+
range of IP addresses.
57+
58+
Django_'s internal checks against the ``INTERNAL_IPS`` tuple take the form
59+
``if addr in INTERNAL_IPS`` or ``if addr not in INTERNAL_IPS``. This works
60+
transparently with the :class:`iptools.IpRangeList` object because it
61+
implements the magic method ``__contains__`` which python calls when the
62+
``in`` or ``not in`` operators are used.
63+
64+
.. code-block:: python
65+
66+
import iptools
67+
68+
INTERNAL_IPS = iptools.IpRangeList(
69+
'127.0.0.1', # single ip
70+
'192.168/16', # CIDR network block
71+
('10.0.0.1', '10.0.0.19'), # arbitrary inclusive range
72+
)
73+
74+
75+
***
76+
API
77+
***
78+
79+
IpRangeList
80+
===========
81+
.. autoclass:: iptools.IpRangeList
82+
:members:
83+
:special-members:
84+
:show-inheritance:
85+
5486

5587
IpRange
5688
=======
57-
.. autoclass:: IpRange
89+
.. autoclass:: iptools.IpRange
5890
:members:
5991
:special-members:
92+
:show-inheritance:
6093

61-
IpRangeList
62-
===========
63-
.. autoclass:: IpRangeList
94+
95+
IPv4
96+
====
97+
.. automodule:: iptools.ipv4
6498
:members:
65-
:special-members:
99+
:show-inheritance:
66100

67-
Constants
68-
=========
69-
.. automodule:: iptools.constants
101+
102+
IPv6
103+
====
104+
.. automodule:: iptools.ipv6
70105
:members:
106+
:show-inheritance:
71107

72-
Indices and tables
73-
==================
74108

109+
******************
110+
Indices and tables
111+
******************
75112
* :ref:`genindex`
76113
* :ref:`search`
114+
115+
.. _iptools: http://pypi.python.org/pypi/iptools
116+
.. _Django: http://www.djangoproject.com/
117+
.. _pip: http://www.pip-installer.org/en/latest/
118+
.. _setuptools: https://pypi.python.org/pypi/setuptools
119+
.. _CIDR: http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing
120+
.. _INTERNAL_IPS: http://docs.djangoproject.com/en/dev/ref/settings/#internal-ips

iptools/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def __hash__(self):
196196
def _cast(self, item):
197197
if isinstance(item, basestring):
198198
item = ipv4.ip2long(item)
199-
if type(item) not in [type(1), type(ipv4._MAX_IP)]:
199+
if type(item) not in [type(1), type(ipv4.MAX_IP)]:
200200
raise TypeError(
201201
"expected dotted-quad ip address or 32-bit integer")
202202
return item
@@ -377,20 +377,29 @@ def __init__(self, *args):
377377
def __repr__(self):
378378
"""
379379
>>> repr(IpRangeList('127.0.0.1', '10/8', '192.168/16'))
380-
"IpRangeList(IpRange('127.0.0.1', '127.0.0.1'), IpRange('10.0.0.0', '10.255.255.255'), IpRange('192.168.0.0', '192.168.255.255'))"
380+
... #doctest: +NORMALIZE_WHITESPACE
381+
"IpRangeList(IpRange('127.0.0.1', '127.0.0.1'),
382+
IpRange('10.0.0.0', '10.255.255.255'),
383+
IpRange('192.168.0.0', '192.168.255.255'))"
381384
>>> repr(
382385
... IpRangeList(IpRange('127.0.0.1', '127.0.0.1'),
383386
... IpRange('10.0.0.0', '10.255.255.255'),
384387
... IpRange('192.168.0.0', '192.168.255.255')))
385-
"IpRangeList(IpRange('127.0.0.1', '127.0.0.1'), IpRange('10.0.0.0', '10.255.255.255'), IpRange('192.168.0.0', '192.168.255.255'))"
388+
... #doctest: +NORMALIZE_WHITESPACE
389+
"IpRangeList(IpRange('127.0.0.1', '127.0.0.1'),
390+
IpRange('10.0.0.0', '10.255.255.255'),
391+
IpRange('192.168.0.0', '192.168.255.255'))"
386392
"""
387393
return "IpRangeList%r" % (self.ips,)
388394
#end __repr__
389395

390396
def __str__(self):
391397
"""
392398
>>> str(IpRangeList('127.0.0.1', '10/8', '192.168/16'))
393-
"(('127.0.0.1', '127.0.0.1'), ('10.0.0.0', '10.255.255.255'), ('192.168.0.0', '192.168.255.255'))"
399+
... #doctest: +NORMALIZE_WHITESPACE
400+
"(('127.0.0.1', '127.0.0.1'),
401+
('10.0.0.0', '10.255.255.255'),
402+
('192.168.0.0', '192.168.255.255'))"
394403
"""
395404
return "(%s)" % ", ".join(str(i) for i in self.ips)
396405
#end __str__

iptools/constants.py

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)