Removed traling spaces.
[librarian.git] / librarian / __init__.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 class ParseError(Exception):
7     pass
8
9 class ValidationError(Exception):
10     pass
11
12 class NoDublinCore(ValidationError):
13     pass
14
15 class XMLNamespace(object):
16     '''A handy structure to repsent names in an XML namespace.'''
17
18     def __init__(self, uri):
19         self.uri = uri
20
21     def __call__(self, tag):
22         return '{%s}%s' % (self.uri, tag)
23
24     def __contains__(self, tag):
25         return tag.startswith('{' + str(self) + '}')
26
27     def __repr__(self):
28         return 'XMLNamespace(%r)' % self.uri
29
30     def __str__(self):
31         return '%s' % self.uri
32
33 class EmptyNamespace(XMLNamespace):
34     def __init__(self):
35         super(EmptyNamespace, self).__init__('')
36
37     def __call__(self, tag):
38         return tag
39
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")
45
46 WLNS = EmptyNamespace()
47
48 import lxml.etree as etree
49 import dcparser
50
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"],
61           DCNS('description'):
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"],
68           DCNS('rights'):
69             [u"Domena publiczna - zm. [OPIS STANU PRAWNEGO TEKSTU]"] })
70
71 def xinclude_forURI(uri):
72     e = etree.Element(XINS("include"))
73     e.set("href", uri)
74     return etree.tostring(e, encoding=unicode)
75
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
79
80     dcstring = etree.tostring(bookinfo.to_etree(), \
81         method='xml', encoding=unicode, pretty_print=True)
82
83     return u'<utwor>\n' + dcstring + u'\n<plain-text>\n' + ocrtext + \
84         u'\n</plain-text>\n</utwor>';
85
86
87 def serialize_raw(element):
88     b = u'' + (element.text or '')
89
90     for child in element.iterchildren():
91         e = etree.tostring(child, method='xml', encoding=unicode, pretty_print=True)
92         b += e
93
94     return b
95
96 SERIALIZERS = {
97     'raw': serialize_raw,
98 }
99
100 def serialize_children(element, format='raw'):
101     return SERIALIZERS[format](element)