[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Newsgroup Home]
|
[news.eclipse.newcomer] Re: "Copy" from Outline Window
|
- From: Ross <Ross@xxxxxxxxxxxxxxx>
- Date: Fri, 06 Mar 2009 14:42:07 -0500
- Newsgroups: eclipse.newcomer
- Organization: EclipseCorner
- User-agent: Thunderbird 2.0.0.17 (Macintosh/20080914)
As mentioned: a brief Python app that will profile your Python code to
pull out just class definitions, method defs, and any call to a class.
Easy to edit to work on other stuff too probably. This runs with
wxPython and Python 2.5-ish. It's also on mac OS X. For windoze you'd
need to use the appropriate path slant "\" instead of "/" in the path
variable.
For what it's worth:
#!/usr/bin/python
# mainCodeParse.py
#
# A small utility to parse a code file and pull out methods and classes
#
#Imports - python libraries we need to do our stuff
import wx
import os
import wx.aui
from time import strftime
#File and directory names and types
whereAREwe = os.getcwd()
#filename wildcards for save/open files
txtwild = "Python files (*.py)|*.py|All files (*.*)|*.*"
wkgDirHere = whereAREwe
#point to a starting directory
wkgDirHere = whereAREwe +"/../../../EclipseProj/src/root/"
#a subdir in the source area
class BasicFrame(wx.Frame):
#RK was using pos=wx.DefaultPosition,
def __init__(self, parent, id=-1, title="", pos=(100,100),
size=(400, 400), style=wx.DEFAULT_FRAME_STYLE |
wx.SUNKEN_BORDER | wx.FULL_REPAINT_ON_RESIZE |
wx.CLIP_CHILDREN):
#wx.Frame.__init__(self, parent, id, title, pos, size, style)
#wx.Frame.__init__(self, parent, id, title, pos=(10,40),
size=(750, 550))
wx.Frame.__init__(self, parent, id, title, pos, size, style)
#panel = wx.Panel(self, -1)
# tell FrameManager to manage this frame
self._mgr = wx.aui.AuiManager()
self._mgr.SetManagedWindow(self)
self._perspectives = []
self.menuLoadCall = False # diff tween menu load, and default
self.wximagebased = False
txtContent = self.CreateTextCtrl()
self._mgr.AddPane(txtContent,
wx.aui.AuiPaneInfo().Name("text_content").
CenterPane().Hide())
self.Show(True)
openLogDlg = wx.FileDialog(self, message="Open file...",
defaultDir=wkgDirHere,defaultFile="",
wildcard=txtwild, style=wx.OPEN | wx.CHANGE_DIR)
#defines initial filter (from global 'wildcard')
openLogDlg.SetFilterIndex(0)
#show the file dialog and get result.
if openLogDlg.ShowModal() == wx.ID_OK:
fullName = openLogDlg.GetPath()
(_, fName)=os.path.split(fullName)
# separate path from name
codeFile =fName #grab the name of our input file
print "File selected was: " + codeFile
try:
codeF = file(fullName, 'r') # open file
except:
print "Can't open that file!"
else:
#if the file was processed successfully
clssList = [] # here i'll hold all my class names
AllLines = []
#first gather up all the classes
print "Gathering classes"
for tLine in codeF:
# look for classes so we can find calls to them
# we could do the same for method calls,
# but there would be lots of those.
leftPsh = tLine.lstrip()
if "class" in tLine and "(" in tLine \
and ")" in tLine and leftPsh[0] != "#":
#strip the other text and keep name of the class
tLine = tLine.replace('class', '')
startvars = tLine.find("(")
# endvars = tLine.find(")") + 1
#just out of curiosity
#remThis = tLine[startvars:endvars]
#don't need replace just truncate
tLine = tLine[0:startvars]
tLine=tLine.lstrip() #take whitespace off
#put that class into a list
clssList.append(tLine)
#reset the pointer to make another pass
codeF.seek(0)
for tLine in codeF:
currentCont = txtContent.GetValue()
leftPush = tLine.lstrip()
if " def " in tLine and leftPush[0] != "#":
# not a comment only line
txtContent.SetValue(currentCont + tLine)
if "class " in tLine and leftPush[0] != "#":
# not a comment only line
txtContent.SetValue(currentCont + tLine)
else:
#check if a class is being instantiated
for thisCls in clssList:
if thisCls in tLine:
#report the call
reportC = " >call to -> "+thisCls+ '\n'
txtContent.SetValue(currentCont + reportC)
#clean up the results - discard blank lines
freshString = "" #File Parsing Results:\n\n"
linesVers = txtContent.GetValue().splitlines()
for txtLn in linesVers:
if txtLn.strip() != '': #ie not a blank line...
while txtLn[-1] == " " or txtLn[-1] == ":":
txtLn = txtLn[:-1]
if "class" in txtLn:
freshString += "\n"
freshString += txtLn + "\n"
else:
freshString += txtLn + "\n"
#use the freshly cleaned version
txtContent.SetValue(freshString)
openLogDlg.Destroy()
print "Parser Finished " + strftime("%H:%M:%S")
def CreateTextCtrl(self):
self.rptText = ("File Parsing Results:\n\n")
TxtHolder = wx.TextCtrl(self,-1, self.rptText, wx.Point(0, 0),
wx.Size(150, 90), wx.NO_BORDER | wx.TE_MULTILINE)
return TxtHolder
class MyApp(wx.App):
def OnInit(self):
self.count = 0
print "Launched CodeFile Parser " + strftime("%H:%M:%S")
return True
app = MyApp(redirect=False)
BasicFrame(None, wx.ID_ANY, "ParseApp Panel")
app.MainLoop()