--- a/src/pyparser/__init__.py Fri Apr 19 02:08:37 2013 -0500
+++ b/src/pyparser/__init__.py Fri Apr 19 02:25:41 2013 -0500
@@ -29,16 +29,12 @@
Override this method to have your parser know when processing has
@@ -49,8 +45,6 @@
def parse(self, iterable):
--- a/src/pyparser/regex.py Fri Apr 19 02:08:37 2013 -0500
+++ b/src/pyparser/regex.py Fri Apr 19 02:25:41 2013 -0500
@@ -2,15 +2,22 @@
This module contains the RegexParser and Pattern decorator
from pyparser import Parser
-_ATTRIBUTE_NAME = '_re_pattern'
+_ATTRIBUTE_NAME = '_pyparser_re_pattern'
+class Pattern(object): # pylint:disable-msg=R0903 + This class is a decorator that'll attach a regular expression attribute + to a function that the RegexParser class will use to call that function if + the regular expression matches. def __init__(self, pattern):
@@ -35,6 +42,9 @@
The kwargs argument will have the parser call your methods with the match
object's groupdict as keyword args.
+ The handle_errors argument if True, will catch and log any exceptions + caused by the Pattern decorator instead of passing on the exception. Here is an example subclass without using keywords that will count how many
lines contain the string 'foo'.
@@ -72,7 +82,7 @@
self.processor[attribute] = value
- def __init__(self, strip=False, kwargs=False):
+ def __init__(self, strip=False, kwargs=False, handle_errors=True): Parser.__init__(self, strip)
@@ -85,8 +95,15 @@
- self.regexes[re.compile(pattern)] = symbol
+ self.regexes[re.compile(pattern)] = symbol + except Exception, exp: # pylint:disable-msg=W0703 + logging.exception(exp.message) --- a/tests/parser.py Fri Apr 19 02:08:37 2013 -0500
+++ b/tests/parser.py Fri Apr 19 02:25:41 2013 -0500
@@ -2,14 +2,11 @@
Unittests for the main pyparser.Parser class
from pyparser import Parser
-def _get_filename(filename):
- return os.path.join(os.path.dirname(__file__), 'data', filename)
+from utils import get_filename, parse_file class CountingParser(Parser):
@@ -48,10 +45,6 @@
self.parser = CountingParser()
- def tearDown(self): # pylint:disable-msg=C0103
def _parse_file(self, filename, lines):
The main method that does all of the testing
@@ -59,8 +52,8 @@
self.assertFalse(self.parser.started)
self.assertFalse(self.parser.finished)
- ifp = open(_get_filename(filename))
+ ifp = open(get_filename(filename)) parsed = self.parser.parse(ifp)
@@ -119,9 +112,7 @@
the correct number of lines with leading and trailing whitespace.
- ifp = open(_get_filename(filename))
- data = WhiteSpaceParser(strip).parse(ifp)
+ data = parse_file(WhiteSpaceParser, filename, strip=strip) self.assertEqual(data['leading'], leading)
self.assertEqual(data['trailing'], trailing)