Skip to content

Commit

Permalink
support for parsing of comments
Browse files Browse the repository at this point in the history
Not all rdtypes are covered. This patch is incomplete.
  • Loading branch information
uberj committed Jun 14, 2013
1 parent 80cd093 commit 37102dd
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
14 changes: 12 additions & 2 deletions dns/rdata.py
Expand Up @@ -128,9 +128,9 @@ class Rdata(object):
"""Base class for all DNS rdata types.
"""

__slots__ = ['rdclass', 'rdtype']
__slots__ = ['rdclass', 'rdtype', 'comment']

def __init__(self, rdclass, rdtype):
def __init__(self, rdclass, rdtype, comment=None):
"""Initialize an rdata.
@param rdclass: The rdata class
@type rdclass: int
Expand All @@ -140,6 +140,7 @@ def __init__(self, rdclass, rdtype):

self.rdclass = rdclass
self.rdtype = rdtype
self.comment = comment

def covers(self):
"""DNS SIG/RRSIG rdatas apply to a specific type; this type is
Expand Down Expand Up @@ -476,3 +477,12 @@ def from_wire(rdclass, rdtype, wire, current, rdlen, origin = None):
wire = dns.wiredata.maybe_wrap(wire)
cls = get_rdata_class(rdclass, rdtype)
return cls.from_wire(rdclass, rdtype, wire, current, rdlen, origin)


def with_comment(tok, rd):
token = tok.get(want_comment=True)
if token.is_comment():
rd.comment = token.value
else:
tok.unget(token)
return rd
3 changes: 2 additions & 1 deletion dns/rdtypes/IN/A.py
Expand Up @@ -37,8 +37,9 @@ def to_text(self, origin=None, relativize=True, **kw):

def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
address = tok.get_identifier()
rd = dns.rdata.with_comment(tok, cls(rdclass, rdtype, address))
tok.get_eol()
return cls(rdclass, rdtype, address)
return rd

from_text = classmethod(from_text)

Expand Down
3 changes: 2 additions & 1 deletion dns/rdtypes/IN/AAAA.py
Expand Up @@ -37,8 +37,9 @@ def to_text(self, origin=None, relativize=True, **kw):

def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
address = tok.get_identifier()
rd = dns.rdata.with_comment(tok, cls(rdclass, rdtype, address))
tok.get_eol()
return cls(rdclass, rdtype, address)
return rd

from_text = classmethod(from_text)

Expand Down
3 changes: 2 additions & 1 deletion dns/rdtypes/IN/SRV.py
Expand Up @@ -52,8 +52,9 @@ def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
port = tok.get_uint16()
target = tok.get_name(None)
target = target.choose_relativity(origin, relativize)
rd = dns.rdata.with_comment(tok, cls(rdclass, rdtype, priority, weight, port, target))
tok.get_eol()
return cls(rdclass, rdtype, priority, weight, port, target)
return rd

from_text = classmethod(from_text)

Expand Down
3 changes: 2 additions & 1 deletion dns/rdtypes/mxbase.py
Expand Up @@ -45,8 +45,9 @@ def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
preference = tok.get_uint16()
exchange = tok.get_name()
exchange = exchange.choose_relativity(origin, relativize)
rd = dns.rdata.with_comment(tok, cls(rdclass, rdtype, preference, exchange))
tok.get_eol()
return cls(rdclass, rdtype, preference, exchange)
return rd

from_text = classmethod(from_text)

Expand Down
4 changes: 3 additions & 1 deletion dns/rdtypes/nsbase.py
Expand Up @@ -32,6 +32,7 @@ class NSBase(dns.rdata.Rdata):
def __init__(self, rdclass, rdtype, target):
super(NSBase, self).__init__(rdclass, rdtype)
self.target = target
self.comment = ''

def to_text(self, origin=None, relativize=True, **kw):
target = self.target.choose_relativity(origin, relativize)
Expand All @@ -40,8 +41,9 @@ def to_text(self, origin=None, relativize=True, **kw):
def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
target = tok.get_name()
target = target.choose_relativity(origin, relativize)
rd = dns.rdata.with_comment(tok, cls(rdclass, rdtype, target))
tok.get_eol()
return cls(rdclass, rdtype, target)
return rd

from_text = classmethod(from_text)

Expand Down

0 comments on commit 37102dd

Please sign in to comment.