From 0c1cf0bc8e4c272eacff896eaea5b3d0e03776c7 Mon Sep 17 00:00:00 2001 From: Thomas Mansencal Date: Sat, 16 Apr 2016 21:15:31 +1200 Subject: [PATCH] Add "colour.utilities.metadata.set_decorator" decorator. --- colour/utilities/__init__.py | 6 ++- colour/utilities/metadata.py | 60 +++++++++++++++++++++++- colour/utilities/tests/tests_metadata.py | 32 ++++++++++++- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/colour/utilities/__init__.py b/colour/utilities/__init__.py index dfe95c4591..81994c0a06 100644 --- a/colour/utilities/__init__.py +++ b/colour/utilities/__init__.py @@ -20,7 +20,8 @@ Metadata, UnitMetadata, CallableMetadata, - FunctionMetadata) + FunctionMetadata, + set_metadata) from .array import ( as_numeric, closest, @@ -55,7 +56,8 @@ __all__ += ['Metadata', 'UnitMetadata', 'CallableMetadata', - 'FunctionMetadata'] + 'FunctionMetadata', + 'set_metadata'] __all__ += ['as_numeric', 'closest', 'normalise_maximum', diff --git a/colour/utilities/metadata.py b/colour/utilities/metadata.py index e01acb58ed..13fd375392 100644 --- a/colour/utilities/metadata.py +++ b/colour/utilities/metadata.py @@ -11,10 +11,12 @@ - :class:`UnitMetadata` - :class:`CallableMetadata` - :class:`FunctionMetadata` +- :def:`set_metadata` """ from __future__ import division, unicode_literals +import functools from weakref import WeakValueDictionary import colour # noqa @@ -29,7 +31,8 @@ __all__ = ['Metadata', 'UnitMetadata', 'CallableMetadata', - 'FunctionMetadata'] + 'FunctionMetadata', + 'set_metadata'] class Metadata(object): @@ -56,8 +59,8 @@ class Metadata(object): Attributes ---------- family - index instances + index name strict_name @@ -702,3 +705,56 @@ def __repr__(self): self.strict_method) return text + + +def set_metadata(metadata, *args, **kwargs): + """ + Decorator setting given metadata to decorated object. + + Parameters + ---------- + \*args : list, optional + Arguments. + \**kwargs : dict, optional + Keywords arguments. + + Returns + ------- + object + + Examples + -------- + >>> @set_metadata(Metadata, 'Lambda', '$\Lambda$') + ... def f(): + ... pass + >>> f.__metadata__ + Metadata('Lambda', '$\Lambda$') + >>> m = Metadata('Gamma', '$\Gamma$') + >>> @set_metadata(m) + ... def f(): + ... pass + >>> f.__metadata__ + Metadata('Gamma', '$\Gamma$') + """ + + if not isinstance(metadata, Metadata): + metadata = metadata(*args, **kwargs) + + def wrapper(function): + """ + Wrapper for given function. + """ + + function.__metadata__ = metadata + + @functools.wraps(function) + def wrapped(*args, **kwargs): + """ + Wrapped function. + """ + + return function(*args, **kwargs) + + return wrapped + + return wrapper diff --git a/colour/utilities/tests/tests_metadata.py b/colour/utilities/tests/tests_metadata.py index d883e6745d..a7bacdc1c9 100644 --- a/colour/utilities/tests/tests_metadata.py +++ b/colour/utilities/tests/tests_metadata.py @@ -13,7 +13,8 @@ Metadata, UnitMetadata, CallableMetadata, - FunctionMetadata) + FunctionMetadata, + set_metadata) __author__ = 'Colour Developers' __copyright__ = 'Copyright (C) 2013 - 2015 - Colour Developers' @@ -25,7 +26,8 @@ __all__ = ['TestMetadata', 'TestUnitMetadata', 'TestCallableMetadata', - 'TestFunctionMetadata'] + 'TestFunctionMetadata', + 'TestSetMetadata'] class TestMetadata(unittest.TestCase): @@ -210,5 +212,31 @@ def test__repr__(self): UnitMetadata('Lightness', '$L^\star$'), 'CIE 1976', '$CIE 1976$')") +class TestSetMetadata(unittest.TestCase): + """ + Defines :func:`colour.utilities.metadata.set_metadata` definition units + tests methods. + """ + + def test_set_metadata(self): + """ + Tests :func:`colour.utilities.metadata.set_metadata` definition. + """ + + @set_metadata(Metadata, 'Lambda', '$\Lambda$') + def f(): + pass + + self.assertTrue(hasattr(f, '__metadata__')) + + m = Metadata('Gamma', '$\Gamma$') + + @set_metadata(m) + def f(): + pass + + self.assertIs(f.__metadata__, m) + + if __name__ == '__main__': unittest.main()