[z3-checkins] r56146 - in z3/deliverance/sandbox/ianb/deliverance/trunk: . deliverance deliverance/tests

ianb at codespeak.net ianb at codespeak.net
Sat Jun 28 01:24:26 CEST 2008


Author: ianb
Date: Sat Jun 28 01:24:25 2008
New Revision: 56146

Added:
   z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/exceptions.py   (contents, props changed)
   z3/deliverance/sandbox/ianb/deliverance/trunk/test   (contents, props changed)
Modified:
   z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/log.py
   z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/pagematch.py
   z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/rules.py
   z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/ruleset.py
   z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/selector.py
   z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/tests/test_pagematch.txt
   z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/tests/test_selection.txt
Log:
Moved all the exceptions together into one module

Added: z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/exceptions.py
==============================================================================
--- (empty file)
+++ z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/exceptions.py	Sat Jun 28 01:24:25 2008
@@ -0,0 +1,53 @@
+import sys
+
+class DeliveranceError(Exception):
+    """
+    Superclass for all deliverance exceptions
+
+    In addition to a message, this can have a `request` and `element`
+    attached to it.  Elements are the objects (maybe XML, or maybe
+    not) that is applicable.
+    """
+    def __init__(self, msg=None, request=None, element=None):
+        Exception.__init__(self, msg)
+        self.request = request
+        self.element = element
+
+class DeliveranceSyntaxError(DeliveranceError):
+    """
+    Exception raised when there is a syntax error in some file
+    """
+
+class AbortTheme(Exception):
+    """
+    Raised (and caught) when the theming of a request should be
+    aborted.
+    """
+
+def add_exception_info(info, exc_info=None):
+    """
+    Add the given information to the exception (typically context information)
+    """
+    if exc_info is None:
+        exc_info = sys.exc_info()
+    exc_class, exc, tb = exc_info
+    if isinstance(exc_class, basestring):
+        # Not much we can do here, but...
+        exc_class += ' '+info
+        return exc_class, exc, tb
+    prev_message = str(exc)
+    args = getattr(exc, 'args', None)
+    if args is not None:
+        if len(args) != 1 or not isinstance(args[0], basestring):
+            args = tuple(args) + (info,)
+            exc.args = args
+        else:
+            arg = '%s: %s' % (args[0], info)
+            exc.args = (arg,)
+    if args is None or str(exc) == prev_message:
+        # This exception doesn't show information; revert:
+        exc.args = exc.args[:-1]
+        exc = Exception('%s (%s): %s' % (exc, exc.__class__.__name__, info))
+    return exc_class, exc, tb
+        
+        

Modified: z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/log.py
==============================================================================
--- z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/log.py	(original)
+++ z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/log.py	Sat Jun 28 01:24:25 2008
@@ -13,7 +13,7 @@
     """
     Logger that saves all its messages locally.
     """
-    def __init__(self, req, description=True):
+    def __init__(self, request, description=True):
         self.messages = []
         if description:
             self.descriptions = []
@@ -45,8 +45,8 @@
 
 class PrintingLogger(SavingLogger):
 
-    def __init__(self, req, description=True, print_level=logging.DEBUG):
-        super(PrintingLogger, self).__init__(req, description=description)
+    def __init__(self, request, description=True, print_level=logging.DEBUG):
+        super(PrintingLogger, self).__init__(request, description=description)
         self.print_level = print_level
 
     def add_description(self, msg):

Modified: z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/pagematch.py
==============================================================================
--- z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/pagematch.py	(original)
+++ z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/pagematch.py	Sat Jun 28 01:24:25 2008
@@ -2,17 +2,12 @@
 Handles the <match> tag and matching requests and responses against these patterns.
 """
 
+from deliverance.exceptions import DeliveranceSyntaxError, AbortTheme
 from deliverance.stringmatch import compile_matcher, compile_header_matcher
 from deliverance.util.converters import asbool, html_quote
-from deliverance.rules import AbortTheme
 
 __all__ = ['MatchSyntaxError', 'Match']
 
-class MatchSyntaxError(Exception):
-    """
-    Raised if there's some error with the matching.
-    """
-
 class Match(object):
     """
     Represents the <match> tags.
@@ -43,11 +38,11 @@
         abort = asbool(el.get('abort'))
         if not abort and not classes:
             ## FIXME: source location
