Package dns :: Module grange |
[hide private]
[frames] | no frames]

# Source Code for Module dns.grange

     1  # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
     2  #
     3  # Permission to use, copy, modify, and distribute this software and its
     4  # documentation for any purpose with or without fee is hereby granted,
     5  # provided that the above copyright notice and this permission notice
     6  # appear in all copies.
     7  #
     8  # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
     9  # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
    10  # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
    11  # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
    12  # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
    13  # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
    14  # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    15
    16  """DNS GENERATE range conversion."""
    17
    18  import dns
    19
    20 -def from_text(text):
    21      """Convert the text form of a range in a GENERATE statement to an
    22      integer.
    23
    24      @param text: the textual range
    25      @type text: string
    26      @return: The start, stop and step values.
    27      @rtype: tuple
    28      """
    29      # TODO, figure out the bounds on start, stop and step.
    30
    31      import pdb
    32      step = 1
    33      cur = ''
    34      state = 0
    35      # state   0 1 2 3 4
    36      #         x - y / z
    37      for c in text:
    38          if c == '-' and state == 0:
    39              start = int(cur)
    40              cur = ''
    41              state = 2
    42          elif c == '/':
    43              stop = int(cur)
    44              cur = ''
    45              state = 4
    46          elif c.isdigit():
    47              cur += c
    48          else:
    49              raise dns.exception.SyntaxError("Could not parse %s" % (c))
    50
    51      if state in (1, 3):
    52          raise dns.exception.SyntaxError
    53
    54      if state == 2:
    55          stop = int(cur)
    56
    57      if state == 4:
    58          step = int(cur)
    59
    60      assert step >= 1
    61      assert start >= 0
    62      assert start <= stop
    63      # TODO, can start == stop?
    64
    65      return (start, stop, step)
    66