rewrite get_descriptors to use __dict__

This commit is contained in:
ilia 2023-10-24 10:28:38 +03:00
parent 08cbcec767
commit 593ad6b45c

View File

@ -7,6 +7,7 @@ import inspect
class TurtleDevice(SynchronyDevice): class TurtleDevice(SynchronyDevice):
def open(self): def open(self):
self.turtle = Turtle()
super().open() super().open()
def close(self): def close(self):
@ -38,38 +39,25 @@ class TurtleDevice(SynchronyDevice):
def get_descriptors(self): def get_descriptors(self):
descriptors = dict(actions=dict(), traits=dict()) descriptors = dict(actions=dict(), traits=dict())
for k, v in self.turtle.__dict__.items():
if not k.startswith("_"):
descriptors["traits"][k] = v
for m_name, member in inspect.getmembers(Turtle): for m_name, member in inspect.getmembers(Turtle):
if m_name.startswith("_"): continue if m_name.startswith("_"): continue
if m_name.lower() != m_name: continue if m_name.lower() != m_name: continue
doc = inspect.getdoc(member) doc = inspect.getdoc(member)
if doc is None: continue if doc is None: continue
first_line_doc = inspect.getdoc(member).lower().split("\n")[0]
print(first_line_doc)
if not inspect.isfunction(member): if not inspect.isfunction(member):
descriptors["traits"][m_name] = TraitDescriptor(m_name, descriptors["traits"][m_name] = TraitDescriptor(m_name,
doc, doc,
readable=True, readable=True,
writable=False) writable=False)
print(m_name)
continue
if inspect.ismethod(member):
print(m_name)
return_in_doc = ("return" in first_line_doc)
set_in_doc = ("set" in first_line_doc)
if return_in_doc or set_in_doc:
descriptors["traits"][m_name] = TraitDescriptor(m_name,
doc,
readable=return_in_doc,
writable=set_in_doc)
print("TRAIT")
else: else:
sig = inspect.signature(member) sig = inspect.signature(member)
params_dict = dict(sig.parameters) params_dict = dict(sig.parameters)
descriptors["actions"][m_name] = ActionDescriptor(m_name, descriptors["actions"][m_name] = ActionDescriptor(m_name,
arguments=params_dict, arguments=params_dict,
info=doc) info=doc)
print("ACTION")
print("------------------------------------------")
return descriptors return descriptors