[Pythonmac-SIG] Spotlight and Python
David Reed
dreedmac at columbus.rr.com
Wed May 11 19:21:22 CEST 2005
On May 11, 2005, at 11:06 AM, Jonathan Wight wrote:
> It is entirely possible I screwed something up and that it doesn't
> work on anything other than my Powerbook ;-)
>
> However:
>
> As the last part of the install process the installer kicks off a
> script to reindex the Python files on the hard drive. This could be
> failing. To manually reindex runt this:
>
> mdimport -r /Library/Spotlight/Python\ Metadata\ Importer.mdimporter/
>
> You should see a response like this:
>
> 2005-05-11 10:55:48.004 mdimport[21563] Asking server to reimport
> files with UTIs: (
> "dyn.ah62d4rv4ge81a8k",
> "dyn.ah62d4rv4gq81k3p2su11upputf4gu55sfz30g6xmsb4a",
> "public.python-script"
> )
>
> On my machine with 2748 python files indexable by Spotlight
> ( mdfind 'kMDItemContentType == "public.python-script' | wc -l ) it
> seems to take less than 5 minutes with 2 mdimport tasks running at
> between 30-70% CPU.
>
> Note that spotlight doesn't index Python.framework by default - so
> you'd need to configure that somehow (perhaps via the .Spotlight-
> V100 directory).
>
> To confirm that the Python mdimporter is working run mdimport
> against a python file (with classes & functions declared) with
> debugging turned on. You should see something like this, note that
> mdimport is using the Python mdimporter and that the
> 'org_python_functions' attribute is present in the output:
>
> mdimport -d 2 myimporter.py
>
> 2005-05-11 11:03:51.318 mdimport[21650] Import '/Volumes/Home/Users/
> schwa/Desktop/Python Metadata Importer/myimporter.py' type
> 'public.python-script' using 'file://localhost/Library/Spotlight/
> Python%20Metadata%20Importer.mdimporter/'
> 2005-05-11 11:03:51.622 mdimport[21650] Done.
> 2005-05-11 11:03:51.624 mdimport[21650] Sending attributes of '/
> Volumes/Home/Users/schwa/Desktop/Python Metadata Importer/
> myimporter.py' to server. Attributes: '{
> "com_apple_metadata_modtime" = 137481386;
> kMDItemContentCreationDate = "2005-05-11 00:03:18 -0400";
> kMDItemContentModificationDate = "2005-05-11 01:16:26 -0400";
> kMDItemContentType = "public.python-script";
> kMDItemContentTypeTree = (
> "public.python-script",
> "public.shell-script",
> "public.script",
> "public.source-code",
> "public.plain-text",
> "public.text",
> "public.data",
> "public.item",
> "public.content"
> );
> kMDItemDisplayName = {"" = "myimporter.py"; };
> kMDItemKind = {"" = "Python Script"; };
> "org_python_classes" = ();
> "org_python_functions" = (find, main);
>
> Let me know if this works.
>
> Jon.
>
It doesn't appear to be working:
510 mac:~/src/CS160/ImageMessage $ mdimport -d 2 bits.py
2005-05-11 13:17:48.498 mdimport[3670] Import '/Users/dreed/src/CS160/
ImageMessage/bits.py' type 'public.python-script' using 'file://
localhost/Library/Spotlight/Python%20Metadata%20Importer.mdimporter/'
2005-05-11 13:17:48.879 mdimport[3670] -[FileProcessor
importMetadataFromFileAtPì¤í¸ íìot exception Conversion to encoding
30 failed for string "ì
2005-05-11 13:17:48.897 mdimport[3670] Import '/Users/dreed/src/CS160/
ImageMessage/bits.py' type 'public.python-script' no mdimporter
2005-05-11 13:17:48.900 mdimport[3670] Sending attributes of '/Users/
dreed/src/CS160/ImageMessage/bits.py' to server. Attributes: '{
"_kMDItemImporterCrashed" = 1;
"com_apple_metadata_modtime" = 137504114;
kMDItemContentCreationDate = 2005-05-11 07:35:14 -0400;
kMDItemContentModificationDate = 2005-05-11 07:35:14 -0400;
kMDItemContentType = "public.python-script";
kMDItemContentTypeTree = (
"public.python-script",
"public.shell-script",
"public.script",
"public.source-code",
"public.plain-text",
"public.text",
"public.data",
"public.item",
"public.content"
);
kMDItemDisplayName = {"" = "bits.py"; };
kMDItemKind = {
"" = PlainTextType;
ca = "Fitxer de Text Planer";
da = "Almindeligt tekstdokument";
de = "Reine Text-Datei";
en = "Plain Text File";
fr = "Fichier texte brut";
ja = "\U6a19\U6e96\U30c6\U30ad\U30b9\U30c8\U66f8\U985e";
ko = "\Uc77c\Ubc18 \Ud14d\Uc2a4\Ud2b8 \Ud30c\Uc77c";
ru = "\U041f\U0440\U043e\U0441\U0442\U043e\U0439 \U0442\U0435
\U043a\U0441\U0442";
"zh-Hant" = "\U7d14\U6587\U5b57\U6a94";
};
}'
The bits.py file is just:
#!/usr/bin/env python
#----------------------------------------------------------------------
# bits.py
# Dave Reed
# 05/09/2005
#----------------------------------------------------------------------
def get_bits(value, n=8):
'''get_bits(value, n):
generator to return individual bits in a value of n bits'''
pos_value = 2 ** (n-1)
# if a single character string, convert to ASCII code
try:
value = ord(value)
except:
pass
# for each bit
for i in range(n):
# get bit n
bit = value & pos_value
# shift so we can get next bit
value = value << 1
# if bit is pos_value, that bit was 1
if bit == pos_value:
yield 1
else:
yield 0
#----------------------------------------------------------------------
def set_lsb(x, b):
'''set_lsb(x, b):
returns x with lsb of x set to b (b is 0 or 1)'''
return (x & 254) | b
#----------------------------------------------------------------------
def get_lsb(x):
'''get_lsb(x):
returns lsb of x (0 or 1)'''
return x & 1
#----------------------------------------------------------------------
def value_from_bits(bits):
'''value_from_bits(bits):
takes a sequence of bit values (each item is 0 or 1) and returns
the
integer corresponding to that sequence'''
value = 0
for b in bits:
value = 2 * value + b
return value
#----------------------------------------------------------------------
Dave
More information about the Pythonmac-SIG
mailing list