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)  

euibase.py
Go to the documentation of this file.
1 # Copyright (C) 2015 Red Hat, Inc.
2 # Author: Petr Spacek <pspacek@redhat.com>
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 RED HAT 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 
17 import binascii
18 
19 import dns.rdata
20 from dns._compat import xrange
21 
22 
24 
25  """EUIxx record
26 
27  @ivar fingerprint: xx-bit Extended Unique Identifier (EUI-xx)
28  @type fingerprint: string
29  @see: rfc7043.txt"""
30 
31  __slots__ = ['eui']
32  # define these in subclasses
33  # byte_len = 6 # 0123456789ab (in hex)
34  # text_len = byte_len * 3 - 1 # 01-23-45-67-89-ab
35 
36  def __init__(self, rdclass, rdtype, eui):
37  super(EUIBase, self).__init__(rdclass, rdtype)
38  if len(eui) != self.byte_len:
39  raise dns.exception.FormError('EUI%s rdata has to have %s bytes'
40  % (self.byte_len * 8, self.byte_len))
41  self.eui = eui
42 
43  def to_text(self, origin=None, relativize=True, **kw):
44  return dns.rdata._hexify(self.eui, chunksize=2).replace(' ', '-')
45 
46  @classmethod
47  def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
48  text = tok.get_string()
49  tok.get_eol()
50  if len(text) != cls.text_len:
52  'Input text must have %s characters' % cls.text_len)
53  expected_dash_idxs = xrange(2, cls.byte_len * 3 - 1, 3)
54  for i in expected_dash_idxs:
55  if text[i] != '-':
56  raise dns.exception.SyntaxError('Dash expected at position %s'
57  % i)
58  text = text.replace('-', '')
59  try:
60  data = binascii.unhexlify(text.encode())
61  except (ValueError, TypeError) as ex:
62  raise dns.exception.SyntaxError('Hex decoding error: %s' % str(ex))
63  return cls(rdclass, rdtype, data)
64 
65  def to_wire(self, file, compress=None, origin=None):
66  file.write(self.eui)
67 
68  @classmethod
69  def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None):
70  eui = wire[current:current + rdlen].unwrap()
71  return cls(rdclass, rdtype, eui)
dns.exception.FormError
Definition: exception.py:109
dns.exception.SyntaxError
Definition: exception.py:113
dns.rdtypes.euibase.EUIBase.to_wire
def to_wire(self, file, compress=None, origin=None)
Definition: euibase.py:65
dns.rdtypes.euibase.EUIBase.to_text
def to_text(self, origin=None, relativize=True, **kw)
Definition: euibase.py:43
dns.rdata._hexify
def _hexify(data, chunksize=_hex_chunksize)
Definition: rdata.py:40
dns.rdtypes.euibase.EUIBase.__init__
def __init__(self, rdclass, rdtype, eui)
Definition: euibase.py:36
dns.rdtypes.euibase.EUIBase.from_wire
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None)
Definition: euibase.py:69
dns.rdtypes.euibase.EUIBase.from_text
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True)
Definition: euibase.py:47
dns.rdtypes.euibase.EUIBase
Definition: euibase.py:23
dns.rdata
Definition: rdata.py:1
dns.rdtypes.euibase.EUIBase.eui
eui
Definition: euibase.py:41
dns._compat
Definition: _compat.py:1
dns._compat.xrange
xrange
Definition: _compat.py:11
dns.rdata.Rdata
Definition: rdata.py:95