1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
|
odoo.define('web.py_utils', function (require) {
"use strict";
var core = require('web.core');
var _t = core._t;
var py = window.py; // to silence linters
// recursively wraps JS objects passed into the context to attributedicts
// which jsonify back to JS objects
function wrap(value) {
if (value === null) { return py.None; }
switch (typeof value) {
case 'undefined': throw new Error("No conversion for undefined");
case 'boolean': return py.bool.fromJSON(value);
case 'number': return py.float.fromJSON(value);
case 'string': return py.str.fromJSON(value);
}
switch(value.constructor) {
case Object: return wrapping_dict.fromJSON(value);
case Array: return wrapping_list.fromJSON(value);
}
throw new Error("ValueError: unable to wrap " + value);
}
var wrapping_dict = py.type('wrapping_dict', null, {
__init__: function () {
this._store = {};
},
__getitem__: function (key) {
var k = key.toJSON();
if (!(k in this._store)) {
throw new Error("KeyError: '" + k + "'");
}
return wrap(this._store[k]);
},
__getattr__: function (key) {
return this.__getitem__(py.str.fromJSON(key));
},
__len__: function () {
return Object.keys(this._store).length;
},
__nonzero__: function () {
return py.PY_size(this) > 0 ? py.True : py.False;
},
get: function () {
var args = py.PY_parseArgs(arguments, ['k', ['d', py.None]]);
if (!(args.k.toJSON() in this._store)) { return args.d; }
return this.__getitem__(args.k);
},
fromJSON: function (d) {
var instance = py.PY_call(wrapping_dict);
instance._store = d;
return instance;
},
toJSON: function () {
return this._store;
},
});
var wrapping_list = py.type('wrapping_list', null, {
__init__: function () {
this._store = [];
},
__getitem__: function (index) {
return wrap(this._store[index.toJSON()]);
},
__len__: function () {
return this._store.length;
},
__nonzero__: function () {
return py.PY_size(this) > 0 ? py.True : py.False;
},
fromJSON: function (ar) {
var instance = py.PY_call(wrapping_list);
instance._store = ar;
return instance;
},
toJSON: function () {
return this._store;
},
});
function wrap_context(context) {
for (var k in context) {
if (!context.hasOwnProperty(k)) { continue; }
var val = context[k];
// Don't add a test case like ``val === undefined``
// this is intended to prevent letting crap pass
// on the context without even knowing it.
// If you face an issue from here, try to sanitize
// the context upstream instead
if (val === null) { continue; }
if (val.constructor === Array) {
context[k] = wrapping_list.fromJSON(val);
} else if (val.constructor === Object
&& !py.PY_isInstance(val, py.object)) {
context[k] = wrapping_dict.fromJSON(val);
}
}
return context;
}
function eval_contexts(contexts, evaluation_context) {
evaluation_context = _.extend(pycontext(), evaluation_context || {});
return _(contexts).reduce(function (result_context, ctx) {
// __eval_context evaluations can lead to some of `contexts`'s
// values being null, skip them as well as empty contexts
if (_.isEmpty(ctx)) { return result_context; }
if (_.isString(ctx)) {
// wrap raw strings in context
ctx = { __ref: 'context', __debug: ctx };
}
var evaluated = ctx;
switch(ctx.__ref) {
case 'context':
evaluation_context.context = evaluation_context;
evaluated = py.eval(ctx.__debug, wrap_context(evaluation_context));
break;
case 'compound_context':
var eval_context = eval_contexts([ctx.__eval_context]);
evaluated = eval_contexts(
ctx.__contexts, _.extend({}, evaluation_context, eval_context));
break;
}
// add newly evaluated context to evaluation context for following
// siblings
_.extend(evaluation_context, evaluated);
return _.extend(result_context, evaluated);
}, {});
}
function eval_domains(domains, evaluation_context) {
evaluation_context = _.extend(pycontext(), evaluation_context || {});
var result_domain = [];
// Normalize only if the first domain is the array ["|"] or ["!"]
var need_normalization = (
domains &&
domains.length > 0 &&
domains[0].length === 1 &&
(domains[0][0] === "|" || domains[0][0] === "!")
);
_(domains).each(function (domain) {
if (_.isString(domain)) {
// wrap raw strings in domain
domain = { __ref: 'domain', __debug: domain };
}
var domain_array_to_combine;
switch(domain.__ref) {
case 'domain':
evaluation_context.context = evaluation_context;
domain_array_to_combine = py.eval(domain.__debug, wrap_context(evaluation_context));
break;
default:
domain_array_to_combine = domain;
}
if (need_normalization) {
domain_array_to_combine = get_normalized_domain(domain_array_to_combine);
}
result_domain.push.apply(result_domain, domain_array_to_combine);
});
return result_domain;
}
/**
* Returns a normalized copy of the given domain array. Normalization is
* is making the implicit "&" at the start of the domain explicit, e.g.
* [A, B, C] would become ["&", "&", A, B, C].
*
* @param {Array} domain_array
* @returns {Array} normalized copy of the given array
*/
function get_normalized_domain(domain_array) {
var expected = 1; // Holds the number of expected domain expressions
_.each(domain_array, function (item) {
if (item === "&" || item === "|") {
expected++;
} else if (item !== "!") {
expected--;
}
});
var new_explicit_ands = _.times(-expected, _.constant("&"));
return new_explicit_ands.concat(domain_array);
}
function eval_groupbys(contexts, evaluation_context) {
evaluation_context = _.extend(pycontext(), evaluation_context || {});
var result_group = [];
_(contexts).each(function (ctx) {
if (_.isString(ctx)) {
// wrap raw strings in context
ctx = { __ref: 'context', __debug: ctx };
}
var group;
var evaluated = ctx;
switch(ctx.__ref) {
case 'context':
evaluation_context.context = evaluation_context;
evaluated = py.eval(ctx.__debug, wrap_context(evaluation_context));
break;
case 'compound_context':
var eval_context = eval_contexts([ctx.__eval_context]);
evaluated = eval_contexts(
ctx.__contexts, _.extend({}, evaluation_context, eval_context));
break;
}
group = evaluated.group_by;
if (!group) { return; }
if (typeof group === 'string') {
result_group.push(group);
} else if (group instanceof Array) {
result_group.push.apply(result_group, group);
} else {
throw new Error('Got invalid groupby {{'
+ JSON.stringify(group) + '}}');
}
_.extend(evaluation_context, evaluated);
});
return result_group;
}
/**
* Returns the current local date, which means the date on the client (which can be different
* compared to the date of the server).
*
* @return {datetime.date}
*/
function context_today() {
var d = new Date();
return py.PY_call(
py.extras.datetime.date, [d.getFullYear(), d.getMonth() + 1, d.getDate()]);
}
/**
* Returns a timedelta object which represents the timezone offset between the
* local timezone and the UTC time.
*
* This is very useful to generate datetime strings which are 'timezone'
* dependant. For example, we can now write this to generate the correct
* datetime string representing "this morning in the user timezone":
*
* "datetime.datetime.now().replace(hour=0,minute=0,second=0) + tz_offset()).strftime('%Y-%m-%d %H:%M:%S')"
* @returns {datetime.timedelta}
*/
function tz_offset() {
var offset= new Date().getTimezoneOffset();
var kwargs = {minutes: py.float.fromJSON(offset)};
return py.PY_call(py.extras.datetime.timedelta,[],kwargs);
}
function pycontext() {
const d = new Date();
const today = `${
String(d.getFullYear()).padStart(4, "0")}-${
String(d.getMonth() + 1).padStart(2, "0")}-${
String(d.getDate()).padStart(2, "0")}`;
const now = `${
String(d.getUTCFullYear()).padStart(4, "0")}-${
String(d.getUTCMonth() + 1).padStart(2, "0")}-${
String(d.getUTCDate()).padStart(2, "0")} ${
String(d.getUTCHours()).padStart(2, "0")}:${
String(d.getUTCMinutes()).padStart(2, "0")}:${
String(d.getUTCSeconds()).padStart(2, "0")}`;
const { datetime, relativedelta, time } = py.extras;
return {
current_date: today,
datetime,
time,
now,
today,
relativedelta,
context_today,
tz_offset,
};
}
/**
* @param {String} type "domains", "contexts" or "groupbys"
* @param {Array} object domains or contexts to evaluate
* @param {Object} [context] evaluation context
*/
function pyeval(type, object, context) {
context = _.extend(pycontext(), context || {});
//noinspection FallthroughInSwitchStatementJS
switch(type) {
case 'context':
case 'contexts':
if (type === 'context') {
object = [object];
}
return eval_contexts(object, context);
case 'domain':
case 'domains':
if (type === 'domain')
object = [object];
return eval_domains(object, context);
case 'groupbys':
return eval_groupbys(object, context);
}
throw new Error("Unknow evaluation type " + type);
}
function eval_arg(arg) {
if (typeof arg !== 'object' || !arg.__ref) { return arg; }
switch(arg.__ref) {
case 'domain':
return pyeval('domains', [arg]);
case 'context': case 'compound_context':
return pyeval('contexts', [arg]);
default:
throw new Error(_t("Unknown nonliteral type ") + ' ' + arg.__ref);
}
}
/**
* If args or kwargs are unevaluated contexts or domains (compound or not),
* evaluated them in-place.
*
* Potentially mutates both parameters.
*
* @param args
* @param kwargs
*/
function ensure_evaluated(args, kwargs) {
for (var i=0; i<args.length; ++i) {
args[i] = eval_arg(args[i]);
}
for (var k in kwargs) {
if (!kwargs.hasOwnProperty(k)) { continue; }
kwargs[k] = eval_arg(kwargs[k]);
}
}
function eval_domains_and_contexts(source) {
// see Session.eval_context in Python
return {
context: pyeval('contexts', source.contexts || [], source.eval_context),
domain: pyeval('domains', source.domains, source.eval_context),
group_by: pyeval('groupbys', source.group_by_seq || [], source.eval_context),
};
}
function py_eval(expr, context) {
return py.eval(expr, _.extend({}, context || {}, {"true": true, "false": false, "null": null}));
}
/**
* Assemble domains into a single domains using an 'OR' or an 'AND' operator.
*
* .. note:
*
* - this function does not evaluate anything inside the domain. This
* is actually quite critical because this allows the manipulation of
* unevaluated (dynamic) domains.
* - this function gives a normalized domain as result,
* - applied on a list of length 1, it returns the domain normalized.
*
* @param {string[]} domains list of string representing domains
* @param {"AND" | "OR"} operator used to combine domains (default "AND")
* @returns {string} normalized domain
*/
function assembleDomains(domains, operator) {
var ASTs = domains.map(_getPyJSAST);
if (operator === "OR") {
operator = py.tokenize("'|'")[0];
} else {
operator = py.tokenize("'&'")[0];
}
var result = _getPyJSAST("[]");
var normalizedDomains = ASTs
.filter(function (AST) {
return AST.first.length > 0;
})
.map(_normalizeDomainAST);
if (normalizedDomains.length > 0) {
result.first = normalizedDomains.reduce(function (acc, ast) {
return acc.concat(ast.first);
},
_.times(normalizedDomains.length - 1, _.constant(operator))
);
}
return _formatAST(result);
}
/**
* Normalize a domain via its string representation.
*
* Note: this function does not evaluate anything inside the domain. This is
* actually quite critical because this allows the manipulation of unevaluated
* (dynamic) domains.
*
* @param {string} domain string representing a domain
* @returns {string} normalized domain
*/
function normalizeDomain (domain) {
return _formatAST(_normalizeDomainAST(_getPyJSAST(domain)));
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
// Binding power for prefix operator is not accessible in the AST generated by
// py.js, so we have to hardcode some values here
var BINDING_POWERS = {
or: 30,
and: 40,
not: 50,
};
/**
* @private
* Convert a python AST (generated by py.js) to a string form, which should
* represent the same AST.
*
* @param {Object} ast a valid AST obtained by py.js, which represent a python
* expression
* @param {integer} [lbp=0] a binding power. This is necessary to be able to
* format sub expressions: the + sub expression in "3 * (a + 2)" should be
* formatted with parenthesis, because its binding power is lower than the
* binding power of *.
* @returns {string}
*/
function _formatAST(ast, lbp) {
lbp = lbp || 0;
switch (ast.id) {
// basic values
case "(number)":
return String(ast.value);
case "(string)":
return JSON.stringify(ast.value);
case "(constant)":
return ast.value;
case "(name)":
return ast.value;
case "[":
if (ast.second) {
// read a value in a dictionary: d['a']
return _formatAST(ast.first) + '[' + _formatAST(ast.second) + ']';
} else {
// list: [1, 2]
var values = ast.first.map(_formatAST);
return '[' + values.join(', ') + ']';
}
case "{":
var keyValues = ast.first.map(function (kv) {
return _formatAST(kv[0]) + ': ' + _formatAST(kv[1]);
});
return '{' + keyValues.join(', ') + '}';
// relations
case "=":
return _formatAST(ast.first) + ' ' + ast.id + ' ' + _formatAST(ast.second);
// operators
case "-":
case "+":
case "~":
case "*":
case "**":
case "%":
case "//":
case "and":
case "or":
if (ast.second) {
// infix
var r = _formatAST(ast.first, ast.lbp) + ' ' + ast.id + ' ' + _formatAST(ast.second, ast.lbp);
if (ast.lbp < lbp) {
r = '(' + r + ')';
}
return r;
}
// prefix
// real lbp is not accessible, it is inside a closure
var actualBP = BINDING_POWERS[ast.id] || 130;
return ast.id + _formatAST(ast.first, actualBP);
case "if":
var t = _formatAST(ast.ifTrue)
+ ' if ' + _formatAST(ast.condition)
+ ' else ' + _formatAST(ast.ifFalse);
return ast.lbp < lbp ? '(' + t + ')' : t;
case ".":
return _formatAST(ast.first, ast.lbp) + '.' + _formatAST(ast.second);
case "not":
return "not " + _formatAST(ast.first);
case "(comparator)":
var operator = ast.operators[0];
return _formatAST(ast.expressions[0]) + ' ' + operator + ' ' + _formatAST(ast.expressions[1]);
// function call
case "(":
if (ast.second) {
// this is a function call: f(a, b)
return _formatAST(ast.first) + '(' + ast.second.map(_formatAST).join(', ') + ')';
} else {
// this is a tuple
return '(' + ast.first.map(_formatAST).join(', ') + ')';
}
}
throw new Error("Unimplemented python construct");
}
/**
* @private
* Get the PyJs AST representing a domain starting from is string representation
*
* @param {string} domain string representing a domain
* @returns {PyJS AST} PyJS AST representation of domain
*/
function _getPyJSAST(domain) {
return py.parse(py.tokenize(domain));
}
/**
* @private
*
* Normalize a domain, at the level of the AST.
*
* Note: this function does not evaluate anything inside the domain. This is
* actually quite critical because this allows the manipulation of unevaluated
* (dynamic) domains.
*
* @param {PyJS AST} domain valid AST representing a domain
* @returns {PyJS AST} normalized domain AST
*/
function _normalizeDomainAST(domain) {
var expected = 1;
for (var i = 0; i < domain.first.length; i++) {
var value = domain.first[i].value;
if (value === '&' || value === '|') {
expected++;
} else if (value !== '!') {
expected--;
}
}
var andOperator = py.tokenize("'&'")[0];
if (expected < 0) {
domain.first.unshift.apply(domain.first, _.times(Math.abs(expected), _.constant(andOperator)));
}
return domain;
}
return {
context: pycontext,
ensure_evaluated: ensure_evaluated,
eval: pyeval,
eval_domains_and_contexts: eval_domains_and_contexts,
py_eval: py_eval,
normalizeDomain: normalizeDomain,
assembleDomains: assembleDomains,
_getPyJSAST: _getPyJSAST,
_formatAST: _formatAST,
_normalizeDomainAST: _normalizeDomainAST,
};
});
|