Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/rdflib/plugins/parsers/pyRdfa/options.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 # -*- coding: utf-8 -*- | |
2 """ | |
3 L{Options} class: collect the possible options that govern the parsing possibilities. The module also includes the L{ProcessorGraph} class that handles the processor graph, per RDFa 1.1 (i.e., the graph containing errors and warnings). | |
4 | |
5 @summary: RDFa parser (distiller) | |
6 @requires: U{RDFLib<http://rdflib.net>} | |
7 @organization: U{World Wide Web Consortium<http://www.w3.org>} | |
8 @author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">} | |
9 @license: This software is available for use under the | |
10 U{W3C SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231">} | |
11 """ | |
12 | |
13 """ | |
14 $Id: options.py,v 1.20 2013-10-16 11:48:54 ivan Exp $ $Date: 2013-10-16 11:48:54 $ | |
15 """ | |
16 | |
17 import sys, datetime | |
18 | |
19 import rdflib | |
20 from rdflib import URIRef | |
21 from rdflib import Literal | |
22 from rdflib import BNode | |
23 from rdflib import Namespace | |
24 if rdflib.__version__ >= "3.0.0" : | |
25 from rdflib import Graph | |
26 from rdflib import RDF as ns_rdf | |
27 from rdflib import RDFS as ns_rdfs | |
28 else : | |
29 from rdflib.Graph import Graph | |
30 from rdflib.RDFS import RDFSNS as ns_rdfs | |
31 from rdflib.RDF import RDFNS as ns_rdf | |
32 | |
33 from .host import HostLanguage, MediaTypes, content_to_host_language, predefined_1_0_rel, require_embedded_rdf | |
34 from . import ns_xsd, ns_distill, ns_rdfa | |
35 from . import RDFA_Error, RDFA_Warning, RDFA_Info | |
36 from .transform.lite import lite_prune | |
37 | |
38 ns_dc = Namespace("http://purl.org/dc/terms/") | |
39 ns_ht = Namespace("http://www.w3.org/2006/http#") | |
40 | |
41 class ProcessorGraph : | |
42 """Wrapper around the 'processor graph', ie, the (RDF) Graph containing the warnings, | |
43 error messages, and informational messages. | |
44 """ | |
45 def __init__(self) : | |
46 self.graph = Graph() | |
47 | |
48 def add_triples(self, msg, top_class, info_class, context, node) : | |
49 """ | |
50 Add an error structure to the processor graph: a bnode with a number of predicates. The structure | |
51 follows U{the processor graph vocabulary<http://www.w3.org/2010/02/rdfa/wiki/Processor_Graph_Vocabulary>} as described | |
52 on the RDFa WG Wiki page. | |
53 | |
54 @param msg: the core error message, added as an object to a dc:description | |
55 @param top_class: Error, Warning, or Info; an explicit rdf:type added to the bnode | |
56 @type top_class: URIRef | |
57 @param info_class: An additional error class, added as an rdf:type to the bnode in case it is not None | |
58 @type info_class: URIRef | |
59 @param context: An additional information added, if not None, as an object with rdfa:context as a predicate | |
60 @type context: either an URIRef or a URI String (an URIRef will be created in the second case) | |
61 @param node: The node's element name that contains the error | |
62 @type node: string | |
63 @return: the bnode that serves as a subject for the errors. The caller may add additional information | |
64 @rtype: BNode | |
65 """ | |
66 # Lazy binding of relevant prefixes | |
67 self.graph.bind("dcterms", ns_dc) | |
68 self.graph.bind("pyrdfa", ns_distill) | |
69 self.graph.bind("rdf", ns_rdf) | |
70 self.graph.bind("rdfa", ns_rdfa) | |
71 self.graph.bind("ht", ns_ht) | |
72 self.graph.bind("xsd", ns_xsd) | |
73 # Python 3 foolproof way | |
74 try : | |
75 is_context_string = isinstance(context, str) | |
76 except : | |
77 is_context_string = isinstance(context, str) | |
78 | |
79 bnode = BNode() | |
80 | |
81 if node != None: | |
82 try : | |
83 full_msg = "[In element '%s'] %s" % (node.nodeName, msg) | |
84 except : | |
85 full_msg = "[In element '%s'] %s" % (node, msg) | |
86 else : | |
87 full_msg = msg | |
88 | |
89 self.graph.add((bnode, ns_rdf["type"], top_class)) | |
90 if info_class : | |
91 self.graph.add((bnode, ns_rdf["type"], info_class)) | |
92 self.graph.add((bnode, ns_dc["description"], Literal(full_msg))) | |
93 self.graph.add((bnode, ns_dc["date"], Literal(datetime.datetime.utcnow().isoformat(),datatype=ns_xsd["dateTime"]))) | |
94 if context and (isinstance(context,URIRef) or is_context_string): | |
95 htbnode = BNode() | |
96 self.graph.add( (bnode, ns_rdfa["context"],htbnode) ) | |
97 self.graph.add( (htbnode, ns_rdf["type"], ns_ht["Request"]) ) | |
98 self.graph.add( (htbnode, ns_ht["requestURI"], Literal("%s" % context)) ) | |
99 return bnode | |
100 | |
101 def add_http_context(self, subj, http_code) : | |
102 """ | |
103 Add an additional HTTP context to a message with subject in C{subj}, using the U{<http://www.w3.org/2006/http#>} | |
104 vocabulary. Typically used to extend an error structure, as created by L{add_triples}. | |
105 | |
106 @param subj: an RDFLib resource, typically a blank node | |
107 @param http_code: HTTP status code | |
108 """ | |
109 bnode = BNode() | |
110 self.graph.add((subj, ns_rdfa["context"], bnode)) | |
111 self.graph.add((bnode, ns_rdf["type"], ns_ht["Response"])) | |
112 self.graph.add((bnode, ns_ht["responseCode"], URIRef("http://www.w3.org/2006/http#%s" % http_code))) | |
113 | |
114 class Options : | |
115 """Settable options. An instance of this class is stored in | |
116 the L{execution context<ExecutionContext>} of the parser. | |
117 | |
118 @ivar space_preserve: whether plain literals should preserve spaces at output or not | |
119 @type space_preserve: Boolean | |
120 | |
121 @ivar output_default_graph: whether the 'default' graph should be returned to the user | |
122 @type output_default_graph: Boolean | |
123 | |
124 @ivar output_processor_graph: whether the 'processor' graph should be returned to the user | |
125 @type output_processor_graph: Boolean | |
126 | |
127 @ivar processor_graph: the 'processor' Graph | |
128 @type processor_graph: L{ProcessorGraph} | |
129 | |
130 @ivar transformers: extra transformers | |
131 @type transformers: list | |
132 | |
133 @ivar vocab_cache_report: whether the details of vocabulary file caching process should be reported as information (mainly for debug) | |
134 @type vocab_cache_report: Boolean | |
135 | |
136 @ivar refresh_vocab_cache: whether the caching checks of vocabs should be by-passed, ie, if caches should be re-generated regardless of the stored date (important for vocab development) | |
137 @type refresh_vocab_cache: Boolean | |
138 | |
139 @ivar embedded_rdf: whether embedded RDF (ie, turtle in an HTML script element or an RDF/XML content in SVG) should be extracted and added to the final graph. This is a non-standard option... | |
140 @type embedded_rdf: Boolean | |
141 | |
142 @ivar vocab_expansion: whether the @vocab elements should be expanded and a mini-RDFS processing should be done on the merged graph | |
143 @type vocab_expansion: Boolean | |
144 | |
145 @ivar vocab_cache: whether the system should use the vocabulary caching mechanism when expanding via the mini-RDFS, or should just fetch the graphs every time | |
146 @type vocab_cache: Boolean | |
147 | |
148 @ivar host_language: the host language for the RDFa attributes. Default is HostLanguage.xhtml, but it can be HostLanguage.rdfa_core and HostLanguage.html5, or others... | |
149 @type host_language: integer (logically: an enumeration) | |
150 | |
151 @ivar content_type: the content type of the host file. Default is None | |
152 @type content_type: string (logically: an enumeration) | |
153 | |
154 @ivar add_informational_messages: whether informational messages should also be added to the processor graph, or only errors and warnings | |
155 | |
156 @ivar experimental_features: whether experimental features should be activated; that is a developer's option... | |
157 @ivar check_lite: whether RDFa Lite should be checked, to generate warnings. | |
158 """ | |
159 def __init__(self, output_default_graph = True, | |
160 output_processor_graph = False, | |
161 space_preserve = True, | |
162 transformers = [], | |
163 embedded_rdf = True, | |
164 vocab_expansion = False, | |
165 vocab_cache = True, | |
166 vocab_cache_report = False, | |
167 refresh_vocab_cache = False, | |
168 add_informational_messages = False, | |
169 check_lite = False, | |
170 experimental_features = False | |
171 ) : | |
172 self.space_preserve = space_preserve | |
173 self.transformers = transformers | |
174 self.processor_graph = ProcessorGraph() | |
175 self.output_default_graph = output_default_graph | |
176 self.output_processor_graph = output_processor_graph | |
177 self.host_language = HostLanguage.rdfa_core | |
178 self.vocab_cache_report = vocab_cache_report | |
179 self.refresh_vocab_cache = refresh_vocab_cache | |
180 self.embedded_rdf = embedded_rdf | |
181 self.vocab_expansion = vocab_expansion | |
182 self.vocab_cache = vocab_cache | |
183 self.add_informational_messages = add_informational_messages | |
184 self.check_lite = check_lite | |
185 if check_lite : | |
186 self.transformers.append(lite_prune) | |
187 self.experimental_features = experimental_features | |
188 | |
189 def set_host_language(self, content_type) : | |
190 """ | |
191 Set the host language for processing, based on the recognized types. If this is not a recognized content type, | |
192 it falls back to RDFa core (i.e., XML) | |
193 @param content_type: content type | |
194 @type content_type: string | |
195 """ | |
196 if content_type in content_to_host_language : | |
197 self.host_language = content_to_host_language[content_type] | |
198 else : | |
199 self.host_language = HostLanguage.rdfa_core | |
200 | |
201 if self.host_language in require_embedded_rdf : | |
202 self.embedded_rdf = True | |
203 | |
204 def __str__(self) : | |
205 retval = """Current options: | |
206 preserve space : %s | |
207 output processor graph : %s | |
208 output default graph : %s | |
209 host language : %s | |
210 accept embedded RDF : %s | |
211 check rdfa lite : %s | |
212 cache vocabulary graphs : %s | |
213 """ | |
214 return retval % (self.space_preserve, self.output_processor_graph, self.output_default_graph, self.host_language, self.embedded_rdf, self.check_lite, self.vocab_cache) | |
215 | |
216 def reset_processor_graph(self): | |
217 """Empty the processor graph. This is necessary if the same options is reused | |
218 for several RDFa sources, and new error messages should be generated. | |
219 """ | |
220 self.processor_graph.graph.remove((None,None,None)) | |
221 | |
222 def add_warning(self, txt, warning_type=None, context=None, node=None, buggy_value=None) : | |
223 """Add a warning to the processor graph. | |
224 @param txt: the warning text. | |
225 @keyword warning_type: Warning Class | |
226 @type warning_type: URIRef | |
227 @keyword context: possible context to be added to the processor graph | |
228 @type context: URIRef or String | |
229 @keyword buggy_value: a special case when a 'term' is not recognized; no warning is generated for that case if the value is part of the 'usual' XHTML terms, because almost all RDFa file contains some of those and that would pollute the output | |
230 @type buggy_value: String | |
231 """ | |
232 if warning_type == ns_rdfa["UnresolvedTerm"] and buggy_value in predefined_1_0_rel : | |
233 return | |
234 return self.processor_graph.add_triples(txt, RDFA_Warning, warning_type, context, node) | |
235 | |
236 def add_info(self, txt, info_type=None, context=None, node=None, buggy_value=None) : | |
237 """Add an informational comment to the processor graph. | |
238 @param txt: the information text. | |
239 @keyword info_type: Info Class | |
240 @type info_type: URIRef | |
241 @keyword context: possible context to be added to the processor graph | |
242 @type context: URIRef or String | |
243 @keyword buggy_value: a special case when a 'term' is not recognized; no information is generated for that case if the value is part of the 'usual' XHTML terms, because almost all RDFa file contains some of those and that would pollute the output | |
244 @type buggy_value: String | |
245 """ | |
246 if self.add_informational_messages : | |
247 return self.processor_graph.add_triples(txt, RDFA_Info, info_type, context, node) | |
248 else : | |
249 return | |
250 | |
251 def add_error(self, txt, err_type=None, context=None, node=None, buggy_value=None) : | |
252 """Add an error to the processor graph. | |
253 @param txt: the information text. | |
254 @keyword err_type: Error Class | |
255 @type err_type: URIRef | |
256 @keyword context: possible context to be added to the processor graph | |
257 @type context: URIRef or String | |
258 @keyword buggy_value: a special case when a 'term' is not recognized; no error is generated for that case if the value is part of the 'usual' XHTML terms, because almost all RDFa file contains some of those and that would pollute the output | |
259 @type buggy_value: String | |
260 """ | |
261 return self.processor_graph.add_triples(txt, RDFA_Error, err_type, context, node) | |
262 |