[Pythonmac-SIG] Spotlight and Python
Jonathan Wight
jwight_lists at toxicsoftware.com
Wed May 11 08:28:20 CEST 2005
OK. The first version is online at:
http://toxicsoftware.com/_private/Python%20Metadata%20Importer.pkg.zip
The installer installs the mdimporter to /Library/Spotlight/ and then
calls mdimport -r to force Spotlight to reindex all Python files.
You _may_ need Xcode installed to have this work successfully (Apple
defines the "public.python-script" UTI type - and it _may_ be defined
in Xcode - I need to check).
This version scans through .py files looking for function names and
class names. It adds them to the file's 'org_python_functions' and
'org_python_classes' metadata attributes respectively
You can view the python specific metadata using mdls
[schwa at cobweb] Python Metadata Importer$ mdls *.py
myimporter.py -------------
kMDItemContentType = "public.python-script"
... other metadata not shown ...
kMDItemKind = "Python Script"
org_python_functions = (find, main)
And find it using mdfind:
[schwa at cobweb] Spotlight$ mdfind "org_python_functions == 'main'" |
wc -l
41
You can also use the Finder to search for functions/classes: bring up
a new search window, then choose "other…" in the attribute menu, in
the attribute search window look for the keyword "Python"... It is
quite impressive doing a search for python function names from the
finder.
The importer actually uses an embedded python interpreter to parse
the python files (thanks to Bob for suggest the parser module). You
can see the python file inside the Resources folder of the importer.
The code currently leaks a few PyObjects so I'll be working on that
in the next version.
Currently I do not add anything to kMDItemTextContent or any other
metadata attribute but this kind of stuff should be easy to do (just
a one line change to the Python code).
Any feedback quite welcome.
Cheers.
Jon.
More information about the Pythonmac-SIG
mailing list