"""
Whip reader module.
"""

from .json import loads
from .util import ipv4_str_to_int

DEFAULT_RANGE_FIELDS = ('begin', 'end')


def iter_json(fp, range_fields=DEFAULT_RANGE_FIELDS):
    """Read a JSON formatted stream of data from a file like object.

    Each line in the input file must contain a valid JSON document. The
    fields specified by `range_fields` should contain IPv4 addresses
    that are used as the beginning and end of the range.

    The input must already be sorted by IP range, and the ranges must
    not overlap.

    This function yields ``(begin, end, doc)`` tuples, where both
    `begin` and `end` are integer representations of the IP addresses
    contained in the document.
    """

    begin_field, end_field = range_fields
    _ipv4_str_to_int = ipv4_str_to_int
    _loads = loads

    for line in fp:
        doc = _loads(line)
        yield (
            _ipv4_str_to_int(doc[begin_field]),
            _ipv4_str_to_int(doc[end_field]),
            doc,
        )

@@ -66,18 +66,16 @@
 mport ip
-v4
 _str_to_
@@ -78,16 +78,16 @@
 _to_int%0A
+
 %0ADEFAULT
@@ -373,18 +373,16 @@
 ntain IP
-v4
  address
@@ -387,17 +387,17 @@
 sses
-%0A   
  that
+%0A   
  are
@@ -751,26 +751,24 @@
 elds%0A    _ip
-v4
 _str_to_int 
@@ -771,18 +771,16 @@
 int = ip
-v4
 _str_to_
@@ -873,34 +873,32 @@
 %0A            _ip
-v4
 _str_to_int(doc%5B
@@ -931,10 +931,8 @@
  _ip
-v4
 _str

Handle IPv6 in input reader