From 93c34b1cb0bd905130dc4553441206bf16a8e88e Mon Sep 17 00:00:00 2001 From: mulhern Date: Wed, 29 Nov 2023 14:50:39 -0500 Subject: [PATCH] Simplify Signed-off-by: mulhern --- src/stratis_cli/_actions/_dynamic.py | 57 +++++++++------------------- src/stratis_cli/_actions/_top.py | 14 +++---- 2 files changed, 24 insertions(+), 47 deletions(-) diff --git a/src/stratis_cli/_actions/_dynamic.py b/src/stratis_cli/_actions/_dynamic.py index 7f1dd9578..d49311be6 100644 --- a/src/stratis_cli/_actions/_dynamic.py +++ b/src/stratis_cli/_actions/_dynamic.py @@ -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 @@ -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. @@ -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 @@ -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") @@ -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 diff --git a/src/stratis_cli/_actions/_top.py b/src/stratis_cli/_actions/_top.py index 856996075..ca639b0de 100644 --- a/src/stratis_cli/_actions/_top.py +++ b/src/stratis_cli/_actions/_top.py @@ -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 @@ -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 @@ -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: ") @@ -117,7 +117,7 @@ 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), {} @@ -125,14 +125,14 @@ def get_report(namespace): 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} @@ -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)