-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertToStore.py
More file actions
92 lines (78 loc) · 2.43 KB
/
convertToStore.py
File metadata and controls
92 lines (78 loc) · 2.43 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
import rdflib.plugins.parsers.nquads as quads
from rdflib.plugin import Plugin
import rdflib.plugins.stores.berkeleydb
from rdflib import Graph
import berkeleydb
import rdflib.parser as rdfParser
import rdflib.plugins.parsers.ntriples as triples
import sys
import re
import pickle
import os
print(berkeleydb.db)
print(rdflib.plugins.stores.berkeleydb.has_bsddb)
#1 attribute based collection"
#2 class based collection
#3 property type collection
g = rdflib.ConjunctiveGraph('BerkeleyDB', identifier='quad1')
parseq = quads.NQuadsParser()
arg = sys.argv
file = arg[1]
name = arg[2]
store = arg[3]
storeList = {}
if os.path.isfile("DB/lists.pickle"):
storeList = pickle.load( open( "DB/lists.pickle", "rb" ) )
else:
storeList["triplets"] = []
storeList["quads"] = []
storeList["AC"] = []
storeList["CC"] = []
storeList["ACC"] = []
try:
store_dir = ""
if store == "4":
print("Quads")
store_file = "DB/quads/"+name
elif store == "3":
print("Triplets")
store_file = "DB/triplets/"+name
else:
raise Exception
if name in storeList["triplets"] or name in storeList["quads"] or name in storeList["AC"] or name in storeList["CC"] or name in storeList["ACC"]:
print("Name already taken!")
else:
script_dir = os.path.dirname(__file__)
path = os.path.join(script_dir, file)
pathFile = os.path.join(script_dir, store_file)
readFile = open(path, 'rt' )
g.open(pathFile, create=True)
size = 0
i= 0
errNo = 0
for line in readFile.readlines():
i +=1
if line.isspace() or not line:
break
else:
size += 1
insert = ""
tri = ""
graph = ""
if store == "3":
tri = line
else:
tri = re.split(r"<[^<>]+>\s+\.",line)[0]
graph = re.search(r"<[^<>]+>\s+\.", line).group()[:-1]
if store == "4":
insert = "INSERT DATA { GRAPH "+graph+" {"+tri+"} } "
else:
insert = "INSERT DATA {"+tri+"} "
g.update(insert)
if store == "4":
storeList["quads"].append(name)
else:
storeList["triplets"].append(name)
pickle.dump( storeList, open( "DB/lists.pickle", "wb" ) )
except Exception:
print(( "Triplets(3) or Quads(4)! Or another error!"))