Fix day filter
[cds-indico.git] / indico / util / contextManager.py
blob3a269ae24c5750e4b57446f7a781967d1f07e5ba
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/>.
17 """
18 Defines the ContextManager, which provides a global access namespace for
19 storing runtime information
20 """
22 import threading
25 class DummyDict(object):
26 """
27 A context that doesn't react, much like a Null Object
28 """
30 def __init__(self):
31 pass
33 def _dummyMethod(*args, **__):
34 """
35 this method just does nothing, accepting
36 whatever arguments are passed to it
37 """
38 return None
40 def __getattr__(self, name):
41 return self._dummyMethod
43 def __setattr__(self, name, value):
44 return None
46 def __nonzero__(self):
47 return False
49 def __str__(self):
50 return "<DummyDict>"
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):
61 return self.get(elem)
63 def get(self, elem, default=DummyDict()):
64 if elem in self.__dict__:
65 return self.__dict__[elem]
66 else:
67 return default
69 def __setitem__(self, elem, value):
70 self.__dict__[elem] = value
72 def __delitem__(self, elem):
73 del self.__dict__[elem]
75 def clear(self):
76 self.__dict__.clear()
78 def setdefault(self, name, default):
79 return self.__dict__.setdefault(name, default)
82 class ContextManager(object):
83 """
84 A context manager provides a global access namespace (singleton) for storing
85 run-time information.
86 """
88 _context = Context()
90 def __init__(self):
91 pass
93 @classmethod
94 def destroy(cls):
95 """
96 destroy the context
97 """
98 cls._context.clear()
100 @classmethod
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.
106 if elem is None:
107 return cls._context
108 else:
109 return cls._context.get(elem, default)
111 @classmethod
112 def set(cls, elem, value):
113 cls._context[elem] = value
114 return cls.get(elem)
116 @classmethod
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)
124 @classmethod
125 def delete(cls, name, silent=False):
126 try:
127 del cls._context[name]
128 except KeyError:
129 if not silent:
130 raise