11"""The parameters dictionary contains global parameter settings."""
2+ import inspect
23import os
34from abc import ABC , abstractmethod
45from collections import OrderedDict
56from functools import wraps
7+ from types import FunctionType
68
79from devito .logger import info , warning
810from devito .tools import Signer , filter_ordered
@@ -226,7 +228,7 @@ def init_configuration(configuration=configuration, env_vars_mapper=env_vars_map
226228
227229class SwitchDecorator (ABC ):
228230 """
229- Abstract base class that turns a context manager class into a decorator.
231+ Abstract base class that turns a context manager class into a decorator
230232 """
231233 @abstractmethod
232234 def __init__ (self , * args , ** kwargs ):
@@ -240,8 +242,20 @@ def __enter__(self):
240242 def __exit__ (self , exc_type , exc_val , traceback ):
241243 pass
242244
243- def __call__ (self , func , * args , ** kwargs ):
244- """ The call method turns the context manager class into a decorator
245+ def __call__ (self , target , * args , ** kwargs ):
246+ """
247+ The call method turns the context manager class into a decorator
248+ """
249+ if inspect .isclass (target ):
250+ return self ._decorate_class (target , * args , ** kwargs )
251+ elif callable (target ):
252+ return self ._decorate_callable (target , * args , ** kwargs )
253+ raise TypeError ("Decorator is to be applied to a class or callable, not "
254+ f"{ type (target )} " )
255+
256+ def _decorate_callable (self , func , * args , ** kwargs ):
257+ """
258+ Apply the decorator to a function
245259 """
246260 @wraps (func )
247261 def wrapper (* args , ** kwargs ):
@@ -250,6 +264,16 @@ def wrapper(*args, **kwargs):
250264 return result
251265 return wrapper
252266
267+ def _decorate_class (self , cls , * args , ** kwargs ):
268+ """
269+ Apply the decorator to a class, thereby wrapping its methods
270+ """
271+ for name , attr in list (vars (cls ).items ()):
272+ if isinstance (attr , (FunctionType ,)):
273+ setattr (cls , name , self ._decorate_callable (attr , * args , ** kwargs ))
274+
275+ return cls
276+
253277
254278class switchconfig (SwitchDecorator ):
255279 """
0 commit comments