17.8.09

A Python dispatcher pattern

  1. import sys  
  2. ##  the format of the dictionary is as follows:  
  3. ##  'command': {'sub command' :  
  4. ##    {'function':'module.class.function'}  
  5. ##  }  
  6. dispatchDictionary = {  
  7. 'add' : {'friend' :  
  8. {'function':'core.friends.friends.addFriend''help':'blah'}},  
  9. 'connection' : {'info':{'function':'core.class.function','help''prints help'},  
  10. 'help' : {'function' : 'core.shell.help.print_help'}},  
  11. 'list' : {'files' : {'function' : 'core.shell.file.f_list'}},  
  12. 'get'  : {'file' : {'function' : 'core.shell.file.f_get'}}  
  13. }  
  14.   
  15. class dispatch:  
  16.  def __init__(self):  
  17.      pass  
  18.     
  19.  def dispatch(self, input):  
  20.      ## the values 'in' _should_ be a list ['hi', 'hi']  
  21.      try:  
  22.          value = reduce(dict.get, input[:-1], dispatchDictionary)  
  23.      except (TypeError), e:  
  24.          ## empty  
  25.          print e  
  26.          return  
  27.     
  28.      key = input[-1:][0]  
  29.      try:  
  30.          ## make sure it really exists  
  31.          value[key]  
  32.      except KeyError:  
  33.          key = input[:-1][0]  
  34.   
  35.   
  36.      if key not in value:  
  37.          if 'function' not in value:  
  38.              ## no function key  
  39.              return  
  40.          if value['function'is None:  
  41.              ## empty  
  42.              return  
  43.          self.execute(value['function'], input)  
  44.         
  45.      elif key in value:  
  46.          if 'function' not in value[key]:  
  47.              ## no function key  
  48.              print value[input[-1:]]  
  49.              return  
  50.          if value[key]['function'is None:  
  51.              ## empty  
  52.              return  
  53.          self.execute(value[key]['function'], input)  
  54.                 
  55.     
  56.             
  57.  def execute(self, command, input):  
  58.      if not input[-1:][0][:1] == '[' and not input[-1:][0][-1:] == ']':  
  59.          formatted = False  
  60.      else:  
  61.          formatted = True  
  62.     
  63.      cSplit = command.split('.')  
  64.      try:  
  65.          __import__(cSplit[:1][0])  
  66.      except ImportError, e:  
  67.          print e  
  68.      try:  
  69.          module = sys.modules[cSplit[:1][0]]  
  70.      except Exception, e:  
  71.          print e  
  72.          return  
  73.     
  74.      if formatted:  
  75.          splittable = input[-1:][0][1:-1]  
  76.          funcInput = splittable.split('/')  
  77.      else:  
  78.          funcInput = input[1:]  
  79.   
  80.      ## create an instance  
  81.      Class = self.r_getattr(module, '.'.join(cSplit[1:-1]))  
  82.      c = Class()  
  83.      func = self.r_getattr(c, '.'.join(cSplit[-1:]))   
  84.      func(funcInput)  
  85.     
  86.  def r_getattr(self, object, attr):  
  87.      return reduce(getattr, attr.split('.'), object)  
  88.   
  89.  def r_setattr(self, object, attr, value):  
  90.      attrs = attr.split('.')  
  91.      return setattr(reduce(getattr, attrs[:-1], object), attrs[-1], value)  
  92.   
  93.  def add(self):  
  94.      pass  

No comments:

Post a Comment