/usr/share/gEcrit/data/plugins/ClassHierarchyWin.py is in gecrit 2.8.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2011 Groza Cristian
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
import yapsy.IPlugin
from data.plugins.categories import General
class HierarchyCtrl(wx.TreeCtrl):
"""
HierarchyCtrl
Manages data colection, processing and displaying of python
classes from a source of files.
Creates hierarchies of them in tree.
"""
class Class():
def __init__(self, name = "", bases = []):
self.name = name
self.bases = bases
self.children = []
self.tree_ctrl_root = None
def __init__(self,parent = None, id = wx.ID_ANY, pos = (10,10), size=(-1,-1)):
"""
__inti__
Initializes the wx.TreeCtrl object, creates a tree root
and the environment.
"""
wx.TreeCtrl.__init__(self, parent, id,pos,size,style =
wx.TR_HIDE_ROOT | wx.TR_HAS_BUTTONS |
wx.TR_HAS_VARIABLE_ROW_HEIGHT)
self.root = self.AddRoot("Top")
self.classes = []
def GenerateHierarchies(self,docs):
"""
GenerateHierarchies
Gathers data using helper functions and organizes it
on the tree.
First clears the tree with the help of the helper function
and then populates it.
"""
file_list = self.GetFileList(docs)
for fl in file_list:
self.GetClassesFromFile(fl)
# base attributes are nothing but strings now. replace them with real Class objects
for cl in self.classes:
bases = []
for c in self.classes:
if c.name in cl.bases:
bases.append(c)
cl.bases = bases
# get list of children
to_be_removed = []
for cl in self.classes:
for c in self.classes:
if cl in c.bases:
cl.children.append(c)
to_be_removed.append(c)
self.classes = [ c for c in self.classes if not c in to_be_removed ]
def WalkAndAddTree(node,root):
node.tree_ctrl_root = self.AppendItem(root, node.name)
for child in node.children:
child.tree_ctrl_root = self.AppendItem(node.tree_ctrl_root, child.name)
if child.children:
WalkAndAddTree(child, child.tree_ctrl_root)
for cls in self.classes:
cls.tree_ctrl_root = self.AppendItem(self.root, cls.name)
for child in cls.children:
WalkAndAddTree(child,cls.tree_ctrl_root)
self.classes = []
def FindChilds(self,cls):
"""
FindChilds
Finds the child classes of the suplied argument cls.
Uses a helper class to check inheritance.
Return the list of childs.
"""
childs = []
for c in self.classes:
if cls.name in c.bases:
childs.append(c)
return childs
def GetClassesFromFile(self,file_path):
"""
GetClassesFromFile
Reads and collects the class statements from the suplied
file_path argument.
Creates a list of found classes and returns it if not empty.
"""
with open(file_path,"r") as fl:
for line in fl:
if "class" in line and ":" in line:
name = line.lstrip("class").split("(")[0].rstrip(":").strip() # extract class name
bases = line.split("(")[1].split(")")[0].split(",") # extract bases
bases = [ b.strip() for b in bases] # remove whitespace
self.classes.append(HierarchyCtrl.Class(name,bases))
def GetFileList(self,docs):
"""
GetFileList
Iterates through all the editor objects, retrieves their
file paths, gathers them into a list and returns it.
If none Found, return False.
"""
file_list = []
for d in docs:
file_name = d.GetFilePath()
if file_name != "":
file_list.append(file_name)
return file_list
def Refresh(self,docs):
"""
Refresh
Rebuild the class hierarchy tree.
"""
self.DeleteChildren(self.root)
self.GenerateHierarchies(docs)
class ClassHierarchyWin(wx.Frame, General,yapsy.IPlugin.IPlugin):
"""
HierarchyFrame
Provides a display space and controls for the
ClassHierarchyCtrl object.
"""
def __init__(self):
self.name = "Class Hierarchy Tree"
def Init(self,parent = None,id = wx.ID_ANY):
"""
Init
Build the frame GUI components and initializes the wx.Frame
object.
Initializes the ClassHierarchyCtrl object.
"""
self.parent = parent
wx.Frame.__init__(self, parent,id,"Class Hierarchies",size=(300,500))
self.panel = wx.Panel(self)
panel_sizer = wx.BoxSizer(wx.VERTICAL)
self.tree_ctrl = HierarchyCtrl(self.panel)
self.button_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.refresh = wx.Button(self.panel,-1,"Refresh", pos=(200,400),
size=(-1,-1))
self.close = wx.Button(self.panel,-1, "Close", pos = (250,400),
size=(-1,-1))
self.button_sizer.Add(self.refresh,0)
self.button_sizer.Add(self.close,0)
panel_sizer.Add(self.tree_ctrl,1,wx.EXPAND)
panel_sizer.Add(self.button_sizer,0)
self.panel.SetSizer(panel_sizer)
self.panel.Fit()
self.Hide()
self.Bind(wx.EVT_CLOSE, self.HideMe)
self.refresh.Bind(wx.EVT_BUTTON, self.OnRefresh)
self.close.Bind(wx.EVT_BUTTON, self.HideMe)
#creating plugin menu entry
self.plugins_menu = wx.Menu()
show_entry = self.plugins_menu.Append(-1,"Show Tree")
self.menu_item = self.parent.AddToMenuBar("Class Hiererchy Tree",
self.plugins_menu)
self.parent.BindMenubarEvent(show_entry, self.ShowMe)
def ShowMe(self,event):
"""
ShowMe
Makes window visible and refreshes the class hierarchy tree.
"""
self.documents = self.parent.GetAllDocuments()
self.tree_ctrl.Refresh(self.documents)
self.Show()
def HideMe(self,event):
"""
HideMe
Hides the window.
"""
self.Hide()
def OnRefresh(self,event):
"""
OnRefresh
Calls the ClassHierarchyCtrl's function Refresh.
"""
self.tree_ctrl.Refresh(self.documents)
def NotifyTabChanged(self):
self.Notify()
def NotifyDocumentOpened(self):
self.Notify()
def NotifyDocumentSaved(self):
self.Notify()
def Notify(self):
try:#the tab change event is produced prematurely
self.documents = self.parent.GetAllDocuments()
except: pass
def Stop(self):
self.parent.RemoveFromMenubar(self.menu_item)
self.Destroy()
|