X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/8c0d2b20aee2edeb9bd8639ee7fb1887359e9797..f10a4399c9f860f9585d195bd9fd4a3fa43fb6f5:/lib/librarian/dcparser.py?ds=sidebyside diff --git a/lib/librarian/dcparser.py b/lib/librarian/dcparser.py index 9281325c6..a4106863d 100644 --- a/lib/librarian/dcparser.py +++ b/lib/librarian/dcparser.py @@ -45,7 +45,7 @@ def str_to_unicode(value, previous): def str_to_unicode_list(value, previous): if previous is None: previous = [] - previous.append(str_to_unicode(value)) + previous.append(str_to_unicode(value, None)) return previous @@ -76,7 +76,7 @@ def str_to_date(value, previous): # ========== class ParseError(Exception): def __init__(self, message): - super(self, Exception).__init__(message) + super(ParseError, self).__init__(message) class XMLNamespace(object): @@ -134,6 +134,8 @@ class BookInfo(object): raise ParseError(e) description = tree.find('//' + book_info.RDF('Description')) + book_info.wiki_url = description.get(cls.RDF('about'), None) + if description is None: raise ParseError('no Description tag found in document') @@ -157,6 +159,9 @@ class BookInfo(object): root = etree.Element(self.RDF('RDF')) description = etree.SubElement(root, self.RDF('Description')) + if self.wiki_url: + description.set(self.RDF('about'), self.wiki_url) + for tag, (attribute, converter) in self.mapping.iteritems(): if hasattr(self, attribute): e = etree.Element(tag) @@ -165,6 +170,17 @@ class BookInfo(object): return unicode(etree.tostring(root, 'utf-8'), 'utf-8') + def to_dict(self): + etree._namespace_map[str(self.RDF)] = 'rdf' + etree._namespace_map[str(self.DC)] = 'dc' + + result = {'about': self.wiki_url} + for tag, (attribute, converter) in self.mapping.iteritems(): + if hasattr(self, attribute): + result[attribute] = unicode(getattr(self, attribute)) + + return result + def parse(file_name): return BookInfo.from_file(file_name)