|
| 1 | +import compas |
| 2 | +from collections import defaultdict |
| 3 | +if compas.PY2: |
| 4 | + from collections import Mapping |
| 5 | +else: |
| 6 | + from collections.abc import Mapping |
| 7 | +from compas.colors import Color |
| 8 | + |
| 9 | + |
| 10 | +class DescriptorProtocol(type): |
| 11 | + """Meta class to provide support for the descriptor protocol in Python versions lower than 3.6""" |
| 12 | + |
| 13 | + def __init__(cls, name, bases, attrs): |
| 14 | + for k, v in iter(attrs.items()): |
| 15 | + if hasattr(v, '__set_name__'): |
| 16 | + v.__set_name__(cls, k) |
| 17 | + |
| 18 | + |
| 19 | +class ColorDict(object): |
| 20 | + """Descriptor for color dictionaries. |
| 21 | +
|
| 22 | + To use this descriptor, some requirements need to be fulfilled. |
| 23 | +
|
| 24 | + The descriptor should be assigned to a class attribute |
| 25 | + that has a protected counterpart that will hold the actual dictionary values, |
| 26 | + and a corresponding attribute that defines the default value for missing colors. |
| 27 | + Both the protected attribute and the default attribute should follow a specific naming convention. |
| 28 | +
|
| 29 | + For example, to create the property ``vertex_color`` on a ``MeshArtist``, |
| 30 | + the ``MeshArtist`` should have ``self._vertex_color`` for storing the actual dictionary values, |
| 31 | + and ``self.default_vertexcolor`` for storing the replacement value for missing entries in the dict. |
| 32 | +
|
| 33 | + The descriptor will then ensure that all values assigned to ``vertex_color`` will result in the creation |
| 34 | + of a ``defaultdict`` that always returns instances of ``compas.colors.Color`` |
| 35 | + such that colors can be reliably converted between color spaces as needed, regardless of the input. |
| 36 | +
|
| 37 | + """ |
| 38 | + |
| 39 | + def __set_name__(self, owner, name): |
| 40 | + """Record the name of the attribute this descriptor is assigned to. |
| 41 | + The attribute name is then used to identify the corresponding private attribute, and the attribute containing a default value. |
| 42 | +
|
| 43 | + Parameters |
| 44 | + ---------- |
| 45 | + owner : object |
| 46 | + The class owning the attribute. |
| 47 | + name : str |
| 48 | + The name of the attribute. |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + None |
| 53 | +
|
| 54 | + Notes |
| 55 | + ----- |
| 56 | + In Python 3.6+ this method is called automatically. |
| 57 | + For earlier versions it needs to be used with a custom metaclass (``DescriptorProtocol``). |
| 58 | +
|
| 59 | + """ |
| 60 | + self.public_name = name |
| 61 | + self.private_name = '_' + name |
| 62 | + self.default_name = 'default_' + ''.join(name.split('_')) |
| 63 | + |
| 64 | + def __get__(self, obj, otype=None): |
| 65 | + """Get the color dict stored in the private attribute corresponding to the public attribute name of the descriptor. |
| 66 | +
|
| 67 | + Parameters |
| 68 | + ---------- |
| 69 | + obj : object |
| 70 | + The instance owning the instance of the descriptor. |
| 71 | + otype : object, optional |
| 72 | + The type owning the instance of the descriptor. |
| 73 | +
|
| 74 | + Returns |
| 75 | + ------- |
| 76 | + defaultdict |
| 77 | + A defaultdict with the value stored in the default attribute corresponding to the descriptor as a default value. |
| 78 | +
|
| 79 | + """ |
| 80 | + default = getattr(obj, self.default_name) |
| 81 | + return getattr(obj, self.private_name) or defaultdict(lambda: default) |
| 82 | + |
| 83 | + def __set__(self, obj, value): |
| 84 | + """Set a new value for the descriptor. |
| 85 | +
|
| 86 | + Parameters |
| 87 | + ---------- |
| 88 | + obj : object |
| 89 | + The owner of the descriptor. |
| 90 | + value : dict[Any, :class:`~compas.colors.Color`] | :class:`~compas.colors.Color` |
| 91 | + The new value for the descriptor. |
| 92 | + This value is stored in the corresponding private attribute in the form of a defaultdict. |
| 93 | +
|
| 94 | + Returns |
| 95 | + ------- |
| 96 | + None |
| 97 | +
|
| 98 | + """ |
| 99 | + if not value: |
| 100 | + return |
| 101 | + |
| 102 | + if isinstance(value, Mapping): |
| 103 | + default = getattr(obj, self.default_name) |
| 104 | + item_color = defaultdict(lambda: default) |
| 105 | + for item in value: |
| 106 | + color = Color.coerce(value[item]) |
| 107 | + if color: |
| 108 | + item_color[item] = color |
| 109 | + setattr(obj, self.private_name, item_color) |
| 110 | + |
| 111 | + else: |
| 112 | + color = Color.coerce(value) |
| 113 | + setattr(obj, self.private_name, defaultdict(lambda: color)) |
0 commit comments