From a00bc520df4f6e41176347004eccf64f6ab4b490 Mon Sep 17 00:00:00 2001 From: Peter Portante Date: Fri, 28 Apr 2017 10:07:22 -0400 Subject: [PATCH] Here is one example of abstaction hiding This examples uses a "proxy" class. It is specifically not functional as I left a bug in it. Can you see how it hides the mechanism from the caller while reducing the line count. Change-Id: Ia0738bfe8b0450f6da8f2bf88d436fc738033e8f --- lib/Quads.py | 27 +++------------- lib/hardware_services/inventory_service.py | 36 ++++++++++----------- lib/hardware_services/network_service.py | 37 ++++++++++------------ 3 files changed, 36 insertions(+), 64 deletions(-) diff --git a/lib/Quads.py b/lib/Quads.py index dda600dcb..b91dc3f87 100755 --- a/lib/Quads.py +++ b/lib/Quads.py @@ -28,10 +28,8 @@ import urllib import json from subprocess import check_call -from hardware_services.inventory_service import get_inventory_service, set_inventory_service -from hardware_services.network_service import get_network_service, set_network_service -sys.path.append(os.path.dirname(__file__) + "/hardware_services/inventory_drivers/") -sys.path.append(os.path.dirname(__file__) + "/hardware_services/network_drivers/") +from hardware_services.inventory_service import InventoryServiceProxy +from hardware_services.network_service import NetworkServiceProxy class Quads(object): @@ -47,21 +45,8 @@ def __init__(self, config, statedir, movecommand, datearg, syncstate, initialize self.logger = logging.getLogger("quads.Quads") self.logger.setLevel(logging.DEBUG) - #EC528 addition - dynamically import driver module and set inventory and network services - inventoryservice = hardwareservice + "InventoryDriver" - networkservice = hardwareservice + "NetworkDriver" - - importlib.import_module(inventoryservice) - importlib.import_module(networkservice) - - set_inventory_service(getattr(sys.modules[inventoryservice], inventoryservice)()) - set_network_service(getattr(sys.modules[networkservice], networkservice)()) - - self.inventory_service = get_inventory_service() - self.network_service = get_network_service() - - self.hardware_service_url = hardwareserviceurl - + self.inventory_service = InventoryServiceProxy(hardwareservice, hardwarewserviceurl) + self.network_service = NetworkServiceProxy(hardwareservice, hardwareserviceurl) self.inventory_service.load_data(self, force, initialize) @@ -616,7 +601,3 @@ def quads_get(self, url, params=None): @classmethod def quads_delete(self, url): self.quads_status_code_check(requests.delete(url)) - - - - diff --git a/lib/hardware_services/inventory_service.py b/lib/hardware_services/inventory_service.py index 78379e740..9c1b02fc7 100755 --- a/lib/hardware_services/inventory_service.py +++ b/lib/hardware_services/inventory_service.py @@ -32,9 +32,24 @@ # will be able to continue with minimal changes (only thing that this will change is where QUADS pulls its data from) ##################################################################################################################### -import sys +import os, sys, importlib from abc import ABCMeta, abstractmethod + +sys.path.append(os.path.dirname(__file__) + "/hardware_services/inventory_drivers/") + + +class InventoryServiceProxy(object): + def __init__(self, name): + self.name = name + inventoryservice = name + "InventoryDriver" + importlib.import_module() + self.invsrv = getattr(sys.modules[inventoryservice], inventoryservice)() + + def __getattr__(self, attr_name): + return getattr(self.invsrv, attr_name) + + class InventoryService(object): __metaclass__ = ABCMeta @@ -88,22 +103,3 @@ def sync_state(self, quads): def write_data(self, quads, doexit): """ TODO add documentation """ - - -_inventory_service = None - - -def set_inventory_service(inventory_service): - - global _inventory_service - if _inventory_service is not None: - sys.exit("Error: _inventory_service already set") - - _inventory_service = inventory_service - - -def get_inventory_service(): - return _inventory_service - - - diff --git a/lib/hardware_services/network_service.py b/lib/hardware_services/network_service.py index 404bd6d86..3168487a8 100755 --- a/lib/hardware_services/network_service.py +++ b/lib/hardware_services/network_service.py @@ -32,34 +32,29 @@ # will be able to continue with minimal changes (only thing that this will change is where QUADS pulls its data from) ##################################################################################################################### -import sys +import os, sys, importlib from abc import ABCMeta, abstractmethod -class NetworkService(object): - - __metaclass__ = ABCMeta - - - @abstractmethod - def move_hosts(self, **kwargs): - """ TODO add documentation - """ +sys.path.append(os.path.dirname(__file__) + "/hardware_services/network_drivers/") -_network_service = None +class NetworkServiceProxy(object): + def __init__(self, name): + self.name = name + networkservice = name + "NetworkDriver" + importlib.import_module() + self.invsrv = getattr(sys.modules[networkservice], networkservice)() -def set_network_service(network_service): + def __getattr__(self, attr_name): + return getattr(self.netsrv, attr_name) - global _network_service - if _network_service is not None: - sys.exit("Error: _network_service already set") - - _network_service = network_service - - -def get_network_service(): - return _network_service +class NetworkService(object): + __metaclass__ = ABCMeta + @abstractmethod + def move_hosts(self, **kwargs): + """ TODO add documentation + """