File indexing completed on 2024-05-12 05:35:10

0001 # SPDX-License-Identifier: GPL-3.0-or-later
0002 # SPDX-FileCopyrightText: 2021 Anupam Basak <anupam.basak27@gmail.com>
0003 
0004 import sys
0005 import configparser
0006 import pathlib
0007 from . import constants as conf
0008 from PicoWizard.utils.logger import Logger
0009 
0010 
0011 class Config:
0012     # TODO: Check if this should be singleton class or static implementation is fine
0013 
0014     log = Logger.getLogger(__name__)
0015     config = None
0016 
0017     @staticmethod
0018     def __getConfig__(config_section, config_name, fallback=''):
0019         if Config.config is None:
0020             Config.config = configparser.ConfigParser()
0021             
0022             # Read main config from /etc/pico-wizard/pico-wizard.conf
0023             read_files = Config.config.read(conf.CONFIG_FILE_PATH)
0024             
0025             if len(read_files) == 0:
0026                 Config.log.error('Could not find or open config file ' + conf.CONFIG_FILE_PATH)
0027                 sys.exit(1)
0028             else:
0029                 # Main config file read successfully.
0030                 # Read all custom configs from /etc/pico-wizard/pico-wizard.conf.d/*.conf (if any) and override values
0031                 Config.config.read(sorted(pathlib.Path(conf.CONFIG_FILE_PATH+".d").glob("*.conf")))
0032                 
0033         return Config.config[config_section].get(config_name, fallback=fallback)
0034 
0035     @staticmethod
0036     def getModules():
0037         modules_value = Config.__getConfig__('GENERAL', 'MODULES')
0038         modules = [x.strip() for x in modules_value.split(",") if len(x) > 0]
0039 
0040         return modules
0041 
0042     @staticmethod
0043     def getPasswordType():
0044         passwordType = Config.__getConfig__('GENERAL', 'PASSWORD_TYPE', 'alphanumeric')
0045 
0046         return passwordType
0047 
0048     @staticmethod
0049     def getLogLevel():
0050         logLevel = Config.__getConfig__('GENERAL', 'LOGLEVEL', 'info')
0051         print(f"loglevel: {logLevel.upper()}")
0052 
0053         return logLevel.upper()