dnspython  1.16.0
About: dnspython is a DNS toolkit (for Python 2.x) that supports almost all record types.
  Fossies Dox: dnspython-1.16.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

rdataclass.py
Go to the documentation of this file.
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2 
3 # Copyright (C) 2001-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.
17 
18 """DNS Rdata Classes."""
19 
20 import re
21 
22 import dns.exception
23 
24 RESERVED0 = 0
25 IN = 1
26 CH = 3
27 HS = 4
28 NONE = 254
29 ANY = 255
30 
31 _by_text = {
32  'RESERVED0': RESERVED0,
33  'IN': IN,
34  'CH': CH,
35  'HS': HS,
36  'NONE': NONE,
37  'ANY': ANY
38 }
39 
40 # We construct the inverse mapping programmatically to ensure that we
41 # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
42 # would cause the mapping not to be true inverse.
43 
44 _by_value = {y: x for x, y in _by_text.items()}
45 
46 # Now that we've built the inverse map, we can add class aliases to
47 # the _by_text mapping.
48 
49 _by_text.update({
50  'INTERNET': IN,
51  'CHAOS': CH,
52  'HESIOD': HS
53 })
54 
55 _metaclasses = {
56  NONE: True,
57  ANY: True
58 }
59 
60 _unknown_class_pattern = re.compile('CLASS([0-9]+)$', re.I)
61 
62 
64  """A DNS class is unknown."""
65 
66 
67 def from_text(text):
68  """Convert text into a DNS rdata class value.
69 
70  The input text can be a defined DNS RR class mnemonic or
71  instance of the DNS generic class syntax.
72 
73  For example, "IN" and "CLASS1" will both result in a value of 1.
74 
75  Raises ``dns.rdatatype.UnknownRdataclass`` if the class is unknown.
76 
77  Raises ``ValueError`` if the rdata class value is not >= 0 and <= 65535.
78 
79  Returns an ``int``.
80  """
81 
82  value = _by_text.get(text.upper())
83  if value is None:
84  match = _unknown_class_pattern.match(text)
85  if match is None:
86  raise UnknownRdataclass
87  value = int(match.group(1))
88  if value < 0 or value > 65535:
89  raise ValueError("class must be between >= 0 and <= 65535")
90  return value
91 
92 
93 def to_text(value):
94  """Convert a DNS rdata type value to text.
95 
96  If the value has a known mnemonic, it will be used, otherwise the
97  DNS generic class syntax will be used.
98 
99  Raises ``ValueError`` if the rdata class value is not >= 0 and <= 65535.
100 
101  Returns a ``str``.
102  """
103 
104  if value < 0 or value > 65535:
105  raise ValueError("class must be between >= 0 and <= 65535")
106  text = _by_value.get(value)
107  if text is None:
108  text = 'CLASS' + repr(value)
109  return text
110 
111 
112 def is_metaclass(rdclass):
113  """True if the specified class is a metaclass.
114 
115  The currently defined metaclasses are ANY and NONE.
116 
117  *rdclass* is an ``int``.
118  """
119 
120  if rdclass in _metaclasses:
121  return True
122  return False
dns.rdataclass.UnknownRdataclass
Definition: rdataclass.py:63
dns.exception.DNSException
Definition: exception.py:24
dns.rdataclass.is_metaclass
def is_metaclass(rdclass)
Definition: rdataclass.py:112
dns.rdataclass.to_text
def to_text(value)
Definition: rdataclass.py:93
dns.exception
Definition: exception.py:1
dns.rdataclass.from_text
def from_text(text)
Definition: rdataclass.py:67