| 1 |
############################################################################## |
| 2 |
# |
| 3 |
# Copyright (c) 2011 Zope Foundation and Contributors. |
| 4 |
# All Rights Reserved. |
| 5 |
# |
| 6 |
# This software is subject to the provisions of the Zope Public License, |
| 7 |
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
| 8 |
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
| 9 |
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 10 |
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
| 11 |
# FOR A PARTICULAR PURPOSE. |
| 12 |
# |
| 13 |
############################################################################## |
| 14 |
import doctest |
| 15 |
import re |
| 16 |
import sys |
| 17 |
|
| 18 |
class DocTestParser(doctest.DocTestParser): |
| 19 |
"Doctest parser that creates calls into JavaScript." |
| 20 |
|
| 21 |
def __init__(self, *args, **kw): |
| 22 |
ps1 = kw.pop('ps1', '>>>') |
| 23 |
comment_prefix = kw.pop('comment_prefix', '#') |
| 24 |
self.transform = kw.pop('transform', lambda s: s) |
| 25 |
try: |
| 26 |
getattr( |
| 27 |
doctest.DocTestParser, '__init__', lambda : None)(*args, **kw) |
| 28 |
except TypeError: |
| 29 |
# Python 3 support. |
| 30 |
super(doctest.DocTestParser, self).__init__(*args, **kw) |
| 31 |
self._EXAMPLE_RE = re.compile( |
| 32 |
r''' |
| 33 |
# Source consists of a PS1 line followed by zero or more PS2 lines. |
| 34 |
(?P<source> |
| 35 |
(?:^(?P<indent> [ ]*) %(ps1)s .*) # PS1 line |
| 36 |
(?:\n [ ]* \.\.\. .*)*) # PS2 lines |
| 37 |
\n? |
| 38 |
# Want consists of any non-blank lines that do not start with PS1. |
| 39 |
(?P<want> (?:(?![ ]*$) # Not a blank line |
| 40 |
(?![ ]*%(ps1)s) # Not a line starting with PS1 |
| 41 |
.*$\n? # But any other line |
| 42 |
)*) |
| 43 |
''' % dict(ps1=ps1), re.MULTILINE | re.VERBOSE) |
| 44 |
|
| 45 |
self._OPTION_DIRECTIVE_RE = re.compile( |
| 46 |
comment_prefix +r'\s*doctest:\s*([^\n\'"]*)$', re.MULTILINE) |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
def parse(self, string, name='<string>'): |
| 51 |
r =doctest.DocTestParser.parse(self, string, name) |
| 52 |
for s in r: |
| 53 |
if isinstance(s, doctest.Example): |
| 54 |
s.source = self.transform(s.source) |
| 55 |
return r |