]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Common/caching.py
BaseTools: Replace BSD License with BSD+Patent License
[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 # SPDX-License-Identifier: BSD-2-Clause-Patent
7 #
8
9 ## Import Modules
10 #
11
12 # for class function
13 class cached_class_function(object):
14 def __init__(self, function):
15 self._function = function
16 def __get__(self, obj, cls):
17 def CallMeHere(*args,**kwargs):
18 Value = self._function(obj, *args,**kwargs)
19 obj.__dict__[self._function.__name__] = lambda *args,**kwargs:Value
20 return Value
21 return CallMeHere
22
23 # for class property
24 class cached_property(object):
25 def __init__(self, function):
26 self._function = function
27 def __get__(self, obj, cls):
28 Value = obj.__dict__[self._function.__name__] = self._function(obj)
29 return Value
30
31 # for non-class function
32 class cached_basic_function(object):
33 def __init__(self, function):
34 self._function = function
35 # wrapper to call _do since <class>.__dict__ doesn't support changing __call__
36 def __call__(self,*args,**kwargs):
37 return self._do(*args,**kwargs)
38 def _do(self,*args,**kwargs):
39 Value = self._function(*args,**kwargs)
40 self.__dict__['_do'] = lambda self,*args,**kwargs:Value
41 return Value