powersupply: Import sublcass module only if used

This way we avoid unconditionally importing all subclass dependencies,
and make them optional based on whether the setup has devices of that
type or not.

Change-Id: I0d2f8b26364b45f0d837cc51078784d1d0fa7ea1
This commit is contained in:
Pau Espin 2020-04-17 16:53:14 +02:00
parent 4b5a09a83c
commit 2b959580b9
1 changed files with 8 additions and 14 deletions

View File

@ -50,23 +50,17 @@ class PowerSupply(log.Origin, metaclass=ABCMeta):
MainLoop.sleep(self, sleep)
self.power_set(True)
from . import powersupply_sispm, powersupply_intellinet
KNOWN_PWSUPPLY_TYPES = {
'sispm' : powersupply_sispm.PowerSupplySispm,
'intellinet' : powersupply_intellinet.PowerSupplyIntellinet,
}
def register_type(name, clazz):
"""Register a new PoerSupply child class at runtime."""
KNOWN_PWSUPPLY_TYPES[name] = clazz
def get_instance_by_type(pwsupply_type, pwsupply_opt):
"""Allocate a PowerSupply child class based on type. Opts are passed to the newly created object."""
obj = KNOWN_PWSUPPLY_TYPES.get(pwsupply_type, None)
if not obj:
if pwsupply_type == 'sispm':
from powersupply_sispm import PowerSupplySispm
obj = PowerSupplySispm
elif pwsupply_type == 'intellinet':
from powersupply_intellinet import PowerSupplyIntellinet
obj = PowerSupplyIntellinet
else:
raise log.Error('PowerSupply type not supported:', pwsupply_type)
return obj(pwsupply_opt)