sqlalchemy / doc / build / testdocs.py

import sys
sys.path = ['../../lib', './lib/'] + sys.path

import os
import re
import doctest
import sqlalchemy.util as util
import sqlalchemy.log as salog
import logging

rootlogger = logging.getLogger('sqlalchemy.engine.base.Engine')
class MyStream(object):
    def write(self, string):
        sys.stdout.write(string)
        sys.stdout.flush()
    def flush(self):
        pass
handler = logging.StreamHandler(MyStream())
handler.setFormatter(logging.Formatter('%(message)s'))
rootlogger.addHandler(handler)


def teststring(s, name, globs=None, verbose=None, report=True,
               optionflags=0, extraglobs=None, raise_on_error=False,
               parser=doctest.DocTestParser()):

    from doctest import DebugRunner, DocTestRunner, master

    # Assemble the globals.
    if globs is None:
        globs = {}
    else:
        globs = globs.copy()
    if extraglobs is not None:
        globs.update(extraglobs)

    if raise_on_error:
        runner = DebugRunner(verbose=verbose, optionflags=optionflags)
    else:
        runner = DocTestRunner(verbose=verbose, optionflags=optionflags)

    test = parser.get_doctest(s, globs, name, name, 0)
    runner.run(test)

    if report:
        runner.summarize()

    if master is None:
        master = runner
    else:
        master.merge(runner)

    return runner.failures, runner.tries

def replace_file(s, newfile):
    engine = r"'(sqlite|postgresql|mysql):///.*'"
    engine = re.compile(engine, re.MULTILINE)
    s, n = re.subn(engine, "'sqlite:///" + newfile + "'", s)
    if not n:
        raise ValueError("Couldn't find suitable create_engine call to replace '%s' in it" % oldfile)
    return s

for filename in 'orm/tutorial','core/tutorial',:
    filename = '%s.rst' % filename
    s = open(filename).read()
    #s = replace_file(s, ':memory:')
    s = re.sub(r'{(?:stop|sql|opensql)}', '', s)
    teststring(s, filename)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.