1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
18 Defines the ContextManager, which provides a global access namespace for
19 storing runtime information
25 class DummyDict(object):
27 A context that doesn't react, much like a Null Object
33 def _dummyMethod(*args
, **__
):
35 this method just does nothing, accepting
36 whatever arguments are passed to it
40 def __getattr__(self
, name
):
41 return self
._dummyMethod
43 def __setattr__(self
, name
, value
):
46 def __nonzero__(self
):
53 class Context(threading
.local
):
54 def __init__(self
, **kwargs
):
55 self
.__dict
__.update(kwargs
)
57 def __contains__(self
, elem
):
58 return elem
in self
.__dict
__
60 def __getitem__(self
, elem
):
63 def get(self
, elem
, default
=DummyDict()):
64 if elem
in self
.__dict
__:
65 return self
.__dict
__[elem
]
69 def __setitem__(self
, elem
, value
):
70 self
.__dict
__[elem
] = value
72 def __delitem__(self
, elem
):
73 del self
.__dict
__[elem
]
78 def setdefault(self
, name
, default
):
79 return self
.__dict
__.setdefault(name
, default
)
82 class ContextManager(object):
84 A context manager provides a global access namespace (singleton) for storing
101 def get(cls
, elem
=None, default
=DummyDict()):
103 If no set has been done over the variable before,
104 a dummy context will be returned.
109 return cls
._context
.get(elem
, default
)
112 def set(cls
, elem
, value
):
113 cls
._context
[elem
] = value
117 def setdefault(cls
, name
, default
):
119 If no set has been done over the variable before,
120 a default value is *set* and *returned*
122 return cls
._context
.setdefault(name
, default
)
125 def delete(cls
, name
, silent
=False):
127 del cls
._context
[name
]