From 07b0b00b902c51beea029c5c4889e786441adc28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Thu, 10 Mar 2022 21:05:59 +0100 Subject: [PATCH] Done refactoring module loader --- server/src/server/settings.py.sample | 4 ++-- server/src/uds/REST/__init__.py | 4 ---- server/src/uds/plugins/__init__.py | 20 +++++------------ server/src/uds/reports/__init__.py | 8 ++----- server/src/uds/services/__init__.py | 33 +++++++++------------------- 5 files changed, 20 insertions(+), 49 deletions(-) diff --git a/server/src/server/settings.py.sample b/server/src/server/settings.py.sample index 916993aee..147451b5f 100644 --- a/server/src/server/settings.py.sample +++ b/server/src/server/settings.py.sample @@ -134,8 +134,8 @@ CACHES = { # 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', # } 'memory': { - 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', - 'LOCATION': '127.0.0.1:11211', + 'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', + 'LOCATION': 'db.dkmon.com:11211', }, } diff --git a/server/src/uds/REST/__init__.py b/server/src/uds/REST/__init__.py index 607a788ce..877765758 100644 --- a/server/src/uds/REST/__init__.py +++ b/server/src/uds/REST/__init__.py @@ -29,11 +29,7 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import importlib import logging -import os.path -import pkgutil -import sys import typing from django import http diff --git a/server/src/uds/plugins/__init__.py b/server/src/uds/plugins/__init__.py index c0308e71e..1ad9d49f2 100644 --- a/server/src/uds/plugins/__init__.py +++ b/server/src/uds/plugins/__init__.py @@ -27,29 +27,21 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import os.path -import pkgutil -import sys -import importlib import logging -import typing + +from uds.core.util import modfinder logger = logging.getLogger(__name__) -def loadPlugins(): +def __loadPlugins(): """ This imports all packages that are descendant of this package, and, after that, it register all subclases of service provider as """ logger.debug('Initializing plugins...') - # Dinamycally import children of this package. The __init__.py files must import classes - pkgpath = os.path.dirname(typing.cast(str, sys.modules[__name__].__file__)) - for _, name, _ in pkgutil.iter_modules([pkgpath]): - # __import__(name, globals(), locals(), [], 1) - importlib.import_module('.' + name, __name__) # Local import + # Load all modules + modfinder.importModules(__name__) - importlib.invalidate_caches() - -loadPlugins() +__loadPlugins() diff --git a/server/src/uds/reports/__init__.py b/server/src/uds/reports/__init__.py index ed7becdf0..7a725c496 100644 --- a/server/src/uds/reports/__init__.py +++ b/server/src/uds/reports/__init__.py @@ -38,10 +38,6 @@ The registration of modules is done locating subclases of :py:class:`uds.core.re .. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com """ -import os.path -import pkgutil -import importlib -import sys import logging import typing @@ -53,7 +49,7 @@ logger = logging.getLogger(__name__) availableReports: typing.List[typing.Type['reports.Report']] = [] -def __init__() -> None: +def __loadModules() -> None: """ This imports all packages that are descendant of this package, and, after that, """ @@ -69,4 +65,4 @@ def __init__() -> None: checker=lambda x: x.uuid is not None and x.uuid not in alreadyAdded, ) -__init__() +__loadModules() diff --git a/server/src/uds/services/__init__.py b/server/src/uds/services/__init__.py index e64070d87..6cd45f868 100644 --- a/server/src/uds/services/__init__.py +++ b/server/src/uds/services/__init__.py @@ -41,37 +41,24 @@ The registration of modules is done locating subclases of :py:class:`uds.core.au .. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com """ -# pylint: disable=maybe-no-member -import os.path -import pkgutil -import sys -import importlib import logging -import typing + +from uds.core.util import modfinder logger = logging.getLogger(__name__) -def __init__(): +def __loadModules(): """ This imports all packages that are descendant of this package, and, after that, it register all subclases of service provider as """ from uds.core import services + + modfinder.dynamicLoadAndRegisterModules( + services.factory(), + services.ServiceProvider, + __name__ + ) - # Dinamycally import children of this package. - pkgpath = os.path.dirname(typing.cast(str, sys.modules[__name__].__file__)) - for _, name, _ in pkgutil.iter_modules([pkgpath]): - # __import__('uds.services.' + name, globals(), locals(), []) - importlib.import_module('.' + name, __name__) # import module - - importlib.invalidate_caches() - - for p in [services.ServiceProvider]: - # This is marked as error in IDE, but it's not (__subclasses__) - for cls in p.__subclasses__(): - # Skip ClusteredServiceProvider - services.factory().insert(cls) - - -__init__() +__loadModules()