Wakaba plugin (base)
[chanspy.git] / daemon / services.py
blobb33ca29ec1c2dba367a8356a69112a219fdb1ca3
1 import gobject
2 import dbus
3 import dbus.service
4 import dbus.mainloop.glib
6 import signal
7 import logging
8 import os
9 import imp
11 import config
12 from chanpool import ChanPool
13 from chan import Chan
15 INTERFACE = 'py.chans.dbus'
16 OBJ_PATH = '/pool'
17 PLUGINS_DIR = 'plugins'
19 mainloop = gobject.MainLoop()
22 def start(logfile):
23 signal.signal(signal.SIGTERM, sigTermCB)
24 logging.basicConfig(level=logging.DEBUG, filename=logfile, format='[%(asctime)s] [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
26 options = config.read_config('configs/config.cfg')
27 boards = load_boards('plugins')
29 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
30 session_bus = dbus.SessionBus()
31 name = dbus.service.BusName(INTERFACE, session_bus)
33 chans = []
34 for chan in options.keys(): # ['2ch', 'iichan']
35 myboards = []
36 for board_type in options[chan]['boards'].keys(): # {'wakaba': ['b', 's'], 'kareha': ['beta/v', 'beta/a']}
37 if boards.has_key(board_type):
38 for board in options[chan]['boards'][board_type]: # ['b', 's']
39 myboards.append( boards[board_type](session_bus, OBJ_PATH +'/'+ chan +'/'+ board, name=board, base_uri=options[chan]['base_uri']) )
41 chans.append( Chan(session_bus, OBJ_PATH +'/'+ chan, name=chan, base_uri=options[chan]['base_uri'], boards=myboards) )
43 ChanPool(session_bus, OBJ_PATH, chans)
44 mainloop.run()
47 def load_boards(path):
48 boards = {}
50 for plugin in os.listdir(path):
51 if plugin.startswith('plugin_') and plugin.endswith('.py'):
52 plugin = plugin[:-3]
53 board_type = plugin[7:]
54 file, pathname, description = imp.find_module(path + '/' + plugin)
56 #try:
57 metaboard = imp.load_module(plugin, file, pathname, description).MetaBoard
58 #except:
59 # logging.error('PLUGIN: can\'t load %s' %board_type)
60 #else:
61 boards[board_type] = metaboard
62 logging.info('PLUGIN: %s loaded' %board_type)
64 return boards
67 def sigTermCB(signum, frame):
68 try:
69 os.remove('chanspyd.pid')
70 finally:
71 mainloop.quit()