Changeset 4953
- Timestamp:
- 07/14/08 10:58:00 (1 month ago)
- Files:
-
- trunk/docs/conf.py (modified) (1 diff)
- trunk/docs/tgext.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/docs/conf.py
r4830 r4953 149 149 #latex_use_modindex = True 150 150 151 code_path = os.path.dirname(os.path.abspath(__file__)) + os.path.sep + 'project_code' + os.path.sep152 151 code_scm = 'svn' 152 code_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 17 17 from docutils.parsers.rst import directives 18 18 19 try: 20 from pysvn import Client, ClientError, Revision, opt_revision_kind 21 except ImportError: 22 pass 19 import pysvn 20 import nose 21 from mercurial import hg, ui 23 22 23 24 24 beginmarker_re = re.compile(r'##\{(?P<section>.+)}') 25 25 endmarker_re = re.compile(r'##') 26 26 27 # This is based on 'Multi-line string block formatting' recipe by Brett Levin 28 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/145672 27 29 def format_block(block): 28 30 """Format the given block of text, trimming leading/trailing … … 39 41 if ws: 40 42 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 lines43 while lines and not lines[0]: del lines[0]44 while lines and not lines[-1]: del lines[-1]45 43 return '\n'.join(lines)+'\n' 46 44 … … 69 67 70 68 class HgClient: 69 """ Class that represents a Mercurial client """ 71 70 def __init__(self, path): 72 from mercurial import hg, ui73 71 self.repo = hg.repository(ui.ui(interactive=False), path=path) 74 72 75 73 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() 77 75 78 76 79 77 class SVNClient: 78 """ Class that represents a Subversion client """ 80 79 def __init__(self): 81 self.client = Client()80 self.client = pysvn.Client() 82 81 83 82 def get_file(self, path, revision='HEAD'): 84 83 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)) 86 86 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))) 88 90 89 91 92 def 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 90 107 108 109 # code directive 91 110 def code_directive(name, arguments, options, content, lineno, 92 111 content_offset, block_text, state, state_machine): 93 112 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)] 95 115 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)) 98 122 99 revision = options.get('revision', '')100 123 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) 107 128 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)) 113 135 retnode = nodes.literal_block(source, source) 136 retnode.line = 1 114 137 except Exception, e: 115 138 retnode = state.document.reporter.warning( 116 139 'Reading file %r failed: %r' % (arguments[0], str(e)), line=lineno) 117 140 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'): 120 153 retnode['language'] = options['language'] 121 154 return [retnode] … … 124 157 code_options = {'section': directives.unchanged, 125 158 'language': directives.unchanged, 159 'test': directives.unchanged, 126 160 'revision': directives.unchanged} 127 161 app.add_config_value('code_path', '', True) 128 162 app.add_config_value('code_scm', '', True) 163 app.add_config_value('test_path', '', True) 129 164 app.add_directive('code', code_directive, 1, (1, 0, 1), **code_options)