Changeset - 0dd05e9d4ae7
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 20 months ago 2023-05-24 06:43:07
drewp@bigasterisk.com
ran into this line- maybe it is not NotImplemented after all?
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
light9/effect/settings.py
Show inline comments
 
@@ -51,97 +51,97 @@ def getVal(graph, subj):
 
GraphType = SyncedGraph | LocalSyncedGraph
 

	
 

	
 
class _Settings:
 
    """
 
    Generic for DeviceUri/DeviceAttr/VTUnion or EffectClass/EffectAttr/VTUnion
 

	
 
    default values are 0 or '#000000'. Internal rep must not store zeros or some
 
    comparisons will break.
 
    """
 
    EntityType = DeviceUri
 
    AttrType = DeviceAttr
 

	
 
    def __init__(self, graph: GraphType, settingsList: List[Tuple[Any, Any, VTUnion]]):
 
        self.graph = graph  # for looking up all possible attrs
 
        self._compiled: Dict[self.__class__.EntityType, Dict[self.__class__.AttrType, VTUnion]] = {}
 
        for e, a, v in settingsList:
 
            attrVals = self._compiled.setdefault(e, {})
 
            if a in attrVals:
 
                v = resolve(
 
                    e,  # Hey, this is supposed to be DeviceClass (which is not convenient for us), but so far resolve() doesn't use that arg
 
                    a,
 
                    [attrVals[a], v])
 
            attrVals[a] = v
 
        # self._compiled may not be final yet- see _fromCompiled
 
        self._delZeros()
 

	
 
    @classmethod
 
    def _fromCompiled(cls, graph: GraphType, compiled: Dict[EntityType, Dict[AttrType, VTUnion]]):
 
        obj = cls(graph, [])
 
        obj._compiled = compiled
 
        obj._delZeros()
 
        return obj
 

	
 
    @classmethod
 
    def fromList(cls, graph: GraphType, others: List[_Settings]):
 
        """note that others may have multiple values for an attr"""
 
        self = cls(graph, [])
 
        for s in others:
 
            # if not isinstance(s, cls):
 
            #     raise TypeError(s)
 
            for row in s.asList():  # could work straight from s._compiled
 
                if row[0] is None:
 
                    raise TypeError('bad row %r' % (row,))
 
                dev, devAttr, value = row
 
                devDict = self._compiled.setdefault(dev, {})
 
                if devAttr in devDict:
 
                    existingVal: VTUnion = devDict[devAttr]
 
                    raise NotImplementedError('fixme: dev is to be a deviceclass (but it is currently unused)')
 
                    # raise NotImplementedError('fixme: dev is to be a deviceclass (but it is currently unused)')
 
                    value = resolve(dev, devAttr, [existingVal, value])
 
                devDict[devAttr] = value
 
        self._delZeros()
 
        return self
 

	
 
    @classmethod
 
    def _mult(cls, weight, row, dd) -> VTUnion:
 
        if isinstance(row[2], str):
 
            prev = parseHexNorm(dd.get(row[1], '#000000'))
 
            return toHex(prev + weight * numpy.array(parseHexNorm(row[2])))
 
        else:
 
            return dd.get(row[1], 0) + weight * row[2]
 

	
 
    @classmethod
 
    def fromBlend(cls, graph: GraphType, others: List[Tuple[float, _Settings]]):
 
        """others is a list of (weight, Settings) pairs"""
 
        out = cls(graph, [])
 
        for weight, s in others:
 
            if not isinstance(s, cls):
 
                raise TypeError(s)
 
            for row in s.asList():  # could work straight from s._compiled
 
                if row[0] is None:
 
                    raise TypeError('bad row %r' % (row,))
 
                dd = out._compiled.setdefault(row[0], {})
 

	
 
                newVal = cls._mult(weight, row, dd)
 
                dd[row[1]] = newVal
 
        out._delZeros()
 
        return out
 

	
 
    def _zeroForAttr(self, attr: AttrType) -> VTUnion:
 
        if attr == L9['color']:
 
            return HexColor('#000000')
 
        return 0.0
 

	
 
    def _delZeros(self):
 
        for dev, av in list(self._compiled.items()):
 
            for attr, val in list(av.items()):
 
                if val == self._zeroForAttr(attr):
 
                    del av[attr]
 
            if not av:
 
                del self._compiled[dev]
 

	
 
    def __hash__(self):
 
        itemed = tuple([(d, tuple([(a, v) for a, v in sorted(av.items())])) for d, av in sorted(self._compiled.items())])
 
        return hash(itemed)
 

	
 
    def __eq__(self, other):
0 comments (0 inline, 0 general)