Use XMLRPC if the DBUS version is too old.
[rox-lib/lack.git] / python / rox / filer.py
blobdf63fd761bf201544f505f7d01db69b87a173d30
1 """An easy way to get ROX-Filer to do things."""
3 rox_filer_interface = "http://rox.sourceforge.net/2005/interfaces/ROX-Filer"
5 # Note: do a double-fork in case it's an old version of the filer
6 # and doesn't automatically background itself.
7 def _spawn(argv):
8 from os import fork, _exit, execvp, waitpid
9 child = fork()
10 if child == 0:
11 # We are the child
12 child = fork()
13 if child == 0:
14 # Grandchild
15 try:
16 execvp(argv[0], argv)
17 except:
18 pass
19 print "Warning: exec('%s') failed!" % argv[0]
20 _exit(1)
21 elif child == -1:
22 print "Error: fork() failed!"
23 _exit(1)
24 elif child == -1:
25 print "Error: fork() failed!"
26 waitpid(child, 0)
28 def spawn_rox(args):
29 """Run rox (either from PATH or through Zero Install) with the
30 given arguments."""
31 import os.path
32 binpath = os.environ.get('PATH', '').split(':')
33 # Try to run with '0launch'
34 for bindir in binpath:
35 path = os.path.join(bindir, '0launch')
36 if os.path.isfile(path):
37 _spawn(('0launch', rox_filer_interface) + args)
38 return
39 # Try to run 'rox'
40 for bindir in binpath:
41 path = os.path.join(bindir, 'rox')
42 if os.path.isfile(path):
43 _spawn(('rox',) + args)
44 return
45 # Try to run through the zero install filesystem
46 if os.path.exists('/uri/0install/rox.sourceforge.net'):
47 _spawn(('/bin/0run', 'rox.sourceforge.net/rox 2002-01-01') + args)
48 else:
49 print "Didn't find rox in PATH, and Zero Install not present. Trying 'rox' anyway..."
50 _spawn(('rox',) + args)
52 def open_dir(dir):
53 "Open 'dir' in a new filer window."
54 spawn_rox(('-d', dir))
56 def examine(file):
57 """'file' may have changed (maybe you just created it, for example). Update
58 any filer views of it."""
59 spawn_rox(('-x', file))
61 def show_file(file):
62 """Open a directory and draw the user's attention to this file. Useful for
63 'Up' toolbar buttons that show where a file is saved."""
64 spawn_rox(('-s', file))