| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Run the pyRdfa package locally, ie, on a local file |
| 5 |
|
| 6 |
""" |
| 7 |
|
| 8 |
import sys, getopt, platform, logging |
| 9 |
|
| 10 |
sys.path.insert(0,"/Users/ivan/Library/Python") |
| 11 |
sys.path.insert(0,"/Users/ivan/Library/Python/RDFa") |
| 12 |
|
| 13 |
from pyRdfa import pyRdfa |
| 14 |
from pyRdfa.transform.metaname import meta_transform |
| 15 |
from pyRdfa.transform.OpenID import OpenID_transform |
| 16 |
from pyRdfa.transform.DublinCore import DC_transform |
| 17 |
from pyRdfa.options import Options |
| 18 |
|
| 19 |
extraTransformers = [ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
] |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
usageText="""Usage: %s -[vjxtnpzsb:g:ryle] [filename[s]] |
| 29 |
where: |
| 30 |
-x: output format RDF/XML |
| 31 |
-t: output format Turtle (default) |
| 32 |
-j: output format JSON-LD |
| 33 |
-n: output format N Triples |
| 34 |
-p: output format pretty RDF/XML |
| 35 |
-z: exceptions should be returned as graphs instead of exceptions raised |
| 36 |
-b: give the base URI; if a file name is given, this can be left empty and the file name is used |
| 37 |
-s: whitespace on plain literals are not preserved (default: preserved, per RDFa syntax document); this is a non-standard feture |
| 38 |
-l: run in RDFa 1.1 Lite mode (non-RDFa Lite generate warnings) (default: False) |
| 39 |
-r: report on the details of the vocabulary caching process |
| 40 |
-y: bypass the vocabulary cache checking, generate a new cache every time (good for debugging) (default:False) |
| 41 |
-v: perform vocabulary expansion (default: False) |
| 42 |
-g: value can be 'output', 'processor', 'output,processor' or 'processor,output'; controls which graphs are returned |
| 43 |
-e: embedded (in a <script> element) Turtle content or RDF/XML (in its own namespace) _not_ parsed and added to the output graph (default:True, i.e., parsed) |
| 44 |
-w: value is a filename, to be used instead of the default standard error for logging errors, warnings, etc. |
| 45 |
|
| 46 |
'Filename' can be a local file name or a URI. In case there is no filename, stdin is used. |
| 47 |
|
| 48 |
The -g option may be unnecessary, the script tries to make a guess based on a default xmlns value for XHTML or SVG. |
| 49 |
""" |
| 50 |
|
| 51 |
def usage() : |
| 52 |
print(usageText % sys.argv[0]) |
| 53 |
|
| 54 |
format = "turtle" |
| 55 |
extras = [] |
| 56 |
value = "" |
| 57 |
space_preserve = True |
| 58 |
base = "" |
| 59 |
value = [] |
| 60 |
rdfOutput = False |
| 61 |
output_default_graph = True |
| 62 |
output_processor_graph = True |
| 63 |
vocab_cache_report = False |
| 64 |
refresh_vocab_cache = False |
| 65 |
vocab_expansion = False |
| 66 |
vocab_cache = True |
| 67 |
embedded_rdf = True |
| 68 |
check_lite = False |
| 69 |
log = None |
| 70 |
|
| 71 |
try : |
| 72 |
opts, value = getopt.getopt(sys.argv[1:],"vxetjnpzsb:g:rylw:",['graph=']) |
| 73 |
for o,a in opts: |
| 74 |
if o == "-t" : |
| 75 |
format = "turtle" |
| 76 |
elif o == "-n" : |
| 77 |
format = "nt" |
| 78 |
elif o == "-j" : |
| 79 |
format = "json-ld" |
| 80 |
elif o == "-p" or o == "-x": |
| 81 |
format = "pretty-xml" |
| 82 |
elif o == "-z" : |
| 83 |
rdfOutput = True |
| 84 |
elif o == "-b" : |
| 85 |
base = a |
| 86 |
elif o == "-e" : |
| 87 |
embedded_rdf = False |
| 88 |
elif o == "-s" : |
| 89 |
space_preserve = False |
| 90 |
elif o == "-l" : |
| 91 |
check_lite = True |
| 92 |
elif o == "-r" : |
| 93 |
vocab_cache_report = True |
| 94 |
elif o == "-v" : |
| 95 |
vocab_expansion = True |
| 96 |
elif o == "-y" : |
| 97 |
bypass_vocab_cache = True |
| 98 |
elif o in ("-g", "--graph") : |
| 99 |
if a == "processor" : |
| 100 |
output_default_graph = False |
| 101 |
output_processor_graph = True |
| 102 |
elif a == "processor,default" or a == "default,processor" : |
| 103 |
output_processor_graph = True |
| 104 |
elif a == "default" : |
| 105 |
output_default_graph = True |
| 106 |
output_processor_graph = False |
| 107 |
elif o == "-w" : |
| 108 |
log = a |
| 109 |
else : |
| 110 |
usage() |
| 111 |
sys.exit(1) |
| 112 |
except : |
| 113 |
usage() |
| 114 |
sys.exit(1) |
| 115 |
|
| 116 |
if log is not None : |
| 117 |
logging.basicConfig(filename=log) |
| 118 |
else : |
| 119 |
logging.basicConfig() |
| 120 |
|
| 121 |
options = Options(output_default_graph = output_default_graph, |
| 122 |
output_processor_graph = output_processor_graph, |
| 123 |
space_preserve=space_preserve, |
| 124 |
transformers = extras, |
| 125 |
embedded_rdf = embedded_rdf, |
| 126 |
vocab_expansion = vocab_expansion, |
| 127 |
vocab_cache = vocab_cache, |
| 128 |
vocab_cache_report = vocab_cache_report, |
| 129 |
refresh_vocab_cache = refresh_vocab_cache, |
| 130 |
check_lite = check_lite, |
| 131 |
experimental_features = True |
| 132 |
) |
| 133 |
|
| 134 |
processor = pyRdfa(options, base) |
| 135 |
if len(value) >= 1 : |
| 136 |
print(processor.rdf_from_sources(value, outputFormat = format, rdfOutput = rdfOutput)) |
| 137 |
else : |
| 138 |
print(processor.rdf_from_source(sys.stdin, outputFormat = format, rdfOutput = rdfOutput)) |
| 139 |
|
| 140 |
|