Changeset 78 for epics2xml/epics2xml.py


Ignore:
Timestamp:
Nov 6, 2009 10:10:08 AM (14 years ago)
Author:
jemian
Message:

Refs #2: cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • epics2xml/epics2xml.py

    r77 r78  
    2828import datetime
    2929import copy
    30 
    31 
    32 class Epics2Www:
     30import pvConnect
     31
     32
     33class Epics2Xml:
    3334    '''watch PVs and write to XMl'''
    3435
    35     def __init__(self, settingsFile=None):
     36    def __init__(self, configFile=None):
    3637        '''prepare the settings file
    37             @param settingsFile: [string] name of XML file with settings'''
    38         self.rootElement = 'epics2www'
     38            @param configFile: [string] name of XML file with settings'''
     39        self.rootElement = 'epics2xml'
    3940        self.Clear()
    40         self.SetSettingsFile(settingsFile)
     41        self.SetConfigFile(configFile)
    4142
    4243    def GetDb(self):
     
    4445        return self.db
    4546
    46     def GetSettingsFile(self):
     47    def GetConfigFile(self):
    4748        '''@return: name of XML settings file'''
    4849        return self.settingsFile
    4950
    50     def SetSettingsFile(self, thefile):
    51         '''set the name of XML settings file
    52             @param thefile: [string] name of XML file with settings'''
     51    def SetConfigFile(self, thefile):
     52        '''set the name of XML configuration file
     53            @param thefile: [string] name of XML file with the configuration'''
    5354        self.settingsFile = thefile
    5455
     
    5657        '''reset the internal data representation (db) to empty'''
    5758        self.db = {}
    58 
    59     def NewPair(self, title=''):
    60         ''' create space in the database (db) for a new pair
    61             and sets defaults for fields
    62 
    63             @param title: [string] the title of the XY_pair set (default="")
    64             @return: the index number'''
    65         if self.CountPairs() == -1:
    66             self.db[u"pairs"] = []
    67         pairdb = {}
    68         pairdb[u"@name"] = title
    69         pairdb[u"@selected"] = False
    70         self.db[u'pairs'].append(pairdb)
    71         return len(self.db[u'pairs'])-1
    72 
    73     def GetPairTitle(self, pairnum):
    74         '''return the name of the XY_pair
    75             @param pairnum: [int] index number of the XY_pair'''
    76         return self.db[u"pairs"][pairnum][u"@name"]
    77 
    78     def SetPairTitle(self, pairnum, title):
    79         '''set the name of the XY_pair
    80             @param pairnum: [int] index number of the XY_pair'
    81             @param title: [string] name of the XY_pair'''
    82         self.db[u"pairs"][pairnum][u"@name"] = title
    83 
    84     def SelectPair(self, pairnum):
    85         '''set the "selected" attribute of the pair
    86             @param pairnum: [int] index number of the XY_pair'''
    87         for pair in self.db[u"pairs"]:   # first, deselect all pairs
    88             pair[u"@selected"] = False
    89         self.db[u"pairs"][pairnum][u"@selected"] = True
    90 
    91     def GetSelectedPair(self):
    92         '''@return: index number of the "selected" pair (-1 if none selected)'''
    93         selected = -1
    94         try:
    95             pairs = self.db[u"pairs"]
    96             for pairnum in range(len(pairs)):
    97                 if pairs[pairnum][u"@selected"]:
    98                     selected = pairnum
    99                     break
    100         except:
    101             pass
    102         return selected
    103 
    104     def CountPairs(self):
    105         '''@return: number of pairs'''
    106         try:
    107             return len(self.db[u"pairs"])
    108         except:
    109             return -1
    110 
    111     def NewEpicsConfig(self, pairnum):
    112         '''Create internal space for a new EPICS configuration
    113             @param pairnum: [int] index number of the XY_pair'''
    114         pairdb = self.db[u"pairs"][pairnum]
    115         pairdb[u"epics"] = {}
    116         epicsdb = pairdb[u"epics"]
    117         for axis in ['x', 'y']:
    118             epicsdb[axis] = {}
    119             axisdb = epicsdb[axis]
    120             for field in wxmtxy_axis.field_list:
    121                 axisdb[field] = ""
    122             axisdb[u"isMotorRec"] = False
    123 
    124     def GetEpicsConfig(self, pairnum):
    125         '''Get a deep copy Python dictionary of the current EPICS PV config.
    126             @param pairnum: [int] index number of the XY_pair
    127             @return: the current EPICS configuration'''
    128         return copy.deepcopy(self.db[u"pairs"][pairnum][u"epics"])
    129 
    130     def SetEpicsConfig(self, pairnum, config):
    131         '''set the current EPICS configuration
    132             @param pairnum: [int] index number of the XY_pair
    133             @param config: Python dictionary of EPICS PV configuration'''
    134         pairdb = self.db[u"pairs"][pairnum]
    135         pairdb[u"epics"] = copy.deepcopy(config)
    136 
    137     def SetEpicsField(self, pairnum, axis, field, value):
    138         '''Define the EPICS config for a specific field
    139             @param pairnum: [int] index number of the XY_pair'
    140             @param axis: [string] "x" or "y"'
    141             @param field: [string] member of wxmtxy_axis.field_list'
    142             @param value: [string] value of this field'''
    143         try:
    144             axisdb = self.db[u"pairs"][pairnum][u"epics"][axis]
    145             axisdb[field] = value
    146         except:
    147             print "Could not assign EPICS field", pairnum, axis, field, value
    148 
    149 
    150     def NewTab(self, pairnum, title=''):
    151         ''' create space in the database (db) pair for a new tab
    152             and sets defaults for fields
    153 
    154             @param pairnum: [int] index number of the XY_pair
    155             @param title: the title of the pair set (default="")
    156             @return the index number'''
    157         pairdb = self.db[u"pairs"][pairnum]
    158         if not pairdb.has_key(u"tabs"):
    159             pairdb[u"tabs"] = []
    160         tabdb = {}
    161         tabdb[u"@name"] = title
    162         tabdb[u"@selected"] = False
    163         pairdb[u"tabs"].append(tabdb)
    164         return len(pairdb[u"tabs"])-1
    165 
    166     def GetTabTitle(self, pairnum, tabnum):
    167         '''return the name of the tab
    168             @param pairnum: [int] index number of the XY_pair
    169             @param tabnum: [int] index number of the Tab object'''
    170         return self.db[u"pairs"][pairnum][u"tabs"][tabnum][u"@name"]
    171 
    172     def SetTabTitle(self, pairnum, tabnum, title):
    173         '''set the name attribute of the tab
    174             @param pairnum: [int] index number of the XY_pair
    175             @param tabnum: [int] index number of the Tab object
    176             @param title: [string] title the Tab object'''
    177         self.db[u"pairs"][pairnum][u"tabs"][tabnum]["@name"] = title
    178 
    179     def SelectTab(self, pairnum, tabnum):
    180         '''set the selected attribute of the pair
    181             @param pairnum: [int] index number of the XY_pair
    182             @param tabnum: [int] index number of the Tab object'''
    183         pairdb = self.db[u"pairs"][pairnum]
    184         for tab in pairdb[u"tabs"]:   # first, deselect all tabs
    185             tab[u"@selected"] = False
    186         pairdb[u"tabs"][tabnum][u"@selected"] = True
    187 
    188     def GetSelectedTab(self, pairnum):
    189         '''return the index number of the selected tab
    190             @param pairnum: [int] index number of the XY_pair'''
    191         selected = -1
    192         try:
    193             tabs = self.db[u"pairs"][pairnum][u"tabs"]
    194             for tabnum in range(len(tabs)):
    195                 if tabs[tabnum][u"@selected"]:
    196                     selected = tabnum
    197                     break
    198         except:
    199             pass
    200         return selected
    201 
    202     def CountTabs(self, pairnum):
    203         '''return the number of tabs
    204             @param pairnum: [int] index number of the XY_pair'''
    205         try:
    206             return len(self.db[u"pairs"][pairnum][u"tabs"])
    207         except:
    208             return -1
    209 
    210     def NewRow(self, pairnum, tabnum, title=''):
    211         ''' create space in the database (db) pair for a new tab
    212             and sets defaults for fields
    213 
    214             @param pairnum: [int] index number of the XY_pair
    215             @param tabnum: [int] index number of the Tab object
    216             @param title: the title of the Tab object (default="")
    217             @return the index number'''
    218         tabdb = self.db[u"pairs"][pairnum][u"tabs"][tabnum]
    219         if not tabdb.has_key(u"rows"):
    220             tabdb[u"rows"] = []
    221         rowdb = {}
    222         rowdb[u"@name"] = title
    223         rowdb[u"@selected"] = False
    224         rowdb[u"@x"] = ""
    225         rowdb[u"@y"] = ""
    226         tabdb[u"rows"].append(rowdb)
    227         return len(tabdb[u"rows"])-1
    228 
    229     def GetRowTitle(self, pairnum, tabnum, rownum):
    230         '''return the name of the row
    231             @param pairnum: [int] index number of the XY_pair
    232             @param tabnum: [int] index number of the Tab object
    233             @param rownum: [int] index number of the Row object'''
    234         tabdb = self.db[u"pairs"][pairnum][u"tabs"][tabnum]
    235         return tabdb[u"rows"][rownum][u"@name"]
    236 
    237     def SetRowTitle(self, pairnum, tabnum, rownum, title):
    238         '''set the name attribute of the row
    239             @param pairnum: [int] index number of the XY_pair
    240             @param tabnum: [int] index number of the Tab object
    241             @param rownum: [int] index number of the Row object
    242             @param title: [string] title the Tab object'''
    243         tabdb = self.db[u"pairs"][pairnum][u"tabs"][tabnum]
    244         tabdb[u"rows"][rownum][u"@name"] = title
    245 
    246     def GetRowXY(self, pairnum, tabnum, rownum):
    247         '''return the name of the row
    248             @param pairnum: [int] index number of the XY_pair
    249             @param tabnum: [int] index number of the Tab object
    250             @param rownum: [int] index number of the Row object'''
    251         tabdb = self.db[u"pairs"][pairnum][u"tabs"][tabnum]
    252         x = tabdb[u"rows"][rownum][u"@x"]
    253         y = tabdb[u"rows"][rownum][u"@y"]
    254         return x, y
    255 
    256     def SetRowXY(self, pairnum, tabnum, rownum, x, y):
    257         '''set the name attribute of the row
    258             @param pairnum: [int] index number of the XY_pair
    259             @param tabnum: [int] index number of the Tab object
    260             @param x: [float] X axis position
    261             @param y: [float] Y axis position'''
    262         tabdb = self.db[u"pairs"][pairnum][u"tabs"][tabnum]
    263         tabdb[u"rows"][rownum][u"@x"] = x
    264         tabdb[u"rows"][rownum][u"@y"] = y
    265 
    266     def CountRows(self, pairnum, tabnum):
    267         '''return the number of rows
    268             @param pairnum: [int] index number of the XY_pair
    269             @param tabnum: [int] index number of the Tab object'''
    270         try:
    271             return len(self.db[u"pairs"][pairnum][u"tabs"][tabnum][u"rows"])
    272         except:
    273             return -1
    274 
    275     def GetSelectedRow(self, pairnum, tabnum):
    276         '''return the index number of the selected row
    277             @param pairnum: [int] index number of the XY_pair
    278             @param tabnum: [int] index number of the Tab object'''
    279         selected = -1
    280         try:
    281             rows = self.db[u"pairs"][pairnum][u"tabs"][tabnum][u"rows"]
    282             for rownum in range(len(rows)):
    283                 if rows[rownum][u"@selected"]:
    284                     selected = rownum
    285                     break
    286         except:
    287             pass
    288         return selected
    289 
    290     def SelectRow(self, pairnum, tabnum, rownum):
    291         '''set the selected attribute of the pair
    292             @param pairnum: [int] index number of the XY_pair
    293             @param tabnum: [int] index number of the Tab object
    294             @param rownum: [int] index number of the Row object'''
    295         tabdb = self.db[u"pairs"][pairnum][u"tabs"][tabnum]
    296         for row in tabdb[u"rows"]:   # first, deselect all rows
    297             row[u"@selected"] = False
    298         tabdb[u"rows"][rownum][u"@selected"] = True
    29959
    30060    def ReadXmlFile(self):
     
    419179        self._SetAttr(root, "time", hhmmss)
    420180        doc.appendChild(root)
    421         selectedpairnum = self.GetSelectedPair()
    422         for pairnum in range(self.CountPairs()):
    423             pairnode = doc.createElement("XYpair")
    424             self._SetAttr(pairnode, "name",
    425                    self.GetPairTitle(pairnum))
    426             if selectedpairnum == pairnum:
    427                 self._SetAttr(pairnode, "selected", "True")
    428             if self.db[u"pairs"][pairnum].has_key(u"epics"):
    429                 epicsnode = doc.createElement("EPICS_configuration")
    430                 epicsdb = self.db[u"pairs"][pairnum][u"epics"]
    431                 for axis in epicsdb:
    432                     axisnode = doc.createElement("axis")
    433                     self._SetAttr(axisnode, "name", axis)
    434                     field = "isMotorRec"
    435                     node = doc.createElement("flag")
    436                     self._SetAttr(node, field, str(epicsdb[axis][field]))
    437                     axisnode.appendChild(node)
    438                     for field in wxmtxy_axis.field_list:
    439                         if len(epicsdb[axis][field])>0:
    440                             node = doc.createElement("field")
    441                             self._SetAttr(node, "name", field)
    442                             self._SetAttr(node, "pv", str(epicsdb[axis][field]))
    443                             axisnode.appendChild(node)
    444                     epicsnode.appendChild(axisnode)
    445                 pairnode.appendChild(epicsnode)
    446             selectedtabnum = self.GetSelectedTab(pairnum)
    447             for tabnum in range(self.CountTabs(pairnum)):
    448                 tabnode = doc.createElement("tab")
    449                 self._SetAttr(tabnode, "name", self.GetTabTitle(pairnum, tabnum))
    450                 if selectedtabnum == tabnum:
    451                     self._SetAttr(tabnode, "selected", "True")
    452                 selectedrownum = self.GetSelectedRow(pairnum, tabnum)
    453                 for rownum in range(self.CountRows(pairnum, tabnum)):
    454                     rownode = doc.createElement("row")
    455                     label = self.GetRowTitle(pairnum, tabnum, rownum)
    456                     x, y = self.GetRowXY(pairnum, tabnum, rownum)
    457                     self._SetAttr(rownode, "name", label)
    458                     self._SetAttr(rownode, "x", x)
    459                     self._SetAttr(rownode, "y", y)
    460                     if selectedrownum == rownum:
    461                         self._SetAttr(rownode, "selected", "True")
    462                     tabnode.appendChild(rownode)
    463                 pairnode.appendChild(tabnode)
    464             root.appendChild(pairnode)
     181#        selectedpairnum = self.GetSelectedPair()
     182#        for pairnum in range(self.CountPairs()):
     183#            pairnode = doc.createElement("XYpair")
     184#            self._SetAttr(pairnode, "name",
     185#                   self.GetPairTitle(pairnum))
     186#            if selectedpairnum == pairnum:
     187#                self._SetAttr(pairnode, "selected", "True")
     188#            if self.db[u"pairs"][pairnum].has_key(u"epics"):
     189#                epicsnode = doc.createElement("EPICS_configuration")
     190#                epicsdb = self.db[u"pairs"][pairnum][u"epics"]
     191#                for axis in epicsdb:
     192#                    axisnode = doc.createElement("axis")
     193#                    self._SetAttr(axisnode, "name", axis)
     194#                    field = "isMotorRec"
     195#                    node = doc.createElement("flag")
     196#                    self._SetAttr(node, field, str(epicsdb[axis][field]))
     197#                    axisnode.appendChild(node)
     198#                    for field in wxmtxy_axis.field_list:
     199#                        if len(epicsdb[axis][field])>0:
     200#                            node = doc.createElement("field")
     201#                            self._SetAttr(node, "name", field)
     202#                            self._SetAttr(node, "pv", str(epicsdb[axis][field]))
     203#                            axisnode.appendChild(node)
     204#                    epicsnode.appendChild(axisnode)
     205#                pairnode.appendChild(epicsnode)
     206#            selectedtabnum = self.GetSelectedTab(pairnum)
     207#            for tabnum in range(self.CountTabs(pairnum)):
     208#                tabnode = doc.createElement("tab")
     209#                self._SetAttr(tabnode, "name", self.GetTabTitle(pairnum, tabnum))
     210#                if selectedtabnum == tabnum:
     211#                    self._SetAttr(tabnode, "selected", "True")
     212#                selectedrownum = self.GetSelectedRow(pairnum, tabnum)
     213#                for rownum in range(self.CountRows(pairnum, tabnum)):
     214#                    rownode = doc.createElement("row")
     215#                    label = self.GetRowTitle(pairnum, tabnum, rownum)
     216#                    x, y = self.GetRowXY(pairnum, tabnum, rownum)
     217#                    self._SetAttr(rownode, "name", label)
     218#                    self._SetAttr(rownode, "x", x)
     219#                    self._SetAttr(rownode, "y", y)
     220#                    if selectedrownum == rownum:
     221#                        self._SetAttr(rownode, "selected", "True")
     222#                    tabnode.appendChild(rownode)
     223#                pairnode.appendChild(tabnode)
     224#            root.appendChild(pairnode)
    465225        return doc.toprettyxml(indent="  ")
    466226
    467227
    468228if __name__ == '__main__':
    469     rc = Settings("examples/test-settings.xml")
    470     rc.ReadXmlFile()
    471     rc.SetSettingsFile('output-test.xml')
    472     rc.SaveXmlFile()
    473 
    474     pj = Settings()
    475     pairnum = pj.NewPair("my test pair")
    476     tabnum = pj.NewTab(pairnum, title='my test tab')
    477     tabnum = pj.NewTab(pairnum, title='another')
    478     pj.SelectTab(pairnum, tabnum)
    479     tabnum = pj.NewTab(pairnum, title='another')
    480     tabnum = pj.NewTab(pairnum, title='another')
    481     pairnum = pj.NewPair("another")
    482     pj.SelectPair(pairnum)
    483     tabnum = pj.NewTab(pairnum, title='another')
    484     rownum = pj.NewRow(pairnum, tabnum, "beam center")
    485     pj.SelectRow(pairnum, tabnum, rownum)
    486     tabnum = pj.NewTab(pairnum, title='another')
    487     pairnum = pj.NewPair("another")
    488     print str(pj)
     229#    rc = Settings("examples/test-settings.xml")
     230#    rc.ReadXmlFile()
     231#    rc.SetConfigFile('output-test.xml')
     232#    rc.SaveXmlFile()
     233#
     234#    pj = Settings()
     235#    pairnum = pj.NewPair("my test pair")
     236#    tabnum = pj.NewTab(pairnum, title='my test tab')
     237#    tabnum = pj.NewTab(pairnum, title='another')
     238#    pj.SelectTab(pairnum, tabnum)
     239#    tabnum = pj.NewTab(pairnum, title='another')
     240#    tabnum = pj.NewTab(pairnum, title='another')
     241#    pairnum = pj.NewPair("another")
     242#    pj.SelectPair(pairnum)
     243#    tabnum = pj.NewTab(pairnum, title='another')
     244#    rownum = pj.NewRow(pairnum, tabnum, "beam center")
     245#    pj.SelectRow(pairnum, tabnum, rownum)
     246#    tabnum = pj.NewTab(pairnum, title='another')
     247#    pairnum = pj.NewPair("another")
     248#    print str(pj)
     249    print "Hello"
Note: See TracChangeset for help on using the changeset viewer.