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 # was deleted, but still used???
35 class NoDublinCore(ValidationError):
39 class BuildError(Exception):
43 class EmptyNamespace(XMLNamespace):
45 super(EmptyNamespace, self).__init__('')
47 def __call__(self, tag):
50 # some common namespaces we use
51 RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
52 DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/')
53 XINS = XMLNamespace("http://www.w3.org/2001/XInclude")
54 XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml")
55 NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/")
56 OPFNS = XMLNamespace("http://www.idpf.org/2007/opf")
58 SSTNS = XMLNamespace('http://nowoczesnapolska.org.pl/sst#')
62 """Represents a WL URI. Extracts slug from it."""
65 example = 'http://wolnelektury.pl/katalog/lektura/template/'
66 _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/(?P<slug>[-a-z0-9]+)/?$')
68 def __init__(self, uri):
71 self.slug = uri.rstrip('/').rsplit('/', 1)[-1]
75 match = cls._re_wl_uri.match(uri)
77 raise ValidationError(u'Invalid URI (%s). Should match: %s' % (
78 uri, cls._re_wl_uri.pattern))
82 def from_slug(cls, slug):
83 """Contructs an URI from slug.
85 >>> WLURI.from_slug('a-slug').uri
86 u'http://wolnelektury.pl/katalog/lektura/a-slug/'
89 uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
92 def __unicode__(self):
98 def __eq__(self, other):
99 return self.slug == other.slug
102 class URLOpener(urllib.FancyURLopener):
103 version = 'FNP Librarian (http://git.nowoczesnapolska.org.pl/?p=librarian.git)'
104 urllib._urlopener = URLOpener()