generated from rexzhang/python-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
constants.py
374 lines (295 loc) · 9.19 KB
/
constants.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
from typing import Union, NewType, Optional
import re
from enum import Enum, IntEnum
from time import time
from uuid import UUID
from dataclasses import dataclass, field
from collections import namedtuple
import arrow
DAV_METHODS = {
# rfc4918:9.1
"PROPFIND",
# rfc4918:9.2
"PROPPATCH",
# rfc4918:9.3
"MKCOL",
# rfc4918:9.4
"GET",
"HEAD",
# rfc4918:9.6
"DELETE",
# rfc4918:9.7
"PUT",
# rfc4918:9.8
"COPY",
# rfc4918:9.9
"MOVE",
# rfc4918:9.10
"LOCK",
# rfc4918:9.11
"UNLOCK",
"OPTIONS",
}
DAVMethod = namedtuple("DAVMethodClass", DAV_METHODS)(*DAV_METHODS)
class DAVPath:
raw: str # must start with '/' or empty, and not end with '/'
parts: list[str]
count: int # len(parts)
def _update_value(self, parts: list[str], count: int):
self.raw = "/" + "/".join(parts)
self.parts = parts
self.count = count
def __init__(
self,
path: Union[str, bytes, None] = None,
parts: list[str] = None,
count: int = None,
):
if path is None and parts is not None and count is not None:
self._update_value(parts=parts, count=count)
return
elif not isinstance(path, (str, bytes)):
raise Exception("Except path for DAVPath:{}".format(path))
if isinstance(path, bytes):
path = str(path, encoding="utf-8")
parts = list()
for item in path.split("/"):
if len(item) == 0:
continue
if item == "..":
try:
parts.pop()
except IndexError:
raise Exception("Except path for DAVPath:{}".format(path))
continue
parts.append(item)
self._update_value(parts=parts, count=len(parts))
@property
def parent(self) -> "DAVPath":
return DAVPath(parts=self.parts[: self.count - 1], count=self.count - 1)
@property
def name(self) -> str:
if self.count == 0:
return "/"
return self.parts[self.count - 1]
def startswith(self, path: "DAVPath") -> bool:
return self.parts[: path.count] == path.parts
def get_child(self, parent: "DAVPath") -> "DAVPath":
new_parts = self.parts[parent.count :]
return DAVPath(parts=new_parts, count=self.count - parent.count)
def add_child(self, child: Union["DAVPath", str]) -> "DAVPath":
if not isinstance(child, DAVPath):
child = DAVPath(child)
return DAVPath(
parts=self.parts + child.parts,
count=self.count + child.count,
)
def __hash__(self):
return hash(self.raw)
def __eq__(self, other):
return self.raw == other.raw
def __lt__(self, other):
return self.raw < other.raw
def __repr__(self):
return "DAVPath('{}')".format(self.raw)
def __str__(self):
return self.raw
class DAVDepth(Enum):
d0 = 0
d1 = 1
infinity = "infinity"
class DAVTime:
timestamp: float
def __init__(self, timestamp: Optional[float] = None):
if timestamp is None:
timestamp = time()
self.timestamp = timestamp
self.arrow = arrow.get(timestamp)
def iso_8601(self) -> str:
return self.arrow.format(arrow.FORMAT_RFC3339)
def http_date(self) -> str:
# https://datatracker.ietf.org/doc/html/rfc7232#section-2.2
# 2.2. Last-Modified
#
# The "Last-Modified" header field in a response provides a timestamp
# indicating the date and time at which the origin server believes the
# selected representation was last modified, as determined at the
# conclusion of handling the request.
#
# Last-Modified = HTTP-date
#
# An example of its use is
#
# Last-Modified: Tue, 15 Nov 1994 12:45:26 GMT
# https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Last-Modified
# Last-Modified:
# <day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT
return self.arrow.format("ddd, DD MMM YYYY HH:mm:ss ZZZ")
def ui_display(self) -> str:
return self.arrow.format(arrow.FORMAT_W3C)
def dav_creation_date(self) -> str:
# format borrowed from Apache mod_webdav
return self.arrow.format("YYYY-MM-DDTHH:mm:ssZ")
def __repr__(self):
return self.arrow.isoformat()
class DAVLockScope(IntEnum):
"""
https://tools.ietf.org/html/rfc4918
14.13. lockscope XML Element
Name: lockscope
Purpose: Specifies whether a lock is an exclusive lock, or a shared
lock.
<!ELEMENT lockscope (exclusive | shared) >
"""
exclusive = 1
shared = 2
@dataclass
class DAVLockInfo:
path: DAVPath
depth: DAVDepth
timeout: int
expire: float = field(init=False)
scope: DAVLockScope
owner: str
token: UUID # <opaquelocktoken:UUID.__str__()>
def __post_init__(self):
self.update_expire()
def update_expire(self):
self.expire = time() + self.timeout
def __repr__(self):
s = ", ".join(
[
self.path.raw,
self.depth.__str__(),
self.timeout.__str__(),
self.expire.__str__(),
self.scope.name,
self.owner,
self.token.hex,
]
)
return "DAVLockInfo({})".format(s)
@dataclass
class DAVUser:
username: str
password: str
permissions: list[str]
admin: bool
permissions_allow: list[str] = field(default_factory=list)
permissions_deny: list[str] = field(default_factory=list)
def __post_init__(self):
for permission in self.permissions:
if permission[0] == "+":
self.permissions_allow.append(permission[1:])
elif permission[0] == "-":
self.permissions_deny.append(permission[1:])
else:
raise
def check_paths_permission(self, paths: list[DAVPath]) -> bool:
for path in paths:
allow = False
for permission in self.permissions_allow: # allow: or
m = re.match(permission, path.raw)
if m is not None:
allow = True
break
if not allow:
return False
for path in paths:
allow = True
for permission in self.permissions_deny: # deny: and
m = re.match(permission, path.raw)
if m is not None:
allow = False
break
if not allow:
return False
return True
def __str__(self):
return "{}, allow:{}, deny:{}".format(
self.username, self.permissions_allow, self.permissions_deny
)
DAV_PROPERTY_BASIC_KEYS = {
# Identify
"displayname",
"getetag",
# Date Time
"creationdate",
"getlastmodified",
# File Properties
"getcontenttype",
"getcontentlength",
# 'getcontentlanguage',
# is dir
"resourcetype",
"encoding",
# 'supportedlock', 'lockdiscovery'
# 'executable'
}
DAVPropertyIdentity = NewType(
# (namespace, key)
"DAVPropertyIdentity",
tuple[str, str],
)
DAVPropertyPatches = NewType(
"DAVPropertyPatches",
list[
# (DAVPropertyIdentity(sn_key), value, set<True>/remove<False>)
tuple[DAVPropertyIdentity, str, bool]
],
)
DEFAULT_FILENAME_CONTENT_TYPE_MAPPING = {
"README": "text/plain",
"LICENSE": "text/plain",
".gitignore": "text/plain",
".coveragerc": "text/plain",
# docker
"Dockerfile": "text/plain",
".dockerignore": "text/plain",
}
DEFAULT_SUFFIX_CONTENT_TYPE_MAPPING = {
".cfg": "text/plain",
".md": "text/plain",
".toml": "text/plain",
".yaml": "text/plain",
".yml": "text/plain",
# code source
".php": "text/plain",
}
# https://en.wikipedia.org/wiki/.DS_Store
# https://en.wikipedia.org/wiki/AppleSingle_and_AppleDouble_formats
# https://en.wikipedia.org/wiki/Windows_thumbnail_cache
DEFAULT_DIR_FILE_IGNORE_RULES = {
"": "^#recycle$|^@eaDir$",
"WebDAVFS": r"^Thumbs\.db$",
"Microsoft-WebDAV-MiniRedir": r"^\.DS_Store$$|^\._\.",
}
RESPONSE_DATA_BLOCK_SIZE = 64 * 1024
class DAVAcceptEncoding:
# https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Encoding
# https://caniuse.com/?search=gzip
# identity
gzip: bool = False
br: bool = False
def __repr__(self):
return "gzip:{}, br:{}".format(self.gzip, self.br)
DEFAULT_COMPRESSION_CONTENT_TYPE_RULE = r"^application/xml$|^text/"
class DAVCompressLevel(Enum):
"""
http://mattmahoney.net/dc/text.html
https://quixdb.github.io/squash-benchmark/
https://sites.google.com/site/powturbo/home/benchmark
"""
FAST = "fast"
RECOMMEND = "recommend"
BEST = "best"
@dataclass
class AppEntryParameters:
bind_host: Optional[str] = None
bind_port: Optional[int] = None
config_file: Optional[str] = None
admin_user: Optional[tuple[str, str]] = None
root_path: Optional[str] = None
dev_mode: bool = False
logging_display_datetime: bool = True
logging_use_colors: bool = True