home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / rpc / jsonrpc.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  5.9 KB  |  183 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import simplejson
  5. import time
  6. import wx.webview as wx
  7. import traceback
  8. from util.primitives.funcs import Delegate
  9. from logging import getLogger
  10. log = getLogger('jsonrpc')
  11.  
  12. class JSPythonBridge(object):
  13.     
  14.     def __init__(self, webview):
  15.         self.id_count = 0
  16.         self.callbacks = { }
  17.         self.webview = webview
  18.         self.webview.Bind(wx.webview.EVT_WEBVIEW_RECEIVED_TITLE, self.on_before_load)
  19.         self.specifiers = { }
  20.         self.on_call = Delegate()
  21.  
  22.     
  23.     def on_before_load(self, e):
  24.         url = e.Title
  25.         if not url.startswith('digsby:'):
  26.             return None
  27.         if url == 'digsby:clear':
  28.             return None
  29.         json_data = url.decode('utf8url')
  30.         json_data = json_data.split(':', 1)[1]
  31.         json_obj = simplejson.loads(json_data)
  32.         self.json(json_obj)
  33.         return True
  34.  
  35.     
  36.     def Call(self, call, success = None, error = None, **k):
  37.         id = self.gen_id()
  38.         self.callbacks[id] = d = dict(success = success, error = error)
  39.         self.Dcallback(call, id, **k)
  40.  
  41.     
  42.     def gen_id(self):
  43.         id = '%s_%s' % (int(time.time() * 1000), self.id_count)
  44.         self.id_count += 1
  45.         return id
  46.  
  47.     
  48.     def Dcallback(self, method, id, **k):
  49.         args = simplejson.dumps({
  50.             'params': [
  51.                 {
  52.                     'method': method,
  53.                     'args': k }],
  54.             'method': 'callbackCall',
  55.             'id': id })
  56.         script = 'Digsby.requestIn(%s);' % args
  57.         self.evaljs(script)
  58.  
  59.     
  60.     def evaljs(self, js):
  61.         return self.webview.RunScript(js)
  62.  
  63.     
  64.     def register_specifier(self, specifier, func):
  65.         self.specifiers[specifier] = func
  66.  
  67.     
  68.     def json(self, json_decoded):
  69.         d = json_decoded
  70.         if 'result' not in d:
  71.             if not self.on_call:
  72.                 specifier = d.pop('specifier')
  73.                 s = self.specifiers.get(specifier)
  74.                 if s is not None:
  75.                     
  76.                     try:
  77.                         return s(d, self)
  78.                     except AttributeError:
  79.                         traceback.print_exc()
  80.                     except:
  81.                         None<EXCEPTION MATCH>AttributeError
  82.                     
  83.  
  84.                 None<EXCEPTION MATCH>AttributeError
  85.             
  86.             return self.on_call(d)
  87.         cbs = self.callbacks.pop(d.pop('id'))
  88.         if not cbs:
  89.             return None
  90.         if d['error'] is not None:
  91.             if cbs['error'] is not None:
  92.                 cbs['error'](d['error'])
  93.             
  94.         elif d['result'] is not None:
  95.             if cbs['success'] is not None:
  96.                 cbs['success'](d['result'])
  97.             
  98.         
  99.  
  100.     
  101.     def RunScript(self, script):
  102.         wx.CallAfter(self.webview.RunScript, script)
  103.  
  104.  
  105.  
  106. def Dsuccess(id, webview, **k):
  107.     if not wx.IsMainThread():
  108.         raise AssertionError('subthread called Dsuccess')
  109.     wx.IsMainThread()
  110.     val = simplejson.dumps({
  111.         'result': [
  112.             k],
  113.         'error': None,
  114.         'id': id })
  115.     script = 'Digsby.resultIn(%s);' % val
  116.     webview.RunScript(script)
  117.  
  118.  
  119. def Derror(id, webview, error_obj = None, *a, **k):
  120.     if not wx.IsMainThread():
  121.         raise AssertionError('subthread called Derror')
  122.     wx.IsMainThread()
  123.     if error_obj is None:
  124.         error_obj = 'error'
  125.     
  126.     val = simplejson.dumps({
  127.         'result': None,
  128.         'error': error_obj,
  129.         'id': id })
  130.     script = 'Digsby.resultIn(%s);' % val
  131.     webview.RunScript(script)
  132.  
  133.  
  134. class RPCClient(object):
  135.     _rpc_handlers = { }
  136.     
  137.     def json(self, rpc, webview):
  138.         method = rpc.get('method')
  139.         args = rpc.get('params')[0]
  140.         if hasattr(args, 'items'):
  141.             kwargs = dict((lambda .0: for k, v in .0:
  142. (k.encode('utf8'), v))(args.items()))
  143.             args = ()
  144.         else:
  145.             args = tuple(rpc.get('params', ()))
  146.             kwargs = { }
  147.         
  148.         try:
  149.             getattr(self, self._rpc_handlers.get(method, '_default_rpc'), self._default_rpc)(rpc, webview, rpc.get('id'), *args, **kwargs)
  150.         except Exception:
  151.             e = None
  152.             import traceback
  153.             traceback.print_exc()
  154.             self._rpc_error(rpc, webview, e)
  155.  
  156.  
  157.     
  158.     def _rpc_error(self, rpc, webview, e):
  159.         self.Dexcept(webview, rpc.get('id', 0), 'oh noes!')
  160.  
  161.     
  162.     def _default_rpc(self, rpc, webview, *a, **k):
  163.         raise Exception('Unknown RPC call: extra args = %r, extra kwargs = %r', rpc, a, k)
  164.  
  165.     
  166.     def rpc_hook(self, rpc, webview, id, *args, **kwargs):
  167.         import hooks
  168.         hooks.notify(*args, **kwargs)
  169.  
  170.     
  171.     def Dsuccess(self, webview, id, **k):
  172.         Dsuccess(id, webview, **k)
  173.  
  174.     
  175.     def Dexcept(self, webview, id, response = None, *a, **k):
  176.         self.Derror(webview, id, *a, **k)
  177.  
  178.     
  179.     def Derror(self, webview, id, *a, **k):
  180.         Derror(id, webview, error_obj = k.pop('error', k))
  181.  
  182.  
  183.