X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/e180078f82a3d7e73857a9544b3b7fdfd475f93f..84857c8e944af74ee5904db84149ab2ceef8bfef:/lib/dcparser/dcparser.py?ds=inline diff --git a/lib/dcparser/dcparser.py b/lib/dcparser/dcparser.py index e8a733a73..7d4386237 100644 --- a/lib/dcparser/dcparser.py +++ b/lib/dcparser/dcparser.py @@ -13,17 +13,14 @@ except ImportError: import converters - __all__ = ('parse', 'ParseError') - class ParseError(Exception): def __init__(self, message): super(self, Exception).__init__(message) - class XMLNamespace(object): '''Represents XML namespace.''' @@ -37,13 +34,12 @@ class XMLNamespace(object): return tag.startswith(str(self)) def __repr__(self): - return 'NS(%r)' % self.uri + return 'XMLNamespace(%r)' % self.uri def __str__(self): return '%s' % self.uri - class BookInfo(object): RDF = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#') DC = XMLNamespace('http://purl.org/dc/elements/1.1/') @@ -61,15 +57,14 @@ class BookInfo(object): DC('publisher') : ('publisher', converters.str_to_unicode), DC('source') : ('source_name', converters.str_to_unicode), DC('source.URL') : ('source_url', converters.str_to_unicode), + DC('identifier.url') : ('url', converters.str_to_unicode), + DC('relation.hasPart') : ('parts', converters.str_to_unicode_list), } - @classmethod def from_string(cls, xml): - """docstring for from_string""" from StringIO import StringIO return cls.from_file(StringIO(xml)) - @classmethod def from_file(cls, xml_file): @@ -89,15 +84,13 @@ class BookInfo(object): return book_info - def parse_element(self, element): try: attribute, converter = self.mapping[element.tag] - setattr(self, attribute, converter(element.text)) + setattr(self, attribute, converter(element.text, getattr(self, attribute, None))) except KeyError: pass - def to_xml(self): """XML representation of this object.""" ET._namespace_map[str(self.RDF)] = 'rdf' @@ -118,4 +111,3 @@ class BookInfo(object): def parse(file_name): return BookInfo.from_file(file_name) -