memegen.services.link   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 28
rs 10
c 0
b 0
f 0
ccs 19
cts 19
cp 1
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A LinkService.encode() 0 7 2
A LinkService.__init__() 0 3 1
A LinkService.decode() 0 8 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