-            raise MatchSyntaxError(
+            raise DeliveranceSyntaxError(
                 "You must provide some classes in the class attribute")
         if abort and classes:
             ## FIXME: source location
-            raise MatchSyntaxError(
+            raise DeliveranceSyntaxError(
                 'You cannot provide both abort="1" and class="%s"'
                 % (' '.join(classes)))
         path = cls._parse_attr(el, 'path', default='path')

Modified: z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/rules.py
==============================================================================
--- z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/rules.py	(original)
+++ z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/rules.py	Sat Jun 28 01:24:25 2008
@@ -1,28 +1,13 @@
 """
-Represents individual actions (<append> etc) and the RuleSet that puts them together
+Represents individual actions (``<append>`` etc) and the RuleSet that
+puts them together
 """
 
-from deliverance.exceptions import add_exception_info
+from deliverance.exceptions import add_exception_info, DeliveranceSyntaxError
 from deliverance.util.converters import asbool, html_quote
 from deliverance.selector import Selector
 from lxml import etree
 
-class RuleSyntaxError(Exception):
-    """
-    Exception raised when a rule itself is invalid
-    """
-
-class SelectionError(Exception):
-    """
-    Exception raised when a selection somehow isn't right (e.g.,
-    returns no elements when it should return an element).
-    """
-
-class AbortTheme(Exception):
-    """
-    Raised when something aborts via something like nocontent="abort"
-    """
-
 CONTENT_ATTRIB = 'x-a-marker-attribute-for-deliverance'
 
 class Rule(object):
@@ -81,7 +66,7 @@
     Parses an element into an action object.
     """
     if el.tag not in _actions:
-        raise RuleSyntaxError(
+        raise DeliveranceSyntaxError(
             "There is no rule with the name %s"
             % el.tag)
     Class = _actions[el.tag]
@@ -118,7 +103,7 @@
             if value not in self._no_allowed:
                 bad_options = self._no_allowed
         if bad_options:
-            raise RuleSyntaxError(
+            raise DeliveranceSyntaxError(
                 'The attribute %s="%s" should have a value of one of: %s'
                 % (name, value, ', '.join(v for v in bad_options if v)))
         if not value:
@@ -323,7 +308,7 @@
         for content_type in self.content.selector_types():
             for theme_type in self.theme.selector_types():
                 if (theme_type, content_type) not in self._compatible_types:
-                    raise RuleSyntaxError(
+                    raise DeliveranceSyntaxError(
                         'Selector type %s (from content="%s") and type %s (from theme="%s") are not compatible'
                         % (content_type, self.content, theme_type, self.theme))
         self.if_content = if_content

Modified: z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/ruleset.py
==============================================================================
--- z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/ruleset.py	(original)
+++ z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/ruleset.py	Sat Jun 28 01:24:25 2008
@@ -1,5 +1,6 @@
+from deliverance.exceptions import AbortTheme
 from deliverance.pagematch import run_matches, Match
-from deliverance.rules import Rule, remove_content_attribs, AbortTheme
+from deliverance.rules import Rule, remove_content_attribs
 from lxml.html import tostring, document_fromstring
 from lxml.etree import XML
 import re

Modified: z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/selector.py
==============================================================================
--- z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/selector.py	(original)
+++ z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/selector.py	Sat Jun 28 01:24:25 2008
@@ -2,6 +2,8 @@
 Implements the element selection; XPath, CSS, and the modifiers on
 those selections.
 """
+
+from deliverance.exceptions import DeliveranceSyntaxError
 from lxml.etree import XPath
 from lxml.cssselect import CSSSelector
 import re
@@ -10,9 +12,6 @@
 type_map = dict(element='elements', attribute='attributes')
 attributes_re = re.compile(r'^attributes[(]([a-zA-Z0-9_,-]+)[)]:')
 
-class SelectorSyntaxError(Exception):
-    pass
-
 class Selector(object):
     """
     Represents one selection attribute
@@ -95,7 +94,7 @@
         """
         type, attributes, rest_expr = self.parse_prefix(expr, default_type=default_type)
         if not self.types_compatible(type, self.major_type):
-            raise SelectorSyntaxError(
+            raise DeliveranceSyntaxError(
                 "Expression %s in selector %r uses the type %r, but this is not compatible "
                 "with the type %r already declared earlier in the selector"
                 % (expr, self, type, self.major_type))

Modified: z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/tests/test_pagematch.txt
==============================================================================
--- z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/tests/test_pagematch.txt	(original)
+++ z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/tests/test_pagematch.txt	Sat Jun 28 01:24:25 2008
@@ -11,7 +11,7 @@
     >>> def match(matcher, request, response_headers, show_log=True):
     ...     if isinstance(matcher, basestring):
     ...         matcher = make(matcher)
-    ...     log = SavingLogger()
+    ...     log = SavingLogger(None)
     ...     if isinstance(response_headers, list):
     ...         response_headers = HeaderDict(response_headers)
     ...     result = matcher(request, response_headers, log)
@@ -25,7 +25,7 @@
     >>> make('<match path="foo" />')
     Traceback (most recent call last):
         ...
-    MatchSyntaxError: You must provide some classes in the class attribute
+    DeliveranceSyntaxError: You must provide some classes in the class attribute
 
 Matches get normalized:
 

Modified: z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/tests/test_selection.txt
==============================================================================
--- z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/tests/test_selection.txt	(original)
+++ z3/deliverance/sandbox/ianb/deliverance/trunk/deliverance/tests/test_selection.txt	Sat Jun 28 01:24:25 2008
@@ -95,7 +95,7 @@
     ...     rule = parse_action(rule, None)
     ...     theme_copy = copy.deepcopy(theme)
     ...     theme_copy.make_links_absolute()
-    ...     logger = SavingLogger()
+    ...     logger = SavingLogger(request=None)
     ...     content_copy = copy.deepcopy(content)
     ...     rule.apply(content_copy, theme_copy, None, logger)
     ...     remove_content_attribs(theme_copy)

Added: z3/deliverance/sandbox/ianb/deliverance/trunk/test
==============================================================================
--- (empty file)
+++ z3/deliverance/sandbox/ianb/deliverance/trunk/test	Sat Jun 28 01:24:25 2008
@@ -0,0 +1,8 @@
+#!/bin/sh
+NOSE_WITH_DOCTEST=t
+export NOSE_WITH_DOCTEST
+NOSE_DOCTEST_EXTENSION=txt
+export NOSE_DOCTEST_EXTENSION
+#NOSE_DETAILED_ERRORS=t
+#export NOSE_DETAILED_ERRORS
+nosetests $*


More information about the z3-checkins mailing list