From 066349a3cb4424c4a0317002cdb5918c16614491 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Tue, 22 Sep 2020 12:02:39 +0200 Subject: [PATCH 1/5] implemented base pype module class in modules --- pype/modules/__init__.py | 5 +++++ pype/modules/base.py | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 pype/modules/base.py diff --git a/pype/modules/__init__.py b/pype/modules/__init__.py index e69de29bb2..32b7426ba5 100644 --- a/pype/modules/__init__.py +++ b/pype/modules/__init__.py @@ -0,0 +1,5 @@ +from .base import PypeModule + +__all__ = ( + "PypeModule", +) diff --git a/pype/modules/base.py b/pype/modules/base.py new file mode 100644 index 0000000000..2d97bc5e14 --- /dev/null +++ b/pype/modules/base.py @@ -0,0 +1,26 @@ +from uuid import uuid4 +from pype.api import Logger + + +class PypeModule: + """Base class of pype module.""" + enabled = False + name = None + _id = None + + def __init__(self, settings): + if self.name is None: + self.name = self.__class__.__name__ + + self.log = Logger().get_logger(self.name) + + self.settings = settings.get(self.name) + self.enabled = settings.get("enabled", False) + self._id = uuid4() + + @property + def id(self): + return self._id + + def startup_environments(self): + return {} From b50438a66cebbbb1dc7767a6973ff781c5b429b4 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Tue, 22 Sep 2020 12:03:35 +0200 Subject: [PATCH 2/5] implemented basic pype modules manager which does not much do at this time --- pype/modules_manager.py | 104 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 pype/modules_manager.py diff --git a/pype/modules_manager.py b/pype/modules_manager.py new file mode 100644 index 0000000000..073b153f08 --- /dev/null +++ b/pype/modules_manager.py @@ -0,0 +1,104 @@ +import os +import sys +import inspect + +import pype.modules +from pype.modules import PypeModule +from pype.settings import system_settings +from pype.api import Logger + + +class PypeModuleManager: + skip_module_names = ("__pycache__", ) + + def __init__(self): + self.log = Logger().get_logger( + "{}.{}".format(__name__, self.__class__.__name__) + ) + + self.pype_modules = self.find_pype_modules() + + def modules_environments(self): + environments = {} + for pype_module in self.pype_modules.values(): + environments.update(pype_module.startup_environments()) + return environments + + def find_pype_modules(self): + settings = system_settings() + modules = [] + dirpath = os.path.dirname(pype.modules.__file__) + for module_name in os.listdir(dirpath): + # Check if path lead to a folder + full_path = os.path.join(dirpath, module_name) + if not os.path.isdir(full_path): + continue + + # Skip known invalid names + if module_name in self.skip_module_names: + continue + + import_name = "pype.modules.{}".format(module_name) + try: + modules.append( + __import__(import_name, fromlist=[""]) + ) + print(import_name, sys.modules.get("PyQt5")) + + except Exception: + self.log.warning( + "Couldn't import {}".format(import_name), exc_info=True + ) + + pype_module_classes = [] + for module in modules: + try: + pype_module_classes.extend( + self._classes_from_module(PypeModule, module) + ) + except Exception: + self.log.warning( + "Couldn't import {}".format(import_name), exc_info=True + ) + + pype_modules = {} + for pype_module_class in pype_module_classes: + try: + pype_module = pype_module_class(settings) + if pype_module.enabled: + pype_modules[pype_module.id] = pype_module + except Exception: + self.log.warning( + "Couldn't create instance of {}".format( + pype_module_class.__class__.__name__ + ), + exc_info=True + ) + return pype_modules + + def _classes_from_module(self, superclass, module): + classes = list() + + def recursive_bases(klass): + output = [] + output.extend(klass.__bases__) + for base in klass.__bases__: + output.extend(recursive_bases(base)) + return output + + for name in dir(module): + # It could be anything at this point + obj = getattr(module, name) + + if not inspect.isclass(obj) or not len(obj.__bases__) > 0: + continue + + # Use string comparison rather than `issubclass` + # in order to support reloading of this module. + bases = recursive_bases(obj) + if not any(base.__name__ == superclass.__name__ for base in bases): + continue + + classes.append(obj) + + return classes From d5c0fa464ae7b0fdc61367e8b14719aa972a589a Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Tue, 22 Sep 2020 14:05:36 +0200 Subject: [PATCH 3/5] removed debug prints --- pype/modules_manager.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pype/modules_manager.py b/pype/modules_manager.py index 073b153f08..4b337c075b 100644 --- a/pype/modules_manager.py +++ b/pype/modules_manager.py @@ -43,7 +43,6 @@ class PypeModuleManager: modules.append( __import__(import_name, fromlist=[""]) ) - print(import_name, sys.modules.get("PyQt5")) except Exception: self.log.warning( From 4db75b45e5c26dac46ddbf75a11676adee5d9c44 Mon Sep 17 00:00:00 2001 From: iLLiCiTiT Date: Tue, 22 Sep 2020 14:06:43 +0200 Subject: [PATCH 4/5] PypeModule inherit from abstract class --- pype/modules/base.py | 3 ++- pype/modules_manager.py | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pype/modules/base.py b/pype/modules/base.py index 2d97bc5e14..ede2f41bd5 100644 --- a/pype/modules/base.py +++ b/pype/modules/base.py @@ -1,8 +1,9 @@ from uuid import uuid4 +from abc import ABC from pype.api import Logger -class PypeModule: +class PypeModule(ABC): """Base class of pype module.""" enabled = False name = None diff --git a/pype/modules_manager.py b/pype/modules_manager.py index 4b337c075b..6538187ea9 100644 --- a/pype/modules_manager.py +++ b/pype/modules_manager.py @@ -1,5 +1,4 @@ import os -import sys import inspect import pype.modules From b4080ebbe23855a943f3b61ba9ae24deabe307bc Mon Sep 17 00:00:00 2001 From: Ondrej Samohel Date: Tue, 22 Sep 2020 16:12:25 +0200 Subject: [PATCH 5/5] docstrings and abstract method --- pype/modules/__init__.py | 1 + pype/modules/base.py | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pype/modules/__init__.py b/pype/modules/__init__.py index 32b7426ba5..aacd541e18 100644 --- a/pype/modules/__init__.py +++ b/pype/modules/__init__.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from .base import PypeModule __all__ = ( diff --git a/pype/modules/base.py b/pype/modules/base.py index ede2f41bd5..ee90aa4cbb 100644 --- a/pype/modules/base.py +++ b/pype/modules/base.py @@ -1,10 +1,19 @@ +# -*- coding: utf-8 -*- +"""Base class for Pype Modules.""" from uuid import uuid4 -from abc import ABC +from abc import ABC, abstractmethod from pype.api import Logger class PypeModule(ABC): - """Base class of pype module.""" + """Base class of pype module. + + Attributes: + id (UUID): Module id. + enabled (bool): Is module enabled. + name (str): Module name. + """ + enabled = False name = None _id = None @@ -23,5 +32,7 @@ class PypeModule(ABC): def id(self): return self._id + @abstractmethod def startup_environments(self): + """Get startup environments for module.""" return {}