-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapi_util.py
More file actions
166 lines (124 loc) · 3.95 KB
/
api_util.py
File metadata and controls
166 lines (124 loc) · 3.95 KB
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
from enum import Enum
from typing import List, Dict
import re
class LINK(Enum):
CONTAINER = '<http://www.w3.org/ns/ldp#BasicContainer>; rel="type"'
RESOURCE = '<http://www.w3.org/ns/ldp#Resource>; rel="type"'
def append_slashes_at_end(url) -> str:
if url[-1] != '/':
url += '/'
return url
def remove_slashes_at_end(url) -> str:
if url[-1] == '/':
url = url[:-1]
return url
def get_root_url(url: str) -> str:
slash_count = 0
for i in range(len(url)):
if url[i] == '/':
slash_count += 1
if slash_count == 3:
break
if slash_count == 3:
return url[:i + 1]
else:
return append_slashes_at_end(url)
def get_parent_url(url) -> str:
url = remove_slashes_at_end(url)
if url.count('/') == 2: # is base url, no parent url, return it self
return append_slashes_at_end(url)
i = url.rindex('/')
return url[:i + 1]
def get_item_name(url) -> str:
url = remove_slashes_at_end(url)
if url.count('/') == 2: # is base url, no item name
return ''
i = url.rindex('/')
return url[i + 1:]
def are_folders(urls: List) -> bool:
pass
def are_files(urls: List) -> bool:
pass
def get_links_from_response(response) -> dict:
link_header = response.headers.get('link')
if (not link_header) or (link_header == ''):
return {}
else:
return parse_link_header(link_header, response.url)
def parse_link_header(link_header: str, item_url) -> Dict:
link_dict = {}
link_header_list = parse_link_header_to_array(link_header)
if len(link_header_list) > 0:
for link in link_header_list:
url = link[link.index('<')+ 1:link.index('>')]
original_rel = link[link.index('rel="')+ 5:link.rindex('"')]
if (original_rel.lower() == 'describedby'):
rel = 'meta'
else:
rel = original_rel
if rel in ['meta', 'acl']:
link_dict[rel] = url_join(url, item_url)
return link_dict
def parse_link_header_to_array(link_header: str) -> List:
if (not link_header): return
linkexp = '<[^>]*>\s*(\s*;\s*[^()<>@,;:"/[\]?={} \t]+=(([^\(\)<>@,;:"\/\[\]\?={} \t]+)|("[^"]*")))*(,|$)'
match = re.finditer(linkexp, link_header)
links = [x.group() for x in match]
return links
def url_join(given, base) -> str:
base = str(base)
base_hash = base.find('#')
if (base_hash > 0):
base = base[0:base_hash]
if (len(given) == 0):
return base
if (given.find('#') == 0):
return base + given
colon = given.find(':')
if (colon >= 0) :
return given
base_colon = base.find(':')
if (len(base) == 0) :
return given
if (base_colon < 0) :
return given
if (+ base_colon + 1):
end_index = +base_colon + 1
else:
end_index = 9e9
base_scheme = base[:end_index]
if (given.find('//') == 0) :
return base_scheme + given
if (base.find('//', base_colon) == base_colon + 1):
base_single = base.find('/', base_colon + 3)
if (base_single < 0):
if (len(base) - base_colon - 3 > 0):
return base + '/' + given
else:
return base_scheme + given
else:
base_single = base.find('/', base_colon + 1)
if (base_single < 0):
if (len(base) - base_colon - 1 > 0) :
return base + '/' + given
else:
return base_scheme + given
if (given.find('/') == 0) :
return base[:base_single] + given
path = base[base_single:]
try:
last_slash = path.rindex('/')
except:
return base_scheme + given
if (last_slash >= 0 and last_slash < len(path) - 1) :
if (+last_slash + 1):
end_index = +last_slash + 1
else:
end_index = 9e9
path = path[:end_index]
path += given
while (re.match("[^\/]*\/\.\.\/", path)) :
path = re.sub("[^\/]*\/\.\.\/", '', path, 1)
path = re.sub("\.\/", '', path)
path = re.sub("\/\.$", '/', path)
return base[:base_single] + path