From 290374ca89c59d1f92290bf70cfd8715896d990e Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Wed, 11 Jul 2007 18:15:42 +0000 Subject: Checkpoint svn path=/scripts/rpki/sax_handler.py; revision=751 --- scripts/rpki/sax_utils.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 scripts/rpki/sax_utils.py (limited to 'scripts/rpki/sax_utils.py') diff --git a/scripts/rpki/sax_utils.py b/scripts/rpki/sax_utils.py new file mode 100644 index 00000000..b4259d2a --- /dev/null +++ b/scripts/rpki/sax_utils.py @@ -0,0 +1,38 @@ +# $Id$ + +import xml.sax + +def snarf(obj, attrs, key, func=None): + """ + Utility function to consolidate the steps needed to extract a field + from the SAX XML parse and insert it as an object attribute of the + same name. + """ + + try: + val = attrs.getValue(key).encode("ascii") + if func: + val = func(val) + except KeyError: + val = None + setattr(obj, key, val) + +class handler(xml.sax.handler.ContentHandler): + """ + SAX handler for RPKI protocols. Handles a few tasks + common to all of these protocols, needs to be subtyped + to handle protocol-specific details. + """ + + def __init__(self): + self.text = "" + self.obj = None + + def startElementNS(self, name, qname, attrs): + return self.startElement(name[1], attrs) + + def endElementNS(self, name, qname): + return self.endElement(name[1]) + + def characters(self, content): + self.text += content -- cgit v1.2.3