-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsparqltex.py
executable file
·99 lines (92 loc) · 3.34 KB
/
sparqltex.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
#!/usr/bin/env python
from SPARQLWrapper import SPARQLWrapper, JSON
import sys, os
import urllib
import simplejson
## Edit ad needed
PDFLATEX = '/usr/texbin/pdflatex'
## Do not edit (or change accordingly in sparql.sty)
TMPFILE = './tmpres'
def prefix(prefixes, query):
## Config file should go there
prefixes = """
PREFIX bibtex: <http://zeitkunst.org/bibtex/0.1/bibtex.owl#>
PREFIX bio: <http://purl.org/vocab/bio/0.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX swc: <http://data.semanticweb.org/ns/swc/ontology#>
PREFIX swcp: <http://apassant.net/home/2010/05/swcp/ns#>
PREFIX swrc: <http://swrc.ontoware.org/ontology#>
PREFIX vcard: <http://www.w3.org/2006/vcard/ns#>
PREFIX ical: <http://www.w3.org/2002/12/cal/icaltzd#>
PREFIX dc: <http://purl.org/dc/terms/>
"""
return "%s %s" %(prefixes, query)
## Run the SPARQL query
def sparql(query, template, tlist, **kwargs):
## Get prefixes
query = prefix('', input.query)
## Run through a SPARQL endpoint with curl
if 'endpoint' in kwargs.keys():
endpoint = kwargs['endpoint']
url = "%s%s" %(endpoint, urllib.quote_plus(query))
raw_json = urllib.urlopen(url).read()
## Run through a RDF file with roqet + any23 (helps to clean messy HTML)
elif 'data' in kwargs.keys():
data = kwargs['data']
data = "'http://api.sindice.com/any23/any23/?format=best&uri=%s'" %urllib.quote_plus(data)
cmd = 'roqet -e "%s" -D %s -r json 2>/dev/null' %(query, data)
raw_json = os.popen(cmd).read()
## Get JSON in Python object and render
json = simplejson.loads(raw_json)
return sparql_template(json, template, tlist)
## Transform JSON into LaTeX, using the template
def sparql_template(json, template, tlist):
## Get vars and data
vars = json['head']['vars']
res = json['results']['bindings']
## Render output
l = ''
s = ''
if len(res) > 1:
l = "\\begin{%s}\n" %tlist
s = '\item '
for r in res:
foo = {}
for v in vars:
## TODO - get a list of accentuated chars
value = r[v]['value']
value = value.replace('é', "\'{e}")
value = value.replace('&', '\\&')
value = value.replace('- ', '\item ')
foo[str(v)] = value
l += s + template % foo + '\n'
if len(res) > 1:
l += "\end{%s}\n" %tlist
return l
## RUN
if __name__ == "__main__":
argv = sys.argv
if len(argv) == 1:
print """
Usage: sparqltex foobar.tex
- foobar.tex : the TeX file to compile
See http://github.com/terraces/sparqltex for details
"""
else:
if argv[1] == '-run':
input = __import__(argv[2])
## List type
tlist = input.tlist if 'tlist' in dir(input) else 'itemize'
## Endpoint versus data
if 'endpoint' in dir(input):
l = sparql(input.query, input.template, tlist, endpoint = input.endpoint)
elif 'data' in dir(input):
l = sparql(input.query, input.template, tlist, data = input.data)
file = open(TMPFILE, 'w')
file.write(l.encode('utf-8'))
file.close
else:
cmd = """%s -shell-escape %s""" %(PDFLATEX, argv[1])
os.system(cmd)