1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
"""Filling is the gui tree control through which a user can navigate
the local namespace or any object."""
__author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
import wx
from . import dispatcher
from . import editwindow
from . import images
import inspect
from . import introspect
import types
COMMONTYPES = [getattr(types, t) for t in dir(types) \
if not t.startswith('_') \
and t not in ('ClassType', 'InstanceType', 'ModuleType')]
DOCTYPES = ('BuiltinFunctionType', 'BuiltinMethodType', 'ClassType',
'FunctionType', 'GeneratorType', 'InstanceType',
'LambdaType', 'MethodType', 'ModuleType',
'UnboundMethodType', 'method-wrapper')
SIMPLETYPES = [getattr(types, t) for t in dir(types) \
if not t.startswith('_') and t not in DOCTYPES]
#del t
try:
COMMONTYPES.append(type(''.__repr__)) # Method-wrapper in version 2.2.x.
except AttributeError:
pass
class FillingTree(wx.TreeCtrl):
"""FillingTree based on TreeCtrl."""
name = 'Filling Tree'
def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.TR_DEFAULT_STYLE,
rootObject=None, rootLabel=None, rootIsNamespace=False,
static=False):
"""Create FillingTree instance."""
wx.TreeCtrl.__init__(self, parent, id, pos, size, style)
self.rootIsNamespace = rootIsNamespace
import __main__
if rootObject is None:
rootObject = __main__.__dict__
self.rootIsNamespace = True
if rootObject is __main__.__dict__ and rootLabel is None:
rootLabel = 'locals()'
if not rootLabel:
rootLabel = 'Ingredients'
self.item = self.root = self.AddRoot(rootLabel, -1, -1, rootObject)
self.SetItemHasChildren(self.root, self.objHasChildren(rootObject))
self.Bind(wx.EVT_TREE_ITEM_EXPANDING, self.OnItemExpanding, id=self.GetId())
self.Bind(wx.EVT_TREE_ITEM_COLLAPSED, self.OnItemCollapsed, id=self.GetId())
self.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnSelChanged, id=self.GetId())
self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnItemActivated, id=self.GetId())
if not static:
dispatcher.connect(receiver=self.push, signal='Interpreter.push')
def push(self, command, more):
"""Receiver for Interpreter.push signal."""
if not self:
dispatcher.disconnect(receiver=self.push, signal='Interpreter.push')
return
self.display()
def OnItemExpanding(self, event):
"""Add children to the item."""
busy = wx.BusyCursor()
item = event.GetItem()
if self.IsExpanded(item):
return
self.addChildren(item)
# self.SelectItem(item)
def OnItemCollapsed(self, event):
"""Remove all children from the item."""
busy = wx.BusyCursor()
item = event.GetItem()
# self.CollapseAndReset(item)
# self.DeleteChildren(item)
# self.SelectItem(item)
def OnSelChanged(self, event):
"""Display information about the item."""
busy = wx.BusyCursor()
self.item = event.GetItem()
self.display()
def OnItemActivated(self, event):
"""Launch a DirFrame."""
item = event.GetItem()
text = self.getFullName(item)
obj = self.GetItemData(item)
frame = FillingFrame(parent=self, size=(600, 100), rootObject=obj,
rootLabel=text, rootIsNamespace=False)
frame.Show()
def objHasChildren(self, obj):
"""Return true if object has children."""
if self.objGetChildren(obj):
return True
else:
return False
def objGetChildren(self, obj):
"""Return dictionary with attributes or contents of object."""
busy = wx.BusyCursor()
otype = type(obj)
if (isinstance(obj, dict)
or 'BTrees' in str(otype)
and hasattr(obj, 'keys')):
return obj
d = {}
if isinstance(obj, (list, tuple)):
for n in range(len(obj)):
key = '[' + str(n) + ']'
d[key] = obj[n]
if otype not in COMMONTYPES:
for key in introspect.getAttributeNames(obj):
if wx.Platform == '__WXMSW__':
if key == 'DropTarget': # Windows bug fix.
continue
# Believe it or not, some attributes can disappear,
# such as the exc_traceback attribute of the sys
# module. So this is nested in a try block.
try:
d[key] = getattr(obj, key)
except Exception:
pass
return d
def addChildren(self, item):
self.DeleteChildren(item)
obj = self.GetItemData(item)
children = self.objGetChildren(obj)
if not children:
return
keys = sorted(children, key=lambda x: str(x).lower())
for key in keys:
itemtext = str(key)
# Show string dictionary items with single quotes, except
# for the first level of items, if they represent a
# namespace.
if isinstance(obj, dict) \
and isinstance(key, str) \
and (item != self.root
or (item == self.root and not self.rootIsNamespace)):
itemtext = repr(key)
child = children[key]
branch = self.AppendItem(parent=item, text=itemtext, data=child)
self.SetItemHasChildren(branch, self.objHasChildren(child))
def display(self):
item = self.item
if not item:
return
if self.IsExpanded(item):
self.addChildren(item)
self.setText('')
obj = self.GetItemData(item)
if wx.Platform == '__WXMSW__':
if obj is None: # Windows bug fix.
return
self.SetItemHasChildren(item, self.objHasChildren(obj))
otype = type(obj)
text = ''
text += self.getFullName(item)
text += '\n\nType: ' + str(otype)
try:
value = str(obj)
except Exception:
value = ''
if isinstance(obj, str):
value = repr(obj)
text += u'\n\nValue: ' + value
if otype not in SIMPLETYPES:
try:
text += '\n\nDocstring:\n\n"""' + \
inspect.getdoc(obj).strip() + '"""'
except Exception:
pass
if isinstance(obj, type):
try:
text += '\n\nClass Definition:\n\n' + \
inspect.getsource(obj.__class__)
except Exception:
pass
else:
try:
text += '\n\nSource Code:\n\n' + \
inspect.getsource(obj)
except Exception:
pass
self.setText(text)
def getFullName(self, item, partial=''):
"""Return a syntactically proper name for item."""
name = self.GetItemText(item)
parent = None
obj = None
if item != self.root:
parent = self.GetItemParent(item)
obj = self.GetItemData(parent)
# Apply dictionary syntax to dictionary items, except the root
# and first level children of a namespace.
if ((isinstance(obj, dict)
or 'BTrees' in str(type(obj))
and hasattr(obj, 'keys'))
and ((item != self.root and parent != self.root)
or (parent == self.root and not self.rootIsNamespace))):
name = '[' + name + ']'
# Apply dot syntax to multipart names.
if partial:
if partial[0] == '[':
name += partial
else:
name += '.' + partial
# Repeat for everything but the root item
# and first level children of a namespace.
if (item != self.root and parent != self.root) \
or (parent == self.root and not self.rootIsNamespace):
name = self.getFullName(parent, partial=name)
return name
def setText(self, text):
"""Display information about the current selection."""
# This method will likely be replaced by the enclosing app to
# do something more interesting, like write to a text control.
print(text)
def setStatusText(self, text):
"""Display status information."""
# This method will likely be replaced by the enclosing app to
# do something more interesting, like write to a status bar.
print(text)
class FillingText(editwindow.EditWindow):
"""FillingText based on StyledTextCtrl."""
name = 'Filling Text'
def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.CLIP_CHILDREN,
static=False):
"""Create FillingText instance."""
editwindow.EditWindow.__init__(self, parent, id, pos, size, style)
# Configure various defaults and user preferences.
self.SetReadOnly(True)
self.SetWrapMode(True)
self.SetMarginWidth(1, 0)
if not static:
dispatcher.connect(receiver=self.push, signal='Interpreter.push')
def push(self, command, more):
"""Receiver for Interpreter.push signal."""
if not self:
dispatcher.disconnect(receiver=self.push, signal='Interpreter.push')
return
self.Refresh()
def SetText(self, *args, **kwds):
self.SetReadOnly(False)
editwindow.EditWindow.SetText(self, *args, **kwds)
self.SetReadOnly(True)
class Filling(wx.SplitterWindow):
"""Filling based on wxSplitterWindow."""
name = 'Filling'
def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.SP_3D|wx.SP_LIVE_UPDATE,
name='Filling Window', rootObject=None,
rootLabel=None, rootIsNamespace=False, static=False):
"""Create a Filling instance."""
wx.SplitterWindow.__init__(self, parent, id, pos, size, style, name)
self.tree = FillingTree(parent=self, rootObject=rootObject,
rootLabel=rootLabel,
rootIsNamespace=rootIsNamespace,
static=static)
self.text = FillingText(parent=self, static=static)
wx.CallLater(25, self.SplitVertically, self.tree, self.text, 200)
self.SetMinimumPaneSize(1)
# Override the filling so that descriptions go to FillingText.
self.tree.setText = self.text.SetText
# Display the root item.
self.tree.SelectItem(self.tree.root)
self.tree.display()
self.Bind(wx.EVT_SPLITTER_SASH_POS_CHANGED, self.OnChanged)
def OnChanged(self, event):
#this is important: do not evaluate this event=> otherwise,
# splitterwindow behaves strangely
#event.Skip()
pass
def LoadSettings(self, config):
pos = config.ReadInt('Sash/FillingPos', 200)
if not self.IsSplit():
self.SplitVertically(self.tree, self.text)
wx.CallLater(250, self.SetSashPosition, pos)
zoom = config.ReadInt('View/Zoom/Filling', -99)
if zoom != -99:
self.text.SetZoom(zoom)
def SaveSettings(self, config):
config.WriteInt('Sash/FillingPos', self.GetSashPosition())
config.WriteInt('View/Zoom/Filling', self.text.GetZoom())
class FillingFrame(wx.Frame):
"""Frame containing the namespace tree component."""
name = 'Filling Frame'
def __init__(self, parent=None, id=-1, title='PyFilling',
pos=wx.DefaultPosition, size=(600, 400),
style=wx.DEFAULT_FRAME_STYLE, rootObject=None,
rootLabel=None, rootIsNamespace=False, static=False):
"""Create FillingFrame instance."""
wx.Frame.__init__(self, parent, id, title, pos, size, style)
intro = 'PyFilling - The Tastiest Namespace Inspector'
self.CreateStatusBar()
self.SetStatusText(intro)
self.SetIcon(images.getPyIcon())
self.filling = Filling(parent=self, rootObject=rootObject,
rootLabel=rootLabel,
rootIsNamespace=rootIsNamespace,
static=static)
# Override so that status messages go to the status bar.
self.filling.tree.setStatusText = self.SetStatusText
class App(wx.App):
"""PyFilling standalone application."""
def OnInit(self):
self.fillingFrame = FillingFrame()
self.fillingFrame.Show(True)
self.SetTopWindow(self.fillingFrame)
return True
|