lib/actions.py
changeset 76 147eddb3826c
parent 74 6784c4350b41
equal deleted inserted replaced
75:4f6b7b48322f 76:147eddb3826c
     1 import imp
     1 import imp
     2 import sys
     2 import sys
     3 import os
     3 import os
     4 
     4 
     5 
     5 
     6 class ActionsLoader:
     6 class Action:
     7 	def __init__(self):
     7 	def __init__(self, mname, fname):
     8 		self._actions = {}
     8 		self.mname = mname
     9 	def keys(self):
     9 		self.fname = fname
    10 		return self._actions.keys()
    10 	def _getmodule(self):
    11 	def _getmodule(self, name):
       
    12 		modpath = os.path.join(os.path.dirname(__file__), 'actions')
    11 		modpath = os.path.join(os.path.dirname(__file__), 'actions')
    13 		fp, pathname, description = imp.find_module(name, [modpath])
    12 		fp, pathname, description = imp.find_module(self.mname, [modpath])
    14 		try:
    13 		try:
    15 			return imp.load_module(name, fp, pathname, description)
    14 			return imp.load_module(self.mname, fp, pathname, description)
    16 		finally:
    15 		finally:
    17 			if fp:
    16 			if fp:
    18 				fp.close()
    17 				fp.close()
    19 	def __getitem__(self, item):
    18 	def __call__(self, *args):
    20 		mname, fname = self._actions[item]
    19 		apply(self._getmodule().__dict__[self.fname],args)
    21 		return self._getmodule(mname).__dict__[fname]
       
    22 	def __setitem__(self, key, value):
       
    23 		self._actions[key] = value
       
    24 		
    20 		
    25 
    21 actions = {}
    26 actions = ActionsLoader()
    22 actions['index'] = Action('index', 'do_it')
    27 actions['index'] = ('index', 'do_it')
    23 actions['import'] = Action('imp_xbel', 'do_it')
    28 actions['import'] = ('imp_xbel', 'do_it')
    24 actions['bookmark'] = Action('bookmark', 'do_it')
    29 actions['bookmark'] = ('bookmark', 'do_it')
    25 actions['keywords'] = Action('editkw', 'do_it')
    30 actions['keywords'] = ('editkw', 'do_it')
    26 actions['prefs'] = Action('edit_prefs', 'do_it')
    31 actions['prefs'] = ('edit_prefs', 'do_it')
    27 actions['folder'] = Action('folder', 'do_it')
    32 actions['folder'] = ('folder', 'do_it')
       
    33 
       
    34 actions['default'] = actions['index']
    28 actions['default'] = actions['index']