-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.py
198 lines (168 loc) · 4.63 KB
/
service.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
""" Service to extract text from a file using Tika
` Author: Bontenakel Lenny - [email protected]
Created: 23 September 2022
"""
import os
import logging
logging.basicConfig(level = os.environ.get("LOGLEVEL", "INFO"))
import tika #https://github.com/chrismattmann/tika
tika.initVM()
from tika import parser
from string import Template
from helpers import query
from escape_helpers import *
CUSTOM_QUERY_PATH = os.environ.get("CUSTOM_QUERY_PATH", "")
""" index physical file
@params:
- uri: uri of a physical file
"""
def indexFile(uri, overwrite=False):
virtualFileURI = queryDataSource(uri)
if virtualFileURI == "":
logging.info(f"No Datasource found for {uri}. Skipping.")
return f"No Datasource found for {uri}. Skipping."
if not overwrite:
content = queryContent(virtualFileURI)
if(content != ""):
logging.info(f'{uri} already indexed.')
return f'{uri} already indexed.'
path = uri.replace('share://', '/share/')
try:
fileContent = parser.from_file(path)["content"]
except FileNotFoundError as e:
logging.exception(e)
except Exception as e:
logging.exception(e)
raise e
try:
saveContent(virtualFileURI, fileContent)
logging.info(f'{uri} successfully indexed.')
return f'{uri} successfully indexed.'
except Exception as e:
logging.error(e)
raise e
""" Index saved physical files
"""
def indexAll(overwrite=False):
uris = queryFileURIs()
physicalURIs = [ i for i in uris if 'share://' in i ]
skippedFiles = []
for i in physicalURIs:
logging.info(f"INDEXING {i}")
try:
indexFile(i, overwrite=overwrite)
except RuntimeError as e:
return "Runtime error"
except Exception:
skippedFiles.append(i)
logging.info("Finished")
return "Finished"
""" save text from a file into a triple store
@params:
- uri: uri of the virtual file to save the text on
- content: extracted text to save
TODO: use a query parser.
"""
def saveContent(uri, content, graph=os.environ.get("DEFAULT_GRAPH")):
if(CUSTOM_QUERY_PATH != ""):
try:
with open(CUSTOM_QUERY_PATH, "r") as customQuery:
s = customQuery.read()
query_template = Template(s)
except Exception as e:
logging.exception(e)
raise e
else:
query_template = Template("""
PREFIX sioc: <http://rdfs.org/sioc/ns#>
DELETE {
GRAPH $graph {
$uri sioc:content ?o .
}
}
INSERT {
GRAPH $graph {
$uri sioc:content $content .
}
}"""
)
logging.info(f'query template: {query_template.__repr__()}')
query_string = query_template.substitute(
uri=sparql_escape_uri(uri),
content=sparql_escape_string(content),
graph=sparql_escape_uri(graph),
)
query_result = query(query_string)
return query_result
""" get the uuids of all physical files
@returns: list of uuids
"""
def queryFileURIs():
query_string = """
prefix nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>
select distinct(?uri) {
?uri a nfo:FileDataObject .
}"""
query_result = query(query_string)['results']['bindings']
fileUuids = [ i['uri']['value'] for i in query_result ]
return fileUuids
""" Find the physical path of a file based on its uuid
@params:
- uri: uri of a file
@returns:
- fileName: name of the file
"""
def queryFileName(uri):
query_string = Template("""
prefix nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#>
select ?fileName {
$uri nfo:fileName ?fileName .
}"""
).substitute(
uri=sparql_escape_uri(uri)
)
query_result = query(query_string)['results']['bindings']
if(len(query_result) <= 0):
return ""
fileName = query_result[0]['fileName']['value']
return fileName
""" Get the data source of a physical file
@params:
- uri: uri of a physical file
@returns:
- dataSource: IRI of corresponding virtual file
"""
def queryDataSource(uri):
query_string = Template("""
prefix nie: <http://www.semanticdesktop.org/ontologies/2007/01/19/nie#>
select ?dataSource {
$uri nie:dataSource ?dataSource .
}"""
).substitute(
uri=sparql_escape_uri(uri)
)
query_result = query(query_string)['results']['bindings']
if(len(query_result) <= 0):
return ""
dataSource = query_result[0]['dataSource']['value']
return dataSource
""" check if a file is already indexed
@params:
- uri: uri of a virtual file
@returns:
- content: content of a file
"""
def queryContent(uri):
query_string = Template("""
PREFIX sioc: <http://rdfs.org/sioc/ns#>
select ?content {
$uri sioc:content ?content .
}"""
).substitute(
uri=sparql_escape_uri(uri)
)
query_result = query(query_string)['results']['bindings']
if(len(query_result) <= 0):
return ""
content = query_result[0]['content']['value']
return content