home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- import simplejson
- import time
- import wx.webview as wx
- import traceback
- from util.primitives.funcs import Delegate
- from logging import getLogger
- log = getLogger('jsonrpc')
-
- class JSPythonBridge(object):
-
- def __init__(self, webview):
- self.id_count = 0
- self.callbacks = { }
- self.webview = webview
- self.webview.Bind(wx.webview.EVT_WEBVIEW_RECEIVED_TITLE, self.on_before_load)
- self.specifiers = { }
- self.on_call = Delegate()
-
-
- def on_before_load(self, e):
- url = e.Title
- if not url.startswith('digsby:'):
- return None
- if url == 'digsby:clear':
- return None
- json_data = url.decode('utf8url')
- json_data = json_data.split(':', 1)[1]
- json_obj = simplejson.loads(json_data)
- self.json(json_obj)
- return True
-
-
- def Call(self, call, success = None, error = None, **k):
- id = self.gen_id()
- self.callbacks[id] = d = dict(success = success, error = error)
- self.Dcallback(call, id, **k)
-
-
- def gen_id(self):
- id = '%s_%s' % (int(time.time() * 1000), self.id_count)
- self.id_count += 1
- return id
-
-
- def Dcallback(self, method, id, **k):
- args = simplejson.dumps({
- 'params': [
- {
- 'method': method,
- 'args': k }],
- 'method': 'callbackCall',
- 'id': id })
- script = 'Digsby.requestIn(%s);' % args
- self.evaljs(script)
-
-
- def evaljs(self, js):
- return self.webview.RunScript(js)
-
-
- def register_specifier(self, specifier, func):
- self.specifiers[specifier] = func
-
-
- def json(self, json_decoded):
- d = json_decoded
- if 'result' not in d:
- if not self.on_call:
- specifier = d.pop('specifier')
- s = self.specifiers.get(specifier)
- if s is not None:
-
- try:
- return s(d, self)
- except AttributeError:
- traceback.print_exc()
- except:
- None<EXCEPTION MATCH>AttributeError
-
-
- None<EXCEPTION MATCH>AttributeError
-
- return self.on_call(d)
- cbs = self.callbacks.pop(d.pop('id'))
- if not cbs:
- return None
- if d['error'] is not None:
- if cbs['error'] is not None:
- cbs['error'](d['error'])
-
- elif d['result'] is not None:
- if cbs['success'] is not None:
- cbs['success'](d['result'])
-
-
-
-
- def RunScript(self, script):
- wx.CallAfter(self.webview.RunScript, script)
-
-
-
- def Dsuccess(id, webview, **k):
- if not wx.IsMainThread():
- raise AssertionError('subthread called Dsuccess')
- wx.IsMainThread()
- val = simplejson.dumps({
- 'result': [
- k],
- 'error': None,
- 'id': id })
- script = 'Digsby.resultIn(%s);' % val
- webview.RunScript(script)
-
-
- def Derror(id, webview, error_obj = None, *a, **k):
- if not wx.IsMainThread():
- raise AssertionError('subthread called Derror')
- wx.IsMainThread()
- if error_obj is None:
- error_obj = 'error'
-
- val = simplejson.dumps({
- 'result': None,
- 'error': error_obj,
- 'id': id })
- script = 'Digsby.resultIn(%s);' % val
- webview.RunScript(script)
-
-
- class RPCClient(object):
- _rpc_handlers = { }
-
- def json(self, rpc, webview):
- method = rpc.get('method')
- args = rpc.get('params')[0]
- if hasattr(args, 'items'):
- kwargs = dict((lambda .0: for k, v in .0:
- (k.encode('utf8'), v))(args.items()))
- args = ()
- else:
- args = tuple(rpc.get('params', ()))
- kwargs = { }
-
- try:
- getattr(self, self._rpc_handlers.get(method, '_default_rpc'), self._default_rpc)(rpc, webview, rpc.get('id'), *args, **kwargs)
- except Exception:
- e = None
- import traceback
- traceback.print_exc()
- self._rpc_error(rpc, webview, e)
-
-
-
- def _rpc_error(self, rpc, webview, e):
- self.Dexcept(webview, rpc.get('id', 0), 'oh noes!')
-
-
- def _default_rpc(self, rpc, webview, *a, **k):
- raise Exception('Unknown RPC call: extra args = %r, extra kwargs = %r', rpc, a, k)
-
-
- def rpc_hook(self, rpc, webview, id, *args, **kwargs):
- import hooks
- hooks.notify(*args, **kwargs)
-
-
- def Dsuccess(self, webview, id, **k):
- Dsuccess(id, webview, **k)
-
-
- def Dexcept(self, webview, id, response = None, *a, **k):
- self.Derror(webview, id, *a, **k)
-
-
- def Derror(self, webview, id, *a, **k):
- Derror(id, webview, error_obj = k.pop('error', k))
-
-
-