From 2cad4fc204e6fb7127ac7b07ec0aece19fed769b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Rossigneux?= Date: Thu, 31 Jan 2013 17:12:42 +0100 Subject: [PATCH] Dynamic imports of drivers classes. Change-Id: I10274bf9bada30f0fbd4ed27f367454c09dad5c2 --- kwapi/drivers/__init__.py | 18 ------------------ kwapi/drivers/driver_manager.py | 13 +++++++------ 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/kwapi/drivers/__init__.py b/kwapi/drivers/__init__.py index 74cbc34..e69de29 100644 --- a/kwapi/drivers/__init__.py +++ b/kwapi/drivers/__init__.py @@ -1,18 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Author: François Rossigneux -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from wattsup import Wattsup -from dummy import Dummy diff --git a/kwapi/drivers/driver_manager.py b/kwapi/drivers/driver_manager.py index 3f70cbc..fcafb32 100644 --- a/kwapi/drivers/driver_manager.py +++ b/kwapi/drivers/driver_manager.py @@ -17,7 +17,6 @@ """Loads and checks driver threads.""" import ast -import sys import signal import thread from threading import Timer @@ -61,17 +60,19 @@ def load_all_drivers(): def load_driver(class_name, probe_ids, kwargs): """Starts a probe thread.""" try: - probeClass = getattr(sys.modules['kwapi.drivers'], class_name) - except AttributeError: + module = __import__('kwapi.drivers.' + class_name.lower(), + fromlist=class_name) + probe_class = getattr(module, class_name) + except ImportError: raise NameError("%s doesn't exist." % class_name) try: - probeObject = probeClass(probe_ids, **kwargs) + probe_object = probe_class(probe_ids, **kwargs) except Exception as exception: LOG.error('Exception occurred while initializing %s(%s, %s): %s' % (class_name, probe_ids, kwargs, exception)) else: - probeObject.start() - return probeObject + probe_object.start() + return probe_object def check_drivers_alive():