-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppData.py
More file actions
78 lines (60 loc) · 2.56 KB
/
AppData.py
File metadata and controls
78 lines (60 loc) · 2.56 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
from collections import defaultdict
import os
import owlready2
import urllib.request
import json
from CommonFunctions import getPathsToElement, getOntologyFilePath
from Constants import ONTOLOGY, PROPERTIES, TERM_ID, ITEMS, TOP_SCHEMA_FN
class AppData:
def __init__(self, ontologies):
self._pathsWithOntologyUrls = defaultdict(list)
self._ontologies = ontologies
def getPathsWithOntologyUrls(self):
return self._pathsWithOntologyUrls
def getOntologies(self):
return self._ontologies
def initApp(self, data, tmpDir=None):
if tmpDir:
schemas = {}
for filename in os.listdir(tmpDir):
if filename.endswith(".json"):
with open(os.path.join(tmpDir, filename)) as schemaFile:
schema = json.load(schemaFile)
schemas[filename] = schema
pathsToElement = getPathsToElement(TERM_ID, data=schemas[TOP_SCHEMA_FN], schemas=schemas)
else:
schemaUrl = data['@schema']
pathsToElement = getPathsToElement(TERM_ID, url=schemaUrl)
ontologyUrlsMap = self._getOntologyUrlsFromSchema(pathsToElement)
self._pathsWithOntologyUrls = ontologyUrlsMap
self._downloadOntologyFiles()
def _downloadOntologyFiles(self):
ontologyUrls = set()
for path, ontoUrls in self._pathsWithOntologyUrls:
for ontoUrl in ontoUrls:
ontologyUrls.add(ontoUrl)
for url in ontologyUrls:
path = getOntologyFilePath(url)
if not os.path.exists(path):
print('downloading: ' + url)
ontoFile, _ = urllib.request.urlretrieve(url, path)
print('downloaded: ' + url)
#ontoFile, _ = urllib.request.urlretrieve(url, path)
if url not in self._ontologies:
print('loading: ' + url)
ontology = owlready2.get_ontology(path)
ontology.load()
print('loaded: ' + url)
self._ontologies[url] = ontology
def _getOntologyUrlsFromSchema(self, paths):
pathsAndUrls = []
for url, path, val in paths:
ontologyUrls = []
if ONTOLOGY in val:
ontologyUrls = val[ONTOLOGY]
if not isinstance(ontologyUrls, list):
ontologyUrls = [ontologyUrls]
newPath = [p for p in path if p != PROPERTIES and p != ITEMS]
if ontologyUrls:
pathsAndUrls.append((newPath, ontologyUrls))
return pathsAndUrls