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
    21 -def from_text(text):
    22      """Convert the text form of a range in a GENERATE statement to an
    23      integer.
    24
    25      @param text: the textual range
    26      @type text: string
    27      @return: The start, stop and step values.
    28      @rtype: tuple
    29      """
    30      # TODO, figure out the bounds on start, stop and step.
    31
    32      step = 1
    33      cur = ''
    34      state = 0
    35      # state   0 1 2 3 4
    36      #         x - y / z
    37
    38      if text and text[0] == '-':
    39          raise dns.exception.SyntaxError("Start cannot be a negative number")
    40
    41      for c in text:
    42          if c == '-' and state == 0:
    43              start = int(cur)
    44              cur = ''
    45              state = 2
    46          elif c == '/':
    47              stop = int(cur)
    48              cur = ''
    49              state = 4
    50          elif c.isdigit():
    51              cur += c
    52          else:
    53              raise dns.exception.SyntaxError("Could not parse %s" % (c))
    54
    55      if state in (1, 3):
    56          raise dns.exception.SyntaxError()
    57
    58      if state == 2:
    59          stop = int(cur)
    60
    61      if state == 4:
    62          step = int(cur)
    63
    64      assert step >= 1
    65      assert start >= 0
    66      assert start <= stop
    67      # TODO, can start == stop?
    68
    69      return (start, stop, step)
    70