diff options
| author | Xavier Morel <xmo@openerp.com> | 2012-04-04 13:46:47 +0200 |
|---|---|---|
| committer | Xavier Morel <xmo@openerp.com> | 2012-04-04 13:46:47 +0200 |
| commit | 2a8d78c88ccdbdffdad39e41539068bb8fc32fe5 (patch) | |
| tree | b23a8f5f26941c7e602d8a5a790317b7c5bfd22b /addons/web/static/lib/py.js | |
| parent | d70220631a8053722b46dd6c4ae68ccac5486c00 (diff) | |
[UP] py.js for Object and Array converters to py types
bzr revid: xmo@openerp.com-20120404114647-t81pw90940canfwg
Diffstat (limited to 'addons/web/static/lib/py.js')
| -rw-r--r-- | addons/web/static/lib/py.js/.hg_archival.txt | 4 | ||||
| -rw-r--r-- | addons/web/static/lib/py.js/lib/py.js | 22 | ||||
| -rw-r--r-- | addons/web/static/lib/py.js/test/test.js | 11 |
3 files changed, 32 insertions, 5 deletions
diff --git a/addons/web/static/lib/py.js/.hg_archival.txt b/addons/web/static/lib/py.js/.hg_archival.txt index f5355e28a12..35e98c8501d 100644 --- a/addons/web/static/lib/py.js/.hg_archival.txt +++ b/addons/web/static/lib/py.js/.hg_archival.txt @@ -1,5 +1,5 @@ repo: 076b192d0d8ab2b92d1dbcfa3da055382f30eaea -node: 5adb2d9c89e53a6445e3799f9c4dc9110458c149 +node: 1758bfec1ec1dcff95dcc4c72269cc0e3d000afd branch: default latesttag: 0.5 -latesttagdistance: 9 +latesttagdistance: 11 diff --git a/addons/web/static/lib/py.js/lib/py.js b/addons/web/static/lib/py.js/lib/py.js index 3c0b13d0717..a8c952679af 100644 --- a/addons/web/static/lib/py.js/lib/py.js +++ b/addons/web/static/lib/py.js/lib/py.js @@ -384,6 +384,21 @@ var py = {}; return new py.def(val); } + switch(val.constructor) { + case Object: + var o = new py.object(); + for (var prop in val) { + if (val.hasOwnProperty(prop)) { + o[prop] = val[prop]; + } + } + return o; + case Array: + var a = new py.list(); + a.values = val; + return a; + } + throw new Error("Could not convert " + val + " to a pyval"); } // Builtins @@ -448,7 +463,7 @@ var py = {}; __getattribute__: function (name) { if (name in this) { var val = this[name]; - if ('__get__' in val) { + if (typeof val === 'object' && '__get__' in val) { // TODO: second argument should be class return val.__get__(this); } @@ -627,6 +642,9 @@ var py = {}; } return py.False; }, + __getitem__: function (index) { + return PY_ensurepy(this.values[index.toJSON()]); + }, toJSON: function () { var out = []; for (var i=0; i<this.values.length; ++i) { @@ -846,7 +864,7 @@ var py = {}; case '[': if (expr.second) { return py.evaluate(expr.first, context) - .__getitem__(expr.evaluate(expr.second, context)); + .__getitem__(py.evaluate(expr.second, context)); } var list_exprs = expr.first, list_values = []; for (var k=0; k<list_exprs.length; ++k) { diff --git a/addons/web/static/lib/py.js/test/test.js b/addons/web/static/lib/py.js/test/test.js index 31fccc23240..c1f92c44d81 100644 --- a/addons/web/static/lib/py.js/test/test.js +++ b/addons/web/static/lib/py.js/test/test.js @@ -376,4 +376,13 @@ describe('numerical protocols', function () { }); }); }); -});
\ No newline at end of file +}); +describe('Type converter', function () { + it('should convert bare objects to objects', function () { + expect(py.eval('foo.bar', {foo: {bar: 3}})).to.be(3); + }); + it('should convert arrays to lists', function () { + expect(py.eval('foo[3]', {foo: [9, 8, 7, 6, 5]})) + .to.be(6); + }); +}); |
