2018-07-04 15:39:44 +01:00
|
|
|
# Copyright 2018 ARM Limited
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
2017-04-12 11:39:46 +01:00
|
|
|
# "environment" management:
|
|
|
|
__environments = {}
|
|
|
|
__active_environment = None
|
|
|
|
|
|
|
|
|
|
|
|
def activate_environment(name):
|
|
|
|
"""
|
|
|
|
Sets the current tracking environment to ``name``. If an
|
|
|
|
environment with that name does not already exist, it will be
|
|
|
|
created.
|
|
|
|
"""
|
2018-07-04 17:44:55 +01:00
|
|
|
# pylint: disable=W0603
|
2017-04-12 11:39:46 +01:00
|
|
|
global __active_environment
|
|
|
|
|
2018-05-30 13:58:49 +01:00
|
|
|
if name not in list(__environments.keys()):
|
2017-04-12 11:39:46 +01:00
|
|
|
init_environment(name)
|
|
|
|
__active_environment = name
|
|
|
|
|
2018-07-02 12:24:10 +01:00
|
|
|
|
2017-04-12 11:39:46 +01:00
|
|
|
def init_environment(name):
|
|
|
|
"""
|
|
|
|
Create a new environment called ``name``, but do not set it as the
|
|
|
|
current environment.
|
|
|
|
|
|
|
|
:raises: ``ValueError`` if an environment with name ``name``
|
|
|
|
already exists.
|
|
|
|
"""
|
2018-05-30 13:58:49 +01:00
|
|
|
if name in list(__environments.keys()):
|
2017-04-12 11:39:46 +01:00
|
|
|
msg = "Environment {} already exists".format(name)
|
|
|
|
raise ValueError(msg)
|
|
|
|
__environments[name] = []
|
|
|
|
|
2018-07-02 12:24:10 +01:00
|
|
|
|
2017-04-12 11:39:46 +01:00
|
|
|
def reset_environment(name=None):
|
|
|
|
"""
|
|
|
|
Reset method call tracking for environment ``name``. If ``name`` is
|
|
|
|
not specified or is ``None``, reset the current active environment.
|
|
|
|
|
|
|
|
:raises: ``ValueError`` if an environment with name ``name``
|
|
|
|
does not exist.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if name is not None:
|
2018-05-30 13:58:49 +01:00
|
|
|
if name not in list(__environments.keys()):
|
2017-04-12 11:39:46 +01:00
|
|
|
msg = "Environment {} does not exist".format(name)
|
|
|
|
raise ValueError(msg)
|
|
|
|
__environments[name] = []
|
|
|
|
else:
|
|
|
|
if __active_environment is None:
|
|
|
|
activate_environment('default')
|
|
|
|
__environments[__active_environment] = []
|
|
|
|
|
2018-07-02 12:24:10 +01:00
|
|
|
|
2017-04-12 11:39:46 +01:00
|
|
|
# The decorators:
|
|
|
|
def once_per_instance(method):
|
|
|
|
"""
|
|
|
|
The specified method will be invoked only once for every bound
|
|
|
|
instance within the environment.
|
|
|
|
"""
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if __active_environment is None:
|
|
|
|
activate_environment('default')
|
2018-05-22 16:17:10 +01:00
|
|
|
func_id = repr(method.__hash__()) + repr(args[0].__hash__())
|
2017-04-12 11:39:46 +01:00
|
|
|
if func_id in __environments[__active_environment]:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
__environments[__active_environment].append(func_id)
|
|
|
|
return method(*args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
2018-07-02 12:24:10 +01:00
|
|
|
|
2017-04-12 11:39:46 +01:00
|
|
|
def once_per_class(method):
|
|
|
|
"""
|
|
|
|
The specified method will be invoked only once for all instances
|
|
|
|
of a class within the environment.
|
|
|
|
"""
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if __active_environment is None:
|
|
|
|
activate_environment('default')
|
|
|
|
|
2018-05-30 13:58:49 +01:00
|
|
|
func_id = repr(method.__name__) + repr(args[0].__class__)
|
2017-04-12 11:39:46 +01:00
|
|
|
|
|
|
|
if func_id in __environments[__active_environment]:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
__environments[__active_environment].append(func_id)
|
|
|
|
return method(*args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
2018-07-02 12:24:10 +01:00
|
|
|
|
2020-02-07 16:30:04 +00:00
|
|
|
def once_per_attribute_value(attr_name):
|
|
|
|
"""
|
|
|
|
The specified method will be invoked once for all instances that share the
|
|
|
|
same value for the specified attribute (sameness is established by comparing
|
|
|
|
repr() of the values).
|
|
|
|
"""
|
|
|
|
def wrapped_once_per_attribute_value(method):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if __active_environment is None:
|
|
|
|
activate_environment('default')
|
|
|
|
|
|
|
|
attr_value = getattr(args[0], attr_name)
|
|
|
|
func_id = repr(method.__name__) + repr(args[0].__class__) + repr(attr_value)
|
|
|
|
|
|
|
|
if func_id in __environments[__active_environment]:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
__environments[__active_environment].append(func_id)
|
|
|
|
return method(*args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
return wrapped_once_per_attribute_value
|
|
|
|
|
|
|
|
|
2017-04-12 11:39:46 +01:00
|
|
|
def once(method):
|
|
|
|
"""
|
|
|
|
The specified method will be invoked only once within the
|
|
|
|
environment.
|
|
|
|
"""
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if __active_environment is None:
|
|
|
|
activate_environment('default')
|
|
|
|
|
2018-01-09 16:37:57 +00:00
|
|
|
func_id = repr(method.__code__)
|
2017-04-12 11:39:46 +01:00
|
|
|
|
|
|
|
if func_id in __environments[__active_environment]:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
__environments[__active_environment].append(func_id)
|
|
|
|
return method(*args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|