-
Notifications
You must be signed in to change notification settings - Fork 46
/
utility.py
109 lines (98 loc) · 2.8 KB
/
utility.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
defaultOptions = {
"backgroundColor": "rgba(171, 184, 195, 1)",
"code": "",
"dropShadow": True,
"dropShadowBlurRadius": "68px",
"dropShadowOffsetY": "20px",
"exportSize": "2x",
"fontFamily": "Hack",
"firstLineNumber": 1,
"fontSize": "14px",
"language": "auto",
"lineNumbers": False,
"paddingHorizontal": "56px",
"paddingVertical": "56px",
"squaredImage": False,
"theme": "seti",
"watermark": False,
"widthAdjustment": True,
"windowControls": True,
"windowTheme": None,
}
optionToQueryParam = {
"backgroundColor": "bg",
"code": "code",
"dropShadow": "ds",
"dropShadowBlurRadius": "dsblur",
"dropShadowOffsetY": "dsyoff",
"exportSize": "es",
"fontFamily": "fm",
"firstLineNumber": "fl",
"fontSize": "fs",
"language": "l",
"lineNumbers": "ln",
"paddingHorizontal": "ph",
"paddingVertical": "pv",
"squaredImage": "si",
"theme": "t",
"watermark": "wm",
"widthAdjustment": "wa",
"windowControls": "wc",
"windowTheme": "wt",
}
ignoredOptions = [
# Can't pass these as URL (So no support now)
"backgroundImage",
"backgroundImageSelection",
"backgroundMode",
"squaredImage",
"hiddenCharacters",
"name",
"lineHeight",
"loading",
"icon",
"isVisible",
"selectedLines",
]
def validateBody(body_):
validatedBody = {}
if not body_['code']:
raise Exception("code is required for creating carbon")
for option in body_:
if option in ignoredOptions:
print(f"Unsupported option: {option} found. Ignoring!")
continue
if (not (option in defaultOptions)):
continue
print(f"Unexpected option: {option} found. Ignoring!")
#raise Exception(f"Unexpected option: {option}")
validatedBody[option] = body_[option]
return validatedBody
def createURLString(validatedBody):
base_url = "https://carbon.now.sh/"
first = True
url = ""
try:
if validatedBody['backgroundColor'].startswith('#') or checkHex(validatedBody['backgroundColor'].upper()) == True:
validatedBody['backgroundColor'] = hex2rgb(
validatedBody['backgroundColor'])
except KeyError:
pass
for option in validatedBody:
if first:
first = False
url = base_url + \
f"?{optionToQueryParam[option]}={validatedBody[option]}"
else:
url = url + \
f"&{optionToQueryParam[option]}={validatedBody[option]}"
return url
def hex2rgb(h):
h = h.lstrip('#')
return ('rgb'+str(tuple(int(h[i:i+2], 16) for i in (0, 2, 4))))
def checkHex(s):
for ch in s:
if ((ch < '0' or ch > '9') and (ch < 'A' or ch > 'F')):
return False
return True