From 12118606ec3afbbfb1b27186a38d398c9be06520 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Thu, 1 Dec 2022 01:43:46 +0200 Subject: [PATCH] Separate creating the Inventory instance from singleton --- src/inventory.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/inventory.py b/src/inventory.py index 985f1382..e6d4a24c 100644 --- a/src/inventory.py +++ b/src/inventory.py @@ -7,6 +7,16 @@ from bmconfigparser import config from singleton import Singleton +def create_inventory_instance(backend="sqlite"): + """ + Create an instance of the inventory class + defined in `storage.`. + """ + return getattr( + getattr(storage, backend), + "{}Inventory".format(backend.title()))() + + @Singleton class Inventory(): """ @@ -15,11 +25,7 @@ class Inventory(): """ def __init__(self): self._moduleName = config.safeGet("inventory", "storage") - self._inventoryClass = getattr( - getattr(storage, self._moduleName), - "{}Inventory".format(self._moduleName.title()) - ) - self._realInventory = self._inventoryClass() + self._realInventory = create_inventory_instance(self._moduleName) self.numberOfInventoryLookupsPerformed = 0 # cheap inheritance copied from asyncore