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)  

e164.py
Go to the documentation of this file.
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2 
3 # Copyright (C) 2006-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 E.164 helpers."""
19 
20 import dns.exception
21 import dns.name
22 import dns.resolver
23 from ._compat import string_types, maybe_decode
24 
25 #: The public E.164 domain.
26 public_enum_domain = dns.name.from_text('e164.arpa.')
27 
28 
29 def from_e164(text, origin=public_enum_domain):
30  """Convert an E.164 number in textual form into a Name object whose
31  value is the ENUM domain name for that number.
32 
33  Non-digits in the text are ignored, i.e. "16505551212",
34  "+1.650.555.1212" and "1 (650) 555-1212" are all the same.
35 
36  *text*, a ``text``, is an E.164 number in textual form.
37 
38  *origin*, a ``dns.name.Name``, the domain in which the number
39  should be constructed. The default is ``e164.arpa.``.
40 
41  Returns a ``dns.name.Name``.
42  """
43 
44  parts = [d for d in text if d.isdigit()]
45  parts.reverse()
46  return dns.name.from_text('.'.join(parts), origin=origin)
47 
48 
49 def to_e164(name, origin=public_enum_domain, want_plus_prefix=True):
50  """Convert an ENUM domain name into an E.164 number.
51 
52  Note that dnspython does not have any information about preferred
53  number formats within national numbering plans, so all numbers are
54  emitted as a simple string of digits, prefixed by a '+' (unless
55  *want_plus_prefix* is ``False``).
56 
57  *name* is a ``dns.name.Name``, the ENUM domain name.
58 
59  *origin* is a ``dns.name.Name``, a domain containing the ENUM
60  domain name. The name is relativized to this domain before being
61  converted to text. If ``None``, no relativization is done.
62 
63  *want_plus_prefix* is a ``bool``. If True, add a '+' to the beginning of
64  the returned number.
65 
66  Returns a ``text``.
67 
68  """
69  if origin is not None:
70  name = name.relativize(origin)
71  dlabels = [d for d in name.labels if d.isdigit() and len(d) == 1]
72  if len(dlabels) != len(name.labels):
73  raise dns.exception.SyntaxError('non-digit labels in ENUM domain name')
74  dlabels.reverse()
75  text = b''.join(dlabels)
76  if want_plus_prefix:
77  text = b'+' + text
78  return maybe_decode(text)
79 
80 
81 def query(number, domains, resolver=None):
82  """Look for NAPTR RRs for the specified number in the specified domains.
83 
84  e.g. lookup('16505551212', ['e164.dnspython.org.', 'e164.arpa.'])
85 
86  *number*, a ``text`` is the number to look for.
87 
88  *domains* is an iterable containing ``dns.name.Name`` values.
89 
90  *resolver*, a ``dns.resolver.Resolver``, is the resolver to use. If
91  ``None``, the default resolver is used.
92  """
93 
94  if resolver is None:
96  e_nx = dns.resolver.NXDOMAIN()
97  for domain in domains:
98  if isinstance(domain, string_types):
99  domain = dns.name.from_text(domain)
100  qname = dns.e164.from_e164(number, domain)
101  try:
102  return resolver.query(qname, 'NAPTR')
103  except dns.resolver.NXDOMAIN as e:
104  e_nx += e
105  raise e_nx
dns.exception.SyntaxError
Definition: exception.py:113
dns.name.from_text
def from_text(text, origin=root, idna_codec=None)
Definition: name.py:873
dns.e164.to_e164
def to_e164(name, origin=public_enum_domain, want_plus_prefix=True)
Definition: e164.py:49
dns._compat.maybe_decode
def maybe_decode(x)
Definition: _compat.py:22
dns.e164.from_e164
def from_e164(text, origin=public_enum_domain)
Definition: e164.py:29
dns.resolver
Definition: resolver.py:1
dns.name
Definition: name.py:1
dns.resolver.get_default_resolver
def get_default_resolver()
Definition: resolver.py:1070
dns.resolver.NXDOMAIN
Definition: resolver.py:50
dns.e164.query
def query(number, domains, resolver=None)
Definition: e164.py:81
dns.exception
Definition: exception.py:1