Corrected doc comment on get() function
[rox-lib.git] / ROX-Lib2 / python / rox / uri_handler.py
blob43e443272ff3fdd0eb78e7e607f6184f8936a285
1 """Look up or launch the handler for a URI. URIs are in the form scheme:other
2 and the handler for a given scheme is <Choices>.rox.sourceforge.net/URI/scheme
4 To open up a web page in the user's prefered web browser:
6 import rox.uri_handler
7 try:
8 pid=rox.uri_handler.launch('http://rox.sourceforge.net/')
9 except:
10 # Try firefox instead
11 pid=os.spawnlp(os.P_NOWAIT, 'firefox', 'firefox',
12 'http://rox.sourceforge.net/')
14 os.waitpid(pid)
16 """
18 import os, urlparse
20 import rox
21 from rox import basedir
23 def get(scheme):
24 """Return the handler for URI's of the named scheme (e.g. http, file, ftp,
25 etc.) The handler for file is always rox, otherwise it obtained from
26 the configuration directory rox.sourceforge.net/URI. None is returned if
27 no handler is defined.
29 The returned string may contain %s in which case it should be replaced
30 with the URI, otherwise append the URI (after a space).
31 """
33 if scheme=='file':
34 return 'rox -U "%s"'
36 path=basedir.load_first_config('rox.sourceforge.net', 'URI', scheme)
37 if not path:
38 return
40 if rox.isappdir(path):
41 path=os.path.join(path, 'AppRun')
43 return path
45 def launch(uri):
46 """For a given URI pass it to the appropriate launcher.
47 rox.uri_handler.get() is used to look up the launcher command which is
48 executed. The process id of the command is returned (see os.wait()), or
49 None if no launcher is defined for that URI."""
50 comp=urlparse.urlparse(uri)
51 handler=get(comp[0])
52 if not handler:
53 return
54 if '%s' in handler:
55 cmd=handler % uri
56 else:
57 cmd=handler+' '+uri
58 #print cmd
60 return os.spawnlp(os.P_NOWAIT, 'sh', 'sh', '-c', cmd)
62 if __name__=='__main__':
63 print get('file')
64 print get('http')
65 print get('mailto')
66 print get('svn+ssh')
68 launch('file:///tmp')
69 launch('http://rox.sf.net/')