cherryext/orun/js.py

234 lines
6.0 KiB
Python
Raw Normal View History

2013-04-30 09:49:34 +04:00
import types
import json
AJAX_URL = '/ajax_callback'
js_manager = None
js_ajax = None
live_methods = {}
2018-08-09 14:45:14 +03:00
class STUB:
def stub_method(self):
pass
stub_class = STUB()
2018-08-08 22:15:37 +03:00
class FuncWithParams:
2018-08-11 16:25:59 +03:00
def __init__(self, func, args = [], params = {}):
2018-08-08 12:53:19 +03:00
self.func = func
self.params = params
2018-08-11 16:25:59 +03:00
self.args = args
2018-08-08 12:53:19 +03:00
2018-08-08 22:15:37 +03:00
def list2extjs(l):
2018-08-09 16:49:32 +03:00
return '[ %s ]' % ', '.join([encode(v) for v in l])
2018-08-08 22:15:37 +03:00
def dict2extjs(d):
2018-08-09 16:49:32 +03:00
return '{ %s }' % ', '.join('%s: %s' % (k, encode(v)) for k,v in d.items())
2018-08-08 22:15:37 +03:00
2013-04-30 09:49:34 +04:00
def encode(o):
if isinstance(o, JsNode):
return str(o)
elif isinstance(o, (list, tuple)):
2018-08-08 22:15:37 +03:00
return list2extjs(o)
2018-08-09 20:41:21 +03:00
elif isinstance(o, bool):
return str(o).lower()
2018-08-08 22:15:37 +03:00
elif isinstance(o, int):
2018-08-09 20:41:21 +03:00
return str(o)
elif isinstance(o, float):
return str(o)
2018-08-08 22:15:37 +03:00
elif isinstance(o, str):
return '\'%s\'' % o
elif isinstance(o, types.FunctionType):
return str(function(js_ajax(o)))
2018-08-09 20:41:21 +03:00
elif isinstance(o, types.MethodType):
2018-08-09 14:45:14 +03:00
return str(function(js_ajax(o)))
2018-08-08 22:15:37 +03:00
elif isinstance(o, FuncWithParams):
2018-08-11 16:25:59 +03:00
return str(function(js_ajax(o.func, o.params), o.args))
2018-08-08 22:15:37 +03:00
elif isinstance(o, JsFunction):
return str(o)
elif isinstance(o, dict):
return dict2extjs(o)
2018-08-09 15:59:57 +03:00
elif isinstance(o, JsObject):
return o._id
2013-04-30 09:49:34 +04:00
else:
2018-08-08 22:15:37 +03:00
return o
2013-04-30 09:49:34 +04:00
2018-08-08 22:15:37 +03:00
class JsBlock:
def __init__(self, *args, **kwargs):
self.code = args[0]
2018-08-11 16:25:59 +03:00
self.args = args[1] if len(args) > 1 else []
2013-04-30 09:49:34 +04:00
2018-08-08 22:15:37 +03:00
def __str__(self):
return self.code
2013-04-30 09:49:34 +04:00
class JsFunction(JsBlock):
2018-08-09 08:57:18 +03:00
def __str__(self):
2018-08-11 16:25:59 +03:00
return 'function (%s) { %s }' % (','.join(self.args), self.code)
2013-04-30 09:49:34 +04:00
block = JsBlock
func = function = JsFunction
def write(code):
if js_manager:
js_manager.write(str(code))
def __lshift__(self, value):
print('test')
class JsManager(object):
def __init__(self):
self.output = []
def write(self, data):
self.output.append(data)
def __str__(self):
output = self.output[:]
2018-08-09 08:57:18 +03:00
s = '\n'.join(output)
2013-04-30 09:49:34 +04:00
return s
class JsNode(object):
def __init__(self, name='', parent=None):
if parent and parent.name:
self.name = parent.name + '.' + name
else:
self.name = name
def __getattr__(self, attr):
return JsNode(attr, self)
def __setattr__(self, attr, value):
if attr == 'name':
super(JsNode, self).__setattr__(attr, value)
else:
value = encode(value)
if self is client.var:
s = 'var %s = %s' % (attr, value)
else:
name = self.name + '.' if self.name else ''
s = '%s%s = %s' % (name, attr, value)
write(s)
def __add__(self, other):
return JsNode('%s + %s' % (encode(self), encode(other)))
def __sub__(self, other):
return JsNode('%s - %s' % (encode(self), encode(other)))
def __mul__(self, other):
return JsNode('%s * %s' % (encode(self), encode(other)))
def __truediv__(self, other):
return JsNode('%s / %s' % (encode(self), encode(other)))
def __call__(self, *args, **kwargs):
l = []
d = []
for arg in args:
l.append(encode(arg))
for k, v in kwargs.items():
d.append('%s=%s' % (k, encode(v)))
_args = []
if l:
_args.extend(l)
if d:
_args.extend(d)
s = '%s(%s)' % (self.name, ','.join(_args))
self.name = s
return self
def __str__(self):
return self.name
class JsClient(JsNode):
def __init__(self, name='', parent=None):
if parent and parent.name:
self.name = parent.name + '.' + name
else:
self.name = name
self.__dict__['var'] = JsNode('var')
def __lshift__(self, other):
write(other)
class JsObjectNode(JsNode):
def __call__(self, *args, **kwargs):
super(JsObjectNode, self).__call__(*args, **kwargs)
write(str(self))
class JsOutput(object):
def __init__(self, manager=True):
self.body = []
if manager:
js_manager = self
def __lshift__(self, other):
self.write(other)
def write(self, code):
self.body.append(code)
def __str__(self):
s = ';\n'.join(self.body)
return s
out = output = JsOutput
2013-04-30 09:49:34 +04:00
class JsObject(object):
def __init__(self, *args, **kwargs):
self._loading = True
self._id = 'js_%s' % id(self)
self._create()
self._js = kwargs
self._loading = False
def _create(self):
pass
def _update(self, config):
self._js.update(config)
def __getattr__(self, attr):
if not self.__dict__.get('_loading', True):
if attr in self._js:
return self._js.get(attr)
else:
return JsObjectNode(attr, JsNode(self._id))
def __setattr__(self, attr, value):
if '_js' in self.__dict__ and not attr in self.__dict__:
self[attr] = value
else:
super(JsObject, self).__setattr__(attr, value)
def __setitem__(self, attr, value):
if not self._loading:
write('%s.%s = %s' % (self._id, attr, json.dumps(value)))
self._js[attr] = value
2013-04-30 10:48:44 +04:00
def alert(msg):
write(client.alert(msg))
2013-04-30 09:49:34 +04:00
def load(filename, klass=JsObject):
return klass(**json.load(open(filename)))
cli = client = JsClient()
if __name__ == '__main__':
class MyManager(JsManager):
def write(self, code):
print(code)
js_manager = MyManager()
write(client.console.log('test'))
n = JsNode('console')
write(n.print(n.log({'id': 'item id'})))
client.var.x = 1
client.x.y = client.window.open('http://www.google.com')
client << client.x.y()([client.x])
client << client.Ext.create('window', {'left': 10})
client << client.x
# test block
2018-08-09 08:57:18 +03:00
print(encode({'click': 'call'}))