/usr/share/pyshared/steadymark/core.py is in python-steadymark 0.4.5-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
# <steadymark - markdown-based test runner for python>
# Copyright (C) <2012> Gabriel Falcão <gabriel@nacaolivre.org>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
from __future__ import unicode_literals
import re
import sys
from doctest import (
DocTestParser,
DocTest,
DebugRunner,
DocTestFailure,
)
from datetime import datetime
from misaka import (
BaseRenderer,
Markdown,
EXT_FENCED_CODE,
EXT_NO_INTRA_EMPHASIS,
)
from steadymark.six import text_type
class SteadyMarkDoctestRunner(DebugRunner):
def report_unexpected_exception(self, out, test, example, exc_info):
exc_type, exc_val, tb = exc_info
if exc_type is DocTestFailure:
raise exc_info
raise exc_val
class MarkdownTest(object):
def __init__(self, title, raw_code, globs, locs):
self.title = title
self.raw_code = raw_code
self.globs = globs
self.locs = locs
dt_parser = DocTestParser()
doctests = dt_parser.get_examples(raw_code)
if any(doctests):
self.code = DocTest(
examples=doctests,
globs=self.globs,
name=title,
filename=None,
lineno=None,
docstring=None)
else:
self.code = compile(raw_code, title, "exec")
def _run_raw(self):
return eval(self.code, self.globs, self.locs)
def _run_doctest(self):
if not isinstance(self.code, DocTest):
raise TypeError(
"Attempt to run a non-doctest as doctest: %r" % self.code)
runner = SteadyMarkDoctestRunner(verbose=False)
return runner.run(self.code)
def run(self):
before = datetime.now()
failure = None
result = None
is_doctest = isinstance(self.code, DocTest)
try:
if is_doctest:
result = self._run_doctest()
else:
result = self._run_raw()
except:
failure = sys.exc_info()
after = datetime.now()
return result, failure, before, after
class SteadyMark(BaseRenderer):
title_regex = re.compile(r'(?P<title>[^#]+)(?:[#]+(?P<index>\d+))?')
def preprocess(self, text):
self._tests = [{}]
return text_type(text)
def block_code(self, code, language):
if language != 'python':
return
if re.match('^#\s*steadymark:\s*ignore', code):
return
item = self._tests[-1]
if 'code' in item: # the same title has more than 1 code
found = self.title_regex.search(item['title'])
title = found.group('title').rstrip()
index = int(found.group('index') or 0)
if not index:
index = 1
item['title'] = '{0} #{1}'.format(title, index)
new_item = {
'title': '{0} #{1}'.format(title, index + 1),
'level': item['level'],
'code': code,
}
self._tests.append(new_item)
item = self._tests[-1]
else:
item[u'code'] = text_type(code).strip()
if 'title' not in item:
item[u'title'] = u'Test #{0}'.format(len(self._tests))
self._tests.append({})
def header(self, title, level):
t = text_type(title)
t = re.sub(r'^[# ]*(.*)', '\g<1>', t)
t = re.sub(r'`([^`]*)`', '\033[1;33m\g<1>\033[0m', t)
self._tests.append({
u'title': t,
u'level': int(level),
})
def postprocess(self, full_document):
tests = []
globs = globals()
locs = locals()
for test in filter(lambda x: 'code' in x, self._tests):
raw_code = test['code']
title = test['title']
tests.append(MarkdownTest(title, raw_code, globs=globs, locs=locs))
self.tests = tests
@classmethod
def inspect(cls, markdown):
renderer = cls()
extensions = EXT_FENCED_CODE | EXT_NO_INTRA_EMPHASIS
md = Markdown(renderer, extensions=extensions)
md.render(markdown)
return renderer
|