]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/caching.py
BaseTools: AutoGen refactor ModuleAutoGen caching
[mirror_edk2.git] / BaseTools / Source / Python / Common / caching.py
1 ## @file
2 # help with caching in BaseTools
3 #
4 # Copyright (c) 2018, Intel Corporation. All rights reserved.<BR>
5 #
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
10 #
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 #
14
15 ## Import Modules
16 #
17
18 # for class function
19 class cached_class_function(object):
20 def __init__(self, function):
21 self._function = function
22 def __get__(self, obj, cls):
23 def CallMeHere(*args,**kwargs):
24 Value = self._function(obj, *args,**kwargs)
25 obj.__dict__[self._function.__name__] = lambda *args,**kwargs:Value
26 return Value
27 return CallMeHere
28
29 # for class property
30 class cached_property(object):
31 def __init__(self, function):
32 self._function = function
33 def __get__(self, obj, cls):
34 Value = obj.__dict__[self._function.__name__] = self._function(obj)
35 return Value
36
37 # for non-class function
38 class cached_basic_function(object):
39 def __init__(self, function):
40 self._function = function
41 # wrapper to call _do since <class>.__dict__ doesn't support changing __call__
42 def __call__(self,*args,**kwargs):
43 return self._do(*args,**kwargs)
44 def _do(self,*args,**kwargs):
45 Value = self._function(*args,**kwargs)
46 self.__dict__['_do'] = lambda self,*args,**kwargs:Value
47 return Value