From 3c951910b0bee17bcb8ff0c9d149281972a18911 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Sat, 15 Apr 2023 13:44:00 +0200 Subject: [PATCH] extract description type and add it to dict representation --- grum/descs/desc.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/grum/descs/desc.py b/grum/descs/desc.py index 0c52278..8a85046 100644 --- a/grum/descs/desc.py +++ b/grum/descs/desc.py @@ -2,7 +2,22 @@ class Description: def to_dict(self): - return {k: v for k, v in self.__dict__.items() if not k.startswith("_") and k != "name" and v is not None} + res = {k: v for k, v in self.__dict__.items() if not k.startswith("_") and k != "name" and v is not None} + tn = self.get_type() + res.setdefault("type", tn) + return res + + + @classmethod + def get_type(cls): + tn = cls.__name__ + suffix = "Description" + if not tn.endswith(suffix): + raise ValueError(f'"{tn}" does not end with "{suffix}"') + tn = tn[:-len(suffix)] + tn = tn.casefold() + tn = tn or None + return tn