fake_wx.py
branchwxPython4
changeset 3501 fa291393aac7
child 3549 0af7b6a96c53
equal deleted inserted replaced
3491:88c4b18453d5 3501:fa291393aac7
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 import sys
       
     5 import new
       
     6 from types import ModuleType
       
     7 
       
     8 # TODO use gettext instead
       
     9 def get_translation(txt):
       
    10     return txt
       
    11 
       
    12 
       
    13 class FakeObject:
       
    14     def __init__(self, *args, **kwargs):
       
    15         self.__classname__ = kwargs["__classname__"]
       
    16 
       
    17     def __getattr__(self,name):
       
    18         if name.startswith('__'):
       
    19             raise AttributeError, name
       
    20         return FakeObject(__classname__=self.__classname__+"."+name)
       
    21 
       
    22     def __call__(self, *args, **kwargs):
       
    23         return FakeObject(__classname__=self.__classname__+"()")
       
    24 
       
    25     def __getitem__(self, key):
       
    26         raise IndexError, key
       
    27 
       
    28     def __str__(self):
       
    29         return self.__classname__
       
    30 
       
    31     def __or__(self, other):
       
    32         return FakeObject(__classname__=self.__classname__+"|"+other.__classname__)
       
    33 
       
    34 
       
    35 class FakeClass:
       
    36     def __init__(self, *args, **kwargs):
       
    37         print("DUMMY Class __init__ !",self.__name__,args,kwargs)
       
    38 
       
    39 
       
    40 class FakeModule(ModuleType):
       
    41     def __init__(self, name, classes):
       
    42         self.__modname__ = name
       
    43         self.__objects__ = dict(map(lambda desc: 
       
    44             (desc, new.classobj(desc, (FakeClass,), {}))
       
    45             if type(desc)==str else desc, classes))
       
    46         ModuleType(name)
       
    47 
       
    48     def __getattr__(self,name):
       
    49         if name.startswith('__'):
       
    50             raise AttributeError, name
       
    51   
       
    52         if self.__objects__.has_key(name):
       
    53             return self.__objects__[name]
       
    54   
       
    55         obj = FakeObject(__classname__=self.__modname__+"."+name)
       
    56         self.__objects__[name] = obj
       
    57         return obj
       
    58 
       
    59 
       
    60 # Keep track of already faked modules to catch those
       
    61 # that are already present in sys.modules from start
       
    62 # (i.e. mpl_toolkits for exemple)
       
    63 already_patched = {}
       
    64 
       
    65 for name, classes in [
       
    66     # list given for each module name contains name string for a FakeClass,
       
    67     # otherwise a tuple (name, object) for arbitrary object/function/class
       
    68     ('wx',[
       
    69         'Panel', 'PyCommandEvent', 'Dialog', 'PopupWindow', 'TextEntryDialog',
       
    70         'Notebook', 'ListCtrl', 'TextDropTarget', 'PyControl', 'TextCtrl', 
       
    71         'SplitterWindow', 'Frame', 'Printout', 'StaticBitmap',
       
    72         ('GetTranslation', get_translation)]),
       
    73     ('wx.lib.agw.advancedsplash',[]),
       
    74     ('wx.lib.buttons',['GenBitmapTextButton']),
       
    75     ('wx.adv',['EditableListBox']),
       
    76     ('wx.grid',[
       
    77         'Grid', 'PyGridTableBase', 'GridCellEditor', 'GridCellTextEditor',
       
    78         'GridCellChoiceEditor']),
       
    79     ('wx.lib.agw.customtreectrl',['CustomTreeCtrl']),
       
    80     ('wx.lib.intctrl',['IntCtrl']),
       
    81     ('matplotlib.pyplot',[]),
       
    82     ('matplotlib.backends.backend_wxagg',['FigureCanvasWxAgg']),
       
    83     ('wx.stc',['StyledTextCtrl']),
       
    84     ('wx.lib.scrolledpanel',[]),
       
    85     ('wx.lib.mixins.listctrl',['ColumnSorterMixin', 'ListCtrlAutoWidthMixin']),
       
    86     ('wx.dataview',['PyDataViewIndexListModel']),
       
    87     ('matplotlib.backends.backend_agg',[]),
       
    88     ('wx.aui',[]),
       
    89     ('mpl_toolkits.mplot3d',[])]:
       
    90     modpath = None
       
    91     parentmod = None
       
    92     for identifier in name.split("."):
       
    93         modpath = (modpath + "." + identifier) if modpath else identifier
       
    94         mod = sys.modules.get(modpath, None)
       
    95 
       
    96         if mod is None or modpath not in already_patched:
       
    97             mod = FakeModule(modpath, classes)
       
    98             sys.modules[modpath] = mod
       
    99             already_patched[modpath] = True
       
   100 
       
   101         if parentmod is not None:
       
   102             parentmod.__objects__[identifier] = mod
       
   103 
       
   104         parentmod = mod
       
   105 
       
   106 from six.moves import builtins
       
   107 
       
   108 builtins.__dict__['_'] = get_translation
       
   109