mirror of
https://github.com/OpenMTC/OpenMTC.git
synced 2025-06-16 21:48:06 +00:00
initial commit
This commit is contained in:
51
build/lib/futile/serializer/xml.py
Normal file
51
build/lib/futile/serializer/xml.py
Normal file
@ -0,0 +1,51 @@
|
||||
'''
|
||||
Created on 28.08.2011
|
||||
|
||||
@author: kca
|
||||
'''
|
||||
|
||||
from ..logging import LoggerMixin
|
||||
from logging import DEBUG
|
||||
from ..etree.impl import ElementTree, XML, ParseError as XMLParseError, XMLSyntaxError, tostring
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from futile.serializer.exc import ParseError
|
||||
|
||||
class AbstractXMLSerializer(LoggerMixin):
|
||||
__metaclass__ = ABCMeta
|
||||
|
||||
def load(self, input):
|
||||
if self.logger.isEnabledFor(DEBUG):
|
||||
from cStringIO import StringIO
|
||||
input = input.read()
|
||||
self.logger.debug("Parsing input: %s", input)
|
||||
input = StringIO(input)
|
||||
root = self._load(input)
|
||||
return self._parse_input(root)
|
||||
|
||||
def _load(self, input):
|
||||
try:
|
||||
if isinstance(input, str):
|
||||
return XML(input)
|
||||
else:
|
||||
return ElementTree().parse(input)
|
||||
except Exception, e:
|
||||
self._handle_parse_error(e)
|
||||
raise ParseError(e)
|
||||
|
||||
def _handle_parse_error(self, e):
|
||||
self.logger.exception("Error parsing input: %s", e)
|
||||
|
||||
@abstractmethod
|
||||
def _parse_input(self, root):
|
||||
raise NotImplementedError()
|
||||
|
||||
def dump(self, o, pretty_print = True):
|
||||
raise NotImplementedError()
|
||||
|
||||
def dumps(self, o, pretty_print = True):
|
||||
xml = self._dump_object(o)
|
||||
return tostring(xml, pretty_print = pretty_print)
|
||||
|
||||
@abstractmethod
|
||||
def _dump_object(self, o):
|
||||
raise NotImplementedError()
|
Reference in New Issue
Block a user