11# -*- coding: utf-8 -*-
22import copy
3+ import enum
34import io
45import typing as t
56
@@ -16,6 +17,30 @@ class GuiMetaData(t.TypedDict):
1617 child_gui_metadata : t .Optional ["GuiMetaData" ]
1718
1819
20+ class IntWithGuiMetadata (int ):
21+ pass
22+
23+
24+ class FloatWithGuiMetadata (float ):
25+ pass
26+
27+
28+ class BytesWithGuiMetadata (bytes ):
29+ pass
30+
31+
32+ class BytearrayWithGuiMetadata (bytearray ):
33+ pass
34+
35+
36+ class StrWithGuiMetadata (str ):
37+ pass
38+
39+
40+ class NoneWithGuiMetadata :
41+ pass
42+
43+
1944class ObjProxyWithGuiMetaData (wrapt .ObjectProxy ):
2045 __slots__ = "__construct_editor_metadata__"
2146
@@ -26,10 +51,6 @@ def __init__(self, wrapped: t.Any, gui_metadata: GuiMetaData):
2651 )
2752
2853
29- class BytesWithGuiMetadata (bytes ):
30- pass
31-
32-
3354def get_gui_metadata (obj : t .Any ) -> t .Optional [GuiMetaData ]:
3455 """Get the GUI metadata if they are available"""
3556 try :
@@ -40,22 +61,34 @@ def get_gui_metadata(obj: t.Any) -> t.Optional[GuiMetaData]:
4061
4162def add_gui_metadata (obj : t .Any , gui_metadata : GuiMetaData ) -> t .Any :
4263 """
43- Append gui_metadata to an object.
44-
45- With immutable types like str, bytes, int, ... it is not possible to add
46- metadata to a type dynammically. With other types like enums it is possible,
47- but enums are singleton, so that the metadata of objects is overwritten by
48- the next object with the same value.
49-
50- Because auf this for any object an proxy object is created, so that the
51- metadata can be added to this proxy object.
64+ Append the private field "__construct_editor_metadata__" to an object
5265 """
53- if isinstance (obj , bytes ):
54- # bytes has to be a subtype of bytes or the `stream_write` will raise an error...
66+ obj_type = type (obj )
67+ if isinstance (obj , enum .Enum ):
68+ obj = ObjProxyWithGuiMetaData (obj , gui_metadata )
69+ elif (obj_type is int ) or (obj_type is bool ):
70+ obj = IntWithGuiMetadata (obj )
71+ obj .__construct_editor_metadata__ = gui_metadata
72+ elif obj_type is float :
73+ obj = FloatWithGuiMetadata (obj )
74+ obj .__construct_editor_metadata__ = gui_metadata
75+ elif obj_type is bytes :
5576 obj = BytesWithGuiMetadata (obj )
5677 obj .__construct_editor_metadata__ = gui_metadata
78+ elif obj_type is bytearray :
79+ obj = BytearrayWithGuiMetadata (obj )
80+ obj .__construct_editor_metadata__ = gui_metadata
81+ elif obj_type is str :
82+ obj = StrWithGuiMetadata (obj )
83+ obj .__construct_editor_metadata__ = gui_metadata
84+ elif obj is None :
85+ obj = NoneWithGuiMetadata ()
86+ obj .__construct_editor_metadata__ = gui_metadata
5787 else :
58- obj = ObjProxyWithGuiMetaData (obj , gui_metadata )
88+ try :
89+ obj .__construct_editor_metadata__ = gui_metadata # type: ignore
90+ except AttributeError :
91+ raise ValueError (f"add_gui_metadata dont work with type of { type (obj )} " )
5992 return obj
6093
6194
0 commit comments