fake_wx.py
branchwxPython4
changeset 3501 fa291393aac7
child 3549 0af7b6a96c53
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fake_wx.py	Mon Jun 13 18:05:12 2022 +0200
@@ -0,0 +1,109 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import sys
+import new
+from types import ModuleType
+
+# TODO use gettext instead
+def get_translation(txt):
+    return txt
+
+
+class FakeObject:
+    def __init__(self, *args, **kwargs):
+        self.__classname__ = kwargs["__classname__"]
+
+    def __getattr__(self,name):
+        if name.startswith('__'):
+            raise AttributeError, name
+        return FakeObject(__classname__=self.__classname__+"."+name)
+
+    def __call__(self, *args, **kwargs):
+        return FakeObject(__classname__=self.__classname__+"()")
+
+    def __getitem__(self, key):
+        raise IndexError, key
+
+    def __str__(self):
+        return self.__classname__
+
+    def __or__(self, other):
+        return FakeObject(__classname__=self.__classname__+"|"+other.__classname__)
+
+
+class FakeClass:
+    def __init__(self, *args, **kwargs):
+        print("DUMMY Class __init__ !",self.__name__,args,kwargs)
+
+
+class FakeModule(ModuleType):
+    def __init__(self, name, classes):
+        self.__modname__ = name
+        self.__objects__ = dict(map(lambda desc: 
+            (desc, new.classobj(desc, (FakeClass,), {}))
+            if type(desc)==str else desc, classes))
+        ModuleType(name)
+
+    def __getattr__(self,name):
+        if name.startswith('__'):
+            raise AttributeError, name
+  
+        if self.__objects__.has_key(name):
+            return self.__objects__[name]
+  
+        obj = FakeObject(__classname__=self.__modname__+"."+name)
+        self.__objects__[name] = obj
+        return obj
+
+
+# Keep track of already faked modules to catch those
+# that are already present in sys.modules from start
+# (i.e. mpl_toolkits for exemple)
+already_patched = {}
+
+for name, classes in [
+    # list given for each module name contains name string for a FakeClass,
+    # otherwise a tuple (name, object) for arbitrary object/function/class
+    ('wx',[
+        'Panel', 'PyCommandEvent', 'Dialog', 'PopupWindow', 'TextEntryDialog',
+        'Notebook', 'ListCtrl', 'TextDropTarget', 'PyControl', 'TextCtrl', 
+        'SplitterWindow', 'Frame', 'Printout', 'StaticBitmap',
+        ('GetTranslation', get_translation)]),
+    ('wx.lib.agw.advancedsplash',[]),
+    ('wx.lib.buttons',['GenBitmapTextButton']),
+    ('wx.adv',['EditableListBox']),
+    ('wx.grid',[
+        'Grid', 'PyGridTableBase', 'GridCellEditor', 'GridCellTextEditor',
+        'GridCellChoiceEditor']),
+    ('wx.lib.agw.customtreectrl',['CustomTreeCtrl']),
+    ('wx.lib.intctrl',['IntCtrl']),
+    ('matplotlib.pyplot',[]),
+    ('matplotlib.backends.backend_wxagg',['FigureCanvasWxAgg']),
+    ('wx.stc',['StyledTextCtrl']),
+    ('wx.lib.scrolledpanel',[]),
+    ('wx.lib.mixins.listctrl',['ColumnSorterMixin', 'ListCtrlAutoWidthMixin']),
+    ('wx.dataview',['PyDataViewIndexListModel']),
+    ('matplotlib.backends.backend_agg',[]),
+    ('wx.aui',[]),
+    ('mpl_toolkits.mplot3d',[])]:
+    modpath = None
+    parentmod = None
+    for identifier in name.split("."):
+        modpath = (modpath + "." + identifier) if modpath else identifier
+        mod = sys.modules.get(modpath, None)
+
+        if mod is None or modpath not in already_patched:
+            mod = FakeModule(modpath, classes)
+            sys.modules[modpath] = mod
+            already_patched[modpath] = True
+
+        if parentmod is not None:
+            parentmod.__objects__[identifier] = mod
+
+        parentmod = mod
+
+from six.moves import builtins
+
+builtins.__dict__['_'] = get_translation
+