Changeset 4953

Show
Ignore:
Timestamp:
07/14/08 10:58:00 (1 month ago)
Author:
brunojm
Message:

test option added and some fixes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/docs/conf.py

    r4830 r4953  
    149149#latex_use_modindex = True 
    150150 
    151 code_path = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + 'project_code' + os.path.sep 
    152151code_scm = 'svn' 
     152code_path = test_path = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + 'project_code' + os.path.sep 
     153 
  • trunk/docs/tgext.py

    r4843 r4953  
    1717from docutils.parsers.rst import directives 
    1818 
    19 try: 
    20     from pysvn import Client, ClientError, Revision, opt_revision_kind 
    21 except ImportError: 
    22     pass 
     19import pysvn 
     20import nose 
     21from mercurial import hg, ui 
    2322 
     23  
    2424beginmarker_re = re.compile(r'##\{(?P<section>.+)}') 
    2525endmarker_re = re.compile(r'##') 
    2626 
     27# This is based on 'Multi-line string block formatting' recipe by Brett Levin 
     28# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/145672 
    2729def format_block(block): 
    2830    """Format the given block of text, trimming leading/trailing 
     
    3941    if ws: 
    4042        lines = map( lambda x: x.replace(ws,'',1), lines ) 
    41     # remove leading/trailing blank lines (after leading ws removal) 
    42     # we do this again in case there were pure-whitespace lines 
    43     while lines and not lines[0]:  del lines[0] 
    44     while lines and not lines[-1]: del lines[-1] 
    4543    return '\n'.join(lines)+'\n' 
    4644 
     
    6967 
    7068class HgClient: 
     69    """ Class that represents a Mercurial client """ 
    7170    def __init__(self, path): 
    72         from mercurial import hg, ui 
    7371        self.repo = hg.repository(ui.ui(interactive=False), path=path) 
    7472 
    7573    def get_file(self, path, revision='tip'): 
    76         return self.repo.changectx(rev).filectx(path).data() 
     74        return self.repo.changectx(revision).filectx(path).data() 
    7775 
    7876     
    7977class SVNClient: 
     78    """ Class that represents a Subversion client """ 
    8079    def __init__(self): 
    81         self.client = Client() 
     80        self.client = pysvn.Client() 
    8281 
    8382    def get_file(self, path, revision='HEAD'): 
    8483        if revision == 'HEAD': 
    85             return self.client.cat(path, Revision(opt_revision_kind.head)) 
     84            return self.client.cat(path, 
     85                                   pysvn.Revision(pysvn.opt_revision_kind.head)) 
    8686        else: 
    87             return self.client.cat(path, Revision(opt_revision_kind.number, str(revision))) 
     87            return self.client.cat(path, 
     88                                   pysvn.Revision(pysvn.opt_revision_kind.number, 
     89                                                  str(revision))) 
    8890 
    8991 
     92def get_file(path, revision = None, type = None, repository = None): 
     93    """ Read file from local filesystem of from a SCM repository. """ 
     94    if revision: 
     95        if type == 'svn': 
     96            scm = SVNClient() 
     97        elif type == 'hg': 
     98            scm = HgClient(repository) 
     99        else: 
     100            raise Exception, "SCM tool not correctly specified" 
     101        data = scm.get_file(path, revision).splitlines() 
     102    else: 
     103        fp = open(path) 
     104        data = fp.read().splitlines() 
     105        fp.close() 
     106    return data 
    90107 
     108 
     109# code directive 
    91110def code_directive(name, arguments, options, content, lineno, 
    92111                        content_offset, block_text, state, state_machine): 
    93112    if not state.document.settings.file_insertion_enabled: 
    94         return [state.document.reporter.warning('File insertion disabled', line=lineno)] 
     113        return [state.document.reporter.warning('File insertion disabled', 
     114                                                line=lineno)] 
    95115    environment = state.document.settings.env 
    96     fname = arguments[0] 
    97     fpath = os.path.normpath(os.path.join(environment.config.code_path, fname)) 
     116    file_name = arguments[0] 
     117    if file_name.startswith('/'): 
     118        file_path = file_name 
     119    else: 
     120        file_path = os.path.normpath(os.path.join(environment.config.code_path, 
     121                                                  file_name)) 
    98122     
    99     revision = options.get('revision', '') 
    100123    try: 
    101         if revision: 
    102             if environment.config.code_scm == 'svn': 
    103                 scm = SVNClient() 
    104             elif environment.config.code_scm == 'hg': 
    105                 scm = HgClient(environment.config.code_path) 
    106             data = scm.get_file(fpath, revision).splitlines() 
     124        if options.has_key('revision'): 
     125            data = get_file(file_path, options['revision'], 
     126                            environment.config.code_scm, 
     127                            environment.config.code_path) 
    107128        else: 
    108             fp = open(fpath) 
    109             data = fp.read().splitlines() 
    110             fp.close() 
    111         section_name = options.get('section', '') 
    112         source = format_block(search(data, section_name)) 
     129            data = get_file(file_path) 
     130        if options.has_key('section'): 
     131            section = options['section'] 
     132            source = format_block(search(data, section)) 
     133        else: 
     134            source = format_block('\n'.join(data)) 
    113135        retnode = nodes.literal_block(source, source) 
     136        retnode.line = 1    
    114137    except Exception, e: 
    115138        retnode = state.document.reporter.warning( 
    116139            'Reading file %r failed: %r' % (arguments[0], str(e)), line=lineno) 
    117140    else: 
    118         retnode.line = 1    
    119         if options.get('language', ''): 
     141        if options.has_key('test'): 
     142            test = options['test'] 
     143            if test.startswith('/'): 
     144                result = nose.run(argv = [__file__, options['test']]) 
     145            else: 
     146                result = nose.run(argv = [__file__, environment.config.test_path 
     147                                          + options['test']]) 
     148            if not result: 
     149                retnode = state.document.reporter.warning( 
     150                    'Test(s) associated to %r failed' % (arguments[0],), 
     151                    line=lineno) 
     152        if options.has_key('language'): 
    120153            retnode['language'] = options['language'] 
    121154    return [retnode] 
     
    124157    code_options = {'section': directives.unchanged,  
    125158                    'language': directives.unchanged, 
     159                    'test': directives.unchanged, 
    126160                    'revision': directives.unchanged} 
    127161    app.add_config_value('code_path', '', True) 
    128162    app.add_config_value('code_scm', '', True) 
     163    app.add_config_value('test_path', '', True) 
    129164    app.add_directive('code', code_directive, 1, (1, 0, 1),  **code_options)