From 08cbcec7674394c9dff58538bc542b2abe768526 Mon Sep 17 00:00:00 2001 From: ilia Date: Tue, 24 Oct 2023 10:13:31 +0300 Subject: [PATCH] add get_descriptors with using "return" and "set" --- turtle/equipment/turtle_device.py | 40 ++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/turtle/equipment/turtle_device.py b/turtle/equipment/turtle_device.py index e88adac..0946afd 100644 --- a/turtle/equipment/turtle_device.py +++ b/turtle/equipment/turtle_device.py @@ -3,7 +3,7 @@ from typing import Optional, Collection, Any from controls.device import SynchronyDevice from controls.device import TraitDescriptor from controls.device import ActionDescriptor - +import inspect class TurtleDevice(SynchronyDevice): def open(self): @@ -35,3 +35,41 @@ class TurtleDevice(SynchronyDevice): def invalidate(self, trait_name: str): pass + + def get_descriptors(self): + descriptors = dict(actions=dict(), traits=dict()) + for m_name, member in inspect.getmembers(Turtle): + if m_name.startswith("_"): continue + if m_name.lower() != m_name: continue + doc = inspect.getdoc(member) + if doc is None: continue + first_line_doc = inspect.getdoc(member).lower().split("\n")[0] + print(first_line_doc) + + if not inspect.isfunction(member): + descriptors["traits"][m_name] = TraitDescriptor(m_name, + doc, + readable=True, + 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: + sig = inspect.signature(member) + params_dict = dict(sig.parameters) + descriptors["actions"][m_name] = ActionDescriptor(m_name, + arguments=params_dict, + info=doc) + print("ACTION") + print("------------------------------------------") + return descriptors