Source code for gdeps.config

#!python3

# Copyright 2007-2017 Gemr. All Rights Reserved.
# Licensed to MIT see LICENSE.txt

import configparser
import os
import warnings

import gdeps as GDeps

__author__ = 'Suryavarman (http://sourceforge.net/u/suryavarman/profile/)'


[docs]class Config: """ Some class need to load some data. To do that they have to be a child of this one. """ def __init__(self, inConfigFile, inSectionName=''): """ :param inConfigFile: Config file instance :type inConfigFile: GDeps.ConfigFile :param inSectionName: The section name associate with this object. :type inSectionName: str """ self.m_ConfigFile = inConfigFile self.m_SectionName = inSectionName
[docs] def read(self, inSection): pass
[docs] def write(self): return {}
# call this function at end of child __init__ function
[docs] def Load(self): if len(self.m_SectionName) > 0: self.m_ConfigFile.sectionRead(self)
[docs]class ConfigFile(configparser.ConfigParser): """ Each object with a Config has to be link with a ConfigFile. @link https://docs.python.org/3.4/library/configparser.html """ def __init__(self, inFilePath=''): configparser.ConfigParser.__init__(self) self.m_FilePath = inFilePath self.m_Includes = dict() """ { key : ConfigFile } The includes have to be write in the config file with a relative filepath from the directory of self.m_FilePath. They will be store in m_Includes with a global path """ self.m_SetionDirectories = dict() """ if a section has a location into a another include, you have to add this one in this dictionary { section name : include key } include key € m_Includes """ aFileExist = os.path.exists(self.m_FilePath) if not aFileExist: warnings.warn("The config file doesn't exist : " + self.m_FilePath + " .", ResourceWarning) self.read(self.m_FilePath) for aScetionName in self.sections(): """ find all the includes """ aIncludes = self.getIncludes(aScetionName) for aIncludeKey, aIncludePath in aIncludes.items(): if not os.path.isabs(aIncludePath): aIncludePath = os.path.normpath(os.path.dirname(self.m_FilePath)) + '/' + aIncludePath aIncludePath = os.path.normpath(aIncludePath) else: warnings.warn("The include filename is empty. Please check the config file : " + self.m_FilePath + " .", ResourceWarning) self.m_Includes[aIncludeKey] = ConfigFile(aIncludePath) if (not self.m_Includes) and (not self.sections()): warnings.warn("The config file is empty or invalid. Please check the config file : " + self.m_FilePath + " .", ResourceWarning)
[docs] def sectionRead(self, inConfig): """ Read from the section name from file config """ aSection = self.getSection(inConfig.m_SectionName) if not aSection: aLoadingSection = "The loading sections are : " + ", ".join(str(x) for x in self.sections()).join('[]') assert False, "Section not found. Section Name : " + inConfig.m_SectionName + " . " + aLoadingSection else: inConfig.read(aSection)
[docs] def sectionWrite(self, inConfig): self[inConfig.m_SectionName] = inConfig.write() with open(self.m_FilePath, 'w') as aFile: self.write(aFile)
[docs] def getIncludes(self, inSectionName): """ return a dictionary with all the pair{ key : items } into a section with the prefix GDeps.Keys.ms_IncludePrefix """ aIncludes = dict() for aKey in self[inSectionName]: if GDeps.Keys.ms_IncludePrefix in aKey: aIncludes[aKey] = self[inSectionName][aKey] # set the path return aIncludes
[docs] def getSection(self, inSectionName): """ carefully with the full cycle inclusion. There are no security for that. """ if inSectionName in self: return self[inSectionName] else: for aKey, aConfigFile in self.m_Includes.items(): aResult = aConfigFile.getSection(inSectionName) if aResult: self.m_SetionDirectories[inSectionName] = aKey return aResult return {}
[docs]class Alias(Config): def __init__(self, inConfigFile, inSectionName): Config.__init__(self, inConfigFile, inSectionName) if self.m_SectionName: self.m_ConfigFile.sectionRead(self)
[docs] def read(self, inSection): self.m_Type = inSection["type"] self.m_Name = inSection["name"] self.m_SubAlias = Alias.getSubAlias(inSection)
# return aSection
[docs] def write(self): aSection = Config.write(self) aSection["type"] = self.m_Type aSection["name"] = self.m_Name # write sub alias return aSection
@staticmethod
[docs] def getSubAlias(inSection, inPrefix="alias"): """ return all the items into a section with the prefix inPrefix""" return [aValue for aKey, aValue in inSection.items() if inPrefix in aKey]