示例#1
0
 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)
示例#2
0
 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)
示例#3
0
 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)
示例#4
0
 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)
示例#5
0
 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)
示例#6
0
 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)
示例#7
0
 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)
示例#8
0
 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)
示例#9
0
 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)
示例#10
0
 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)
示例#11
0
def test_existing_key():
    d = {'abc': {'def': {'hij': 'val'}}}
    ensure(d, ['abc', 'def', 'hij'], 'new')
    assert d == {'abc': {'def': {'hij': 'new'}}}
示例#12
0
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)
示例#13
0
def test_nonmapping_key():
    d = {'key': 'val'}
    with pytest.raises(TypeError):
        ensure(d, ['key', 'sub'], 'new')
示例#14
0
def test_toplevel_key():
    d = {'key': 'val'}
    ensure(d, ['key'], 'new')
    assert d == {'key': 'new'}
示例#15
0
def test_unexisting_key_in_unexisting_dict():
    d = {}
    ensure(d, ['abc', 'def', 'hij'], 'new')
    assert d == {'abc': {'def': {'hij': 'new'}}}