Skip to content

Commit 856eaad

Browse files
authored
[sonic-host-server] Simplify register_dbus (#35)
The goal is to prevent use importlib.import_module function. Verified by manual test. Signed-off-by: maipbui <[email protected]>
1 parent 13d9b62 commit 856eaad

File tree

5 files changed

+11
-41
lines changed

5 files changed

+11
-41
lines changed

host_modules/config_engine.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,3 @@ def save(self, config_file):
4646
break
4747
return result.returncode, msg
4848

49-
def register():
50-
"""Return class and module name"""
51-
return Config, MOD_NAME
52-

host_modules/gcu.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,3 @@ def delete_checkpoint(self, checkpoint_file):
6969
break
7070
return result.returncode, msg
7171

72-
def register():
73-
"""Return class and module name"""
74-
return GCU, MOD_NAME
75-

host_modules/host_service.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,3 @@ def __init__(self, mod_name):
3030
self.bus_name = dbus.service.BusName(bus_name(mod_name), self.bus)
3131
super(HostModule, self).__init__(self.bus_name, bus_path(mod_name))
3232

33-
def register():
34-
return HostService, "host_service"

host_modules/showtech.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,3 @@ def info(self, date):
4444
output_filename = output_file_match.group()
4545
return result.returncode, output_filename
4646

47-
def register():
48-
"""Return the class name"""
49-
return Showtech, MOD_NAME
50-

scripts/sonic-host-server

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,19 @@ import dbus.service
1212
import dbus.mainloop.glib
1313

1414
from gi.repository import GObject
15+
from host_modules import config_engine, gcu, host_service, showtech
1516

16-
def find_module_path():
17-
"""Find path for host_moduels"""
18-
try:
19-
from host_modules import host_service
20-
return os.path.dirname(host_service.__file__)
21-
except ImportError as e:
22-
return None
2317

24-
def register_modules(mod_path):
25-
"""Register all host modules"""
26-
sys.path.append(mod_path)
27-
for mod_file in glob.glob(os.path.join(mod_path, '*.py')):
28-
if os.path.isfile(mod_file) and not mod_file.endswith('__init__.py'):
29-
mod_name = os.path.basename(mod_file)[:-3]
30-
module = importlib.import_module(mod_name)
31-
32-
register_cb = getattr(module, 'register', None)
33-
if not register_cb:
34-
raise Exception('Missing register function for ' + mod_name)
35-
36-
register_dbus(register_cb)
37-
38-
def register_dbus(register_cb):
18+
def register_dbus():
3919
"""Register DBus handlers for individual modules"""
40-
handler_class, mod_name = register_cb()
41-
handlers[mod_name] = handler_class(mod_name)
20+
mod_dict = {
21+
'config': config_engine.Config('config'),
22+
'gcu': gcu.GCU('gcu'),
23+
'host_service': host_service.HostService('host_service'),
24+
'showtech': showtech.Showtech('showtech')
25+
}
26+
for mod_name, handler_class in mod_dict.items():
27+
handlers[mod_name] = handler_class
4228

4329
# Create a main loop reactor
4430
GObject.threads_init()
@@ -69,9 +55,7 @@ class SignalManager(object):
6955
loop.quit()
7056

7157
sigmgr = SignalManager()
72-
mod_path = find_module_path()
73-
if mod_path is not None:
74-
register_modules(mod_path)
58+
register_dbus()
7559

7660
# Only run if we actually have some handlers
7761
if handlers:

0 commit comments

Comments
 (0)