Total Complexity | 6 |
Total Lines | 28 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | 1 | import base64 |
|
2 | |||
3 | 1 | from ._base import Service |
|
4 | |||
5 | |||
6 | 1 | class LinkService(Service): |
|
7 | |||
8 | 1 | def __init__(self, template_store, **kwargs): |
|
9 | 1 | super().__init__(**kwargs) |
|
10 | 1 | self.template_store = template_store |
|
11 | |||
12 | 1 | @staticmethod |
|
13 | def encode(key, path): |
||
14 | 1 | slug = '\t'.join((key, path)) |
|
15 | 1 | while len(slug) % 3: |
|
16 | 1 | slug += '\t' |
|
17 | 1 | code = base64.urlsafe_b64encode(slug.encode('utf-8')) |
|
18 | 1 | return code.decode('utf-8') |
|
19 | |||
20 | 1 | def decode(self, code): |
|
21 | 1 | try: |
|
22 | 1 | slug = base64.urlsafe_b64decode(code).decode('utf-8') |
|
23 | 1 | key, path = slug.strip('\t').split('\t') |
|
24 | 1 | except ValueError: |
|
25 | 1 | raise self.exceptions.InvalidMaskedCode |
|
26 | else: |
||
27 | return key, path |
||
28 |