Skip to content

Commit

Permalink
Add "colour.utilities.metadata.set_decorator" decorator.
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Apr 16, 2016
1 parent a54afd8 commit 0c1cf0b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
6 changes: 4 additions & 2 deletions colour/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
Metadata,
UnitMetadata,
CallableMetadata,
FunctionMetadata)
FunctionMetadata,
set_metadata)
from .array import (
as_numeric,
closest,
Expand Down Expand Up @@ -55,7 +56,8 @@
__all__ += ['Metadata',
'UnitMetadata',
'CallableMetadata',
'FunctionMetadata']
'FunctionMetadata',
'set_metadata']
__all__ += ['as_numeric',
'closest',
'normalise_maximum',
Expand Down
60 changes: 58 additions & 2 deletions colour/utilities/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +31,8 @@
__all__ = ['Metadata',
'UnitMetadata',
'CallableMetadata',
'FunctionMetadata']
'FunctionMetadata',
'set_metadata']


class Metadata(object):
Expand All @@ -56,8 +59,8 @@ class Metadata(object):
Attributes
----------
family
index
instances
index
name
strict_name
Expand Down Expand Up @@ -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
32 changes: 30 additions & 2 deletions colour/utilities/tests/tests_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
Metadata,
UnitMetadata,
CallableMetadata,
FunctionMetadata)
FunctionMetadata,
set_metadata)

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013 - 2015 - Colour Developers'
Expand All @@ -25,7 +26,8 @@
__all__ = ['TestMetadata',
'TestUnitMetadata',
'TestCallableMetadata',
'TestFunctionMetadata']
'TestFunctionMetadata',
'TestSetMetadata']


class TestMetadata(unittest.TestCase):
Expand Down Expand Up @@ -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()

0 comments on commit 0c1cf0b

Please sign in to comment.