Changeset 1498


Ignore:
Timestamp:
Feb 7, 2014 3:20:37 PM (8 years ago)
Author:
jemian
Message:

macro expansion in template file name, refs #57

Location:
topdoc/trunk/src/topdoc
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • topdoc/trunk/src/topdoc/__init__.py

    r1497 r1498  
    3636EPICS_MACRO_SPECIFICATION_BD_PATTERN = re.compile(EPICS_MACRO_SPECIFICATION_BD_RE, 0)
    3737
     38RST_UNDERLINES = r'#*=-^"~+'
     39
    3840
    3941class DatabaseException(Exception): pass
     
    4143
    4244
    43 def replaceMacros(source, macros = {}):
     45def replaceMacros(source, **macros):
    4446    '''
    4547    Replace macro parameters in source string.
  • topdoc/trunk/src/topdoc/database.py

    r1496 r1498  
    174174            msg = '(%s)' % self.filename
    175175            msg += ' %s: ' % 'file not found'
    176             raise DatabaseException
     176            raise DatabaseException, msg
    177177        tokenLog.processFile(self.filename)
    178178
     
    263263        :return: list PV names or None if no PVs records have been defined
    264264        '''
    265         return map(lambda x: replaceMacros( x, macros ), self.pv.keys())
     265        return map(lambda x: replaceMacros( x, **macros ), self.pv.keys())
    266266
    267267    def getPvDict(self, macros = {}):
     
    274274        '''
    275275        def re_mac(v):
    276             return replaceMacros( v, macros )
     276            return replaceMacros( v, **macros )
    277277        result = {re_mac(key): {field: re_mac(re_mac(value))
    278278                            for field, value in fieldDict.items()}
     
    280280        return result
    281281
    282     def rst_report(self, **macros):
     282    def rst_report(self, underlines=RST_UNDERLINES, **macros):
    283283        '''return a report (as *str*) of this database in restructured text'''
     284        h1 = underlines[0]
     285        h2 = underlines[1]
     286        sub_uls = underlines[1:]
    284287        rst = []
    285         rst += ['='*len(self.filename),]
    286288        rst.append(self.filename)
    287         rst += ['='*len(self.filename),]
     289        rst += [h1*len(self.filename),]
    288290        rst.append('')
    289291        rst.append(':absolute filename: %s' % self.absolute_filename)
     
    294296                       self.rst_info):
    295297            rst.append('')
    296             rst += method(**macros).splitlines()
     298            rst += method(underlines=sub_uls, **macros).splitlines()
    297299        return '\n'.join(rst)
    298300
    299     def rst_summary(self, **macros):
     301    def rst_summary(self, underlines=RST_UNDERLINES, **macros):
     302        title = 'summary of records'
     303        h1 = underlines[0]
    300304        rst = []
    301         rst.append('summary of records')
    302         rst.append('==================')
     305        rst.append(title)
     306        rst.append(h1*len(title))
    303307        rst.append('')
    304308        t = Table()
     
    310314        return '\n'.join(rst)
    311315
    312     def rst_pvs(self, **macros):
     316    def rst_pvs(self, underlines=RST_UNDERLINES, **macros):
     317        title = 'Process Variables'
     318        h1 = underlines[0]
    313319        rst = []
    314         rst.append('Process Variables')
    315         rst.append('==================')
     320        rst.append('')
     321        rst.append(title)
     322        rst.append(h1*len(title))
    316323        rst.append('')
    317324        # appear in order of line number in file
     
    320327            row = [pv.field['RTYP'], name]
    321328            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]))
    324331            rows[pv.line_number] = row
    325332        t = Table()
     
    334341        return '\n'.join(rst)
    335342
    336     def rst_info(self, **macros):
     343    def rst_info(self, underlines=RST_UNDERLINES, **macros):
     344        title = 'PV Info'
     345        h1 = underlines[0]
    337346        t = Table()
    338347        t.labels = ['line#', 'PV name', ]
     
    349358                    row = [str(pv.line_number), name,]
    350359                    if len(macros) > 0:
    351                         row.append(replaceMacros(name, macros))
     360                        row.append(replaceMacros(name, **macros))
    352361                    row += [k, '\n'.join(['* ' + _ for _ in v])]
    353362                    t.rows.append( row )
    354363        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))
    357367            rst.append('')
    358368            rst += t.reST(fmt='simple').splitlines()
  • topdoc/trunk/src/topdoc/template.py

    r1497 r1498  
    1616        self.filename = filename
    1717        self.patterns = []
     18        # self.exception_message = None
    1819   
    1920    def addPatternSet(self, **patterns):
     
    2526    '''representation of an EPICS database template file'''
    2627   
    27     def __init__(self, filename):
     28    def __init__(self, filename, **macros):
    2829        self.filename = filename
    2930        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):
    3436        '''parse the EPICS database template file
    3537       
     
    6769            if token_key(tok) == 'NAME file':
    6870                tok = tokenLog.nextActionable()
    69                 dbFilename = getFullWord(tokenLog)
     71                dbFilename = strip_quotes(getFullWord(tokenLog))
    7072                dbSpec = DatabaseSpecification(dbFilename)
    71                 self.db.append(dbSpec)
     73                self.dbSpec.append(dbSpec)
    7274                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]
    7382            tok = tokenLog.nextActionable()
    7483   
     
    129138        pass
    130139
    131     def rst_report(self, **macros):
     140    def rst_report(self, underlines=RST_UNDERLINES, **macros):
    132141        '''return a report (as *str*) of this database template in restructured text'''
     142        h1 = underlines[0]
     143        sub_uls = underlines[1:]
    133144        rst = []
    134         rst += ['='*len(self.filename),]
    135145        rst.append(self.filename)
    136         rst += ['='*len(self.filename),]
     146        rst += [h1*len(self.filename),]
    137147        rst.append('')
    138148        rst.append(':absolute filename: %s' % self.absolute_filename)
    139         for db in self.db:
     149        for dbSpec in self.dbSpec:
    140150            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()
    142153        return '\n'.join(rst)
    143154
    144     def rst_report_db(self, db, **macros):
     155    def rst_report_db(self, dbSpec, underlines=RST_UNDERLINES, **macros):
    145156        '''return a report (as *str*) of a database in restructured text'''
    146157        # 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:
    150160            # 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())
    152162            t = Table()
    153163            t.labels = keylist
    154             for pattern in db.patterns:
     164            for pattern in dbSpec.patterns:
    155165                t.rows.append( [pattern[k] for k in keylist] )
    156166       
     167        h1 = underlines[0]
    157168        rst = []
    158169        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 += ['']
    162175            rst += t.reST(fmt='simple').splitlines()
     176            rst += ['']
    163177        else:
    164178            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
    165197        rst.append('')
    166198        return '\n'.join(rst)
     
    182214    template = {}
    183215    for fname in sorted(filelist):
    184         template[fname] = Template(fname)
     216        template[fname] = Template(fname, STD='synApps/std/:')
    185217        print template[fname].rst_report()
    186218
  • topdoc/trunk/src/topdoc/test.template

    r1497 r1498  
    1010
    1111# no quotes around the file name
    12 file test2.db {
     12file test.db {
    1313    { this=sub1,that=sub2 }
    1414    { this=sub3,that=sub4 }
Note: See TracChangeset for help on using the changeset viewer.