Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/dns/grange.py: 11%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
3# Copyright (C) 2012-2017 Nominum, Inc.
4#
5# Permission to use, copy, modify, and distribute this software and its
6# documentation for any purpose with or without fee is hereby granted,
7# provided that the above copyright notice and this permission notice
8# appear in all copies.
9#
10# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18"""DNS GENERATE range conversion."""
20import dns._text_util
21import dns.exception
24def from_text(text: str) -> tuple[int, int, int]:
25 """Convert the text form of a range in a ``$GENERATE`` statement to an
26 integer.
28 :param text: The textual range in ``$GENERATE`` form.
29 :type text: str
30 :rtype: tuple[int, int, int]
31 """
33 start = -1
34 stop = -1
35 step = 1
36 cur = ""
37 state = 0
38 # state 0 1 2
39 # x - y / z
41 if text and text[0] == "-":
42 raise dns.exception.SyntaxError("Start cannot be a negative number")
44 for c in text:
45 if c == "-" and state == 0:
46 start = int(cur)
47 cur = ""
48 state = 1
49 elif c == "/":
50 stop = int(cur)
51 cur = ""
52 state = 2
53 elif dns._text_util.is_ascii_digit(c):
54 cur += c
55 else:
56 raise dns.exception.SyntaxError(f"Could not parse {c}")
58 if state == 0:
59 raise dns.exception.SyntaxError("no stop value specified")
60 elif state == 1:
61 stop = int(cur)
62 else:
63 assert state == 2
64 step = int(cur)
66 assert step >= 1
67 assert start >= 0
68 if start > stop:
69 raise dns.exception.SyntaxError("start must be <= stop")
71 return (start, stop, step)