from _exceptions import *
from narval.lib import dyn_import
from narval.NarvalRC import NarvalRC
from narval import config
from narval.bibal import tell
import sys


def load_protocolhandlers(protocols, callback):
    ''' here callback is the method getPrototype'''
    NARVAL_HOME = config.get_home()
    if NARVAL_HOME not in sys.path:
        sys.path.insert(0,NARVAL_HOME)
    white_list = NarvalRC()['protocol_handlers']
    print 'Looking for protocols:',
    for protocol in white_list.items():
        tell('[%s'%protocol[0])
        try:
            p = dyn_import(protocol[1]['package'],protocol[1]['class'])
            
            protocols[protocol[0]] = [ p, callback(p) ]
        except Exception, e:
            tell(' // PROTOCOL error: %s' % str(e))
        tell('] ')

# { 'protocol_handlers':
#       { 'jabber' : { 'package': narval.bla.bla...,
#                      'class':   JabberProtocolHandler }
#
#    _MASTER = {'disable':None, 'protocol_handlers':None, 'handler':'name'}
#    _S_LIST = ()
#    _LIST = ('actions-module', 'transforms-module', 'cookbook')
#    _KEY = ('encoding', 'package', 'class')
#    _CB = {}
#
# 
#
#
#
