[Pythonmac-SIG] Spotlight and Python
Jonathan Wight
jwight_lists at toxicsoftware.com
Wed May 11 04:25:27 CEST 2005
I looked at parser but discounted it too soon. After a bit of playing
I managed to get the following code to provide lists of class names
and function names.
Thanks Bob!
Jon.
#!/usr/bin/python
import parser
import symbol
def find(inTuple, inSymbol):
theClasses = []
if inTuple[0] == inSymbol:
print 'FOUND'
theClasses += [ inTuple[2][1] ]
for theItem in inTuple[1:]:
if type(theItem) == type(()):
theClasses += find(theItem, inSymbol)
return theClasses
theSource = open('/Users/schwa/Desktop/Test.py').read()
ast = parser.suite(theSource)
tup = ast.totuple()
print find(tup, symbol.classdef)
print find(tup, symbol.funcdef)
On May 10, 2005, at 21:52, Bob Ippolito wrote:
>
> On May 10, 2005, at 8:52 PM, Jonathan Wight wrote:
>
>
>> I've made a first pass at it and have a Spotlight importer that calls
>> a built-in Python function to import a file's metadata.
>>
>> I started to look at module inspect to find out how to extract
>> information from a Python module but then realised that I'd need to
>> import the file the importer is analysing. This would mean it will be
>> executing arbitrary code inside that file. That's got to be a bad
>> thing for security reasons.
>>
>> So instead I'm just going to have to use string processing to scan
>> the file instead. Are there any modules out there for extracting
>> information from Python script files?
>>
>
> You want to use the parser module.
>
> -bob
>
>
More information about the Pythonmac-SIG
mailing list