16
16
import logging
17
17
import os
18
18
import sys
19
+ import shutil
19
20
try :
20
21
import rdfextras #@UnusedImport
21
22
except ImportError , e :
@@ -36,11 +37,11 @@ class UnTabLinker(object):
36
37
'owl' :Namespace ('http://www.w3.org/2002/07/owl#' )
37
38
}
38
39
39
- def __init__ (self , filename , config , level = logging .DEBUG ):
40
+ def __init__ (self , directory , config , level = logging .DEBUG ):
40
41
"""TabLinker constructor
41
42
42
43
Keyword arguments:
43
- filename -- String containing the name of the current Excel file being examined
44
+ directory -- String containing the name turtle file
44
45
config -- Configuration object, loaded from .ini file
45
46
level -- A logging level as defined in the logging module
46
47
"""
@@ -52,19 +53,48 @@ def __init__(self, filename, config, level = logging.DEBUG):
52
53
self .log .debug ('Loading and parsing file' )
53
54
self .graph .parse (filename , format = config .get ('general' , 'format' ))
54
55
55
-
56
56
plugin .register ('sparql' , rdflib .query .Processor ,'rdfextras.sparql.processor' , 'Processor' )
57
57
plugin .register ('sparql' , rdflib .query .Result ,'rdfextras.sparql.query' , 'SPARQLQueryResult' )
58
58
59
- self .wbk = xlwt .Workbook ()
60
- #currently, we assume only 1 sheet per file
61
- self .sheet = self .wbk .add_sheet ('sheet 1' )
59
+
62
60
63
61
64
- def convertToXls (self ):
62
+ def saveFiles (self , directory ):
65
63
"""
66
64
Convert data in rdf to excel
65
+
66
+ Keyword arguments:
67
+ directory -- Directory to save files in
68
+ """
69
+
70
+ #Get file names from dataset
71
+
72
+
73
+
74
+ self .saveFile (directory , "filename" )
75
+
76
+
77
+
78
+ def saveFile (self , directory , filename ):
79
+ """
80
+ Retrieve information from graph, and save file
81
+ """
82
+ self .wbk = xlwt .Workbook ()
83
+
84
+
85
+ #Retrieve sheets for this file
86
+
87
+ #Save sheet in file object
88
+ self .addSheetToXls ("sheetname" )
89
+
90
+ self .wbk .save (directory + basename + '.xls' )
91
+
92
+ def addSheetToXls (self , sheetName ):
93
+ """
94
+ Get values for this sheet, and store in excel object
67
95
"""
96
+ self .sheet = self .wbk .add_sheet (sheetName )
97
+
68
98
#Get the row IDs from the RDF set
69
99
queryResult = self .graph .query (
70
100
"""SELECT DISTINCT ?cell ?value
@@ -78,32 +108,15 @@ def convertToXls(self):
78
108
initNs = self .namespaces
79
109
)
80
110
if (len (queryResult ) == 0 ):
81
- self .log .error ("No rows found in rdf set. Exiting..." )
82
- quit ()
111
+ self .log .error ("No rows found for sheet {0}" . format ( sheetName ) )
112
+
83
113
#Loop through cells and add values to excel
84
114
for resultRow in queryResult .result :
85
115
cell , value = resultRow
86
116
col , row = self .cellname2index (cell )
87
117
self .sheet .write (row , col , value )
88
-
89
-
90
- def addRowToXLS (self , rowID ):
91
- queryResult = self .graph .query (
92
- """SELECT DISTINCT ?col ?value
93
- WHERE {
94
- ?node <http://www.data2semantics.org/core/row> """ + str (rowID ) + """ .
95
- ?node
96
- ?node <http://www.data2semantics.org/core/col> ?col .
97
- } LIMIT 10""" ,
98
- #Can't use prefix d2s. This produces parsing error (event though namespace is defined).
99
- #A bug in the query parser I guess
100
- #Also, dont use [] in this query processor...
101
- initNs = self .namespaces
102
- )
103
- for resultRow in queryResult .result :
104
- col , value = resultRow
105
- self .sheet .write (rowID - 1 , self .excel2num (col ), value )
106
118
119
+
107
120
def cellname2index (self , cellname ):
108
121
matches = re .search ('([A-Z]*)([0-9]*)' ,cellname )
109
122
if (len (matches .groups ()) != 2 ):
@@ -157,10 +170,18 @@ def checkArg() :
157
170
logging .info ("Found {0} files to convert." .format (len (files )))
158
171
159
172
unLinker = UnTabLinker (filename , config , logLevel )
160
- unLinker . convertToXls ()
173
+
161
174
basename = os .path .basename (filename )
162
175
basename = re .search ('(.*)\.ttl' ,basename ).group (1 )
163
- unLinker .wbk .save (config .get ('paths' , 'targetFolder' ) + basename + '.xls' )
176
+ directory = config .get ('paths' , 'targetFolder' ) + basename + "/"
177
+
178
+ if (os .path .isdir (directory )) :
179
+ logging .debug ('Output dir {0} already exists. Deleting' .format (directory ))
180
+ shutil .rmtree (directory )
181
+ logging .debug ('Creating dir {0}' .format (directory ))
182
+ os .makedirs (directory )
183
+ unLinker .saveFiles (directory )
184
+
164
185
165
186
logging .info ("Done" )
166
187
0 commit comments