|
| 1 | +" |
| 2 | +This is a class that's useful for handling outputs of instructions like |
| 3 | +
|
| 4 | +```shell |
| 5 | +python -X importtime -m xyz |
| 6 | +``` |
| 7 | +" |
| 8 | +Class { |
| 9 | + #name : #GtPyImportTimePackage, |
| 10 | + #superclass : #Object, |
| 11 | + #instVars : [ |
| 12 | + 'localTime', |
| 13 | + 'cumulativeTime', |
| 14 | + 'nesting', |
| 15 | + 'name', |
| 16 | + 'children' |
| 17 | + ], |
| 18 | + #category : #GToolkit4Python |
| 19 | +} |
| 20 | + |
| 21 | +{ #category : #'as yet unclassified' } |
| 22 | +GtPyImportTimePackage class >> fromString: aString [ |
| 23 | + "aString looks like |
| 24 | +import time: self [us] | cumulative | imported package |
| 25 | +import time: 77 | 77 | _io |
| 26 | +import time: 19 | 19 | marshal |
| 27 | +import time: 131 | 131 | posix |
| 28 | +import time: 363 | 590 | _frozen_importlib_external |
| 29 | +import time: 450 | 450 | time |
| 30 | +..." |
| 31 | + | lineParser root stack lines | |
| 32 | + lines := aString lines allButFirst reversed select: [:each | each beginsWith: 'import time']. |
| 33 | + lineParser := ('import time:' asPParser trim |
| 34 | + , #digit asPParser plus flatten trim , '|' asPParser trim |
| 35 | + , #digit asPParser plus flatten trim , '|' asPParser , #space asPParser |
| 36 | + , #space asPParser star , #endOfInput asPParser negate plus flatten) |
| 37 | + ==> [ :t | |
| 38 | + GtPyImportTimePackage new |
| 39 | + localTime: (t at: 2) asNumber; |
| 40 | + cumulativeTime: (t at: 4) asNumber; |
| 41 | + nesting: (t at: 7) size / 2; |
| 42 | + name: (t at: 8) ]. |
| 43 | + root := GtPyImportTimePackage new |
| 44 | + localTime: 0; |
| 45 | + cumulativeTime: -1; |
| 46 | + nesting: -1; |
| 47 | + name: 'Root'. |
| 48 | + stack := Stack new. |
| 49 | + stack push: root. |
| 50 | + lines |
| 51 | + do: [ :each | |
| 52 | + | newPackage | |
| 53 | + newPackage := lineParser parse: each. |
| 54 | + [ newPackage nesting <= stack top nesting ] whileTrue: [ stack pop ]. |
| 55 | + stack top addChild: newPackage. |
| 56 | + stack push: newPackage ]. |
| 57 | + ^ root |
| 58 | +] |
| 59 | + |
| 60 | +{ #category : #'as yet unclassified' } |
| 61 | +GtPyImportTimePackage >> addChild: aPyImportTimePackage [ |
| 62 | + children add: aPyImportTimePackage |
| 63 | +] |
| 64 | + |
| 65 | +{ #category : #'as yet unclassified' } |
| 66 | +GtPyImportTimePackage >> children [ |
| 67 | + ^ children |
| 68 | +] |
| 69 | + |
| 70 | +{ #category : #'as yet unclassified' } |
| 71 | +GtPyImportTimePackage >> cumulativeTime [ |
| 72 | + cumulativeTime < 0 ifTrue: [ cumulativeTime := self children sumNumbers: #cumulativeTime ]. |
| 73 | + ^ cumulativeTime |
| 74 | +] |
| 75 | + |
| 76 | +{ #category : #accessing } |
| 77 | +GtPyImportTimePackage >> cumulativeTime: aTime [ |
| 78 | + cumulativeTime := aTime |
| 79 | +] |
| 80 | + |
| 81 | +{ #category : #'as yet unclassified' } |
| 82 | +GtPyImportTimePackage >> gtChildrenFor: aView [ |
| 83 | + <gtView> |
| 84 | + | normalizer | |
| 85 | + children ifNil: [ ^ aView empty ]. |
| 86 | + normalizer := BrColorLinearNormalizer |
| 87 | + inContext: (self withDeepCollect: #children) |
| 88 | + withCommand: #cumulativeTime |
| 89 | + lowColor: Color veryVeryLightGray |
| 90 | + highColor: (Color |
| 91 | + r: 216 |
| 92 | + g: 55 |
| 93 | + b: 62 |
| 94 | + range: 255). |
| 95 | + ^ aView columnedTree |
| 96 | + title: 'Children'; |
| 97 | + items: [ {self} ]; |
| 98 | + expandOneLevel; |
| 99 | + children: [ :each | each children ]; |
| 100 | + column: 'Value' text: [ :each | each name ]; |
| 101 | + column: 'Local time' text: [ :each | each localTime ]; |
| 102 | + column: 'Cumulative time' |
| 103 | + textDo: [ :aColumn | |
| 104 | + aColumn |
| 105 | + format: [ :each | each cumulativeTime ]; |
| 106 | + background: [ :each | normalizer value: each ] ] |
| 107 | +] |
| 108 | + |
| 109 | +{ #category : #'as yet unclassified' } |
| 110 | +GtPyImportTimePackage >> initialize [ |
| 111 | + super initialize. |
| 112 | + children := OrderedCollection new. |
| 113 | +] |
| 114 | + |
| 115 | +{ #category : #'as yet unclassified' } |
| 116 | +GtPyImportTimePackage >> localTime [ |
| 117 | + ^ localTime |
| 118 | +] |
| 119 | + |
| 120 | +{ #category : #accessing } |
| 121 | +GtPyImportTimePackage >> localTime: aTime [ |
| 122 | + localTime := aTime |
| 123 | +] |
| 124 | + |
| 125 | +{ #category : #'as yet unclassified' } |
| 126 | +GtPyImportTimePackage >> name [ |
| 127 | + ^ name |
| 128 | +] |
| 129 | + |
| 130 | +{ #category : #accessing } |
| 131 | +GtPyImportTimePackage >> name: aString [ |
| 132 | + name := aString |
| 133 | +] |
| 134 | + |
| 135 | +{ #category : #'as yet unclassified' } |
| 136 | +GtPyImportTimePackage >> nesting [ |
| 137 | + ^ nesting |
| 138 | +] |
| 139 | + |
| 140 | +{ #category : #accessing } |
| 141 | +GtPyImportTimePackage >> nesting: anInteger [ |
| 142 | + nesting := anInteger |
| 143 | +] |
0 commit comments