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
13 'url': 'https://www.youtube.com/watch?v=%s',
14 'embed': '//www.youtube.com/embed/%s?controls=2&rel=0&showinfo=0&theme=light',
17 'url': 'https://vimeo.com/%s',
18 'embed': '//player.vimeo.com/video/%s',
23 class UnicodeException(Exception):
25 """ Dirty workaround for Python Unicode handling problems. """
26 return unicode(self).encode('utf-8')
28 def __unicode__(self):
29 """ Dirty workaround for Python Unicode handling problems. """
30 args = self.args[0] if len(self.args) == 1 else self.args
32 message = unicode(args)
33 except UnicodeDecodeError:
34 message = unicode(args, encoding='utf-8', errors='ignore')
38 class ParseError(UnicodeException):
42 class ValidationError(UnicodeException):
46 # was deleted, but still used???
47 class NoDublinCore(ValidationError):
51 class BuildError(Exception):
55 class EmptyNamespace(XMLNamespace):
57 super(EmptyNamespace, self).__init__('')
59 def __call__(self, tag):
62 # some common namespaces we use
63 RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
64 DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/')
65 XINS = XMLNamespace("http://www.w3.org/2001/XInclude")
66 XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml")
67 NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/")
68 OPFNS = XMLNamespace("http://www.idpf.org/2007/opf")
70 SSTNS = XMLNamespace('http://nowoczesnapolska.org.pl/sst#')
74 """Represents a WL URI. Extracts slug from it."""
77 example = 'http://wolnelektury.pl/katalog/lektura/template/'
78 _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/(?P<slug>[-a-z0-9]+)/?$')
80 def __init__(self, uri):
83 self.slug = uri.rstrip('/').rsplit('/', 1)[-1]
87 match = cls._re_wl_uri.match(uri)
89 raise ValidationError(u'Invalid URI (%s). Should match: %s' % (
90 uri, cls._re_wl_uri.pattern))
94 def from_slug(cls, slug):
95 """Contructs an URI from slug.
97 >>> WLURI.from_slug('a-slug').uri
98 u'http://wolnelektury.pl/katalog/lektura/a-slug/'
101 uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
104 def __unicode__(self):
110 def __eq__(self, other):
111 return self.slug == other.slug
114 class URLOpener(urllib.FancyURLopener):
115 version = 'FNP Librarian (http://git.nowoczesnapolska.org.pl/?p=librarian.git)'
116 urllib._urlopener = URLOpener()