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

Complexity

Conditions 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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