Skip to content

Commit fae44dc

Browse files
committedJan 13, 2025·
refactor(client): use dynamic wrappers for remote calls
Implement __getattr__ in the client module to check if the remote MultiTenantDeviceAccess has the requested attribute and if it is a callable: a wrapper is then created to pass a session to the remote instance. Static wrappers are removed from the client module. Signed-off-by: Cedric Hombourger <[email protected]>
1 parent 707e2af commit fae44dc

File tree

1 file changed

+10
-139
lines changed

1 file changed

+10
-139
lines changed
 

‎mtda/client.py

+10-139
Original file line numberDiff line numberDiff line change
@@ -62,108 +62,42 @@ def __init__(self, host=None, session=None, config_files=None,
6262
else:
6363
self._session = session
6464

65-
def agent_version(self):
66-
return self._impl.agent_version()
67-
68-
def config_set_power_timeout(self, timeout):
69-
return self._impl.config_set_power_timeout(timeout, self._session)
65+
def __getattr__(self, name):
66+
if self._impl is None:
67+
return None
7068

71-
def config_set_session_timeout(self, timeout):
72-
return self._impl.config_set_session_timeout(timeout, self._session)
69+
attr = getattr(self._impl, name)
70+
if self._session and callable(attr):
71+
def wrapper(*args, **kwargs):
72+
kwargs['session'] = self._session
73+
return attr(*args, **kwargs)
74+
return wrapper
75+
return attr
7376

7477
def console_prefix_key(self):
7578
return self._agent.console_prefix_key()
7679

77-
def command(self, args):
78-
return self._impl.command(args, self._session)
79-
80-
def console_clear(self):
81-
return self._impl.console_clear(self._session)
82-
83-
def console_dump(self):
84-
return self._impl.console_dump(self._session)
85-
86-
def console_flush(self):
87-
return self._impl.console_flush(self._session)
88-
8980
def console_getkey(self):
9081
return self._agent.console_getkey()
9182

9283
def console_init(self):
9384
return self._agent.console_init()
9485

95-
def console_head(self):
96-
return self._impl.console_head(self._session)
97-
98-
def console_lines(self):
99-
return self._impl.console_lines(self._session)
100-
101-
def console_locked(self):
102-
return self._impl.console_locked(self._session)
103-
104-
def console_print(self, data):
105-
return self._impl.console_print(data, self._session)
106-
107-
def console_prompt(self, newPrompt=None):
108-
return self._impl.console_prompt(newPrompt, self._session)
109-
11086
def console_remote(self, host, screen):
11187
return self._agent.console_remote(host, screen)
11288

113-
def console_run(self, cmd):
114-
return self._impl.console_run(cmd, self._session)
115-
116-
def console_send(self, data, raw=False):
117-
return self._impl.console_send(data, raw, self._session)
118-
11989
def console_toggle(self):
12090
return self._agent.console_toggle(self._session)
12191

122-
def console_tail(self):
123-
return self._impl.console_tail(self._session)
124-
125-
def console_wait(self, what, timeout=None):
126-
return self._impl.console_wait(what, timeout, self._session)
127-
128-
def env_get(self, name):
129-
return self._impl.env_get(name, self._session)
130-
131-
def env_set(self, name, value):
132-
return self._impl.env_set(name, value, self._session)
133-
134-
def keyboard_write(self, data):
135-
return self._impl.keyboard_write(data, self._session)
136-
13792
def monitor_remote(self, host, screen):
13893
return self._agent.monitor_remote(host, screen)
13994

140-
def monitor_send(self, data, raw=False):
141-
return self._impl.monitor_send(data, raw, self._session)
142-
143-
def monitor_wait(self, what, timeout=None):
144-
return self._impl.monitor_wait(what, timeout, self._session)
145-
14695
def pastebin_api_key(self):
14796
return self._agent.pastebin_api_key()
14897

14998
def pastebin_endpoint(self):
15099
return self._agent.pastebin_endpoint()
151100

152-
def power_locked(self):
153-
return self._impl.power_locked(self._session)
154-
155-
def storage_bytes_written(self):
156-
return self._impl.storage_bytes_written(self._session)
157-
158-
def storage_close(self):
159-
return self._impl.storage_close(self._session)
160-
161-
def storage_locked(self):
162-
return self._impl.storage_locked(self._session)
163-
164-
def storage_mount(self, part=None):
165-
return self._impl.storage_mount(part, self._session)
166-
167101
def storage_network(self, remote):
168102
cmd = '/usr/sbin/nbd-client'
169103
if os.path.exists(cmd) is False:
@@ -190,9 +124,6 @@ def storage_open(self):
190124
else:
191125
raise
192126

193-
def storage_status(self):
194-
return self._impl.storage_status(self._session)
195-
196127
def storage_update(self, dest, src=None, callback=None):
197128
path = dest if src is None else src
198129
try:
@@ -315,15 +246,6 @@ def parseBmap(self, bmap, bmap_path):
315246
return None
316247
return bmapDict
317248

318-
def storage_to_host(self):
319-
return self._impl.storage_to_host(self._session)
320-
321-
def storage_to_target(self):
322-
return self._impl.storage_to_target(self._session)
323-
324-
def storage_swap(self):
325-
return self._impl.storage_swap(self._session)
326-
327249
def start(self):
328250
return self._agent.start()
329251

@@ -349,57 +271,6 @@ def target_lock(self, retries=0):
349271
time.sleep(60)
350272
return status
351273

352-
def target_locked(self):
353-
return self._impl.target_locked(self._session)
354-
355-
def target_off(self):
356-
return self._impl.target_off(self._session)
357-
358-
def target_on(self):
359-
return self._impl.target_on(self._session)
360-
361-
def target_status(self):
362-
return self._impl.target_status(self._session)
363-
364-
def target_toggle(self):
365-
return self._impl.target_toggle(self._session)
366-
367-
def target_unlock(self):
368-
return self._impl.target_unlock(self._session)
369-
370-
def target_uptime(self):
371-
return self._impl.target_uptime(self._session)
372-
373-
def toggle_timestamps(self):
374-
return self._impl.toggle_timestamps()
375-
376-
def usb_find_by_class(self, className):
377-
return self._impl.usb_find_by_class(className, self._session)
378-
379-
def usb_has_class(self, className):
380-
return self._impl.usb_has_class(className, self._session)
381-
382-
def usb_off(self, ndx):
383-
return self._impl.usb_off(ndx, self._session)
384-
385-
def usb_off_by_class(self, className):
386-
return self._impl.usb_off_by_class(className, self._session)
387-
388-
def usb_on(self, ndx):
389-
return self._impl.usb_on(ndx, self._session)
390-
391-
def usb_on_by_class(self, className):
392-
return self._impl.usb_on_by_class(className, self._session)
393-
394-
def usb_ports(self):
395-
return self._impl.usb_ports(self._session)
396-
397-
def usb_status(self, ndx):
398-
return self._impl.usb_status(ndx, self._session)
399-
400-
def usb_toggle(self, ndx):
401-
return self._impl.usb_toggle(ndx, self._session)
402-
403274
def version(self):
404275
return self._agent.version
405276

0 commit comments

Comments
 (0)
Please sign in to comment.