Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

2009-12-24

Mac OS X Services - Changes between Tiger, Leopard and Snow Leopard

When a file or folder is selected in the Finder and a Mac OS X Service is invoked, either through the Services menu or its keyboard shortcut, the input fed to the service depends on what version of Mac OS X is running.
  • In Tiger, (10.4), no input is fed to the service.
  • In Leopard (10.5), the absolute path to the file is the input. E.g. "/Users/steve/tablet.rtf"
  • In Snow Leopard (10.6), if the service is configured properly to work on files and folders, then the input will be file URL like "file://Users/steve/tablet.rtf".
In Leopard, any service that accepted selected text would automatically work with selected files or folders in the Finder. However, Snow Leopard changed that. It is still possible for a service to accept both selected text and files plus folders, but if you create a Service using Automator, you are forced to choose one or the other. So, pick one and save your Service. For this example, I'm assuming you picked 'Files and Folders'.

Next, edit the ~/Library/Services/Foo.workflow/Contents/Info.plist and change this:


<key>NSSendFileTypes</key>
<array>
<string>public.item</string>
</array>

to this:

<key>NSSendFileTypes</key>
<array>
<string>public.item</string>
</array>
<key>NSSendTypes</key>
<array>
<string>NSStringPboardType</string>
</array>


Then, in the Python program that is your service, you may want something like this to detect that your input is a file path:


if input.starswith('file://') or input.startswith('/'):
# I think I got a file path as input


For more details, see Services Properties in the Mac Dev Center. Happy holidays!

2009-09-24

Calculating the keytag of a DNSKEY in Python

In DNSEC, sometimes you want to know the "keytag" of a DNSKEY record. Here's a Python implementation that uses dnspython:

import struct

def keytag(dnskey):
"""
Given a dns.rdtypes.ANY.DNSKEY dnskey, compute and return its keytag.

For details, see RFC 2535, section 4.1.6
"""
if dnskey.algorithm == 1:
a = ord(dnskey.key[-3]) << 8
b = ord(dnskey.key[-2])
return a + b
else:
header = struct.pack("!HBB", dnskey.flags, dnskey.protocol, dnskey.algorithm)
key = header + dnskey.key
ac = 0
for i, value in enumerate(ord(x) for x in key):
if i % 2:
ac += value
else:
ac += (value << 8)
ac += (ac >> 16) & 0xffff
return ac & 0xffff

2009-09-15

Using Django's Syncdb outside of Django

I'm working on another project that uses Django's ORM that isn't (yet) a web applicaiton. There's no settings.py file as the application has its own configuration system build on ConfigObj and optparase. But, before you can use the app, you need to create the tables that Django's ORM needs for your Models. If you app lives in a python package named 'fooapp', then put your Django models in fooapp.models and try this code:

from django.conf import settings
from django.core.management.commands import syncdb

opts, args = build_configuration()
my_app = 'fooapp'
settings.configure(
DATABASE_ENGINE=opts['db_engine'],
DATABASE_NAME=opts['db_name'],
DATABASE_USER=opts['db_user'],
DATABASE_PASSWORD=opts['db_pass'],
DATABASE_HOST=opts['db_host'],
DATABASE_PORT=opts['db_port'],
INSTALLED_APPS=(my_app,)
)
cmd = syncdb.Command()
cmd.handle_noargs()



And do let me know if that works. Thanks!