|
4 | 4 |
|
5 | 5 | from .errors import DatabaseError |
6 | 6 |
|
7 | | -STANDARD_URLS = { |
8 | | - "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", |
9 | | - "rdfs": "http://www.w3.org/2000/01/rdf-schema#", |
10 | | - "xsd": "http://www.w3.org/2001/XMLSchema#", |
11 | | - "xdd": "http://terminusdb.com/schema/xdd#", |
12 | | - "owl": "http://www.w3.org/2002/07/owl#", |
13 | | - "terminus": "http://terminusdb.com/schema/terminus#", |
14 | | - "vio": "http://terminusdb.com/schema/vio#", |
15 | | - "repo": "http://terminusdb.com/schema/repository#", |
16 | | - "layer": "http://terminusdb.com/schema/layer#", |
17 | | - "woql": "http://terminusdb.com/schema/woql#", |
18 | | - "ref": "http://terminusdb.com/schema/ref#", |
19 | | -} |
20 | | - |
21 | | - |
22 | | -def encode_uri_component(value): |
23 | | - """Encode a URI. |
24 | | -
|
25 | | - Parameters |
26 | | - ---------- |
27 | | - value: Value which needs to be encoded |
28 | | -
|
29 | | - Returns |
30 | | - ------- |
31 | | - Encoded Value |
32 | | - """ |
33 | | - return urllib.parse.urlencode(value, doseq=True) |
34 | | - |
35 | | - |
36 | | -def uri_encode_payload(payload): |
37 | | - """Encode the given payload |
38 | | -
|
39 | | - Parameters |
40 | | - ---------- |
41 | | - payload: dict |
42 | | -
|
43 | | - Returns |
44 | | - ------- |
45 | | - Encoded payload array |
46 | | - """ |
47 | | - if isinstance(payload, str): |
48 | | - return encode_uri_component(payload) |
49 | | - payload_arr = [] |
50 | | - if isinstance(payload, dict): |
51 | | - for key, value in payload.items(): |
52 | | - """ |
53 | | - if dictionary inside dictionary |
54 | | - """ |
55 | | - if isinstance(value, dict): |
56 | | - # for keyElement,valueElement in value.items(): |
57 | | - payload_arr.append(encode_uri_component(value)) |
58 | | - else: |
59 | | - payload_arr.append(encode_uri_component({key: value})) |
60 | | - |
61 | | - return "&".join(payload_arr) |
62 | | - |
63 | | - |
64 | | -def add_params_to_url(url, payload): |
65 | | - """Add params / payload to given url |
66 | | -
|
67 | | - Parameters |
68 | | - ---------- |
69 | | - url: str |
70 | | - payload: dict |
71 | | -
|
72 | | - Returns |
73 | | - ------- |
74 | | - Url with payload appended |
75 | | - """ |
76 | | - if payload: |
77 | | - params = uri_encode_payload(payload) |
78 | | - if params: |
79 | | - url = f"{url}?{params}" |
80 | | - return url |
81 | | - |
82 | | - |
83 | | -# below are utils for WOQLQuery |
84 | | - |
85 | | - |
86 | | -def add_namespaces_to_variable(var): |
87 | | - """Adds namespace to given variable |
88 | | -
|
89 | | - Parameters |
90 | | - ---------- |
91 | | - var: str |
92 | | - Variable |
93 | | -
|
94 | | - Returns |
95 | | - ------- |
96 | | - Variable attached with namespace |
97 | | - """ |
98 | | - if var[:2] != "v:": |
99 | | - return "v:" + var |
100 | | - return var |
101 | | - |
102 | | - |
103 | | -def add_namespaces_to_variables(variables): |
104 | | - """Adds namespace to given variables |
105 | | -
|
106 | | - Parameters |
107 | | - ---------- |
108 | | - variables: list [str] |
109 | | -
|
110 | | - Returns |
111 | | - ------- |
112 | | - Variables attached with namespace |
113 | | - """ |
114 | | - nvars = [] |
115 | | - for v_item in variables: |
116 | | - nvars.append(add_namespaces_to_variable(v_item)) |
117 | | - return nvars |
118 | | - |
119 | | - |
120 | | -def empty(obj): |
121 | | - """* is the object empty? |
122 | | - * returns true if the json object is empty |
123 | | - """ |
124 | | - if not obj: |
125 | | - # Assume if it has a length property with a non-zero value |
126 | | - # that that property is correct. |
127 | | - return True |
128 | | - if len(obj) > 0: |
129 | | - return False |
130 | | - if len(obj) == 0: |
131 | | - return True |
132 | | - # Otherwise, does it have any properties of its own? |
133 | | - # if type(obj) == dict: |
134 | | - # for key in obj.keys(): |
135 | | - # if hasattr(obj,key): |
136 | | - # return False |
137 | | - return True |
138 | | - |
139 | | - |
140 | | -def shorten(url, prefixes=None): |
141 | | - """Get shortened url |
142 | | -
|
143 | | - Parameters |
144 | | - ---------- |
145 | | - url: str |
146 | | - prefixes: dict |
147 | | -
|
148 | | - Returns |
149 | | - ------- |
150 | | - Url with prefixes added and shortened |
151 | | - """ |
152 | | - prefixes = prefixes if prefixes else STANDARD_URLS |
153 | | - for pref, val in prefixes.items(): |
154 | | - short_url = url[: len(val)] |
155 | | - if val == short_url: |
156 | | - return f"{pref}:{short_url}" |
157 | | - return url |
158 | | - |
159 | | - |
160 | | -def is_data_type(stype): |
161 | | - """Checks if the given type is a datatype or not |
162 | | -
|
163 | | - Parameters |
164 | | - ---------- |
165 | | - stype: str |
166 | | -
|
167 | | - Returns |
168 | | - ------- |
169 | | - bool |
170 | | - """ |
171 | | - sh = shorten(stype) |
172 | | - if sh and (sh[:4] == "xsd:" or sh[:4] == "xdd:"): |
173 | | - return True |
174 | | - return False |
175 | | - |
176 | | - |
177 | | -def valid_url(string): |
178 | | - """Checks if the given url is valid |
179 | | -
|
180 | | - Parameters |
181 | | - ---------- |
182 | | - string: str |
183 | | - Url which needs to be validated |
184 | | -
|
185 | | - Returns |
186 | | - ------- |
187 | | - bool |
188 | | - """ |
189 | | - if string and (string[:7] == "http://" or string[:8] == "https://"): |
190 | | - return True |
191 | | - return False |
192 | | - |
193 | | - |
194 | | -def url_fraqment(url): |
195 | | - """Gets the url fragment |
196 | | -
|
197 | | - Parameters |
198 | | - ---------- |
199 | | - url: str |
200 | | -
|
201 | | - Returns |
202 | | - ------- |
203 | | - str |
204 | | - url fragment |
205 | | -
|
206 | | - """ |
207 | | - bits = url.split("#") |
208 | | - if len(bits) > 1: |
209 | | - return bits[1] |
210 | | - return url |
211 | | - |
212 | | - |
213 | | -def label_from_url(url): |
214 | | - """Get the label from url |
215 | | -
|
216 | | - Parameters |
217 | | - ---------- |
218 | | - url: str |
219 | | -
|
220 | | - Returns |
221 | | - ------- |
222 | | - str |
223 | | - Label |
224 | | - """ |
225 | | - nurl = url_fraqment(url) |
226 | | - nurl = nurl if nurl else url |
227 | | - last_char = nurl.rfind("/") |
228 | | - if last_char < len(nurl) - 1: |
229 | | - nurl = nurl[last_char + 1 :] |
230 | | - nurl = nurl.replace("_", " ") |
231 | | - return nurl[0].upper() + nurl[1:] |
232 | | - |
233 | 7 |
|
234 | 8 | def _result2stream(result): |
235 | 9 | """turning JSON string into a interable that give you a stream of dictionary""" |
|
0 commit comments