Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement __cli_output__ for descriptors #1762

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions miio/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ class Descriptor:
#: Access flags (read, write, execute) for the described item.
access: AccessFlags = attr.ib(default=AccessFlags(0))

@property
def __cli_output__(self) -> str:
"""Return a string presentation for the cli."""
s = f"{self.name} ({self.id})\n"
if self.type:
s += f"\tType: {self.type}\n"
if self.unit:
s += f"\tUnit: {self.unit}\n"
if self.status_attribute:
s += f"\tAttribute: {self.status_attribute}\n"
s += f"\tAccess: {self.access}\n"
if self.extras:
s += f"\tExtras: {self.extras}\n"

return s


@attr.s(auto_attribs=True)
class ActionDescriptor(Descriptor):
Expand All @@ -71,6 +87,15 @@ class ActionDescriptor(Descriptor):

access: AccessFlags = attr.ib(default=AccessFlags.Execute)

@property
def __cli_output__(self) -> str:
"""Return a string presentation for the cli."""
s = super().__cli_output__
if self.inputs:
s += f"\tInputs: {self.inputs}\n"

return s


class PropertyConstraint(Enum):
"""Defines constraints for integer based properties."""
Expand Down Expand Up @@ -104,6 +129,20 @@ class PropertyDescriptor(Descriptor):
#: If set, the callable with this name will override the `setter` attribute.
setter_name: Optional[str] = attr.ib(default=None, repr=False)

@property
def __cli_output__(self) -> str:
"""Return a string presentation for the cli."""
s = super().__cli_output__

if self.setter:
s += f"\tSetter: {self.setter}\n"
if self.setter_name:
s += f"\tSetter Name: {self.setter_name}\n"
if self.constraint:
s += f"\tConstraint: {self.constraint}\n"

return s


@attr.s(auto_attribs=True, kw_only=True)
class EnumDescriptor(PropertyDescriptor):
Expand All @@ -115,6 +154,15 @@ class EnumDescriptor(PropertyDescriptor):
#: Enum class containing the available choices.
choices: Optional[Type[Enum]] = attr.ib(default=None, repr=False)

@property
def __cli_output__(self) -> str:
"""Return a string presentation for the cli."""
s = super().__cli_output__
if self.choices:
s += f"\tChoices: {self.choices}\n"

return s


@attr.s(auto_attribs=True, kw_only=True)
class RangeDescriptor(PropertyDescriptor):
Expand All @@ -135,3 +183,10 @@ class RangeDescriptor(PropertyDescriptor):
range_attribute: Optional[str] = attr.ib(default=None)
type: type = int
constraint: PropertyConstraint = PropertyConstraint.Range

@property
def __cli_output__(self) -> str:
"""Return a string presentation for the cli."""
s = super().__cli_output__
s += f"\tRange: {self.min_value} - {self.max_value} (step {self.step})\n"
return s
3 changes: 3 additions & 0 deletions miio/tests/test_descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def test_descriptor(class_, access):
assert desc.extras == {"test": "test"}
assert desc.access == access

# TODO: test for cli output in the derived classes
assert hasattr(desc, "__cli_output__")


def test_actiondescriptor():
"""Test that an action descriptor has the expected API."""
Expand Down