You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.0 KiB

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