memegen.services.link.LinkService.__init__()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 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