Changeset 1498
- Timestamp:
- Feb 7, 2014 3:20:37 PM (8 years ago)
- Location:
- topdoc/trunk/src/topdoc
- Files:
-
- 1 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
topdoc/trunk/src/topdoc/__init__.py
r1497 r1498 36 36 EPICS_MACRO_SPECIFICATION_BD_PATTERN = re.compile(EPICS_MACRO_SPECIFICATION_BD_RE, 0) 37 37 38 RST_UNDERLINES = r'#*=-^"~+' 39 38 40 39 41 class DatabaseException(Exception): pass … … 41 43 42 44 43 def replaceMacros(source, macros = {}):45 def replaceMacros(source, **macros): 44 46 ''' 45 47 Replace macro parameters in source string. -
topdoc/trunk/src/topdoc/database.py
r1496 r1498 174 174 msg = '(%s)' % self.filename 175 175 msg += ' %s: ' % 'file not found' 176 raise DatabaseException 176 raise DatabaseException, msg 177 177 tokenLog.processFile(self.filename) 178 178 … … 263 263 :return: list PV names or None if no PVs records have been defined 264 264 ''' 265 return map(lambda x: replaceMacros( x, macros ), self.pv.keys())265 return map(lambda x: replaceMacros( x, **macros ), self.pv.keys()) 266 266 267 267 def getPvDict(self, macros = {}): … … 274 274 ''' 275 275 def re_mac(v): 276 return replaceMacros( v, macros )276 return replaceMacros( v, **macros ) 277 277 result = {re_mac(key): {field: re_mac(re_mac(value)) 278 278 for field, value in fieldDict.items()} … … 280 280 return result 281 281 282 def rst_report(self, **macros):282 def rst_report(self, underlines=RST_UNDERLINES, **macros): 283 283 '''return a report (as *str*) of this database in restructured text''' 284 h1 = underlines[0] 285 h2 = underlines[1] 286 sub_uls = underlines[1:] 284 287 rst = [] 285 rst += ['='*len(self.filename),]286 288 rst.append(self.filename) 287 rst += [ '='*len(self.filename),]289 rst += [h1*len(self.filename),] 288 290 rst.append('') 289 291 rst.append(':absolute filename: %s' % self.absolute_filename) … … 294 296 self.rst_info): 295 297 rst.append('') 296 rst += method( **macros).splitlines()298 rst += method(underlines=sub_uls, **macros).splitlines() 297 299 return '\n'.join(rst) 298 300 299 def rst_summary(self, **macros): 301 def rst_summary(self, underlines=RST_UNDERLINES, **macros): 302 title = 'summary of records' 303 h1 = underlines[0] 300 304 rst = [] 301 rst.append( 'summary of records')302 rst.append( '==================')305 rst.append(title) 306 rst.append(h1*len(title)) 303 307 rst.append('') 304 308 t = Table() … … 310 314 return '\n'.join(rst) 311 315 312 def rst_pvs(self, **macros): 316 def rst_pvs(self, underlines=RST_UNDERLINES, **macros): 317 title = 'Process Variables' 318 h1 = underlines[0] 313 319 rst = [] 314 rst.append('Process Variables') 315 rst.append('==================') 320 rst.append('') 321 rst.append(title) 322 rst.append(h1*len(title)) 316 323 rst.append('') 317 324 # appear in order of line number in file … … 320 327 row = [pv.field['RTYP'], name] 321 328 if len(macros) > 0: 322 row.append(replaceMacros(name, macros))323 row.append('\n'.join(['* '+replaceMacros(item, macros) for item in pv.alias]))329 row.append(replaceMacros(name, **macros)) 330 row.append('\n'.join(['* '+replaceMacros(item, **macros) for item in pv.alias])) 324 331 rows[pv.line_number] = row 325 332 t = Table() … … 334 341 return '\n'.join(rst) 335 342 336 def rst_info(self, **macros): 343 def rst_info(self, underlines=RST_UNDERLINES, **macros): 344 title = 'PV Info' 345 h1 = underlines[0] 337 346 t = Table() 338 347 t.labels = ['line#', 'PV name', ] … … 349 358 row = [str(pv.line_number), name,] 350 359 if len(macros) > 0: 351 row.append(replaceMacros(name, macros))360 row.append(replaceMacros(name, **macros)) 352 361 row += [k, '\n'.join(['* ' + _ for _ in v])] 353 362 t.rows.append( row ) 354 363 if info_count > 0: 355 rst.append('PV info') 356 rst.append('=======') 364 rst.append('') 365 rst.append(title) 366 rst.append(h1*len(title)) 357 367 rst.append('') 358 368 rst += t.reST(fmt='simple').splitlines() -
topdoc/trunk/src/topdoc/template.py
r1497 r1498 16 16 self.filename = filename 17 17 self.patterns = [] 18 # self.exception_message = None 18 19 19 20 def addPatternSet(self, **patterns): … … 25 26 '''representation of an EPICS database template file''' 26 27 27 def __init__(self, filename ):28 def __init__(self, filename, **macros): 28 29 self.filename = filename 29 30 self.absolute_filename = os.path.abspath(filename) 30 self.db = [] 31 self.parse() 32 33 def parse(self): 31 self.dbSpec = [] 32 self.dbFile = {} 33 self.parse(**macros) 34 35 def parse(self, **macros): 34 36 '''parse the EPICS database template file 35 37 … … 67 69 if token_key(tok) == 'NAME file': 68 70 tok = tokenLog.nextActionable() 69 dbFilename = getFullWord(tokenLog)71 dbFilename = strip_quotes(getFullWord(tokenLog)) 70 72 dbSpec = DatabaseSpecification(dbFilename) 71 self.db .append(dbSpec)73 self.dbSpec.append(dbSpec) 72 74 self._parse_pattern_specifications(dbSpec, tokenLog) 75 expanded = replaceMacros(dbFilename, **macros) 76 if dbFilename not in self.dbFile: 77 try: 78 # TODO: reveal "expanded" somehow 79 self.dbFile[dbFilename] = database.Db(expanded) 80 except DatabaseException, exc: 81 self.dbFile[dbFilename] = exc[0] 73 82 tok = tokenLog.nextActionable() 74 83 … … 129 138 pass 130 139 131 def rst_report(self, **macros):140 def rst_report(self, underlines=RST_UNDERLINES, **macros): 132 141 '''return a report (as *str*) of this database template in restructured text''' 142 h1 = underlines[0] 143 sub_uls = underlines[1:] 133 144 rst = [] 134 rst += ['='*len(self.filename),]135 145 rst.append(self.filename) 136 rst += [ '='*len(self.filename),]146 rst += [h1*len(self.filename),] 137 147 rst.append('') 138 148 rst.append(':absolute filename: %s' % self.absolute_filename) 139 for db in self.db:149 for dbSpec in self.dbSpec: 140 150 rst.append('') 141 rst += self.rst_report_db(db, **macros).splitlines() 151 rst += self.rst_report_db(dbSpec, underlines=sub_uls, **macros).splitlines() 152 rst += self.rst_report_PVs(dbSpec, underlines=sub_uls, **macros).splitlines() 142 153 return '\n'.join(rst) 143 154 144 def rst_report_db(self, db , **macros):155 def rst_report_db(self, dbSpec, underlines=RST_UNDERLINES, **macros): 145 156 '''return a report (as *str*) of a database in restructured text''' 146 157 # TODO: link to the database rst file documentation 147 # TODO: list PVs created here if macros can be expanded 148 title = 'database: ' + db.filename 149 if len(db.patterns) > 0: 158 title = 'database: ' + dbSpec.filename 159 if len(dbSpec.patterns) > 0: 150 160 # note: assumes same number of patterns in each dict (safe assumption) 151 keylist = sorted(db .patterns[0].keys())161 keylist = sorted(dbSpec.patterns[0].keys()) 152 162 t = Table() 153 163 t.labels = keylist 154 for pattern in db .patterns:164 for pattern in dbSpec.patterns: 155 165 t.rows.append( [pattern[k] for k in keylist] ) 156 166 167 h1 = underlines[0] 157 168 rst = [] 158 169 rst.append(title) 159 rst += ['-'*len(title),] 160 rst.append('') 161 if len(db.patterns) > 0: 170 rst += [h1*len(title),] 171 rst.append('') 172 if len(dbSpec.patterns) > 0: 173 rst += ['.. rubric:: macro substitution pottern sets'] 174 rst += [''] 162 175 rst += t.reST(fmt='simple').splitlines() 176 rst += [''] 163 177 else: 164 178 rst.append('no patterns to apply') 179 rst.append('') 180 return '\n'.join(rst) 181 182 def rst_report_PVs(self, dbSpec, underlines=RST_UNDERLINES, **macros): 183 '''return a report (as *str*) of a database in restructured text''' 184 sub_uls = underlines 185 rst = [] 186 rst.append('') 187 dbFilename = dbSpec.filename 188 dbFile = self.dbFile[dbFilename] 189 if isinstance(dbFile, database.Db): 190 if dbFile.filename is None: 191 rst += [dbFilename + ' was not found'] 192 else: 193 for macros in dbSpec.patterns: 194 rst += dbFile.rst_pvs(underlines=sub_uls, **macros).splitlines() 195 else: 196 rst += [dbFilename + ': ' + dbFile] # reports the exception message 165 197 rst.append('') 166 198 return '\n'.join(rst) … … 182 214 template = {} 183 215 for fname in sorted(filelist): 184 template[fname] = Template(fname )216 template[fname] = Template(fname, STD='synApps/std/:') 185 217 print template[fname].rst_report() 186 218 -
topdoc/trunk/src/topdoc/test.template
r1497 r1498 10 10 11 11 # no quotes around the file name 12 file test 2.db {12 file test.db { 13 13 { this=sub1,that=sub2 } 14 14 { this=sub3,that=sub4 }
Note: See TracChangeset
for help on using the changeset viewer.