Added copyright notice about AGPL. Removed setuputils in favour of plain distutils.
[librarian.git] / librarian / __init__.py
1 # -*- coding: utf-8 -*-
2 #
3 #    This file is part of Librarian.
4 #
5 #    Copyright © 2008,2009,2010 Fundacja Nowoczesna Polska <fundacja@nowoczesnapolska.org.pl>
6 #    
7 #    For full list of contributors see AUTHORS file. 
8 #
9 #    This program is free software: you can redistribute it and/or modify
10 #    it under the terms of the GNU Affero General Public License as published by
11 #    the Free Software Foundation, either version 3 of the License, or
12 #    (at your option) any later version.
13 #
14 #    This program is distributed in the hope that it will be useful,
15 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
16 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 #    GNU Affero General Public License for more details.
18 #
19 #    You should have received a copy of the GNU Affero General Public License
20 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22 # exception classes
23
24 class ParseError(Exception):
25     
26     def __init__(self, cause, message=None):
27         self.cause = cause
28         try:
29             self.message = message or self.cause.message
30         except:
31             self.message = "No message."
32
33 class ValidationError(Exception):
34     pass
35
36 class NoDublinCore(ValidationError):
37     pass
38
39 class XMLNamespace(object):
40     '''A handy structure to repsent names in an XML namespace.'''
41
42     def __init__(self, uri):
43         self.uri = uri
44
45     def __call__(self, tag):
46         return '{%s}%s' % (self.uri, tag)
47
48     def __contains__(self, tag):
49         return tag.startswith('{'+str(self)+'}')
50
51     def __repr__(self):
52         return 'XMLNamespace(%r)' % self.uri
53
54     def __str__(self):
55         return '%s' % self.uri
56
57 class EmptyNamespace(XMLNamespace):
58     def __init__(self):
59         super(EmptyNamespace, self).__init__('')
60
61     def __call__(self, tag):
62         return tag
63
64 # some common namespaces we use
65 RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
66 DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/')
67 XINS = XMLNamespace("http://www.w3.org/2001/XInclude")
68 XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml")
69
70 WLNS = EmptyNamespace()
71
72 import lxml.etree as etree
73 import dcparser
74
75 DEFAULT_BOOKINFO = dcparser.BookInfo(
76         { RDFNS('about'): u'http://wiki.wolnepodreczniki.pl/Lektury:Template'},\
77         { DCNS('creator'): [u'Some, Author'],
78           DCNS('title'): [u'Some Title'],
79           DCNS('subject.period'): [u'Unknown'],
80           DCNS('subject.type'): [u'Unknown'],
81           DCNS('subject.genre'): [u'Unknown'],
82           DCNS('date'): ['1970-01-01'],
83           # DCNS('date'): [creation_date],
84           DCNS('publisher'): [u"Fundacja Nowoczesna Polska"],
85           DCNS('description'):
86           [u"""Publikacja zrealizowana w ramach projektu
87              Wolne Lektury (http://wolnelektury.pl). Reprodukcja cyfrowa
88              wykonana przez Bibliotekę Narodową z egzemplarza
89              pochodzącego ze zbiorów BN."""],
90           DCNS('identifier.url'):
91             [u"http://wolnelektury.pl/katalog/lektura/template"],
92           DCNS('rights'):
93             [u"Domena publiczna - zm. [OPIS STANU PRAWNEGO TEKSTU]"] })
94
95 def xinclude_forURI(uri):
96     e = etree.Element( XINS("include") )
97     e.set("href", uri)
98     return etree.tostring(e, encoding=unicode)
99     
100 def wrap_text(ocrtext, creation_date, bookinfo=DEFAULT_BOOKINFO):
101     """Wrap the text within the minimal XML structure with a DC template."""
102     bookinfo.created_at = creation_date
103     
104     dcstring = etree.tostring(bookinfo.to_etree(),\
105         method='xml', encoding=unicode, pretty_print=True)
106
107     return u'<utwor>\n' + dcstring + u'\n<plain-text>\n' + ocrtext +\
108         u'\n</plain-text>\n</utwor>';
109
110
111 def serialize_raw(element):
112     b = u'' + (element.text or '')
113
114     for child in element.iterchildren():
115         e = etree.tostring(child, method='xml', encoding=unicode, pretty_print=True)
116         b += e
117
118     return b
119
120 from wl_light import serialize_nl
121
122
123 SERIALIZERS = {
124     'raw': serialize_raw,
125     'nl': serialize_nl,
126 }
127
128 def serialize_children(element, format='raw'):
129     return SERIALIZERS[format](element)