~openerp-dev/openobject-server/trunk-temporal-db

« back to all changes in this revision

Viewing changes to openerp/tools/misc.py

  • Committer: Olivier Dony
  • Date: 2011-06-17 17:02:15 UTC
  • Revision ID: odo@openerp.com-20110617170215-u6gb7b5q684rmo51
[ADD] tools.misc: UnquoteEvalContext, for funky domain/context eval

UnquoteEvalContext gives you an easy way to work with
domains/context that are passed as strings but need to
be altered, while keeping the unknown symbols/names
unmodified when converted back to string.
Looks weird but is sometimes needed, because much of
the context/domain only make sense on client-side
(one of the reasons they are passed as strings in the
first place). Look at the docstring for an example.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
import time
37
37
import warnings
38
38
import zipfile
 
39
from collections import defaultdict
39
40
from datetime import datetime
40
41
from email.MIMEText import MIMEText
41
42
from email.MIMEBase import MIMEBase
1398
1399
       or escaping, keeping the original string untouched. The name come from Lisp's unquote.
1399
1400
       One of the uses for this is to preserve or insert bare variable names within dicts during eval()
1400
1401
       of a dict's repr(). Use with care.
1401
 
       Some examples:
 
1402
 
 
1403
       Some examples (notice that there are never quotes surrounding
 
1404
       the ``active_id`` name:
 
1405
 
1402
1406
       >>> unquote('active_id')
1403
1407
       active_id
1404
 
       >>> repr(unquote('active_id'))
1405
 
       active_id
1406
1408
       >>> d = {'test': unquote('active_id')}
1407
1409
       >>> d
1408
1410
       {'test': active_id}
1409
 
       >>> repr(d)
1410
 
       "{'test': active_id}"
 
1411
       >>> print d
 
1412
       {'test': active_id}
1411
1413
    """
1412
1414
    def __repr__(self):
1413
1415
        return self
1414
1416
 
 
1417
class UnquoteEvalContext(defaultdict):
 
1418
    """Defaultdict-based evaluation context that returns 
 
1419
       an ``unquote`` string for any missing name used during
 
1420
       the evaluation.
 
1421
       Mostly useful for evaluating OpenERP domains/contexts that
 
1422
       may refer to names that are unknown at the time of eval,
 
1423
       so that when the context/domain is converted back to a string,
 
1424
       the original names are preserved.
 
1425
 
 
1426
       **Warning**: using an ``UnquoteEvalContext`` as context for ``eval()`` or
 
1427
       ``safe_eval()`` will shadow the builtins, which may cause other
 
1428
       failures, depending on what is evaluated.
 
1429
 
 
1430
       Example (notice that ``section_id`` is preserved in the final
 
1431
       result) :
 
1432
 
 
1433
       >>> context_str = "{'default_user_id': uid, 'default_section_id': section_id}"
 
1434
       >>> eval(context_str, UnquoteEvalContext(uid=1))
 
1435
       {'default_user_id': 1, 'default_section_id': section_id}
 
1436
 
 
1437
       """
 
1438
    def __init__(self, *args, **kwargs):
 
1439
        super(UnquoteEvalContext, self).__init__(None, *args, **kwargs)
 
1440
 
 
1441
    def __missing__(self, key):
 
1442
        return unquote(key)
 
1443
 
1415
1444
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: