Attachment 'wxMock.py'

Download

   1 from types import ModuleType
   2 
   3 class Dummy(ModuleType):
   4   def __getattr__(self,name):
   5       if name.startswith('__'):
   6           raise AttributeError, name
   7       if name.endswith('VERSION') or name.startswith('VERSION'):
   8           import wx.__version__
   9           return getattr(sys.modules['wx.__version__'],name)
  10       return lambda *__args,**__kw: None
  11 
  12   def _wxPySetDictionary(self,d):
  13       d['Platform'] = '__WXMSW__'
  14       d['__wxPyPtrTypeMap'] = {}
  15   
  16 _core_ = Dummy("dummy")
  17 _core_.cvar = Dummy("dummy")
  18 _gdi_ = Dummy("dummy")
  19 _gdi_.cvar = Dummy("dummy")
  20 _windows_ = Dummy("dummy")
  21 _windows_.cvar = Dummy("dummy")
  22 _controls_ = Dummy("dummy")
  23 _controls_.cvar = Dummy("dummy")
  24 
  25 import sys
  26 sys.modules['wx._core_'] = _core_
  27 sys.modules['wx._gdi_'] = _gdi_
  28 sys.modules['wx._windows_'] = _windows_
  29 sys.modules['wx._controls_'] = _controls_
  30 sys.modules['wx._misc_'] = _controls_
  31 sys.modules['wx._html'] = _controls_
  32 sys.modules['wx._grid'] = _controls_
  33 import wx
  34 import wx.html
  35 import wx.grid
  36 
  37 from mock import Mock
  38 
  39 def makeAssociation(strClass, strModule = 'wx'):
  40     if 'wxMock' in sys.modules:
  41         setattr(sys.modules[strModule], strClass, getattr(sys.modules['wxMock'], 'My' + strClass))
  42     else:
  43         setattr(sys.modules[strModule], strClass, getattr(sys.modules['__main__'], 'My' + strClass))
  44 
  45 class wxMock(Mock):
  46     def __init__(self, *args, **kwargs):
  47         Mock.__init__(self)
  48 
  49 #################################################
  50 class MyFrame(wxMock):
  51     pass
  52 makeAssociation('Frame')
  53 
  54 class MyIcon(wxMock):
  55     pass
  56 makeAssociation('Icon')
  57 
  58 class MyMenuBar(wxMock):
  59     pass
  60 makeAssociation('MenuBar')
  61 
  62 class MyMenu(wxMock):
  63     pass
  64 makeAssociation('Menu')
  65 
  66 class MyAcceleratorTable(wxMock):
  67     pass
  68 makeAssociation('AcceleratorTable')
  69 
  70 class MyStaticText(wxMock):
  71     pass        
  72 makeAssociation('StaticText')
  73 
  74 class MyTextCtrl(Mock):
  75     def __init__(self, *args, **kwargs):
  76         self.strValue = ''
  77         Mock.__init__(self)
  78         
  79     def GetValue(self):
  80         return self.strValue
  81     def SetValue(self, str):
  82         self.strValue = str
  83         
  84 makeAssociation('TextCtrl')
  85     
  86 class MyNotebook(wxMock):
  87     pass
  88 makeAssociation('Notebook')
  89 
  90 class MyBoxSizer(wxMock):
  91     pass
  92 makeAssociation('BoxSizer')
  93 
  94 class MyRadioButton(wxMock):
  95     pass
  96 makeAssociation('RadioButton')
  97 
  98 class MyPanel(wxMock):
  99     pass
 100 makeAssociation('Panel')
 101 
 102 class MyButton(wxMock):
 103     pass
 104 makeAssociation('Button')
 105 
 106 class MyHtmlEasyPrinting(wxMock):
 107     pass
 108 makeAssociation('HtmlEasyPrinting', 'wx.html')
 109     
 110 class MyHtmlWindow(wxMock):
 111     pass
 112 makeAssociation('HtmlWindow', 'wx.html')
 113 
 114 class MyGrid(Mock):
 115     def __init__(self, *args, **kwargs):
 116         self.data = [[]]
 117         self.nCols = 0
 118         self.nCurRow = 0
 119         Mock.__init__(self)
 120         
 121     def CreateGrid(self, numRows, numCols):
 122         self.data = []
 123         self.nCols = numCols
 124         for nRow in range(numRows):
 125             self.AppendRows(1)
 126         
 127     def SetCellValue(self, row, col, s):
 128         self.data[row][col] = s
 129         
 130     def GetNumberRows(self):
 131         return len(self.data)
 132 
 133     def DeleteRows(self, pos = 0, numRows = 1, updateLabels = True):
 134         print "Todo"
 135         
 136     def AppendRows(self, numRows = 1, updateLabels = True):
 137         for nRow in range(numRows):
 138             self.data.append([ [] for nCol in range(self.nCols)])
 139         
 140         return True
 141         
 142     def GetGridCursorRow(self):
 143         return self.nCurRow
 144         
 145     def MoveCursorDown(self, expandSelection):
 146         self.nCurRow += 1
 147         if self.nCurRow >= self.GetNumberRows():
 148             self.nCurRow -= 1
 149 
 150         
 151 makeAssociation('Grid', 'wx.grid')
 152 
 153 wx.WXK_NUMPAD0 = 326
 154 wx.WXK_NUMPAD9 = 335

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-10-04 09:16:02, 3.8 KB) [[attachment:wxMock.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.