Archive for PyObjC

14

Jul

FinkCommander: not dead.. yet.

The problem with the current FinkCommander is that I’m trying to interface a Perl program (fink) with python. The only way to do this properly is to call the perl command a bunch and hope that it doesn’t cause your processor usage to spike like no tommorrow. So, the best and most logical thing is to rewrite FinkCommander with CamelBones. This way, I can interact directly with fink’s classes and display them with a cocoa window! The other problem is, I don’t know perl all that well, but then again, I didn’t know pyobjc/objc that well either.

16

Jun

Python Exceptions

Programming is by far, a journey. Not like the shoe store, but an actual journey. When working on a new version of PyLauncher, 2.0, I came across a problem. How can I create my own custom NSRunAlertPanel when an error, or exception in Python, has occurred? Behold! There is a solution!

One of the best parts about exceptions are that you can declare what you want to Python to do for each exception that could happen. Let’s look at an example:

#!/usr/local/bin/python2.4
import ofs
import sys
from AppKit import *

I tried to import a module that wasn’t there, and got an error like this:

Traceback (most recent call last):
  File "
“, line 1, in -toplevel-
    import ofs
ImportError: No module named ofs

Whoops! Now, to make this look better or make it easier on people who are novices, you can control what to do if Python encounters this exception:

try:
	import ofs
except ImportError:
	print "Module ofs not found"

Viola! Now, here’s what I wrote to incorporate a custom NSRunAlertPanel if an exception occured:

from AppKit import *
from Foundation import *

try:
	return app(x).launch()
except:
	NSRunAlertPanel("Title of Window", "Message", "Default Button", "Other buttons, can be None if you want", "other stuff or None")

31

May

Idea: python for MacFUSE

As most of us know, MacFUSE is fairly popular, and has many different filesystems implemented to accompany it. From the website: MacFUSE implements a mechanism that makes it possible to implement a fully functional file system in a user-space program on Mac OS X (10.4 and above). It aims to be API-compliant with the FUSE (File-system in USErspace) mechanism that originated on Linux. Therefore, many existing FUSE file systems become readily usable on Mac OS X. The core of MacFUSE is in a dynamically loadable kernel extension. Read more about FUSE over at Wikipedia.

Idea!

Idea!

The thing about MacFUSE is that it does not have Python bindings, and therefore programmers cannot quikly and easily manipulate and interact with FUSE with python. However, it does have Objective-C bindings, so that means one could easily just code in python using PyObjC. Then, from the PyObjC code, one can just extend it to pure python. This, my friends and readers, what I intend to do. I would like to introduce you to Snakes In a Breaker: Python for FUSE. Snatch up the SVN, but there’s no promise that it will work, or on consistent checkins. Example checkout code:

svn co http://svn.dev.spontaneousdancing.net/snakesinabreaker/trunk SnakesInABreaker

Licensing issues: MacFUSE is under the Apache license, but I can license Snakes In a Breaker as LGPL (v. 2 only, since v3 introduces DRM stuff that may inhibit FUSE development), and so I will. You can link to the code as much as you want, but any core changes to the code must be released.

Python: serious business

Credit: thecia.com.au, Warner Bros. Pictures.

23

May

NSTask versus subprocess module

    Apple’s NSTask is a class inherited from Foundation. It allows someone to create a new process under the currently running process in order to communicate with a commandline application, for instance. You just have to set up a delegate like so, in pyobjc fashion.


task = NSTask.new()

task = task.launchedTaskWithLaunchPath_arguments_("echo", "\"Hello, world\"")

And you’ll be done.

    Like the subclass module, NSTask allows for you to provide the process with STDIN and get it’s STDOUT, and if necessary, also obtain it’s STDERR. The issue with the subprocess module is that it expects the process to do nothing when invoked; it expects you to provide a command and appropriate flags for that command, which is expected, but the module expects only one form of STDOUT, and that’s after it’s been given STDIN. This is an issue for a sample script that a fink dev gave me to query it’s database. The script in question says this initially when invoked: "Information about 5810 packages read in 4 seconds." NSTask will allow you to gather that STDOUT when you need to, and so will the subprocess module. But a subprocess object handler will halt any STDOUT after that little informative blurb, and STDIN cannot be provided, nor will any further STDOUT be provided as well.

    The solution: I suggest that you use NSTask over subprocess, and get accurate data.

    P.S.: I still haven’t gotten NSTask to work in pyobjc. Here’s what I keep getting:


Traceback (most recent call last):

  File "fink-server.py", line 32, in     task.launchedTaskWithLaunchPath_arguments_("./fink-server.pl", '/sw')

AttributeError: 'NSConcreteTask' object has no attribute 'launchedTaskWithLaunchPath_arguments_'

  The file is in the subversion repo.