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.
9 from .utils import XMLNamespace
12 class UnicodeException(Exception):
14 """ Dirty workaround for Python Unicode handling problems. """
15 return unicode(self).encode('utf-8')
17 def __unicode__(self):
18 """ Dirty workaround for Python Unicode handling problems. """
19 args = self.args[0] if len(self.args) == 1 else self.args
21 message = unicode(args)
22 except UnicodeDecodeError:
23 message = unicode(args, encoding='utf-8', errors='ignore')
26 class ParseError(UnicodeException):
29 class ValidationError(UnicodeException):
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")
45 NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/")
46 OPFNS = XMLNamespace("http://www.idpf.org/2007/opf")
48 SSTNS = XMLNamespace('http://nowoczesnapolska.org.pl/sst#')
52 """Represents a WL URI. Extracts slug from it."""
55 example = 'http://wolnelektury.pl/katalog/lektura/template/'
56 _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/'
57 '(?P<slug>[-a-z0-9]+)/?$')
59 def __init__(self, uri):
62 self.slug = uri.rstrip('/').rsplit('/', 1)[-1]
66 match = cls._re_wl_uri.match(uri)
68 raise ValidationError(u'Invalid URI (%s). Should match: %s' % (
69 uri, cls._re_wl_uri.pattern))
73 def from_slug(cls, slug):
74 """Contructs an URI from slug.
76 >>> WLURI.from_slug('a-slug').uri
77 u'http://wolnelektury.pl/katalog/lektura/a-slug/'
80 uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
83 def __unicode__(self):
89 def __eq__(self, other):
90 return self.slug == other.slug
93 class URLOpener(urllib.FancyURLopener):
94 version = 'FNP Librarian (http://git.nowoczesnapolska.org.pl/?p=librarian.git)'
95 urllib._urlopener = URLOpener()