[z3-checkins] r33237 - z3/deliverance/branches/packaged/deliverance
ltucker at codespeak.net
ltucker at codespeak.net
Thu Oct 12 20:22:03 CEST 2006
Author: ltucker
Date: Thu Oct 12 20:22:00 2006
New Revision: 33237
Modified:
z3/deliverance/branches/packaged/deliverance/interpreter.py
z3/deliverance/branches/packaged/deliverance/tests.py
z3/deliverance/branches/packaged/deliverance/utils.py
z3/deliverance/branches/packaged/deliverance/wsgifilter.py
z3/deliverance/branches/packaged/deliverance/xslt.py
Log:
comments, cleanup
Modified: z3/deliverance/branches/packaged/deliverance/interpreter.py
==============================================================================
--- z3/deliverance/branches/packaged/deliverance/interpreter.py (original)
+++ z3/deliverance/branches/packaged/deliverance/interpreter.py Thu Oct 12 20:22:00 2006
@@ -14,7 +14,6 @@
def __init__(self, theme, theme_uri, rules, reference_resolver=None):
self.theme = self.fixup_links(theme, theme_uri)
- self.remove_http_equiv_metas(self.theme)
self.rules = rules
# perform xincludes on the rules
if reference_resolver:
@@ -30,7 +29,6 @@
def render(self, content):
result = copy.deepcopy(self.theme)
input = copy.deepcopy(content)
- self.remove_http_equiv_metas(input)
self.apply_rules(self.rules,result,input)
return result
Modified: z3/deliverance/branches/packaged/deliverance/tests.py
==============================================================================
--- z3/deliverance/branches/packaged/deliverance/tests.py (original)
+++ z3/deliverance/branches/packaged/deliverance/tests.py Thu Oct 12 20:22:00 2006
@@ -2,8 +2,8 @@
import os
from lxml import etree
from formencode.doctest_xml_compare import xml_compare
-#from deliverance.interpreter import Renderer
-from deliverance.xslt import Renderer
+from deliverance.interpreter import Renderer
+#from deliverance.xslt import Renderer
import copy
class DeliveranceTestCase:
Modified: z3/deliverance/branches/packaged/deliverance/utils.py
==============================================================================
--- z3/deliverance/branches/packaged/deliverance/utils.py (original)
+++ z3/deliverance/branches/packaged/deliverance/utils.py Thu Oct 12 20:22:00 2006
@@ -48,8 +48,8 @@
def format_error(self, message, rule, elts=None):
"""
Returns a node containing the error message;
- Checks the onerror attribute of the rule element to see if errors should
- be ignored in which case returns None
+ If the onerror attribute of the rule element is set to ignore,
+ returns None
"""
if rule.attrib.get('onerror', None) == 'ignore':
@@ -76,6 +76,9 @@
TAG_MATCHER = re.compile(r'^\.?/?/?(.*?/)*(?P<tag>[^*^(^)^:^[^.^/]+?)(\[.*\])*$',re.I)
def get_tag_from_xpath(self,xpath):
+ """
+ attemtps to extract the tag type that an xpath expression selects (if any)
+ """
match = self.TAG_MATCHER.match(xpath)
if match:
return match.group('tag')
@@ -83,33 +86,38 @@
return None
- def add_to_body_start(self,theme, el):
+ def add_to_body_start(self,doc,el):
+ """
+ inserts the element el into the beginning of body
+ element of the document given
+ """
if not el:
return
- body = theme.find('body')
+ body = doc.find('body')
if body is None:
- body = theme[0]
+ body = doc[0]
body[:0] = [el]
- def replace_element(self,replace, new_el):
- parent = replace.getparent()
- for i in range(len(parent)):
- if parent[i] == replace:
- new_el.tail = replace.tail
- parent[i] = new_el
- break
-
- def mark_bad_elements(self,els):
- for el in els:
- if 'class' in el.attrib:
- el.attrib['class'] += ' deliverance-bad-element'
- else:
- el.attrib['class'] = 'deliverance-bad-element'
+ def replace_element(self,old_el, new_el):
+ """
+ replaces old_el with new_el in the parent
+ element of old_el. The tail of
+ new_el is replaced by the tail of old_el
+ """
+ new_el.tail = old_el.tail
+ parent = old_el.getparent()
+ parent[parent.index(old_el)] = new_el
+
def fixup_links(self, doc, uri):
- """ resolve all links in the ``doc`` element to be absolute; the links
- should be considered relative to ``uri``
+ """
+ replaces relative urls found in the document given
+ with absolute urls by prepending the uri given.
+ <base href> tags are removed from the document.
+
+ Affects urls in href attributes, src attributes and
+ css of the form url(...) in style elements
"""
base_uri = uri
basetags = doc.xpath('//base[@href]')
@@ -132,14 +140,18 @@
def fixup_link_attrs(self, elts, base_uri, attr):
- """ makes all attr values in elts to be absolute uris based on base_uri """
+ """
+ prepends base_uri onto the attribute given by attr for
+ all elements given in elts
+ """
for el in elts:
el.attrib[attr] = urlparse.urljoin(base_uri, el.attrib[attr])
CSS_URL_PAT = re.compile(r'url\((.*?)\)',re.I)
def fixup_css_links(self, elts, base_uri):
- """ fixes @import uris in css style elements to be
+ """
+ prepends url(...) in css style elements to be
absolute links based on base_uri
"""
@@ -150,18 +162,12 @@
if el.text:
el.text = re.sub(self.CSS_URL_PAT,absuri,el.text)
- def remove_http_equiv_metas(self,doc):
- if not doc:
- return
-
- metas = doc.xpath("//meta[translate(@http-equiv,'contenyp','CONTENYP') = 'CONTENT-TYPE']")
- for elt in metas:
- if elt.tail:
- attach_text_to_previous(self,elt,elt.tail)
- elt.getparent().remove(elt)
-
def attach_text_to_previous(self,el,text):
+ """
+ attaches the text given to the nearest previous node to el,
+ ie its preceding sibling or parent
+ """
if text is None:
return
Modified: z3/deliverance/branches/packaged/deliverance/wsgifilter.py
==============================================================================
--- z3/deliverance/branches/packaged/deliverance/wsgifilter.py (original)
+++ z3/deliverance/branches/packaged/deliverance/wsgifilter.py Thu Oct 12 20:22:00 2006
@@ -13,8 +13,8 @@
from paste.wsgilib import intercept_output
from paste.request import construct_url
from paste.response import header_value, replace_header
-#from interpreter import Renderer
-from xslt import Renderer
+from interpreter import Renderer
+#from xslt import Renderer
from htmlserialize import tostring
import sys
import datetime
Modified: z3/deliverance/branches/packaged/deliverance/xslt.py
==============================================================================
--- z3/deliverance/branches/packaged/deliverance/xslt.py (original)
+++ z3/deliverance/branches/packaged/deliverance/xslt.py Thu Oct 12 20:22:00 2006
@@ -32,7 +32,6 @@
theme_copy = copy.deepcopy(theme)
self.fixup_links(theme_copy,theme_uri)
- self.remove_http_equiv_metas(theme_copy)
self.xsl_escape_comments(theme_copy)
if reference_resolver:
@@ -201,7 +200,9 @@
def xsl_escape_comments(self,doc):
"""
- replaces comment nodes with xsl:comment nodes
+ replaces comment nodes with xsl:comment nodes so they will
+ appear in the result of a transform rather than being
+ treated as comments in the transform
"""
comment_nodes = doc.xpath('//comment()')
for c in comment_nodes:
@@ -211,6 +212,9 @@
def add_conditional_missing_content_error(self,theme,rule):
"""
+ adds a node to the body of theme which produces an error
+ message if no content is matched by a rule given
+ during the transformation
"""
err = self.format_error("no content matched", rule)
if err:
@@ -221,6 +225,11 @@
def make_when_otherwise(self, test, whenbody, otherwisebody):
+ """
+ makes a conditional xlst node. when placed into a document,
+ if the xslt expression represented by the string test evaluates
+ to true, whenbody is produced, if false, otherwise body is produced
+ """
choose = etree.Element("{%s}choose" % nsmap["xsl"])
when = etree.Element("{%s}when" % nsmap["xsl"])
when.set("test", test)
@@ -233,6 +242,11 @@
def debug_append(self, parent, child, rule):
+ """
+ helper method for appending a node, if debugging is enabled,
+ the appended node is wrapped in comments referring to the
+ rule that performed the append
+ """
if self.debug:
comment_before,comment_after = self.make_debugging_comments(rule)
parent.append(comment_before)
@@ -243,6 +257,11 @@
def debug_replace(self, old_el, new_el, rule):
+ """
+ helper method for replacing a node, if debugging is enabled,
+ the new node is wrapped in comments referring to the
+ rule that performed the replace
+ """
self.replace_element(old_el, new_el)
if self.debug:
@@ -257,6 +276,11 @@
parent.insert(index+2, comment_after)
def debug_prepend(self, parent, child, rule):
+ """
+ helper method for prepending a node, if debugging is enabled,
+ the prepended node is wrapped in comments referring to the
+ rule that performed the append
+ """
parent.insert(0,child)
child.tail = parent.text
parent.text = None
@@ -271,6 +295,10 @@
def make_debugging_comments(self, rule):
+ """
+ helper method which prepares two xslt:comment nodes used
+ for wrapping inserted content nodes during debugging
+ """
comment_before = etree.Element("{%s}comment" % nsmap["xsl"])
comment_before.text = "Deliverance: applying rule %s" % etree.tostring(rule)
comment_after = etree.Element("{%s}comment" % nsmap["xsl"])
More information about the z3-checkins
mailing list