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.
8 from .utils import XMLNamespace
11 class UnicodeException(Exception):
13 """ Dirty workaround for Python Unicode handling problems. """
14 return unicode(self).encode('utf-8')
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
20 message = unicode(args)
21 except UnicodeDecodeError:
22 message = unicode(args, encoding='utf-8', errors='ignore')
26 class ParseError(UnicodeException):
30 class ValidationError(UnicodeException):
34 class BuildError(Exception):
38 class EmptyNamespace(XMLNamespace):
40 super(EmptyNamespace, self).__init__('')
42 def __call__(self, tag):
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")
53 SSTNS = XMLNamespace('http://nowoczesnapolska.org.pl/sst#')
57 """Represents a WL URI. Extracts slug from it."""
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]+)/?$')
63 def __init__(self, uri):
66 self.slug = uri.rstrip('/').rsplit('/', 1)[-1]
70 match = cls._re_wl_uri.match(uri)
72 raise ValidationError(u'Invalid URI (%s). Should match: %s' % (
73 uri, cls._re_wl_uri.pattern))
77 def from_slug(cls, slug):
78 """Contructs an URI from slug.
80 >>> WLURI.from_slug('a-slug').uri
81 u'http://wolnelektury.pl/katalog/lektura/a-slug/'
84 uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
87 def __unicode__(self):
93 def __eq__(self, other):
94 return self.slug == other.slug
97 class URLOpener(urllib.FancyURLopener):
98 version = 'FNP Librarian (http://git.nowoczesnapolska.org.pl/?p=librarian.git)'
99 urllib._urlopener = URLOpener()