ccpgames / alviss

Configuration file reader with some nifty bells and whistles added
MIT License
0 stars 0 forks source link

The `as_yaml(unmaksed=True)` method is rendering weird stuff #7

Open CCP-Zeulix opened 1 month ago

CCP-Zeulix commented 1 month ago

This code:

    def as_dict(self) -> Dict:
        log.debug(f'as_dict() YAML dump:\n{self._data.as_yaml(unmaksed=True)}')
        return self._data.as_dict(unmaksed=True)

...printed this out in logs...

DEBUG:D:\github\ccpgames\ccp-stencil\ccpstencil\context\_alviss.py:as_dict() YAML dump:
!!python/object/apply:alviss.structs.baseconfig._KwSafeEmptyDict
args:
- !!python/name:ccptools.tpu.structs.empty._empty.%3Clambda%3E ''
dictitems:
  age: 7
  colors:
    favorite: Blue
    weakness: Yellow
  name: Bob

Note the top 4 lines. They're not suppose to be there.

I suspect this has something to do with the _KwSafeEmptyDict which extends the ccptools.structs.EmptyDict which extends the collections.defaultdict and how these "Dict-like" classes are structured in terms of YAML rendering.

Also performing the update(**kwargs) method of BasicConfig seems to add more:

DEBUG:D:\github\ccpgames\ccp-stencil\ccpstencil\context\_alviss.py:nested_update(key_tuple='colors.favorite', value='Green')
DEBUG:D:\github\ccpgames\ccp-stencil\ccpstencil\context\_alviss.py:as_dict() YAML dump:
!!python/object/apply:alviss.structs.baseconfig._KwSafeEmptyDict
args:
- !!python/name:ccptools.tpu.structs.empty._empty.%3Clambda%3E ''
dictitems:
  age: 7
  colors: !!python/object/apply:alviss.structs.baseconfig._KwSafeEmptyDict
    args:
    - !!python/name:ccptools.tpu.structs.empty._empty.%3Clambda%3E ''
    dictitems:
      favorite: Green
      weakness: Yellow
  name: Bob the Cat
CCP-Zeulix commented 1 month ago

Just discovered that this ONLY applies if unmaksed=True which propegates down to as_dict and runs:

return self._data if unmaksed else self._repr_dump(self._data)

...so self,_data is returned "unchanged" instead of going through this method...

    @classmethod
    def _repr_dump(cls, cfg_map: dict):
        d = {}
        for k, v in cfg_map.items():
            if isinstance(v, dict):
                d[k] = cls._repr_dump(v)
            elif isinstance(v, (str, bytes)) and cls._is_key_secret(k):
                d[k] = '********'
            else:
                d[k] = v
        return d

...which ensures that the end result is just a "plain-old-dict".