-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
61 lines (50 loc) · 1.61 KB
/
helpers.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
from rdflib import Graph
from conversor.subjects import Subject
def get_graph_from_message(message) -> Graph:
"""
Given a message in a String, this function will return a Graph parsing the message. It will be used to read data
from Kakfa.
:param message: message to be parsed as RDF
:return: Graph representation of the RDF input data.
"""
g = Graph()
g.parse(data=message)
return g
def get_graph(filename) -> Graph:
"""
Helper function which opens a RDF file and returns its RDF Graph
:param filename:
:return: rdflib.Graph
"""
g = Graph()
with open(filename, "r", encoding='UTF-8') as f:
g.parse(file=f)
return g
def encode_url(uri_string: str) -> str:
"""
This function will provide an URL properly encoded to be sent as a paramater in the HTTP repests.
:param uri_string:
:return:
"""
result = ''
accepted = [c for c in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~'.encode('utf-8')]
for char in uri_string.encode('utf-8'):
result += chr(char) if char in accepted else '%{}'.format(hex(char)[2:]).upper()
return result
def apply_filters(subject: Subject, filter: set) -> bool:
"""
Whis function will filter a Subject according to a set of conditions.
:param subject:
:param filter:
:return: boolean - Whether if it is valid or not.
"""
if filter is None:
return True
r = True
if len(filter) > 0:
for f in filter:
prefix = subject.prefix
r = r and eval(f)
if not r:
break
return r