File indexing completed on 2024-12-01 11:12:11

0001 # SPDX-License-Identifier: GPL-3.0-or-later
0002 # SPDX-FileCopyrightText: 2021 Anupam Basak <anupam.basak27@gmail.com>
0003 
0004 import importlib
0005 import sys
0006 
0007 from PySide2.QtCore import QUrl, Slot, QObject, Signal, Property
0008 
0009 from PicoWizard.utils.config import Config
0010 from PicoWizard.utils.logger import Logger
0011 
0012 
0013 class ModuleLoader(QObject):
0014     __modules = []
0015     _currentModuleIndex = 0
0016 
0017     log = Logger.getLogger(__name__)
0018 
0019     def __init__(self, parent=None):
0020         super().__init__(parent)
0021         ModuleLoader.log.info('Initializing ModuleLoader')
0022 
0023     @staticmethod
0024     def registerModuleTypes():
0025         modules = Config.getModules()
0026         modules.insert(0, "Welcome")
0027         modules.append("Finish")
0028 
0029         sys.path.insert(1, '/etc')
0030 
0031         importedModules = importlib.import_module('PicoWizard.modules')
0032 
0033         try:
0034             importedCustomModules = importlib.import_module('pico-wizard.custom-modules')
0035         except ModuleNotFoundError:
0036             pass
0037 
0038         for moduleName in modules:
0039             if hasattr(importedModules, moduleName):
0040                 ModuleLoader.log.info(f'Importing module {moduleName}')
0041 
0042                 cls = getattr(importedModules, moduleName)
0043                 ModuleLoader.__modules.append(cls)
0044                 cls.registerTypes()
0045             elif hasattr(importedCustomModules, moduleName):
0046                 ModuleLoader.log.info(f'Importing module {moduleName}')
0047 
0048                 cls = getattr(importedCustomModules, moduleName)
0049                 ModuleLoader.__modules.append(cls)
0050                 cls.registerTypes()
0051             else:
0052                 ModuleLoader.log.error(f"ERROR : Unknown module {moduleName}")
0053                 ModuleLoader.log.error("Exiting...")
0054                 sys.exit(1)
0055 
0056     @Slot(None, result=QUrl)
0057     def welcomeModule(self):
0058         return self.__modules[0].qmlPath()
0059 
0060     @Slot(None, result=None)
0061     def nextModule(self):
0062         if self._hasNext():
0063             self._currentModuleIndex = self._currentModuleIndex + 1
0064             self.loadModule.emit(self.__modules[self._currentModuleIndex].qmlPath())
0065 
0066             self.hasPreviousChanged.emit()
0067             self.hasNextChanged.emit()
0068 
0069     @Slot(None, result=None)
0070     def previousModule(self):
0071         if self._hasPrevious():
0072             self._currentModuleIndex = self._currentModuleIndex - 1
0073 
0074             self.hasPreviousChanged.emit()
0075             self.hasNextChanged.emit()
0076 
0077     def _hasPrevious(self):
0078         return self._currentModuleIndex > 0
0079 
0080     def _hasNext(self):
0081         return self._currentModuleIndex < len(self.__modules) - 1
0082 
0083     @Signal
0084     def hasNextChanged(self):
0085         pass
0086 
0087     @Signal
0088     def hasPreviousChanged(self):
0089         pass
0090 
0091     hasPrevious = Property(bool, _hasPrevious, notify=hasPreviousChanged)
0092     hasNext = Property(bool, _hasNext, notify=hasNextChanged)
0093 
0094     loadModule = Signal(QUrl)