More wx.NewID abuse removal. This time BeremizIDE.py was cleaned. wx.NewID_abuse_fix
authorEdouard Tisserant
Thu, 28 Mar 2019 13:08:20 +0100
branchwx.NewID_abuse_fix
changeset 2569 4f4d778c9376
parent 2567 eb9392511989
child 2570 1df8262b7d15
child 2580 9234abac093f
More wx.NewID abuse removal. This time BeremizIDE.py was cleaned.
Beremiz.py
BeremizIDE.py
--- a/Beremiz.py	Wed Mar 27 14:10:39 2019 +0100
+++ b/Beremiz.py	Thu Mar 28 13:08:20 2019 +0100
@@ -34,16 +34,22 @@
 from wx.lib.agw.advancedsplash import AdvancedSplash, AS_NOTIMEOUT, AS_CENTER_ON_SCREEN
 
 import traceback
+import time
 from pprint import pprint
 orig_NewId = wx.NewId
 log_newid = {}
+last = 0 
 def NewId(*a,**k):
+    global last
     res = orig_NewId(*a,**k)
     bt = '|'.join([':'.join([f,str(l)]) for f,l,_0,_1 in traceback.extract_stack()[-2:-1]])
     log_newid[bt] = log_newid.get(bt, 0) + 1
     worst = log_newid.items()
     worst.sort(key=lambda x:x[1])
-    pprint(worst)
+    now = time.time()
+    if now - last > 1:
+        pprint(worst)
+        last = now
     return res
 wx.NewId = NewId
 
--- a/BeremizIDE.py	Wed Mar 27 14:10:39 2019 +0100
+++ b/BeremizIDE.py	Thu Mar 28 13:08:20 2019 +0100
@@ -276,17 +276,14 @@
                                (wx.ID_PRINT, "print", _(u'Print'), None)])
 
     def _RecursiveAddMenuItems(self, menu, items):
-        for name, text, help, children in items:
-            new_id = wx.NewId()
+        for name, text, helpstr, children in items:
             if len(children) > 0:
                 new_menu = wx.Menu(title='')
-                menu.AppendMenu(new_id, text, new_menu)
+                menu.AppendMenu(wx.ID_ANY, text, new_menu)
                 self._RecursiveAddMenuItems(new_menu, children)
             else:
-                AppendMenu(menu, help=help, id=new_id,
-                           kind=wx.ITEM_NORMAL, text=text)
-                self.Bind(wx.EVT_MENU, self.GetAddConfNodeFunction(name),
-                          id=new_id)
+                item = menu.Append(wx.ID_ANY, text, helpstr)
+                self.Bind(wx.EVT_MENU, self.GetAddConfNodeFunction(name), item)
 
     def _init_coll_AddMenu_Items(self, parent):
         IDEFrame._init_coll_AddMenu_Items(self, parent, False)
@@ -299,9 +296,8 @@
                 _(u'Community support'),
                 wx.OK | wx.ICON_INFORMATION)
 
-        id = wx.NewId()
-        parent.Append(help='', id=id, kind=wx.ITEM_NORMAL, text=_(u'Community support'))
-        self.Bind(wx.EVT_MENU, handler, id=id)
+        item = parent.Append(wx.ID_ANY, _(u'Community support'), '')
+        self.Bind(wx.EVT_MENU, handler, item)
 
         parent.Append(help='', id=wx.ID_ABOUT,
                       kind=wx.ITEM_NORMAL, text=_(u'About'))
@@ -696,14 +692,11 @@
 
             if idx < self.RecentProjectsMenu.GetMenuItemCount():
                 item = self.RecentProjectsMenu.FindItemByPosition(idx)
-                id = item.GetId()
                 item.SetItemLabel(text)
                 self.Disconnect(id, id, wx.EVT_BUTTON._getEvtType())
             else:
-                id = wx.NewId()
-                AppendMenu(self.RecentProjectsMenu, help='', id=id,
-                           kind=wx.ITEM_NORMAL, text=text)
-            self.Bind(wx.EVT_MENU, self.GenerateOpenRecentProjectFunction(projectpath), id=id)
+                item = self.RecentProjectsMenu.Append(wx.ID_ANY, text, '')
+            self.Bind(wx.EVT_MENU, self.GenerateOpenRecentProjectFunction(projectpath), item)
 
     def GenerateOpenRecentProjectFunction(self, projectpath):
         def OpenRecentProject(event):
@@ -983,20 +976,17 @@
             if confnode is not None:
                 menu_items = confnode.GetContextualMenuItems()
                 if menu_items is not None:
-                    for text, help, callback in menu_items:
-                        new_id = wx.NewId()
-                        confnode_menu.Append(help=help, id=new_id, kind=wx.ITEM_NORMAL, text=text)
-                        self.Bind(wx.EVT_MENU, callback, id=new_id)
+                    for text, helpstr, callback in menu_items:
+                        item = confnode_menu.Append(wx.ID_ANY, text, helpstr)
+                        self.Bind(wx.EVT_MENU, callback, item)
                 else:
-                    for name, XSDClass, help in confnode.CTNChildrenTypes:
+                    for name, XSDClass, helpstr in confnode.CTNChildrenTypes:
                         if not hasattr(XSDClass, 'CTNMaxCount') or not confnode.Children.get(name) \
                                 or len(confnode.Children[name]) < XSDClass.CTNMaxCount:
-                            new_id = wx.NewId()
-                            confnode_menu.Append(help=help, id=new_id, kind=wx.ITEM_NORMAL, text=_("Add") + " " + name)
-                            self.Bind(wx.EVT_MENU, self.GetAddConfNodeFunction(name, confnode), id=new_id)
-            new_id = wx.NewId()
-            AppendMenu(confnode_menu, help='', id=new_id, kind=wx.ITEM_NORMAL, text=_("Delete"))
-            self.Bind(wx.EVT_MENU, self.GetDeleteMenuFunction(confnode), id=new_id)
+                            item = confnode_menu.Append(wx.ID_ANY, _("Add") + " " + name, helpstr)
+                            self.Bind(wx.EVT_MENU, self.GetAddConfNodeFunction(name, confnode), item)
+            item = confnode_menu.Append(wx.ID_ANY, _("Delete"))
+            self.Bind(wx.EVT_MENU, self.GetDeleteMenuFunction(confnode), item)
 
             self.PopupMenu(confnode_menu)
             confnode_menu.Destroy()