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 def get_provider(provider_id):
24 return VIDEO_PROVIDERS.get(provider_id, VIDEO_PROVIDERS['youtube'])
27 class UnicodeException(Exception):
29 """ Dirty workaround for Python Unicode handling problems. """
30 return unicode(self).encode('utf-8')
32 def __unicode__(self):
33 """ Dirty workaround for Python Unicode handling problems. """
34 args = self.args[0] if len(self.args) == 1 else self.args
36 message = unicode(args)
37 except UnicodeDecodeError:
38 message = unicode(args, encoding='utf-8', errors='ignore')
42 class ParseError(UnicodeException):
46 class ValidationError(UnicodeException):
50 # was deleted, but still used???
51 class NoDublinCore(ValidationError):
55 class BuildError(Exception):
59 class EmptyNamespace(XMLNamespace):
61 super(EmptyNamespace, self).__init__('')
63 def __call__(self, tag):
66 # some common namespaces we use
67 RDFNS = XMLNamespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
68 DCNS = XMLNamespace('http://purl.org/dc/elements/1.1/')
69 XINS = XMLNamespace("http://www.w3.org/2001/XInclude")
70 XHTMLNS = XMLNamespace("http://www.w3.org/1999/xhtml")
71 NCXNS = XMLNamespace("http://www.daisy.org/z3986/2005/ncx/")
72 OPFNS = XMLNamespace("http://www.idpf.org/2007/opf")
74 SSTNS = XMLNamespace('http://nowoczesnapolska.org.pl/sst#')
78 """Represents a WL URI. Extracts slug from it."""
81 example = 'http://wolnelektury.pl/katalog/lektura/template/'
82 _re_wl_uri = re.compile(r'http://(www\.)?wolnelektury.pl/katalog/lektura/(?P<slug>[-a-z0-9]+)/?$')
84 def __init__(self, uri):
87 self.slug = uri.rstrip('/').rsplit('/', 1)[-1]
91 match = cls._re_wl_uri.match(uri)
93 raise ValidationError(u'Invalid URI (%s). Should match: %s' % (
94 uri, cls._re_wl_uri.pattern))
98 def from_slug(cls, slug):
99 """Contructs an URI from slug.
101 >>> WLURI.from_slug('a-slug').uri
102 u'http://wolnelektury.pl/katalog/lektura/a-slug/'
105 uri = 'http://wolnelektury.pl/katalog/lektura/%s/' % slug
108 def __unicode__(self):
114 def __eq__(self, other):
115 return self.slug == other.slug
118 class URLOpener(urllib.FancyURLopener):
119 version = 'FNP Librarian (http://git.nowoczesnapolska.org.pl/?p=librarian.git)'
120 urllib._urlopener = URLOpener()