def store( self, *, key: handlers.HandlerId, record: ProgressRecord, body: bodies.Body, patch: patches.Patch, ) -> None: # Nones are cleaned by K8s API itself. dicts.ensure(patch, self.field + (key, ), record)
def store( self, *, body: bodies.Body, patch: patches.Patch, essence: bodies.BodyEssence, ) -> None: # Store as a single string instead of full dict -- to avoid merges and unexpected data. encoded: str = json.dumps(essence, separators=(',', ':')) # NB: no spaces dicts.ensure(patch, self.field, encoded)
def touch( self, *, body: bodies.Body, patch: patches.Patch, value: Optional[str], ) -> None: key_field = self.touch_field body_value = dicts.resolve(body, key_field, None, assume_empty=True) if body_value != value: # also covers absent-vs-None cases. dicts.ensure(patch, key_field, value)
def touch( self, *, body: bodies.Body, patch: patches.Patch, value: Optional[str], ) -> None: full_key = self.make_key(self.touch_key) key_field = ['metadata', 'annotations', full_key] body_value = dicts.resolve(body, key_field, None, assume_empty=True) if body_value != value: # also covers absent-vs-None cases. dicts.ensure(patch, key_field, value)
def touch( self, *, body: bodies.Body, patch: patches.Patch, value: Optional[str], ) -> None: for full_key in self.make_keys(self.touch_key, body=body): key_field = ['metadata', 'annotations', full_key] body_value = dicts.resolve(body, key_field, None) if body_value != value: # also covers absent-vs-None cases. dicts.ensure(patch, key_field, value) self._store_marker(prefix=self.prefix, patch=patch, body=body)
def store( self, *, key: handlers.HandlerId, record: ProgressRecord, body: bodies.Body, patch: patches.Patch, ) -> None: decoded = {key: val for key, val in record.items() if self.verbose or val is not None} encoded = json.dumps(decoded, separators=(',', ':')) # NB: no spaces for full_key in self.make_keys(key): key_field = ['metadata', 'annotations', full_key] dicts.ensure(patch, key_field, encoded) self._store_marker(prefix=self.prefix, patch=patch, body=body)
def purge( self, *, key: handlers.HandlerId, body: bodies.Body, patch: patches.Patch, ) -> None: absent = object() key_field = self.field + (key, ) body_value = dicts.resolve(body, key_field, absent) patch_value = dicts.resolve(patch, key_field, absent) if body_value is not absent: dicts.ensure(patch, key_field, None) elif patch_value is not absent: dicts.remove(patch, key_field)
def store( self, *, key: handlers.HandlerId, record: ProgressRecord, body: bodies.Body, patch: patches.Patch, ) -> None: full_key = self.make_key(key) key_field = ['metadata', 'annotations', full_key] decoded = { key: val for key, val in record.items() if self.verbose or val is not None } encoded = json.dumps(decoded) dicts.ensure(patch, key_field, encoded)
def purge( self, *, key: handlers.HandlerId, body: bodies.Body, patch: patches.Patch, ) -> None: absent = object() for full_key in self.make_keys(key, body=body): key_field = ['metadata', 'annotations', full_key] body_value = dicts.resolve(body, key_field, absent) patch_value = dicts.resolve(patch, key_field, absent) if body_value is not absent: dicts.ensure(patch, key_field, None) elif patch_value is not absent: dicts.remove(patch, key_field)
def purge( self, *, key: handlers.HandlerId, body: bodies.Body, patch: patches.Patch, ) -> None: absent = object() full_key = self.make_key(key) key_field = ['metadata', 'annotations', full_key] body_value = dicts.resolve(body, key_field, absent, assume_empty=True) patch_value = dicts.resolve(patch, key_field, absent, assume_empty=True) if body_value is not absent: dicts.ensure(patch, key_field, None) elif patch_value is not absent: dicts.remove(patch, key_field)
def test_existing_key(): d = {'abc': {'def': {'hij': 'val'}}} ensure(d, ['abc', 'def', 'hij'], 'new') assert d == {'abc': {'def': {'hij': 'new'}}}
def test_empty_path(): d = {} with pytest.raises(ValueError) as e: ensure(d, [], 'new') assert "Setting a root of a dict is impossible" in str(e.value)
def test_nonmapping_key(): d = {'key': 'val'} with pytest.raises(TypeError): ensure(d, ['key', 'sub'], 'new')
def test_toplevel_key(): d = {'key': 'val'} ensure(d, ['key'], 'new') assert d == {'key': 'new'}
def test_unexisting_key_in_unexisting_dict(): d = {} ensure(d, ['abc', 'def', 'hij'], 'new') assert d == {'abc': {'def': {'hij': 'new'}}}