~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to addons/pate/src/plugins/js_utils/js_engine.py

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from PyQt4.QtScript import QScriptEngine, QScriptValue, QScriptValueIterator
 
2
 
 
3
import re
 
4
try:
 
5
    from collections.abc import Mapping, Iterable
 
6
except ImportError:
 
7
    from collections import Mapping, Iterable
 
8
 
 
9
def iter_js_obj(js_obj):
 
10
    """QScriptValueIterator to python iterator emitting tuples of (str, QScriptValue)"""
 
11
    it = QScriptValueIterator(js_obj)
 
12
 
 
13
    while it.hasNext():
 
14
        it.next()
 
15
        yield (it.name(), it.value())
 
16
 
 
17
 
 
18
def iter_js_arr(js_arr):
 
19
    """iterate array in order"""
 
20
    length = js_arr.property('length').toUInt32()
 
21
 
 
22
    for i in range(length):
 
23
        yield js_arr.property(i)
 
24
 
 
25
 
 
26
class PyJSEngine(QScriptEngine):
 
27
    def py2js(self, py_obj):
 
28
        """python object to QScriptValue"""
 
29
        if isinstance(py_obj, Mapping):
 
30
            ob = self.newObject()
 
31
            for name, value in py_obj.items():
 
32
                ob.setProperty(name, self.py2js(value))
 
33
            return ob
 
34
        elif isinstance(py_obj, Iterable) and not isinstance(py_obj, str):
 
35
            a = self.newArray()
 
36
            for i, item in enumerate(py_obj):
 
37
                a.setProperty(i, self.py2js(item))
 
38
            return a
 
39
        return QScriptValue(self, py_obj)
 
40
 
 
41
    def js_call(self, js_function):
 
42
        """js function to python function acting on python types, returning a python type"""
 
43
        def c(*args):
 
44
            js_val = js_function.call(self.globalObject(), self.py2js(args))
 
45
            return js_val.toVariant()
 
46
        return c
 
47
 
 
48
 
 
49
class JSModule:
 
50
    """Loads a JSModule from filepath into engine"""
 
51
    def __init__(self, engine, filepath, objname):
 
52
        self.engine = engine
 
53
 
 
54
        with open(filepath) as s:
 
55
            code = fix_js(s.read())
 
56
 
 
57
        self.engine.evaluate(code, s.name)
 
58
        if self.engine.hasUncaughtException():
 
59
            raise ValueError('file can’t be evaluated', self.engine.uncaughtException().toVariant())
 
60
 
 
61
        self.obj = self.engine.globalObject().property(objname)
 
62
 
 
63
    def __call__(self, *args):
 
64
        call = self.engine.js_call(self.obj)
 
65
        return call(*args)
 
66
 
 
67
    def __getitem__(self, key):
 
68
        value = self.obj.property(key)
 
69
        if value.isFunction():
 
70
            return self.engine.js_call(value)
 
71
        return value.toVariant()
 
72
 
 
73
 
 
74
# some ECMAScript ReservedWords which can be used as object property names.
 
75
_keywords = 'continue|break|function|var|delete|if|then|else'  # extend as appropriate
 
76
_keyword_in_okey = re.compile(r'\b({})\s*:'.format(_keywords))
 
77
_keyword_in_attr = re.compile(r'\.({})\b'.format(_keywords))
 
78
 
 
79
 
 
80
def fix_js(code):
 
81
    """Fixes JS so that Qt’s engine can digest it.
 
82
 
 
83
    The problem is that unquoted object keys like `{key: 'value'}`,
 
84
    as well as member accessors like `object.member` may be IdentifierNames,
 
85
    while Qt’s implementation expects them not to be ReservedWords.
 
86
 
 
87
        >>> fix_js('var foo = { function: function() {} }')
 
88
        '{ "function": function() {} }'
 
89
        >>> fix_js('foo.function()')
 
90
        'foo["function"]()'
 
91
 
 
92
    * http://www.ecma-international.org/ecma-262/5.1/#sec-7.6
 
93
    * http://www.ecma-international.org/ecma-262/5.1/#sec-11.1.5
 
94
    * http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.1
 
95
    """
 
96
    code = _keyword_in_okey.sub(r'"\1":', code)
 
97
    code = _keyword_in_attr.sub(r'["\1"]', code)
 
98
 
 
99
    return code
 
100
 
 
101
if __name__ == '__main__':
 
102
    import sys
 
103
    from pprint import pprint
 
104
    from PyQt4.QtGui import QApplication
 
105
 
 
106
    app = QApplication(sys.argv)
 
107
 
 
108
    se = PyJSEngine()
 
109
 
 
110
    JSLint = JSModule(se, 'fulljslint.js', 'JSLINT')
 
111
 
 
112
    with open(sys.argv[1]) as s:
 
113
        lint_ok = JSLint(s.read(), {})
 
114
 
 
115
    if lint_ok:
 
116
        print('OK')
 
117
    else:
 
118
        errors = JSLint['errors']
 
119
        pprint(errors)
 
 
b'\\ No newline at end of file'