1 # -*- coding: utf-8 -*-
 
   3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
 
   4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.  
 
   6 class ParseError(Exception):
 
   9 class ValidationError(Exception):
 
  12 class NoDublinCore(ValidationError):
 
  15 class XMLNamespace(object):
 
  16     '''A handy structure to repsent names in an XML namespace.'''
 
  18     def __init__(self, uri):
 
  21     def __call__(self, tag):
 
  22         return '{%s}%s' % (self.uri, tag)
 
  24     def __contains__(self, tag):
 
  25         return tag.startswith('{' + str(self) + '}')
 
  28         return 'XMLNamespace(%r)' % self.uri
 
  31         return '%s' % self.uri
 
  33 class EmptyNamespace(XMLNamespace):
 
  35         super(EmptyNamespace, self).__init__('')
 
  37     def __call__(self, tag):
 
  40 # some common namespaces we use
 
  41 RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
 
  42 DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/')
 
  43 XINS = XMLNamespace("http://www.w3.org/2001/XInclude")
 
  44 XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml")
 
  46 WLNS = EmptyNamespace()
 
  48 import lxml.etree as etree
 
  51 DEFAULT_BOOKINFO = dcparser.BookInfo(
 
  52         { RDFNS('about'): u'http://wiki.wolnepodreczniki.pl/Lektury:Template'}, \
 
  53         { DCNS('creator'): [u'Some, Author'],
 
  54           DCNS('title'): [u'Some Title'],
 
  55           DCNS('subject.period'): [u'Unknown'],
 
  56           DCNS('subject.type'): [u'Unknown'],
 
  57           DCNS('subject.genre'): [u'Unknown'],
 
  58           DCNS('date'): ['1970-01-01'],
 
  59           # DCNS('date'): [creation_date],
 
  60           DCNS('publisher'): [u"Fundacja Nowoczesna Polska"],
 
  62           [u"""Publikacja zrealizowana w ramach projektu
 
  63              Wolne Lektury (http://wolnelektury.pl). Reprodukcja cyfrowa
 
  64              wykonana przez Bibliotekę Narodową z egzemplarza
 
  65              pochodzącego ze zbiorów BN."""],
 
  66           DCNS('identifier.url'):
 
  67             [u"http://wolnelektury.pl/katalog/lektura/template"],
 
  69             [u"Domena publiczna - zm. [OPIS STANU PRAWNEGO TEKSTU]"] })
 
  71 def xinclude_forURI(uri):
 
  72     e = etree.Element(XINS("include"))
 
  74     return etree.tostring(e, encoding=unicode)
 
  76 def wrap_text(ocrtext, creation_date, bookinfo=DEFAULT_BOOKINFO):
 
  77     """Wrap the text within the minimal XML structure with a DC template."""
 
  78     bookinfo.created_at = creation_date
 
  80     dcstring = etree.tostring(bookinfo.to_etree(), \
 
  81         method='xml', encoding=unicode, pretty_print=True)
 
  83     return u'<utwor>\n' + dcstring + u'\n<plain-text>\n' + ocrtext + \
 
  84         u'\n</plain-text>\n</utwor>';
 
  87 def serialize_raw(element):
 
  88     b = u'' + (element.text or '')
 
  90     for child in element.iterchildren():
 
  91         e = etree.tostring(child, method='xml', encoding=unicode, pretty_print=True)
 
 100 def serialize_children(element, format='raw'):
 
 101     return SERIALIZERS[format](element)