Permalink
Browse files

Chasing unnecessary 'bind' statements to avoid unnecessary prefix dec…

…larations at the output
  • Loading branch information...
1 parent 39d3bf2 commit 483da940105c5dc43c3ca8fa263aa3b459ca8614 @iherman iherman committed Oct 16, 2013
Showing with 49 additions and 47 deletions.
  1. +2 −2 pyRdfa/__init__.py
  2. +8 −8 pyRdfa/host/__init__.py
  3. +8 −6 pyRdfa/options.py
  4. +2 −2 pyRdfa/state.py
  5. +28 −28 pyRdfa/termorcurie.py
  6. +1 −1 pyRdfaExtras/__init__.py
View
@@ -155,7 +155,7 @@
"""
"""
- $Id: __init__.py,v 1.90 2013-02-05 13:03:27 ivan Exp $
+ $Id: __init__.py,v 1.91 2013-10-16 11:48:54 ivan Exp $
"""
__version__ = "3.4.3"
@@ -679,7 +679,7 @@ def rdf_from_sources(self, names, outputFormat = "turtle", rdfOutput = False) :
except :
graph = Graph()
- graph.bind("xsd", Namespace('http://www.w3.org/2001/XMLSchema#'))
+ # graph.bind("xsd", Namespace('http://www.w3.org/2001/XMLSchema#'))
# the value of rdfOutput determines the reaction on exceptions...
for name in names :
self.graph_from_source(name, graph, rdfOutput)
View
@@ -30,8 +30,8 @@
"""
"""
-$Id: __init__.py,v 1.20 2013-01-07 13:03:16 ivan Exp $
-$Date: 2013-01-07 13:03:16 $
+$Id: __init__.py,v 1.21 2013-10-16 11:49:11 ivan Exp $
+$Date: 2013-10-16 11:49:11 $
"""
__version__ = "3.0"
@@ -62,12 +62,12 @@ class HostLanguage :
HostLanguage.xhtml : {
"xhv" : "http://www.w3.org/1999/xhtml/vocab#"
},
- HostLanguage.html5 : {
- "xhv" : "http://www.w3.org/1999/xhtml/vocab#"
- },
- HostLanguage.xhtml5 : {
- "xhv" : "http://www.w3.org/1999/xhtml/vocab#"
- },
+ # HostLanguage.html5 : {
+ # "xhv" : "http://www.w3.org/1999/xhtml/vocab#"
+ # },
+ # HostLanguage.xhtml5 : {
+ # "xhv" : "http://www.w3.org/1999/xhtml/vocab#"
+ # },
HostLanguage.atom : {
"atomrel" : "http://www.iana.org/assignments/relation/"
}
View
@@ -11,7 +11,7 @@
"""
"""
-$Id: options.py,v 1.19 2013-01-07 12:46:43 ivan Exp $ $Date: 2013-01-07 12:46:43 $
+$Id: options.py,v 1.20 2013-10-16 11:48:54 ivan Exp $ $Date: 2013-10-16 11:48:54 $
"""
import sys, datetime
@@ -44,11 +44,6 @@ class ProcessorGraph :
"""
def __init__(self) :
self.graph = Graph()
- self.graph.bind("dcterms", ns_dc)
- self.graph.bind("pyrdfa", ns_distill)
- self.graph.bind("rdf", ns_rdf)
- self.graph.bind("rdfa", ns_rdfa)
- self.graph.bind("ht", ns_ht)
def add_triples(self, msg, top_class, info_class, context, node) :
"""
@@ -68,6 +63,13 @@ def add_triples(self, msg, top_class, info_class, context, node) :
@return: the bnode that serves as a subject for the errors. The caller may add additional information
@rtype: BNode
"""
+ # Lazy binding of relevant prefixes
+ self.graph.bind("dcterms", ns_dc)
+ self.graph.bind("pyrdfa", ns_distill)
+ self.graph.bind("rdf", ns_rdf)
+ self.graph.bind("rdfa", ns_rdfa)
+ self.graph.bind("ht", ns_ht)
+ self.graph.bind("xsd", ns_xsd)
# Python 3 foolproof way
try :
is_context_string = isinstance(context, basestring)
View
@@ -17,8 +17,8 @@
"""
"""
-$Id: state.py,v 1.22 2012/10/10 15:19:43 ivan Exp $
-$Date: 2012/10/10 15:19:43 $
+$Id: state.py,v 1.23 2013-10-16 11:48:54 ivan Exp $
+$Date: 2013-10-16 11:48:54 $
"""
import sys
(py_v_major, py_v_minor, py_v_micro, py_v_final, py_v_serial) = sys.version_info
View
@@ -19,8 +19,8 @@
"""
"""
-$Id: termorcurie.py,v 1.11 2013-02-01 10:11:28 ivan Exp $
-$Date: 2013-02-01 10:11:28 $
+$Id: termorcurie.py,v 1.12 2013-10-16 11:48:54 ivan Exp $
+$Date: 2013-10-16 11:48:54 $
"""
import re, sys
@@ -136,7 +136,7 @@ def __init__(self, state, top_level) :
for key in data.terms :
self.terms[key] = URIRef(data.terms[key])
for key in data.ns :
- self.ns[key] = Namespace(data.ns[key])
+ self.ns[key] = (Namespace(data.ns[key]),False)
##################################################################################################################
@@ -188,11 +188,8 @@ def check_prefix(pr) :
# Set the default CURIE URI
if inherited_state == None :
# This is the top level...
- # Add the namespaces bindings defined via a initial context
- for key in default_vocab.ns :
- self.graph.bind(key, default_vocab.ns[key])
self.default_curie_uri = Namespace(XHTML_URI)
- self.graph.bind(XHTML_PREFIX, self.default_curie_uri)
+ # self.graph.bind(XHTML_PREFIX, self.default_curie_uri)
else :
self.default_curie_uri = inherited_state.term_or_curie.default_curie_uri
@@ -236,7 +233,6 @@ def check_prefix(pr) :
# The terms are hardwired...
for key in predefined_1_0_rel :
self.terms[key] = URIRef(XHTML_URI + key)
- self.graph.bind(XHTML_PREFIX, XHTML_URI)
else :
# just refer to the inherited terms
self.terms = inherited_state.term_or_curie.terms
@@ -327,34 +323,23 @@ def check_prefix(pr) :
# If not, the namespaces of the incoming state is
# taken over by reference. Otherwise that is copied to the
# the local dictionary
- inherited_prefixes = default_vocab.ns if inherited_state == None else inherited_state.term_or_curie.ns
+ if inherited_state == None :
+ self.default_prefixes = default_vocab.ns
+ inherited_prefixes = {}
+ else :
+ self.default_prefixes = inherited_state.term_or_curie.default_prefixes
+ inherited_prefixes = inherited_state.term_or_curie.ns
+
if len(dict) == 0 :
self.ns = inherited_prefixes
else :
self.ns = {}
for key in inherited_prefixes : self.ns[key] = inherited_prefixes[key]
for key in dict :
- try :
- if dict[key] != inherited_prefixes[key] :
- state.options.add_warning(err_prefix_redefinition % key, PrefixRedefinitionWarning, node=state.node.nodeName)
- except KeyError :
- pass
+ if (key in inherited_prefixes and dict[key] != inherited_prefixes[key]) or (key in self.default_prefixes and dict[key] != self.default_prefixes[key][0]) :
+ state.options.add_warning(err_prefix_redefinition % key, PrefixRedefinitionWarning, node=state.node.nodeName)
self.ns[key] = dict[key]
- # # Add the namespaces defined via a initial context
- # for key in default_vocab.ns :
- # dict[key] = default_vocab.ns[key]
- # self.graph.bind(key, dict[key])
- #
- # self.ns = {}
- # if len(dict) == 0 and inherited_state :
- # self.ns = inherited_state.term_or_curie.ns
- # else :
- # if inherited_state :
- # for key in inherited_state.term_or_curie.ns : self.ns[key] = inherited_state.term_or_curie.ns[key]
- # for key in dict : self.ns[key] = dict[key]
- # else :
- # self.ns = dict
# the xmlns prefixes have to be stored separately, again for XML Literal generation
self.xmlns = {}
@@ -461,6 +446,17 @@ def CURIE_to_URI(self, val) :
return URIRef(str(self.ns[prefix]))
else :
return self.ns[prefix][reference]
+ elif prefix in self.default_prefixes and self._check_reference(reference) :
+ # this has been defined through the default context
+ if len(reference) == 0 :
+ return URIRef(str(self.default_prefixes[prefix][0]))
+ else :
+ (ns,used) = self.default_prefixes[prefix]
+ # lazy binding of prefixes (to avoid unnecessary prefix definitions in the serializations at the end...)
+ if not used :
+ self.graph.bind(prefix,ns)
+ self.default_prefixes[prefix] = (ns,True)
+ return ns[reference]
else :
# no definition for this thing...
return None
@@ -488,11 +484,15 @@ def term_to_URI(self, term) :
# 1. simple, case sensitive test:
if term in self.terms :
# yep, term is a valid key as is
+ # lazy binding of the xhv prefix for terms...
+ self.graph.bind(XHTML_PREFIX, XHTML_URI)
return self.terms[term]
# 2. case insensitive test
for defined_term in self.terms :
if term.lower() == defined_term.lower() :
+ # lazy binding of the xhv prefix for terms...
+ self.graph.bind(XHTML_PREFIX, XHTML_URI)
return self.terms[defined_term]
# If it got here, it is all wrong...
View
@@ -11,7 +11,7 @@
"""
"""
-$Id: __init__.py,v 1.3 2012/08/20 13:17:05 ivan Exp $ $Date: 2012/08/20 13:17:05 $
+$Id: __init__.py,v 1.4 2013-10-16 11:49:32 ivan Exp $ $Date: 2013-10-16 11:49:32 $
"""

0 comments on commit 483da94

Please sign in to comment.