Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
Signed-off-by: mulhern <[email protected]>
  • Loading branch information
mulkieran committed Nov 29, 2023
1 parent bfe4efe commit 93c34b1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 47 deletions.
57 changes: 17 additions & 40 deletions src/stratis_cli/_actions/_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@
from dbus_python_client_gen import DPClientGenerationError, make_class

from .._errors import StratisCliGenerationError
from ._constants import (
FILESYSTEM_INTERFACE,
MANAGER_INTERFACE,
POOL_INTERFACE,
REPORT_INTERFACE,
)
from ._constants import MANAGER_INTERFACE, REPORT_INTERFACE
from ._environment import get_timeout
from ._introspect import SPECS

Expand All @@ -39,33 +34,6 @@
)


class ClassInfo: # pylint: disable=too-few-public-methods
"""
Information used to construct the dynamically generated class.
"""

def __init__(self, interface_name, *, invoke_name=None):
"""
Initializer.
"""
self.interface_name = interface_name
self.invoke_name = invoke_name


class ClassKey(Enum):
"""
Keys for dynamically generated classes.
"""

FILESYSTEM = ClassInfo(FILESYSTEM_INTERFACE, invoke_name="Filesystem")
MANAGER = ClassInfo(MANAGER_INTERFACE, invoke_name="Manager")
OBJECT_MANAGER = ClassInfo(
"org.freedesktop.DBus.ObjectManager", invoke_name="ObjectManager"
)
POOL = ClassInfo(POOL_INTERFACE, invoke_name="Pool")
REPORT = ClassInfo(REPORT_INTERFACE, invoke_name="Report")


class Purpose(Enum):
"""
Purpose of class to be created.
Expand All @@ -76,6 +44,13 @@ class Purpose(Enum):
SEARCH = 2 # search for object in GEtManagedObjects result


_LOOKUP = {
"Manager": (Purpose.INVOKE, MANAGER_INTERFACE),
"ObjectManager": (Purpose.INVOKE, "org.freedesktop.DBus.ObjectManager"),
"Report": (Purpose.INVOKE, REPORT_INTERFACE),
}


def _add_abs_path_assertion(klass, method_name, key):
"""
Set method_name of method_klass to a new method which checks that the
Expand All @@ -101,24 +76,26 @@ def new_method(proxy, args): # pragma: no cover
setattr(method_class, method_name, new_method)


def make_dyn_class(key, purpose):
def make_dyn_class(name):
"""
Dynamically generate a class from introspection specification.
:param ClassKey key: key that identifies the class to make
:param str name: name of class to make
"""
(purpose, interface_name) = _LOOKUP[name]

if purpose is Purpose.INVOKE:
try:
klass = make_class(
key.value.invoke_name,
ET.fromstring(SPECS[key.value.interface_name]), # nosec B314
name,
ET.fromstring(SPECS[interface_name]), # nosec B314
TIMEOUT,
)

try:
if key == ClassKey.MANAGER:
if name == "Manager":
_add_abs_path_assertion(klass, "CreatePool", "devices")
if key == ClassKey.POOL: # pragma: no cover
if name == "Pool": # pragma: no cover
_add_abs_path_assertion(klass, "InitCache", "devices")
_add_abs_path_assertion(klass, "AddCacheDevs", "devices")
_add_abs_path_assertion(klass, "AddDataDevs", "devices")
Expand All @@ -135,7 +112,7 @@ def make_dyn_class(key, purpose):

except DPClientGenerationError as err: # pragma: no cover
raise StratisCliGenerationError(
f"Failed to generate class {key.value.invoke_name} needed for invoking "
f"Failed to generate class {name} needed for invoking "
"dbus-python methods"
) from err

Expand Down
14 changes: 7 additions & 7 deletions src/stratis_cli/_actions/_top.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from .._stratisd_constants import ReportKey, StratisdErrors
from ._connection import get_object
from ._constants import TOP_OBJECT
from ._dynamic import ClassKey, Purpose, make_dyn_class
from ._dynamic import make_dyn_class
from ._formatting import print_table


Expand All @@ -45,7 +45,7 @@ def _fetch_keylist(proxy):
:rtype: list of str
:raises StratisCliEngineError:
"""
Manager = make_dyn_class(ClassKey.MANAGER, Purpose.INVOKE)
Manager = make_dyn_class("Manager")

(keys, return_code, message) = Manager.Methods.ListKeys(proxy, {})
if return_code != StratisdErrors.OK: # pragma: no cover
Expand All @@ -68,7 +68,7 @@ def _add_update_key(proxy, key_desc, capture_key, *, keyfile_path):
"""
assert capture_key == (keyfile_path is None)

Manager = make_dyn_class(ClassKey.MANAGER, Purpose.INVOKE)
Manager = make_dyn_class("Manager")

if capture_key:
password = getpass(prompt="Enter key data followed by the return key: ")
Expand Down Expand Up @@ -117,22 +117,22 @@ def get_report(namespace):
"""

if namespace.report_name == ReportKey.MANAGED_OBJECTS.value:
ObjectManager = make_dyn_class(ClassKey.OBJECT_MANAGER, Purpose.INVOKE)
ObjectManager = make_dyn_class("ObjectManager")

json_report = ObjectManager.Methods.GetManagedObjects(
get_object(TOP_OBJECT), {}
)

else:
if namespace.report_name == ReportKey.ENGINE_STATE.value:
Manager = make_dyn_class(ClassKey.MANAGER, Purpose.INVOKE)
Manager = make_dyn_class("Manager")

(report, return_code, message) = Manager.Methods.EngineStateReport(
get_object(TOP_OBJECT), {}
)

else:
Report = make_dyn_class(ClassKey.REPORT, Purpose.INVOKE)
Report = make_dyn_class("Report")

(report, return_code, message) = Report.Methods.GetReport(
get_object(TOP_OBJECT), {"name": namespace.report_name}
Expand Down Expand Up @@ -240,7 +240,7 @@ def unset_key(namespace):
:raises StratisCliNoChangeError:
:raises StratisCliIncoherenceError:
"""
Manager = make_dyn_class(ClassKey.MANAGER, Purpose.INVOKE)
Manager = make_dyn_class("Manager")

proxy = get_object(TOP_OBJECT)

Expand Down

0 comments on commit 93c34b1

Please sign in to comment.