-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunsnake.py
794 lines (737 loc) · 28.6 KB
/
runsnake.py
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
#! /usr/bin/env python
"""The main script for the RunSnakeRun profile viewer"""
import wx, sys, os, logging, traceback
import ConfigParser
try:
from wx.py import editor, editwindow
except ImportError, err:
editor = None
from gettext import gettext as _
import pstats
from squaremap import squaremap
from runsnakerun import pstatsloader,pstatsadapter, meliaeloader, meliaeadapter
from runsnakerun import listviews
from runsnakerun import homedirectory
if sys.platform == 'win32':
windows = True
else:
windows = False
log = logging.getLogger(__name__)
ID_OPEN = wx.NewId()
ID_OPEN_MEMORY = wx.NewId()
ID_EXIT = wx.NewId()
ID_PACKAGE_VIEW = wx.NewId()
ID_PERCENTAGE_VIEW = wx.NewId()
ID_ROOT_VIEW = wx.NewId()
ID_BACK_VIEW = wx.NewId()
ID_UP_VIEW = wx.NewId()
ID_DEEPER_VIEW = wx.NewId()
ID_SHALLOWER_VIEW = wx.NewId()
ID_MORE_SQUARE = wx.NewId()
PROFILE_VIEW_COLUMNS = [
listviews.ColumnDefinition(
name = _('Name'),
attribute = 'name',
defaultOrder = True,
targetWidth = 50,
),
listviews.ColumnDefinition(
name = _('Calls'),
attribute = 'calls',
defaultOrder = False,
targetWidth = 50,
),
listviews.ColumnDefinition(
name = _('RCalls'),
attribute = 'recursive',
defaultOrder = False,
targetWidth = 40,
),
listviews.ColumnDefinition(
name = _('Local'),
attribute = 'local',
format = '%0.5f',
defaultOrder = False,
percentPossible = True,
targetWidth = 50,
),
listviews.ColumnDefinition(
name = _('/Call'),
attribute = 'localPer',
defaultOrder = False,
format = '%0.5f',
targetWidth = 50,
),
listviews.ColumnDefinition(
name = _('Cum'),
attribute = 'cummulative',
format = '%0.5f',
percentPossible = True,
targetWidth = 50,
defaultOrder = False,
sortDefault = True,
),
listviews.ColumnDefinition(
name = _('/Call'),
attribute = 'cummulativePer',
format = '%0.5f',
defaultOrder = False,
targetWidth = 50,
),
listviews.ColumnDefinition(
name = _('File'),
attribute = 'filename',
sortOn = ('filename', 'lineno', 'directory',),
defaultOrder = True,
targetWidth = 70,
),
listviews.ColumnDefinition(
name = _('Line'),
attribute = 'lineno',
sortOn = ('filename', 'lineno', 'directory'),
defaultOrder = True,
targetWidth = 30,
),
listviews.ColumnDefinition(
name = _('Directory'),
attribute = 'directory',
sortOn = ('directory', 'filename', 'lineno'),
defaultOrder = True,
targetWidth = 90,
),
]
MAX_NAME_LEN = 64
def mem_name( x ):
if x.get('name'):
return x['name']
value = x.get('value')
if value:
if isinstance(value,(str,unicode)) and len(value) > MAX_NAME_LEN:
return value[:MAX_NAME_LEN-3]+'...'
else:
return value
return ''
MEMORY_VIEW_COLUMNS = [
listviews.DictColumn(
name = _('Type'),
attribute = 'type',
targetWidth = 20,
defaultOrder = True,
),
listviews.DictColumn(
name = _('Name'),
attribute = 'name',
targetWidth = 20,
getter = mem_name,
defaultOrder = True,
),
listviews.DictColumn(
name = _('Cum'),
attribute = 'totsize',
targetWidth = 5,
defaultOrder = False,
format = '%0.1f',
percentPossible = True,
sortDefault = True,
),
listviews.DictColumn(
name = _('Local'),
attribute = 'size',
defaultOrder = False,
format = '%0.1f',
percentPossible = True,
targetWidth = 5,
),
listviews.DictColumn(
name = _('Children'),
attribute = 'rsize',
format = '%0.1f',
percentPossible = True,
defaultOrder = False,
targetWidth = 5,
),
listviews.DictColumn(
name = _('/Refs'),
attribute = 'parents',
defaultOrder = False,
targetWidth = 4,
getter = lambda x: len(x.get('parents',())),
),
listviews.DictColumn(
name = _('Refs/'),
attribute = 'children',
defaultOrder = False,
targetWidth = 4,
getter = lambda x: len(x.get('children',())),
),
]
class MainFrame(wx.Frame):
"""The root frame for the display of a single data-set"""
loader = None
percentageView = False
directoryView = False
memoryView = False
historyIndex = -1
activated_node = None
selected_node = None
TBFLAGS = (
wx.TB_HORIZONTAL
#| wx.NO_BORDER
| wx.TB_FLAT
)
def __init__(
self, parent=None, id=-1,
title=_("Run Snake Run"),
pos=wx.DefaultPosition,
size=wx.DefaultSize,
style=wx.DEFAULT_FRAME_STYLE|wx.CLIP_CHILDREN,
name= _("RunSnakeRun"),
config_parser=None,
):
"""Initialise the Frame"""
wx.Frame.__init__(self, parent, id, title, pos, size, style, name)
# TODO: toolbar for back, up, root, directory-view, percentage view
self.adapter = pstatsadapter.PStatsAdapter()
self.CreateControls(config_parser)
self.history = [] # set of (activated_node, selected_node) pairs...
icon = self.LoadRSRIcon()
if icon:
self.SetIcon( icon )
def CreateControls(self, config_parser):
"""Create our sub-controls"""
self.CreateMenuBar()
self.SetupToolBar()
self.CreateStatusBar()
self.leftSplitter = wx.SplitterWindow(
self
)
self.rightSplitter = wx.SplitterWindow(
self.leftSplitter
)
self.listControl = listviews.DataView(
self.leftSplitter,
columns = PROFILE_VIEW_COLUMNS,
)
self.squareMap = squaremap.SquareMap(
self.rightSplitter,
padding = 6,
labels = True,
adapter = self.adapter,
square_style = True,
)
self.tabs = wx.Notebook(
self.rightSplitter,
)
self.CreateSourceWindow(self.tabs)
self.calleeListControl = listviews.DataView(
self.tabs,
columns = PROFILE_VIEW_COLUMNS,
)
self.allCalleeListControl = listviews.DataView(
self.tabs,
columns = PROFILE_VIEW_COLUMNS,
)
self.allCallerListControl = listviews.DataView(
self.tabs,
columns = PROFILE_VIEW_COLUMNS,
)
self.callerListControl = listviews.DataView(
self.tabs,
columns = PROFILE_VIEW_COLUMNS,
)
self.ProfileListControls = [
self.listControl,
self.calleeListControl,
self.allCalleeListControl,
self.callerListControl,
self.allCallerListControl,
]
self.tabs.AddPage(self.calleeListControl, _('Callees'), True)
self.tabs.AddPage(self.allCalleeListControl, _('All Callees'), False)
self.tabs.AddPage(self.callerListControl, _('Callers'), False)
self.tabs.AddPage(self.allCallerListControl, _('All Callers'), False)
if editor:
self.tabs.AddPage(self.sourceCodeControl, _('Source Code'), False)
self.rightSplitter.SetSashSize(10)
# calculate size as proportional value for initial display...
self.LoadState( config_parser )
width, height = self.GetSizeTuple()
rightsplit = 2 * (height // 3)
leftsplit = width // 3
self.rightSplitter.SplitHorizontally(self.squareMap, self.tabs,
rightsplit)
self.leftSplitter.SplitVertically(self.listControl, self.rightSplitter,
leftsplit)
squaremap.EVT_SQUARE_HIGHLIGHTED(self.squareMap,
self.OnSquareHighlightedMap)
squaremap.EVT_SQUARE_SELECTED(self.listControl,
self.OnSquareSelectedList)
squaremap.EVT_SQUARE_SELECTED(self.squareMap, self.OnSquareSelectedMap)
squaremap.EVT_SQUARE_ACTIVATED(self.squareMap, self.OnNodeActivated)
for control in self.ProfileListControls:
squaremap.EVT_SQUARE_ACTIVATED(control, self.OnNodeActivated)
squaremap.EVT_SQUARE_HIGHLIGHTED(control,
self.OnSquareHighlightedList)
self.moreSquareViewItem.Check(self.squareMap.square_style)
def CreateMenuBar(self):
"""Create our menu-bar for triggering operations"""
menubar = wx.MenuBar()
menu = wx.Menu()
menu.Append(ID_OPEN, _('&Open Profile'), _('Open a cProfile file'))
menu.Append(ID_OPEN_MEMORY, _('Open &Memory'), _('Open a Meliae memory-dump file'))
menu.AppendSeparator()
menu.Append(ID_EXIT, _('&Close'), _('Close this RunSnakeRun window'))
menubar.Append(menu, _('&File'))
menu = wx.Menu()
self.packageMenuItem = menu.AppendCheckItem(
ID_PACKAGE_VIEW, _('&File View'),
_('View time spent by package/module')
)
self.percentageMenuItem = menu.AppendCheckItem(
ID_PERCENTAGE_VIEW, _('&Percentage View'),
_('View time spent as percent of overall time')
)
self.rootViewItem = menu.Append(
ID_ROOT_VIEW, _('&Root View (Home)'),
_('View the root of the tree')
)
self.backViewItem = menu.Append(
ID_BACK_VIEW, _('&Back'), _('Go back in your viewing history')
)
self.upViewItem = menu.Append(
ID_UP_VIEW, _('&Up'),
_('Go "up" to the parent of this node with the largest cummulative total')
)
self.moreSquareViewItem = menu.AppendCheckItem(
ID_MORE_SQUARE, _('&Hierarchic Squares'),
_('Toggle hierarchic squares in the square-map view')
)
# This stuff isn't really all that useful for profiling,
# it's more about how to generate graphics to describe profiling...
# self.deeperViewItem = menu.Append(
# ID_DEEPER_VIEW, _('&Deeper'), _('View deeper squaremap views')
# )
# self.shallowerViewItem = menu.Append(
# ID_SHALLOWER_VIEW, _('&Shallower'), _('View shallower squaremap views')
# )
# wx.ToolTip.Enable(True)
menubar.Append(menu, _('&View'))
self.SetMenuBar(menubar)
wx.EVT_MENU(self, ID_EXIT, lambda evt: self.Close(True))
wx.EVT_MENU(self, ID_OPEN, self.OnOpenFile)
wx.EVT_MENU(self, ID_OPEN_MEMORY, self.OnOpenMemory)
wx.EVT_MENU(self, ID_PACKAGE_VIEW, self.OnPackageView)
wx.EVT_MENU(self, ID_PERCENTAGE_VIEW, self.OnPercentageView)
wx.EVT_MENU(self, ID_UP_VIEW, self.OnUpView)
wx.EVT_MENU(self, ID_DEEPER_VIEW, self.OnDeeperView)
wx.EVT_MENU(self, ID_SHALLOWER_VIEW, self.OnShallowerView)
wx.EVT_MENU(self, ID_ROOT_VIEW, self.OnRootView)
wx.EVT_MENU(self, ID_BACK_VIEW, self.OnBackView)
wx.EVT_MENU(self, ID_MORE_SQUARE, self.OnMoreSquareToggle)
def LoadRSRIcon( self ):
try:
from runsnakerun.resources import rsricon_png
return getIcon( rsricon_png.data )
except Exception, err:
return None
sourceCodeControl = None
def CreateSourceWindow(self, tabs):
"""Create our source-view window for tabs"""
if editor and self.sourceCodeControl is None:
self.sourceCodeControl = wx.py.editwindow.EditWindow(
self.tabs, -1
)
self.sourceCodeControl.SetText(u"")
self.sourceFileShown = None
self.sourceCodeControl.setDisplayLineNumbers(True)
def SetupToolBar(self):
"""Create the toolbar for common actions"""
tb = self.CreateToolBar(self.TBFLAGS)
tsize = (24, 24)
tb.ToolBitmapSize = tsize
open_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR,
tsize)
tb.AddLabelTool(ID_OPEN, "Open", open_bmp, shortHelp="Open",
longHelp="Open a (c)Profile trace file")
tb.AddSeparator()
# self.Bind(wx.EVT_TOOL, self.OnOpenFile, id=ID_OPEN)
self.rootViewTool = tb.AddLabelTool(
ID_ROOT_VIEW, _("Root View"),
wx.ArtProvider.GetBitmap(wx.ART_GO_HOME, wx.ART_TOOLBAR, tsize),
shortHelp=_("Display the root of the current view tree (home view)")
)
self.rootViewTool = tb.AddLabelTool(
ID_BACK_VIEW, _("Back"),
wx.ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_TOOLBAR, tsize),
shortHelp=_("Back to the previously activated node in the call tree")
)
self.upViewTool = tb.AddLabelTool(
ID_UP_VIEW, _("Up"),
wx.ArtProvider.GetBitmap(wx.ART_GO_UP, wx.ART_TOOLBAR, tsize),
shortHelp=_("Go one level up the call tree (highest-percentage parent)")
)
tb.AddSeparator()
# TODO: figure out why the control is sizing the label incorrectly on Linux
self.percentageViewTool = wx.CheckBox(tb, -1, _("Percent "))
self.percentageViewTool.SetToolTip(wx.ToolTip(
_("Toggle display of percentages in list views")))
tb.AddControl(self.percentageViewTool)
wx.EVT_CHECKBOX(self.percentageViewTool,
self.percentageViewTool.GetId(), self.OnPercentageView)
self.packageViewTool = wx.CheckBox(tb, -1, _("File View "))
self.packageViewTool.SetToolTip(wx.ToolTip(
_("Switch between call-hierarchy and package/module/function hierarchy")))
tb.AddControl(self.packageViewTool)
wx.EVT_CHECKBOX(self.packageViewTool, self.packageViewTool.GetId(),
self.OnPackageView)
tb.Realize()
def OnOpenFile(self, event):
"""Request to open a new profile file"""
dialog = wx.FileDialog(self, style=wx.OPEN|wx.FD_MULTIPLE)
if dialog.ShowModal() == wx.ID_OK:
paths = dialog.GetPaths()
if self.loader:
# we've already got a displayed data-set, open new window...
frame = MainFrame()
frame.Show(True)
frame.load(*paths)
else:
self.load(*paths)
def OnOpenMemory(self, event):
"""Request to open a new profile file"""
dialog = wx.FileDialog(self, style=wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
if self.loader:
# we've already got a displayed data-set, open new window...
frame = MainFrame()
frame.Show(True)
frame.load_memory(path)
else:
self.load_memory(path)
def OnShallowerView(self, event):
if not self.squareMap.max_depth:
new_depth = self.squareMap.max_depth_seen or 0 - 5
else:
new_depth = self.squareMap.max_depth - 5
self.squareMap.max_depth = max((1, new_depth))
self.squareMap.Refresh()
def OnDeeperView(self, event):
if not self.squareMap.max_depth:
new_depth = 5
else:
new_depth = self.squareMap.max_depth + 5
self.squareMap.max_depth = max((self.squareMap.max_depth_seen or 0,
new_depth))
self.squareMap.Refresh()
def OnPackageView(self, event):
self.SetPackageView(not self.directoryView)
def SetPackageView(self, directoryView):
"""Set whether to use directory/package based view"""
self.directoryView = not self.directoryView
self.packageMenuItem.Check(self.directoryView)
self.packageViewTool.SetValue(self.directoryView)
if self.loader:
self.SetModel(self.loader)
self.RecordHistory()
def OnPercentageView(self, event):
"""Handle percentage-view event from menu/toolbar"""
self.SetPercentageView(not self.percentageView)
def SetPercentageView(self, percentageView):
"""Set whether to display percentage or absolute values"""
self.percentageView = percentageView
self.percentageMenuItem.Check(self.percentageView)
self.percentageViewTool.SetValue(self.percentageView)
total = self.loader.tree.cummulative
for control in self.ProfileListControls:
control.SetPercentage(self.percentageView, total)
self.adapter.SetPercentage(self.percentageView, total)
def OnUpView(self, event):
"""Request to move up the hierarchy to highest-weight parent"""
node = self.activated_node
if node:
selected_parent = None
if self.memoryView:
parents = self.adapter.parents(node)
if node['type'] == 'type':
module = ".".join( node['name'].split( '.' )[:-1] )
if module:
for mod in parents:
if mod['type'] == 'module' and mod['name'] == module:
selected_parent = mod
else:
if self.directoryView:
tree = pstatsloader.TREE_FILES
else:
tree = pstatsloader.TREE_CALLS
parents = [
parent for parent in
self.adapter.parents(node)
if getattr(parent, 'tree', pstatsloader.TREE_CALLS) == tree
]
if parents:
if not selected_parent:
parents.sort(lambda a, b: cmp(self.adapter.value(node, a),
self.adapter.value(node, b)))
selected_parent = parents[-1]
class event:
node = selected_parent
self.OnNodeActivated(event)
else:
self.SetStatusText(_('No parents for the currently selected node: %(node_name)s')
% dict(node_name=self.adapter.label(node)))
else:
self.SetStatusText(_('No currently selected node'))
def OnBackView(self, event):
"""Request to move backward in the history"""
self.historyIndex -= 1
try:
self.RestoreHistory(self.history[self.historyIndex])
except IndexError, err:
self.SetStatusText(_('No further history available'))
def OnRootView(self, event):
"""Reset view to the root of the tree"""
self.adapter, tree, rows = self.RootNode()
self.squareMap.SetModel(tree, self.adapter)
self.RecordHistory()
def OnNodeActivated(self, event):
"""Double-click or enter on a node in some control..."""
self.activated_node = self.selected_node = event.node
self.squareMap.SetModel(event.node, self.adapter)
self.squareMap.SetSelected( event.node )
if editor:
if self.SourceShowFile(event.node):
if hasattr(event.node,'lineno'):
self.sourceCodeControl.GotoLine(event.node.lineno)
self.RecordHistory()
def SourceShowFile(self, node):
"""Show the given file in the source-code view (attempt it anyway)"""
filename = self.adapter.filename( node )
if filename and self.sourceFileShown != filename:
try:
data = open(filename).read()
except Exception, err:
# TODO: load from zips/eggs? What about .pyc issues?
return None
else:
#self.sourceCodeControl.setText(data)
self.sourceCodeControl.ClearAll()
self.sourceCodeControl.AppendText( data )
return filename
def OnSquareHighlightedMap(self, event):
self.SetStatusText(self.adapter.label(event.node))
self.listControl.SetIndicated(event.node)
text = self.squareMap.adapter.label(event.node)
self.squareMap.SetToolTipString(text)
self.SetStatusText(text)
def OnSquareHighlightedList(self, event):
self.SetStatusText(self.adapter.label(event.node))
self.squareMap.SetHighlight(event.node, propagate=False)
def OnSquareSelectedList(self, event):
self.SetStatusText(self.adapter.label(event.node))
self.squareMap.SetSelected(event.node)
self.OnSquareSelected(event)
self.RecordHistory()
def OnSquareSelectedMap(self, event):
self.listControl.SetSelected(event.node)
self.OnSquareSelected(event)
self.RecordHistory()
def OnSquareSelected(self, event):
"""Update all views to show selection children/parents"""
self.selected_node = event.node
self.calleeListControl.integrateRecords(self.adapter.children( event.node) )
self.callerListControl.integrateRecords(self.adapter.parents( event.node) )
#self.allCalleeListControl.integrateRecords(event.node.descendants())
#self.allCallerListControl.integrateRecords(event.node.ancestors())
def OnMoreSquareToggle( self, event ):
"""Toggle the more-square view (better looking, but more likely to filter records)"""
self.squareMap.square_style = not self.squareMap.square_style
self.squareMap.Refresh()
self.moreSquareViewItem.Check(self.squareMap.square_style)
restoringHistory = False
def RecordHistory(self):
"""Add the given node to the history-set"""
if not self.restoringHistory:
record = self.activated_node
if self.historyIndex < -1:
try:
del self.history[self.historyIndex+1:]
except AttributeError, err:
pass
if (not self.history) or record != self.history[-1]:
self.history.append(record)
del self.history[:-200]
self.historyIndex = -1
def RestoreHistory(self, record):
self.restoringHistory = True
try:
activated = record
class activated_event:
node = activated
if activated:
self.OnNodeActivated(activated_event)
self.squareMap.SetSelected(activated_event.node)
self.listControl.SetSelected(activated_event.node)
finally:
self.restoringHistory = False
def load(self, *filenames):
"""Load our hotshot dataset (iteratively)"""
try:
self.SetModel(pstatsloader.PStatsLoader(*filenames))
self.SetTitle(_("Run Snake Run: %(filenames)s")
% {'filenames': ', '.join(filenames)[:120]})
except (IOError, OSError, ValueError,MemoryError), err:
self.SetStatusText(
_('Failure during load of %(filenames)s: %(err)s'
) % dict(
filenames=" ".join([repr(x) for x in filenames]),
err=err
))
def load_memory(self, filename ):
self.memoryView = True
for view in self.ProfileListControls:
view.SetColumns( MEMORY_VIEW_COLUMNS )
self.SetModel( meliaeloader.load( filename ) )
def SetModel(self, loader):
"""Set our overall model (a loader object) and populate sub-controls"""
self.loader = loader
self.adapter, tree, rows = self.RootNode()
self.listControl.integrateRecords(rows.values())
self.activated_node = tree
self.squareMap.SetModel(tree, self.adapter)
self.RecordHistory()
def RootNode(self):
"""Return our current root node and appropriate adapter for it"""
if self.memoryView:
adapter = meliaeadapter.MeliaeAdapter()
tree,rows = self.loader
else:
if self.directoryView:
adapter = pstatsadapter.DirectoryViewAdapter()
tree = self.loader.location_tree
rows = self.loader.location_rows
else:
adapter = pstatsadapter.PStatsAdapter()
tree = self.loader.tree
rows = self.loader.rows
adapter.SetPercentage(self.percentageView, self.loader.tree.cummulative)
return adapter, tree, rows
def SaveState( self, config_parser ):
"""Retrieve window state to be restored on the next run..."""
if not config_parser.has_section( 'window' ):
config_parser.add_section( 'window' )
if self.IsMaximized():
config_parser.set( 'window', 'maximized', str(True))
else:
config_parser.set( 'window', 'maximized', str(False))
size = self.GetSizeTuple()
position = self.GetPositionTuple()
config_parser.set( 'window', 'width', str(size[0]) )
config_parser.set( 'window', 'height', str(size[1]) )
config_parser.set( 'window', 'x', str(position[0]) )
config_parser.set( 'window', 'y', str(position[1]) )
return config_parser
def LoadState( self, config_parser ):
"""Set our window state from the given config_parser instance"""
if (
not config_parser.has_section( 'window' ) or (
config_parser.has_option( 'window','maximized' ) and
config_parser.getboolean( 'window', 'maximized' )
)
):
self.Maximize(True)
try:
width,height,x,y = [
config_parser.getint( 'window',key )
for key in ['width','height','x','y']
]
self.SetPosition( (x,y))
self.SetSize( (width,height))
except ConfigParser.NoSectionError, err:
# the file isn't written yet, so don't even warn...
pass
except Exception, err:
# this is just convenience, if it breaks in *any* way, ignore it...
log.error(
"Unable to load window preferences, ignoring: %s", traceback.format_exc()
)
self.config = config_parser
wx.EVT_CLOSE( self, self.OnCloseWindow )
def OnCloseWindow( self, event=None ):
try:
self.SaveState( self.config )
config = config_file()
temp = config + '~'
self.config.write( open( temp,'w') )
os.rename( temp, config )
except Exception, err:
log.error( "Unable to write window preferences, ignoring: %s", traceback.format_exc())
self.Destroy()
class RunSnakeRunApp(wx.App):
"""Basic application for holding the viewing Frame"""
def OnInit(self):
"""Initialise the application"""
wx.InitAllImageHandlers()
frame = MainFrame( config_parser = load_config())
frame.Show(True)
self.SetTopWindow(frame)
if sys.argv[1:]:
if sys.argv[1] == '-m':
if sys.argv[2:]:
wx.CallAfter( frame.load_memory, sys.argv[2] )
else:
log.warn( 'No memory file specified' )
else:
wx.CallAfter(frame.load, *sys.argv[1:])
return True
class MeliaeViewApp(wx.App):
def OnInit(self):
"""Initialise the application"""
wx.InitAllImageHandlers()
frame = MainFrame( config_parser = load_config())
frame.Show(True)
self.SetTopWindow(frame)
if sys.argv[1:]:
wx.CallAfter( frame.load_memory, sys.argv[1] )
else:
log.warn( 'No memory file specified' )
return True
def getIcon( data ):
"""Return the data from the resource as a wxIcon"""
import cStringIO
stream = cStringIO.StringIO(data)
image = wx.ImageFromStream(stream)
icon = wx.EmptyIcon()
icon.CopyFromBitmap(wx.BitmapFromImage(image))
return icon
def config_directory():
base = homedirectory.appdatadirectory()
directory = os.path.join( base, 'RunSnakeRun' )
if not os.path.exists( directory ):
os.makedirs( directory )
return directory
def config_file():
directory = config_directory()
return os.path.join( directory, 'runsnake.conf' )
def load_config( ):
config = ConfigParser.SafeConfigParser()
filename = config_file()
if os.path.exists( filename ):
config.read( filename )
return config
usage = """runsnake.py profilefile
runsnake.py -m meliae.memoryfile
profilefile -- a file generated by a HotShot profile run from Python
"""
def main():
"""Mainloop for the application"""
logging.basicConfig(level=logging.INFO)
app = RunSnakeRunApp(0)
app.MainLoop()
def meliaemain():
logging.basicConfig(level=logging.INFO)
app = MeliaeViewApp(0)
app.MainLoop()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()