/
converters.py
87 lines (65 loc) · 2.41 KB
/
converters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
"""
converters
~~
url converters and mixin classes
:copyright: Copyright (C) 2015 by the respective authors; see AUTHORS.
:license: GPLv3, see LICENSE for details.
"""
import re
from base64 import urlsafe_b64decode, urlsafe_b64encode
from binascii import Error as BinError
from binascii import hexlify, unhexlify
from os import path
from flask import current_app, request
from werkzeug.routing import BaseConverter
class UnhexMixin:
def to_url(self, value, length=None):
length = length if length else self.length
def f(v):
v = '{:0>{length}}'.format(v[-(length):], length=length)
return urlsafe_b64encode(unhexlify(v)).decode('utf-8')
if isinstance(value, str):
return f(value)
sid, filename = value
ext = path.splitext(filename)[1] if filename else ''
return '{}{}'.format(f(sid), ext)
class SREMixin:
def to_python(self, value):
name, label = self.sre.match(value).groups()
return label, name
class SIDConverter(UnhexMixin, BaseConverter):
def __init__(self, map, length):
super().__init__(map)
self.regex = '(([A-Za-z0-9_-]{{{}}})(?:[.][^/]*)?)'.format(length)
self.sre = re.compile(self.regex)
if length % 4 != 0:
raise NotImplementedError('{} % 4 != 0; kthx'.format(length))
self.length = 6 * (length // 4)
def to_python(self, value):
name, sid = self.sre.match(value).groups()
try:
_hex = hexlify(urlsafe_b64decode(sid)[-20:]).decode('utf-8')
except BinError:
_hex = None
return _hex, name, value[:4]
class SHA1Converter(UnhexMixin, SREMixin, BaseConverter):
regex = '(([A-Za-z0-9]{40})(?:[.][^/]*)?)'
def __init__(self, map):
super().__init__(map)
self.sre = re.compile(self.regex)
self.length = 42
class LabelConverter(SREMixin, BaseConverter):
regex = '(((?:~[^/.]+)|(?:[^/.]{1}))(?:[.][^/]*)?)'
def __init__(self, map):
super().__init__(map)
self.sre = re.compile(self.regex)
def to_url(self, value, length=None):
if isinstance(value, str):
return value
label, filename = value
ext = path.splitext(filename)[1] if filename else ''
return '{}{}'.format(label, ext)
class NamespaceConverter(LabelConverter):
regex = '(([^/.]*)(?:[.][^/]*)?)'
weight = 120