home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 October / maximum-cd-2011-10.iso / DiscContents / digsby_setup.exe / lib / rpc / jsonrpc.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2011-06-22  |  6.4 KB  |  201 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 SetPageSource(self, source, baseurl):
  24.         self._update_origin_whitelist(baseurl)
  25.         return self.webview.SetPageSource(source, baseurl)
  26.  
  27.     
  28.     def LoadURL(self, url):
  29.         self._update_origin_whitelist(url)
  30.         return self.webview.LoadURL(url)
  31.  
  32.     
  33.     def _update_origin_whitelist(self, url):
  34.         pass
  35.  
  36.     
  37.     def on_before_load(self, e):
  38.         url = e.Title
  39.         prefix = 'digsby://digsbyjsonrpc/'
  40.         if not url.startswith(prefix):
  41.             return None
  42.         if url == prefix + 'clear':
  43.             return None
  44.         url = url[len(prefix):]
  45.         if not url.startswith('json='):
  46.             return None
  47.         url = url[len('json='):]
  48.         json_data = url.decode('utf8url')
  49.         json_obj = simplejson.loads(json_data)
  50.         self.json(json_obj)
  51.         return True
  52.  
  53.     
  54.     def Call(self, call, success = None, error = None, **k):
  55.         id = self.gen_id()
  56.         self.callbacks[id] = d = dict(success = success, error = error)
  57.         self.Dcallback(call, id, **k)
  58.  
  59.     
  60.     def gen_id(self):
  61.         id = '%s_%s' % (int(time.time() * 1000), self.id_count)
  62.         self.id_count += 1
  63.         return id
  64.  
  65.     
  66.     def Dcallback(self, method, id, **k):
  67.         args = simplejson.dumps({
  68.             'params': [
  69.                 {
  70.                     'method': method,
  71.                     'args': k }],
  72.             'method': 'callbackCall',
  73.             'id': id })
  74.         script = 'Digsby.requestIn(%s);' % args
  75.         self.evaljs(script)
  76.  
  77.     
  78.     def evaljs(self, js):
  79.         return self.webview.RunScript(js)
  80.  
  81.     
  82.     def register_specifier(self, specifier, func):
  83.         self.specifiers[specifier] = func
  84.  
  85.     
  86.     def json(self, json_decoded):
  87.         d = json_decoded
  88.         if 'result' not in d:
  89.             if not self.on_call:
  90.                 specifier = d.pop('specifier')
  91.                 s = self.specifiers.get(specifier)
  92.                 if s is not None:
  93.                     
  94.                     try:
  95.                         return s(d, self)
  96.                     except AttributeError:
  97.                         traceback.print_exc()
  98.                     except:
  99.                         None<EXCEPTION MATCH>AttributeError
  100.                     
  101.  
  102.                 None<EXCEPTION MATCH>AttributeError
  103.             
  104.             return self.on_call(d)
  105.         cbs = self.callbacks.pop(d.pop('id'))
  106.         if not cbs:
  107.             return None
  108.         if d['error'] is not None:
  109.             if cbs['error'] is not None:
  110.                 cbs['error'](d['error'])
  111.             
  112.         elif d['result'] is not None:
  113.             if cbs['success'] is not None:
  114.                 cbs['success'](d['result'])
  115.             
  116.         
  117.  
  118.     
  119.     def RunScript(self, script):
  120.         wx.CallAfter(self.webview.RunScript, script)
  121.  
  122.  
  123.  
  124. def Dsuccess(id, webview, **k):
  125.     if not wx.IsMainThread():
  126.         raise AssertionError('subthread called Dsuccess')
  127.     wx.IsMainThread()
  128.     val = simplejson.dumps({
  129.         'result': [
  130.             k],
  131.         'error': None,
  132.         'id': id })
  133.     script = 'Digsby.resultIn(%s);' % val
  134.     webview.RunScript(script)
  135.  
  136.  
  137. def Derror(id, webview, error_obj = None, *a, **k):
  138.     if not wx.IsMainThread():
  139.         raise AssertionError('subthread called Derror')
  140.     wx.IsMainThread()
  141.     if error_obj is None:
  142.         error_obj = 'error'
  143.     
  144.     val = simplejson.dumps({
  145.         'result': None,
  146.         'error': error_obj,
  147.         'id': id })
  148.     script = 'Digsby.resultIn(%s);' % val
  149.     webview.RunScript(script)
  150.  
  151.  
  152. class RPCClient(object):
  153.     _rpc_handlers = { }
  154.     
  155.     def json(self, rpc, webview):
  156.         method = rpc.get('method')
  157.         args = rpc.get('params')[0]
  158.         if hasattr(args, 'items'):
  159.             kwargs = dict((lambda .0: for k, v in .0:
  160. (k.encode('utf8'), v))(args.items()))
  161.             args = ()
  162.         else:
  163.             args = tuple(rpc.get('params', ()))
  164.             kwargs = { }
  165.         
  166.         try:
  167.             getattr(self, self._rpc_handlers.get(method, '_default_rpc'), self._default_rpc)(rpc, webview, rpc.get('id'), *args, **kwargs)
  168.         except Exception:
  169.             e = None
  170.             import traceback
  171.             traceback.print_exc()
  172.             self._rpc_error(rpc, webview, e)
  173.  
  174.  
  175.     
  176.     def _rpc_error(self, rpc, webview, e):
  177.         self.Dexcept(webview, rpc.get('id', 0), 'oh noes!')
  178.  
  179.     
  180.     def _default_rpc(self, rpc, webview, *a, **k):
  181.         raise Exception('Unknown RPC call: extra args = %r, extra kwargs = %r', rpc, a, k)
  182.  
  183.     
  184.     def rpc_hook(self, rpc, webview, id, *args, **kwargs):
  185.         import hooks
  186.         hooks.notify(*args, **kwargs)
  187.  
  188.     
  189.     def Dsuccess(self, webview, id, **k):
  190.         Dsuccess(id, webview, **k)
  191.  
  192.     
  193.     def Dexcept(self, webview, id, response = None, *a, **k):
  194.         self.Derror(webview, id, *a, **k)
  195.  
  196.     
  197.     def Derror(self, webview, id, *a, **k):
  198.         Derror(id, webview, error_obj = k.pop('error', k))
  199.  
  200.  
  201.