Skip to content

Commit bd5d69c

Browse files
author
Martin Belanger
committed
python bindings: redesign Phase 2 — API surface polish
- Convert connected() and is_registration_supported() to read-only Python properties (connected, registration_supported) - Delete set_symname(); host.hostsymname is already writable via the generated SWIG field descriptor - Rename registration_ctrl() to registration_control() to reflect that the method is a multi-action control interface (register, deregister, update via NVMF_DIM_TAS_*), not a single register operation - Update test-objects.py to match the new API Signed-off-by: Martin Belanger <[email protected]> Assisted-by: Claude Sonnet <[email protected]>
1 parent 84dd0d9 commit bd5d69c

2 files changed

Lines changed: 40 additions & 23 deletions

File tree

libnvme/libnvme/nvme.i

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -887,12 +887,6 @@ struct libnvme_ns * libnvme_ctrl_next_ns(struct libnvme_ctrl * c, struct libnvme
887887
}
888888

889889

890-
%define SET_SYMNAME_DOCSTRING
891-
"Set or clear the host's symbolic name.
892-
893-
Args:
894-
hostsymname: Symbolic name string, or None to clear it."
895-
%enddef
896890

897891
%pythonappend libnvme_host::libnvme_host(struct libnvme_global_ctx *ctx,
898892
const char *hostnqn,
@@ -938,11 +932,6 @@ Args:
938932
struct libnvme_host* __exit__(PyObject *type, PyObject *value, PyObject *traceback) {
939933
return $self;
940934
}
941-
%feature("autodoc", SET_SYMNAME_DOCSTRING) set_symname;
942-
void set_symname(const char *hostsymname) {
943-
libnvme_host_set_hostsymname($self, hostsymname);
944-
}
945-
946935
PyObject* __str__() {
947936
return PyUnicode_FromFormat("nvme.Host(%s,%s)", STR_OR_NONE($self->hostnqn), STR_OR_NONE($self->hostid));
948937
}
@@ -1161,10 +1150,6 @@ Args:
11611150
return;
11621151
}
11631152
}
1164-
%feature("autodoc", "Return True if this controller is currently connected.") connected;
1165-
bool connected() {
1166-
return libnvme_ctrl_get_name($self) != NULL;
1167-
}
11681153
%feature("autodoc", "Rescan this controller and refresh its namespace list.") rescan;
11691154
void rescan() {
11701155
libnvme_rescan_ctrl($self);
@@ -1190,16 +1175,28 @@ Args:
11901175
connect_err = 2;
11911176
}
11921177

1193-
%feature("autodoc", "Return True if this controller supports explicit host registration.") is_registration_supported;
1194-
bool is_registration_supported() {
1178+
bool _registration_supported() {
11951179
return libnvmf_is_registration_supported($self);
11961180
}
1181+
bool _connected() {
1182+
return libnvme_ctrl_get_name($self) != NULL;
1183+
}
1184+
%pythoncode %{
1185+
@property
1186+
def connected(self):
1187+
"""True if this controller is currently connected."""
1188+
return self._connected()
1189+
@property
1190+
def registration_supported(self):
1191+
"""True if this controller supports explicit host registration."""
1192+
return self._registration_supported()
1193+
%}
11971194

11981195
%feature("autodoc", "Register this controller with the NVMe-oF DIM service.\n"
11991196
"\n"
12001197
"Returns:\n"
1201-
" None on success, or an error string describing the failure.") registration_ctrl;
1202-
PyObject *registration_ctrl(enum nvmf_dim_tas tas) {
1198+
" None on success, or an error string describing the failure.") registration_control;
1199+
PyObject *registration_control(enum nvmf_dim_tas tas) {
12031200
__u32 result;
12041201
int status;
12051202

libnvme/libnvme/tests/test-objects.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ def test_context_manager(self):
3434
with nvme.GlobalCtx() as ctx:
3535
self.assertIsNotNone(ctx)
3636

37+
def test_context_manager_methods_accessible(self):
38+
with nvme.GlobalCtx() as ctx:
39+
hosts = list(ctx.hosts())
40+
self.assertIsInstance(hosts, list)
41+
3742
def test_hosts_iterator_returns_list(self):
3843
ctx = nvme.GlobalCtx()
3944
hosts = list(ctx.hosts())
@@ -82,10 +87,10 @@ def test_creation_with_hostsymname(self):
8287
host = nvme.Host(self.ctx, hostnqn=hostnqn, hostsymname=symname)
8388
self.assertEqual(host.hostsymname, symname)
8489

85-
def test_set_symname(self):
90+
def test_set_hostsymname(self):
8691
hostnqn = 'nqn.2014-08.com.example:test-host-set-symname'
8792
host = nvme.Host(self.ctx, hostnqn=hostnqn)
88-
host.set_symname('updated-symname')
93+
host.hostsymname = 'updated-symname'
8994
self.assertEqual(host.hostsymname, 'updated-symname')
9095

9196
def test_dhchap_host_key_is_none_by_default(self):
@@ -106,6 +111,12 @@ def test_context_manager(self):
106111
with nvme.Host(self.ctx) as h:
107112
self.assertIsNotNone(h)
108113

114+
def test_context_manager_properties_accessible(self):
115+
hostnqn = 'nqn.2014-08.com.example:test-host-ctx-mgr'
116+
with nvme.Host(self.ctx, hostnqn=hostnqn) as h:
117+
self.assertEqual(h.hostnqn, hostnqn)
118+
self.assertIsNone(h.dhchap_host_key)
119+
109120

110121
class TestCtrl(unittest.TestCase):
111122

@@ -163,7 +174,7 @@ def test_trsvcid_property(self):
163174

164175
def test_connected_returns_false_before_connect(self):
165176
ctrl = self._make_loop_ctrl()
166-
self.assertFalse(ctrl.connected())
177+
self.assertFalse(ctrl.connected)
167178

168179
def test_name_is_none_before_connect(self):
169180
ctrl = self._make_loop_ctrl()
@@ -181,6 +192,15 @@ def test_context_manager(self):
181192
}) as c:
182193
self.assertIsNotNone(c)
183194

195+
def test_context_manager_properties_accessible(self):
196+
with nvme.Ctrl(self.ctx, {
197+
'subsysnqn': self.subsysnqn,
198+
'transport': 'loop',
199+
}) as c:
200+
self.assertEqual(c.transport, 'loop')
201+
self.assertEqual(c.subsysnqn, self.subsysnqn)
202+
self.assertFalse(c.connected)
203+
184204
def test_namespaces_iterator_returns_list(self):
185205
ctrl = self._make_loop_ctrl()
186206
nss = list(ctrl.namespaces())
@@ -216,7 +236,7 @@ def test_multiple_ctrls_same_ctx(self):
216236
ctrls = [self._make_loop_ctrl() for _ in range(5)]
217237
self.assertEqual(len(ctrls), 5)
218238
for c in ctrls:
219-
self.assertFalse(c.connected())
239+
self.assertFalse(c.connected)
220240

221241

222242
class TestCtrlErrorHandling(unittest.TestCase):

0 commit comments

Comments
 (0)