Have node_setattr delete keys when the value to set is None.
Note that this looks at the value directly; when using {'key': None} as value, it is not None and thus will be set. This thus is NOT a way to be 100% sure there are no None values.
This commit is contained in:
@@ -135,14 +135,18 @@ class ShotManager(object):
|
|||||||
def node_setattr(node, key, value):
|
def node_setattr(node, key, value):
|
||||||
"""Sets a node property by dotted key.
|
"""Sets a node property by dotted key.
|
||||||
|
|
||||||
Modifies the node in-place.
|
Modifies the node in-place. Deletes None values.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
set_on = node
|
set_on = node
|
||||||
while key and '.' in key:
|
while key and '.' in key:
|
||||||
head, key = key.split('.', 1)
|
head, key = key.split('.', 1)
|
||||||
set_on = node[head]
|
set_on = set_on[head]
|
||||||
set_on[key] = value
|
|
||||||
|
if value is None:
|
||||||
|
set_on.pop(key, None)
|
||||||
|
else:
|
||||||
|
set_on[key] = value
|
||||||
|
|
||||||
|
|
||||||
@register_patch_handler(node_type_shot['name'])
|
@register_patch_handler(node_type_shot['name'])
|
||||||
|
@@ -157,7 +157,7 @@ class NodeSetattrTest(unittest.TestCase):
|
|||||||
node = {}
|
node = {}
|
||||||
node_setattr(node, 'a', None)
|
node_setattr(node, 'a', None)
|
||||||
node_setattr(node, None, 'b')
|
node_setattr(node, None, 'b')
|
||||||
self.assertEqual({'a': None, None: 'b'}, node)
|
self.assertEqual({None: 'b'}, node)
|
||||||
|
|
||||||
def test_none_dotted(self):
|
def test_none_dotted(self):
|
||||||
from attract.shots import node_setattr
|
from attract.shots import node_setattr
|
||||||
@@ -167,15 +167,16 @@ class NodeSetattrTest(unittest.TestCase):
|
|||||||
|
|
||||||
node = {'b': {}}
|
node = {'b': {}}
|
||||||
node_setattr(node, 'b.simple', None)
|
node_setattr(node, 'b.simple', None)
|
||||||
self.assertEqual({'b': {'simple': None}}, node)
|
self.assertEqual({'b': {}}, node)
|
||||||
|
|
||||||
node_setattr(node, 'b.complex', {'yes': None})
|
node_setattr(node, 'b.complex', {'yes': None})
|
||||||
self.assertEqual({'b': {'simple': None,
|
self.assertEqual({'b': {'complex': {'yes': None}}}, node)
|
||||||
'complex': {'yes': None}}}, node)
|
|
||||||
|
node_setattr(node, 'b.complex.yes', None)
|
||||||
|
self.assertEqual({'b': {'complex': {}}}, node)
|
||||||
|
|
||||||
node_setattr(node, 'b.complex', {None: 5})
|
node_setattr(node, 'b.complex', {None: 5})
|
||||||
self.assertEqual({'b': {'simple': None,
|
self.assertEqual({'b': {'complex': {None: 5}}}, node)
|
||||||
'complex': {None: 5}}}, node)
|
|
||||||
|
|
||||||
|
|
||||||
class PatchShotTest(AbstractShotTest):
|
class PatchShotTest(AbstractShotTest):
|
||||||
|
Reference in New Issue
Block a user