Changeset 1512 for trunk/GSASII.py


Ignore:
Timestamp:
Oct 1, 2014 10:35:23 PM (8 years ago)
Author:
toby
Message:

implement logging and config storage

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASII.py

    r1508 r1512  
    7777import GSASIIobj as G2obj
    7878import GSASIIlattice as G2lat
     79import GSASIIlog as log
    7980
    8081#wx inspector - use as needed
     
    116117    '''Define the main GSAS-II frame and its associated menu items
    117118    '''
     119    def MenuBinding(self,event):
     120        '''Called when a menu is clicked upon; looks up the binding in table
     121        '''
     122        log.InvokeMenuCommand(event.GetId(),self,event)
     123           
     124    def Bind(self,eventtype,handler,*args,**kwargs):
     125        '''Override the Bind function so that we can wrap calls that will be logged.
     126       
     127        N.B. This is a bit kludgy. Menu bindings with an id are wrapped and
     128        menu bindings with an object and no id are not.
     129        '''
     130        if eventtype == wx.EVT_MENU and 'id' in kwargs:
     131            menulabels = log.SaveMenuCommand(kwargs['id'],self,handler)
     132            if menulabels:
     133                wx.Frame.Bind(self,eventtype,self.MenuBinding,*args,**kwargs)
     134                return
     135        wx.Frame.Bind(self,eventtype,handler,*args,**kwargs)     
    118136   
    119137    def _Add_FileMenuItems(self, parent):
     
    16421660        return # success
    16431661
     1662    def OnMacroRecordStatus(self,event,setvalue=None):
     1663        '''Called when the record macro menu item is used which toggles the
     1664        value. Alternately a value to be set can be provided. Note that this
     1665        routine is made more complex because on the Mac there are lots of menu
     1666        items (listed in self.MacroStatusList) and this loops over all of them.
     1667        '''
     1668        nextvalue = log.ShowLogStatus() != True
     1669        if setvalue is not None:
     1670            nextvalue = setvalue
     1671        if nextvalue:
     1672            log.LogOn()
     1673            set2 = True
     1674        else:
     1675            log.LogOff()
     1676            set2 = False
     1677        for menuitem in self.MacroStatusList:
     1678            menuitem.Check(set2)
     1679
     1680    def _init_Macro(self):
     1681        '''Define the items in the macro menu.
     1682        '''
     1683        menu = self.MacroMenu
     1684        item = menu.Append(
     1685                help='Start or stop recording of menu actions, etc.', id=wx.ID_ANY,
     1686                kind=wx.ITEM_CHECK,text='Record actions')
     1687        self.MacroStatusList.append(item)
     1688        item.Check(log.ShowLogStatus())
     1689        self.Bind(wx.EVT_MENU, self.OnMacroRecordStatus, item)
     1690
     1691        # this may only be of value for development work
     1692        item = menu.Append(
     1693            help='Show logged commands', id=wx.ID_ANY,
     1694            kind=wx.ITEM_NORMAL,text='Show log')
     1695        def OnShowLog(event):
     1696            print 70*'='
     1697            print 'List of logged actions'
     1698            for i,line in enumerate(log.G2logList):
     1699                if line: print i,line
     1700            print 70*'='
     1701        self.Bind(wx.EVT_MENU, OnShowLog, item)
     1702
     1703        item = menu.Append(
     1704            help='Clear logged commands', id=wx.ID_ANY,
     1705            kind=wx.ITEM_NORMAL,text='Clear log')
     1706        def OnClearLog(event): log.G2logList=[None]
     1707        self.Bind(wx.EVT_MENU, OnClearLog, item)
     1708       
     1709        item = menu.Append(
     1710            help='Save logged commands to file', id=wx.ID_ANY,
     1711            kind=wx.ITEM_NORMAL,text='Save log')
     1712        def OnSaveLog(event):
     1713            import cPickle
     1714            defnam = os.path.splitext(
     1715                os.path.split(self.GSASprojectfile)[1]
     1716                )[0]+'.gcmd'
     1717            dlg = wx.FileDialog(self,
     1718                'Choose an file to save past actions', '.', defnam,
     1719                'GSAS-II cmd output (*.gcmd)|*.gcmd',
     1720                wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT|wx.CHANGE_DIR)
     1721            dlg.CenterOnParent()
     1722            try:
     1723                if dlg.ShowModal() == wx.ID_OK:
     1724                    filename = dlg.GetPath()
     1725                    # make sure extension is correct
     1726                    filename = os.path.splitext(filename)[0]+'.gcmd'
     1727                else:
     1728                    filename = None
     1729            finally:
     1730                dlg.Destroy()
     1731            if filename:
     1732                fp = open(filename,'wb')
     1733                fp.write(str(len(log.G2logList)-1)+'\n')
     1734                for item in log.G2logList:
     1735                    if item: cPickle.dump(item,fp)
     1736                fp.close()
     1737        self.Bind(wx.EVT_MENU, OnSaveLog, item)
     1738
     1739        item = menu.Append(
     1740            help='Load logged commands from file', id=wx.ID_ANY,
     1741            kind=wx.ITEM_NORMAL,text='Load log')
     1742        def OnLoadLog(event):
     1743            # this appends. Perhaps we should ask to clear?
     1744            import cPickle
     1745            defnam = os.path.splitext(
     1746                os.path.split(self.GSASprojectfile)[1]
     1747                )[0]+'.gcmd'
     1748            dlg = wx.FileDialog(self,
     1749                'Choose an file to read saved actions', '.', defnam,
     1750                'GSAS-II cmd output (*.gcmd)|*.gcmd',
     1751                wx.OPEN|wx.CHANGE_DIR)
     1752            dlg.CenterOnParent()
     1753            try:
     1754                if dlg.ShowModal() == wx.ID_OK:
     1755                    filename = dlg.GetPath()
     1756                    # make sure extension is correct
     1757                    filename = os.path.splitext(filename)[0]+'.gcmd'
     1758                else:
     1759                    filename = None
     1760            finally:
     1761                dlg.Destroy()
     1762            if filename and os.path.exists(filename):
     1763                fp = open(filename,'rb')
     1764                lines = fp.readline()
     1765                for i in range(int(lines)):
     1766                    log.G2logList.append(cPickle.load(fp))
     1767                fp.close()
     1768        self.Bind(wx.EVT_MENU, OnLoadLog, item)
     1769
     1770        item = menu.Append(
     1771            help='Replay saved commands', id=wx.ID_ANY,
     1772            kind=wx.ITEM_NORMAL,text='Replay log')
     1773        self.Bind(wx.EVT_MENU, log.ReplayLog, item)
     1774
    16441775    def _init_Exports(self,menu):
    16451776        '''Find exporter routines and add them into menus
     
    17921923
    17931924    def FillMainMenu(self,menubar):
    1794         '''Define contents of the main GSAS-II menu for the (main) data tree window
    1795         in the mac, used also for the data item windows as well.
     1925        '''Define contents of the main GSAS-II menu for the (main) data tree window.
     1926        For the mac, this is also called for the data item windows as well so that
     1927        the main menu items are data menu as well.
    17961928        '''
    17971929        File = wx.Menu(title='')
     
    18491981        self._init_Exports(self.ExportMenu)
    18501982        self._Add_ExportMenuItems(self.ExportMenu)
     1983        if GSASIIpath.GetConfigValue('Enable_logging'):
     1984            self.MacroMenu = wx.Menu(title='')
     1985            menubar.Append(menu=self.MacroMenu, title='Macro')
     1986            self._init_Macro()
    18511987        HelpMenu=G2gd.MyHelp(self,helpType='Data tree',
    18521988            morehelpitems=[('&Tutorials','Tutorials')])
     
    18732009        #
    18742010        self.GSASIIMenu = wx.MenuBar()
     2011        # create a list of all dataframe menus (appended in PrefillDataMenu)
     2012        self.dataMenuBars = [self.GSASIIMenu]
     2013        self.MacroStatusList = []
    18752014        self.FillMainMenu(self.GSASIIMenu)
    18762015        self.SetMenuBar(self.GSASIIMenu)
     
    18802019       
    18812020        wxID_PATTERNTREE = wx.NewId()
    1882         self.PatternTree = wx.TreeCtrl(id=wxID_PATTERNTREE,
     2021        #self.PatternTree = wx.TreeCtrl(id=wxID_PATTERNTREE, # replaced for logging
     2022        self.PatternTree = G2gd.G2TreeCtrl(id=wxID_PATTERNTREE,
    18832023            parent=self.mainPanel, pos=wx.Point(0, 0),style=wx.TR_DEFAULT_STYLE )
    1884         self.PatternTree.Bind(wx.EVT_TREE_SEL_CHANGED,
    1885             self.OnPatternTreeSelChanged, id=wxID_PATTERNTREE)
     2024        self.PatternTree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnPatternTreeSelChanged)
    18862025        self.PatternTree.Bind(wx.EVT_TREE_ITEM_COLLAPSED,
    18872026            self.OnPatternTreeItemCollapsed, id=wxID_PATTERNTREE)
     
    18962035        self.PatternTree.Bind(wx.EVT_TREE_END_DRAG,
    18972036            self.OnPatternTreeEndDrag, id=wxID_PATTERNTREE)       
    1898         self.root = self.PatternTree.AddRoot('Loaded Data: ')
     2037        #self.root = self.PatternTree.AddRoot('Loaded Data: ')
     2038        self.root = self.PatternTree.root
    18992039        plotFrame = wx.Frame(None,-1,'GSASII Plots',size=wx.Size(700,600), \
    19002040            style=wx.DEFAULT_FRAME_STYLE ^ wx.CLOSE_BOX)
Note: See TracChangeset for help on using the changeset viewer.