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