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