Skip to content

Commit e686f7f

Browse files
committed
Use purpose argument
Signed-off-by: mulhern <[email protected]>
1 parent 5c5f2ea commit e686f7f

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

src/stratis_cli/_actions/_dynamic.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ClassKey(Enum):
6666
REPORT = ClassInfo(REPORT_INTERFACE, invoke_name="Report")
6767

6868

69-
class PurposeKey(Enum):
69+
class Purpose(Enum):
7070
"""
7171
Purpose of class to be created.
7272
"""
@@ -101,39 +101,42 @@ def new_method(proxy, args): # pragma: no cover
101101
setattr(method_class, method_name, new_method)
102102

103103

104-
def make_dyn_class(key):
104+
def make_dyn_class(key, purpose):
105105
"""
106106
Dynamically generate a class from introspection specification.
107107
108108
:param ClassKey key: key that identifies the class to make
109109
"""
110-
try:
111-
klass = make_class(
112-
key.value.invoke_name,
113-
ET.fromstring(SPECS[key.value.interface_name]), # nosec B314
114-
TIMEOUT,
115-
)
116-
110+
if purpose is Purpose.INVOKE:
117111
try:
118-
if key == ClassKey.MANAGER:
119-
_add_abs_path_assertion(klass, "CreatePool", "devices")
120-
if key == ClassKey.POOL:
121-
_add_abs_path_assertion(klass, "InitCache", "devices")
122-
_add_abs_path_assertion(klass, "AddCacheDevs", "devices")
123-
_add_abs_path_assertion(klass, "AddDataDevs", "devices")
124-
except AttributeError as err: # pragma: no cover
125-
# This can only happen if the expected method is missing from
126-
# the XML spec or code generation has a bug, we will never
127-
# test for these conditions.
112+
klass = make_class(
113+
key.value.invoke_name,
114+
ET.fromstring(SPECS[key.value.interface_name]), # nosec B314
115+
TIMEOUT,
116+
)
117+
118+
try:
119+
if key == ClassKey.MANAGER:
120+
_add_abs_path_assertion(klass, "CreatePool", "devices")
121+
if key == ClassKey.POOL:
122+
_add_abs_path_assertion(klass, "InitCache", "devices")
123+
_add_abs_path_assertion(klass, "AddCacheDevs", "devices")
124+
_add_abs_path_assertion(klass, "AddDataDevs", "devices")
125+
except AttributeError as err: # pragma: no cover
126+
# This can only happen if the expected method is missing from
127+
# the XML spec or code generation has a bug, we will never
128+
# test for these conditions.
129+
raise StratisCliGenerationError(
130+
"Malformed class definition; could not access a class or "
131+
"method in the generated class definition"
132+
) from err
133+
134+
return klass
135+
136+
except DPClientGenerationError as err: # pragma: no cover
128137
raise StratisCliGenerationError(
129-
"Malformed class definition; could not access a class or "
130-
"method in the generated class definition"
138+
f"Failed to generate class {key.value.invoke_name} needed for invoking "
139+
"dbus-python methods"
131140
) from err
132141

133-
return klass
134-
135-
except DPClientGenerationError as err: # pragma: no cover
136-
raise StratisCliGenerationError(
137-
f"Failed to generate class {key.value.invoke_name} needed for invoking "
138-
"dbus-python methods"
139-
) from err
142+
assert False # pragma: no cover

src/stratis_cli/_actions/_top.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from .._stratisd_constants import ReportKey, StratisdErrors
3434
from ._connection import get_object
3535
from ._constants import TOP_OBJECT
36-
from ._dynamic import ClassKey, make_dyn_class
36+
from ._dynamic import ClassKey, Purpose, make_dyn_class
3737
from ._formatting import print_table
3838

3939

@@ -45,7 +45,7 @@ def _fetch_keylist(proxy):
4545
:rtype: list of str
4646
:raises StratisCliEngineError:
4747
"""
48-
Manager = make_dyn_class(ClassKey.MANAGER)
48+
Manager = make_dyn_class(ClassKey.MANAGER, Purpose.INVOKE)
4949

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

71-
Manager = make_dyn_class(ClassKey.MANAGER)
71+
Manager = make_dyn_class(ClassKey.MANAGER, Purpose.INVOKE)
7272

7373
if capture_key:
7474
password = getpass(prompt="Enter key data followed by the return key: ")
@@ -117,22 +117,22 @@ def get_report(namespace):
117117
"""
118118

119119
if namespace.report_name == ReportKey.MANAGED_OBJECTS.value:
120-
ObjectManager = make_dyn_class(ClassKey.OBJECT_MANAGER)
120+
ObjectManager = make_dyn_class(ClassKey.OBJECT_MANAGER, Purpose.INVOKE)
121121

122122
json_report = ObjectManager.Methods.GetManagedObjects(
123123
get_object(TOP_OBJECT), {}
124124
)
125125

126126
else:
127127
if namespace.report_name == ReportKey.ENGINE_STATE.value:
128-
Manager = make_dyn_class(ClassKey.MANAGER)
128+
Manager = make_dyn_class(ClassKey.MANAGER, Purpose.INVOKE)
129129

130130
(report, return_code, message) = Manager.Methods.EngineStateReport(
131131
get_object(TOP_OBJECT), {}
132132
)
133133

134134
else:
135-
Report = make_dyn_class(ClassKey.REPORT)
135+
Report = make_dyn_class(ClassKey.REPORT, Purpose.INVOKE)
136136

137137
(report, return_code, message) = Report.Methods.GetReport(
138138
get_object(TOP_OBJECT), {"name": namespace.report_name}
@@ -240,7 +240,7 @@ def unset_key(namespace):
240240
:raises StratisCliNoChangeError:
241241
:raises StratisCliIncoherenceError:
242242
"""
243-
Manager = make_dyn_class(ClassKey.MANAGER)
243+
Manager = make_dyn_class(ClassKey.MANAGER, Purpose.INVOKE)
244244

245245
proxy = get_object(TOP_OBJECT)
246246

0 commit comments

Comments
 (0)