02464ef98d2eda8f14c3be81cf70c6e8b2a0d966
[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 import re
7 import urllib
8 from .utils import XMLNamespace
9
10
11 class UnicodeException(Exception):
12     def __str__(self):
13         """ Dirty workaround for Python Unicode handling problems. """
14         return unicode(self).encode('utf-8')
15
16     def __unicode__(self):
17         """ Dirty workaround for Python Unicode handling problems. """
18         args = self.args[0] if len(self.args) == 1 else self.args
19         try:
20             message = unicode(args)
21         except UnicodeDecodeError:
22             message = unicode(args, encoding='utf-8', errors='ignore')
23         return message
24
25
26 class ParseError(UnicodeException):
27     pass
28
29
30 class ValidationError(UnicodeException):
31     pass
32
33
34 class BuildError(Exception):
35     pass
36
37
38 class EmptyNamespace(XMLNamespace):
39     def __init__(self):
40         super(EmptyNamespace, self).__init__('')
41
42     def __call__(self, tag):
43         return tag
44
45 # some common namespaces we use
46 RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
47 DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/')
48 XINS = XMLNamespace("http://www.w3.org/2001/XInclude")
49 XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml")
50 NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/")
51 OPFNS = XMLNamespace("http://www.idpf.org/2007/opf")
52
53 SSTNS = XMLNamespace('http://nowoczesnapolska.org.pl/sst#')
54
55
56 class WLURI(object):
57     """Represents a WL URI. Extracts slug from it."""
58     slug = None
59
60     example = 'http://wolnelektury.pl/katalog/lektura/template/'
61     _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/(?P<slug>[-a-z0-9]+)/?$')
62
63     def __init__(self, uri):
64         uri = unicode(uri)
65         self.uri = uri
66         self.slug = uri.rstrip('/').rsplit('/', 1)[-1]
67
68     @classmethod
69     def strict(cls, uri):
70         match = cls._re_wl_uri.match(uri)
71         if not match:
72             raise ValidationError(u'Invalid URI (%s). Should match: %s' % (
73                         uri, cls._re_wl_uri.pattern))
74         return cls(uri)
75
76     @classmethod
77     def from_slug(cls, slug):
78         """Contructs an URI from slug.
79
80         >>> WLURI.from_slug('a-slug').uri
81         u'http://wolnelektury.pl/katalog/lektura/a-slug/'
82
83         """
84         uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
85         return cls(uri)
86
87     def __unicode__(self):
88         return self.uri
89
90     def __str__(self):
91         return self.uri
92
93     def __eq__(self, other):
94         return self.slug == other.slug
95
96
97 class URLOpener(urllib.FancyURLopener):
98     version = 'FNP Librarian (http://git.nowoczesnapolska.org.pl/?p=librarian.git)'
99 urllib._urlopener = URLOpener()