python: Define deprecated and callOnce decorators
Change-Id: I85d52a65308b9d5068a9aaa46597e5eaf8175064 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/53523 Reviewed-by: Jason Lowe-Power <power.jg@gmail.com> Maintainer: Jason Lowe-Power <power.jg@gmail.com> Reviewed-by: Andreas Sandberg <andreas.sandberg@arm.com> Maintainer: Andreas Sandberg <andreas.sandberg@arm.com> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
committed by
Meatboy 106
parent
4c1422e3ba
commit
a9beed3a03
@@ -41,6 +41,8 @@ import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
from functools import wraps
|
||||
|
||||
from . import convert
|
||||
|
||||
from .attrdict import attrdict, multiattrdict, optiondict
|
||||
@@ -71,6 +73,36 @@ def warn(fmt, *args):
|
||||
def inform(fmt, *args):
|
||||
print('info:', fmt % args, file=sys.stdout)
|
||||
|
||||
def callOnce(func):
|
||||
"""Decorator that enables to run a given function only once. Subsequent
|
||||
calls are discarded."""
|
||||
@wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
if not wrapper.has_run:
|
||||
wrapper.has_run = True
|
||||
return func(*args, **kwargs)
|
||||
wrapper.has_run = False
|
||||
return wrapper
|
||||
|
||||
def deprecated(replacement=None, logger=warn):
|
||||
"""This decorator warns the user about a deprecated function."""
|
||||
def decorator(func):
|
||||
@callOnce
|
||||
def notifyDeprecation():
|
||||
try:
|
||||
func_name = lambda f: f.__module__ + '.' + f.__qualname__
|
||||
message = f'Function {func_name(func)} is deprecated.'
|
||||
if replacement:
|
||||
message += f' Prefer {func_name(replacement)} instead.'
|
||||
except AttributeError:
|
||||
message = f'Function {func} is deprecated.'
|
||||
if replacement:
|
||||
message += f' Prefer {replacement} instead.'
|
||||
logger(message)
|
||||
notifyDeprecation()
|
||||
return func
|
||||
return decorator
|
||||
|
||||
class Singleton(type):
|
||||
def __call__(cls, *args, **kwargs):
|
||||
if hasattr(cls, '_instance'):
|
||||
|
||||
Reference in New Issue
Block a user