1 # directory-provider.py - Registers and runs single-JavaScript-file services
2 # (c) 2008 Sander Dijkhuis <sander.dijkhuis@gmail.com>
4 from __future__
import with_statement
6 import dbus
.mainloop
.glib
12 USAGE
= """Usage: python directory-provider.py /PATH/TO/SERVICE_DIRECTORY
13 Provides a service X for each file X.js in the specified directory."""
15 class DirectoryProvider(dbus
.service
.Object
):
16 def __init__(self
, path
):
19 object_path
= '/nl/momple/Atlantis/DirectoryProvider'
20 dbus
.service
.Object
.__init
__(self
, dbus
.SessionBus(), object_path
)
22 bus_name
= 'nl.momple.Atlantis.DirectoryProvider'
23 self
.connection
.request_name(bus_name
)
25 server
= self
.connection
.get_object('nl.momple.Atlantis.Server',
26 '/nl/momple/Atlantis/Server')
28 for file in os
.listdir(path
):
29 if file[-3:] == '.js':
30 server
.RegisterService(bus_name
, object_path
, file[:-3])
32 @dbus.service
.method(dbus_interface
='nl.momple.Atlantis.Service',
33 in_signature
='ss', out_signature
='s')
34 def GetContent(self
, service
, url_path
):
35 if url_path
== '/main.js' \
36 and os
.path
.exists('%s/%s.js' % (self
.path
, service
)):
37 print '200: /-/%s/main.js' % service
38 with
open('%s/%s.js' % (self
.path
, service
)) as f
:
39 return 'HTTP/1.1 200 OK\nContent-Type: text/javascript\n\n' \
40 + ''.join(f
.readlines())
42 print '404: %s' % url_path
43 return 'HTTP/1.1 404 Not Found\n\nNot found'
45 if __name__
== '__main__':
46 if len(sys
.argv
) != 2:
49 dbus
.mainloop
.glib
.DBusGMainLoop(set_as_default
=True)
51 ts
= DirectoryProvider(sys
.argv
[1])
53 loop
= gobject
.MainLoop()
56 except KeyboardInterrupt: