11import logging
22from collections import UserDict
3+ from enum import Enum
34from inspect import getmembers
45from typing import TYPE_CHECKING , Generic , TypeVar , cast
56
1213 PropertyDescriptor ,
1314 RangeDescriptor ,
1415)
16+ from .exceptions import DeviceException
1517
1618_LOGGER = logging .getLogger (__name__ )
1719
@@ -42,10 +44,11 @@ def descriptors_from_object(self, obj):
4244 2. Going through all members and looking if they have a '_descriptor' attribute set by a decorator
4345 """
4446 _LOGGER .debug ("Adding descriptors from %s" , obj )
47+ descriptors_to_add = []
4548 # 1. Check for existence of _descriptors as DeviceStatus' metaclass collects them already
4649 if descriptors := getattr (obj , "_descriptors" ): # noqa: B009
4750 for _name , desc in descriptors .items ():
48- self . add_descriptor (desc )
51+ descriptors_to_add . append (desc )
4952
5053 # 2. Check if object members have descriptors
5154 for _name , method in getmembers (obj , lambda o : hasattr (o , "_descriptor" )):
@@ -55,7 +58,10 @@ def descriptors_from_object(self, obj):
5558 continue
5659
5760 prop_desc .method = method
58- self .add_descriptor (prop_desc )
61+ descriptors_to_add .append (prop_desc )
62+
63+ for desc in descriptors_to_add :
64+ self .add_descriptor (desc )
5965
6066 def add_descriptor (self , descriptor : Descriptor ):
6167 """Add a descriptor to the collection.
@@ -102,7 +108,11 @@ def _handle_property_descriptor(self, prop: PropertyDescriptor) -> None:
102108 if prop .access & AccessFlags .Write and prop .setter is None :
103109 raise ValueError (f"Neither setter or setter_name was defined for { prop } " )
104110
105- self ._handle_constraints (prop )
111+ # TODO: temporary hack as this should not cause I/O nor fail
112+ try :
113+ self ._handle_constraints (prop )
114+ except DeviceException as ex :
115+ _LOGGER .error ("Adding constraints failed: %s" , ex )
106116
107117 def _handle_constraints (self , prop : PropertyDescriptor ) -> None :
108118 """Set attribute-based constraints for the descriptor."""
@@ -112,7 +122,11 @@ def _handle_constraints(self, prop: PropertyDescriptor) -> None:
112122 retrieve_choices_function = getattr (
113123 self ._device , prop .choices_attribute
114124 )
115- prop .choices = retrieve_choices_function ()
125+ choices = retrieve_choices_function ()
126+ if isinstance (choices , dict ):
127+ prop .choices = Enum (f"GENERATED_ENUM_{ prop .name } " , choices )
128+ else :
129+ prop .choices = choices
116130
117131 if prop .choices is None :
118132 raise ValueError (
0 commit comments