Remove DateValue, drop Py<3.6, fix tests.
[librarian.git] / src / librarian / meta / types / wluri.py
1 import re
2 from .base import MetaValue
3
4
5 class WLURI(MetaValue):
6     """Represents a WL URI. Extracts slug from it."""
7     template = 'http://wolnelektury.pl/katalog/lektura/%s/'
8     _re_wl_uri = re.compile(
9         r'http://(www\.)?wolnelektury.pl/katalog/lektur[ay]/'
10         '(?P<slug>[-a-z0-9]+)/?$'
11     )
12
13     def __init__(self, slug, uri=None):
14         """Contructs an URI from slug.
15
16         >>> print(WLURI('a-slug').uri)
17         http://wolnelektury.pl/katalog/lektura/a-slug/
18
19         """
20         if uri is None:
21             uri = self.template % slug
22         self.uri = uri
23         return super().__init__(slug)
24
25     @classmethod
26     def from_text(cls, uri):
27         slug = uri.rstrip('/').rsplit('/', 1)[-1]
28         return cls(slug, uri)
29
30     def validate(self):
31         match = self._re_wl_uri.match(self.uri)
32         if not match:
33             raise ValidationError('Invalid URI (%s). Should match: %s' % (
34                         self.uri, self._re_wl_uri.pattern))
35
36     def __str__(self):
37         return self.uri
38
39     def __eq__(self, other):
40         return self.value == other.value
41
42     @property
43     def slug(self):
44         return self.value