plugins, parsing threads list with BeautifulSoup
[chanspy.git] / daemon.shit / services.py
blob33b6f543e2f7383c78c3f3ed07f78c8b5e2dbf8e
1 import gobject
2 import dbus
3 import dbus.service
4 import dbus.mainloop.glib
6 import logging
7 import os
8 import imp
10 from chanpool import ChanPool
11 from chan import Chan
13 INTERFACE = 'py.chans.dbus'
14 OBJ_PATH = '/pool'
16 mainloop = gobject.MainLoop()
18 #----------------------------------------------
19 def start(logfile, opt_chans):
20 """Loading plugins and run dbus mainloop."""
22 # settings in config file?
23 logging.basicConfig(level=logging.DEBUG, filename=logfile, format='[%(asctime)s] [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
25 boards = load_plugins('plugins')
27 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
28 session_bus = dbus.SessionBus()
29 tmp = dbus.service.BusName(INTERFACE, session_bus)
31 chans = []
32 for chan in opt_chans: # [{'2ch': ...}, {'iichan': ...}]
33 chan_name, settings = chan.popitem()
35 myboards = []
36 for board_dict in settings['boards']: # [{'wakaba': ['b', 's']}, {'kareha': ['beta/v', 'beta/a']}]
37 board_type, boards_list = board_dict.popitem()
39 if boards.has_key(board_type):
40 for board in boards_list: # ['b', 's']
41 myboards.append( boards[board_type](session_bus, OBJ_PATH +'/'+ chan_name +'/'+ board, name=board, base_uri=settings['base_uri']) )
43 chans.append( Chan(session_bus, OBJ_PATH +'/'+ chan_name, name=chan_name, base_uri=settings['base_uri'], boards=myboards) )
45 ChanPool(session_bus, OBJ_PATH, chans)
46 mainloop.run()
48 #----------------------------------------------
49 def load_plugins(path):
50 """Load boards engines."""
52 boards = {}
54 for plugin in os.listdir(path):
55 if plugin.startswith('plugin_') and plugin.endswith('.py'):
56 plugin = plugin[:-3]
57 board_type = plugin[7:]
58 file, pathname, description = imp.find_module(path + '/' + plugin)
60 #try:
61 metaboard = imp.load_module(plugin, file, pathname, description).MetaBoard
62 #except:
63 # logging.error('PLUGIN: can\'t load %s' %board_type)
64 #else:
65 boards[board_type] = metaboard
66 logging.info('PLUGIN: %s loaded' %board_type)
68 return boards
70 #----------------------------------------------
71 def stop():
72 """Stopping services."""
74 mainloop.quit()