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
|
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
A commandline tool for parsing RDF in different formats and serializing the
resulting graph to a chosen format.
"""
from __future__ import absolute_import
import sys
from optparse import OptionParser
import logging
import rdflib
from rdflib import plugin
from rdflib.store import Store
from rdflib.graph import ConjunctiveGraph
from rdflib.namespace import RDF, RDFS, OWL, XSD
from rdflib.parser import Parser
from rdflib.serializer import Serializer
from rdflib.util import guess_format
from six import PY3
DEFAULT_INPUT_FORMAT = 'xml'
DEFAULT_OUTPUT_FORMAT = 'n3'
def parse_and_serialize(input_files, input_format, guess,
outfile, output_format, ns_bindings,
store_conn="", store_type=None):
if store_type:
store = plugin.get(store_type, Store)()
store.open(store_conn)
graph = ConjunctiveGraph(store)
else:
store = None
graph = ConjunctiveGraph()
for prefix, uri in ns_bindings.items():
graph.namespace_manager.bind(prefix, uri, override=False)
for fpath in input_files:
use_format, kws = _format_and_kws(input_format)
if fpath == '-':
fpath = sys.stdin
elif not input_format and guess:
use_format = guess_format(fpath) or DEFAULT_INPUT_FORMAT
graph.parse(fpath, format=use_format, **kws)
if outfile:
output_format, kws = _format_and_kws(output_format)
kws.setdefault('base', None)
graph.serialize(destination=outfile, format=output_format, **kws)
if store:
store.rollback()
def _format_and_kws(fmt):
"""
>>> _format_and_kws("fmt")
('fmt', {})
>>> _format_and_kws("fmt:+a")
('fmt', {'a': True})
>>> _format_and_kws("fmt:a")
('fmt', {'a': True})
>>> _format_and_kws("fmt:+a,-b") #doctest: +SKIP
('fmt', {'a': True, 'b': False})
>>> _format_and_kws("fmt:c=d")
('fmt', {'c': 'd'})
>>> _format_and_kws("fmt:a=b:c")
('fmt', {'a': 'b:c'})
"""
fmt, kws = fmt, {}
if fmt and ':' in fmt:
fmt, kwrepr = fmt.split(':', 1)
for kw in kwrepr.split(','):
if '=' in kw:
k, v = kw.split('=')
kws[k] = v
elif kw.startswith('-'):
kws[kw[1:]] = False
elif kw.startswith('+'):
kws[kw[1:]] = True
else: # same as "+"
kws[kw] = True
return fmt, kws
def make_option_parser():
parser_names = _get_plugin_names(Parser)
serializer_names = _get_plugin_names(Serializer)
kw_example = "FORMAT:(+)KW1,-KW2,KW3=VALUE"
oparser = OptionParser(
"%prog [-h] [-i INPUT_FORMAT] [-o OUTPUT_FORMAT] " +
"[--ns=PFX=NS ...] [-] [FILE ...]",
description=__doc__.strip() + (
" Reads file system paths, URLs or from stdin if '-' is given."
" The result is serialized to stdout."),
version="%prog " + "(using rdflib %s)" % rdflib.__version__)
oparser.add_option(
'-i', '--input-format',
type=str, # default=DEFAULT_INPUT_FORMAT,
help="Format of the input document(s)."
" Available input formats are: %s." % parser_names +
" If no format is given, it will be " +
"guessed from the file name extension." +
" Keywords to parser can be given after format like: %s." % kw_example,
metavar="INPUT_FORMAT")
oparser.add_option(
'-o', '--output-format',
type=str, default=DEFAULT_OUTPUT_FORMAT,
help="Format of the graph serialization."
" Available output formats are: %s."
% serializer_names +
" Default format is: '%default'." +
" Keywords to serializer can be given after format like: %s." %
kw_example,
metavar="OUTPUT_FORMAT")
oparser.add_option(
'--ns',
action="append", type=str,
help="Register a namespace binding (QName prefix to a base URI). "
"This can be used more than once.",
metavar="PREFIX=NAMESPACE")
oparser.add_option(
'--no-guess', dest='guess',
action='store_false', default=True,
help="Don't guess format based on file suffix.")
oparser.add_option(
'--no-out',
action='store_true', default=False,
help="Don't output the resulting graph " +
"(useful for checking validity of input).")
oparser.add_option(
'-w', '--warn',
action='store_true', default=False,
help="Output warnings to stderr (by default only critical errors).")
return oparser
def _get_plugin_names(kind): return ", ".join(
p.name for p in plugin.plugins(kind=kind))
def main():
oparser = make_option_parser()
opts, args = oparser.parse_args()
if len(args) < 1:
oparser.print_usage()
oparser.exit()
if opts.warn:
loglevel = logging.WARNING
else:
loglevel = logging.CRITICAL
logging.basicConfig(level=loglevel)
ns_bindings = {}
if opts.ns:
for ns_kw in opts.ns:
pfx, uri = ns_kw.split('=')
ns_bindings[pfx] = uri
outfile = sys.stdout
if PY3:
outfile = sys.stdout.buffer
if opts.no_out:
outfile = None
parse_and_serialize(args, opts.input_format, opts.guess,
outfile, opts.output_format, ns_bindings)
if __name__ == "__main__":
main()
|