#!/usr/bin/python # -*- coding: GBK -*- # # @todo: # # @author: Alee # @date 2018-1-3 ÏÂÎç05:52:47 # @version 1.0 # # @note: # #--------------------------------------------------------------------- import ConfigParser import logging class AppConfig(): def __init__(self, filePath): self.config = ConfigParser.ConfigParser() self.config.read(filePath) def SetSection(self, sectionName ): self.sectionName = sectionName def GetValue(self, key): return self.config.get(self.sectionName,key) def GetIntValue(self, key): return self.config.getint(self.sectionName,key) class GameConfig(): def __init__(self, filePath): config = AppConfig( filePath ) logging.debug( "Config file:%s", filePath ) config.SetSection( "ServerConfig" ) self.ip = config.GetValue( "IP" ) self.port = config.GetIntValue( "Port" ) config.SetSection( "VersionConfig" ) self.ClientVersion = config.GetValue( "ClientVersion" ) self.VersionNO = config.GetIntValue( "VERSION_NO" ) self.StartCnt = config.GetIntValue( "PACK_START_COUNT" ) self.KeyName = config.GetValue( "Key" ) keylen = len(self.KeyName)-1 self.KeyName = self.KeyName[1:keylen] self.SendKeyDictPath = config.GetValue( "SendKeyDictFile" ) config.SetSection( "MapDataConfig" ) self.MapFilePath = config.GetValue( "MapFilePath" ) self.MapPostFix = config.GetValue( "MapPostFix" ) config.SetSection( "ThreadNumberControl" ) self.IOThreadNum = config.GetIntValue( "IOWorkerThreadNum" ) self.AIThreadNum = config.GetIntValue( "AIWorkerThreadNum" ) config.SetSection( "RunningParams" ) self.UsePsyco = config.GetIntValue( "UsePsyco" ) self.AISleepPeriod = config.GetIntValue( "AISleepPeriod" ) self.PlayerOffTime = config.GetIntValue( "PlayerOffTime") self.ProcessFindTJGTime = config.GetIntValue( "ProcessFindTJGTime") self.ServerDBConfigPath = config.GetValue( "ServerDBConfigPath") self.StartRunTime = config.GetIntValue( "StartRunTime") self.AccountSource = config.GetIntValue( "AccountSource") self.MaxPlayerCnt = config.GetIntValue( "MaxPlayerCnt") def GetMaxPlayerCnt(self): return self.MaxPlayerCnt # Õ˺ŻñÈ¡À´Ô´ 0:×Ô¶¯Éú³É£¬ 1£ºÈ¡¿âÖÐÒÑÓÐÕ˺Šdef GetAccountSource(self): return self.AccountSource def GetIOThreadNum(self): return self.IOThreadNum def GetAIThreadNum(self): return self.AIThreadNum def GetServerIP(self): return self.ip def GetServerPort(self): return self.port def GetClientVersion(self): return self.ClientVersion def GetVersionNo(self): return self.VersionNO def GetPackStartCount(self): return self.StartCnt def GetKeyString(self): return self.KeyName def GetSendKeyDictFilePath(self): return self.SendKeyDictPath def GetMapFilePath(self): return self.MapFilePath def GetMapFilePostFix(self): return self.MapPostFix def GetUsePsyco(self): return self.UsePsyco def GetAISleepPeriod(self): return self.AISleepPeriod # Íæ¼ÒÏÂÏßʱ³¤ def GetPlayerOffTime(self): return self.PlayerOffTime # ´ÓÊý¾Ý¿â²éѯÍÑ»ú¹ÒÍæ¼ÒµÄ¼ä¸ô def GetProcessFindTJGTime(self): return self.ProcessFindTJGTime def GetServerDBConfigPath(self): return self.ServerDBConfigPath def GetStartRunTime(self): return self.StartRunTime def __str__(self): return "ClientVersion:[%s]\n" \ "KeyString:[%s]\n" \ "MapFilePath:[%s]\n" \ "MapFilePostFix:[%s]\n" \ "PackStartCount:[%u]\n" \ "SendKeyDictFile:[%s]\n" \ "ServerIP:[%s]\n" \ "ServerPort:[%u]\n" \ "VersionNO:[%u]\n" \ "IOThreadNum:[%u]\n" \ "AIThreadNum:[%u]\n" \ "UsePsyco:[%u]\n" \ "AISleepPeriod:[%u]\n" \ % \ ( \ self.GetClientVersion(),\ self.GetKeyString(), \ self.GetMapFilePath(), \ self.GetMapFilePostFix(), \ self.GetPackStartCount(), \ self.GetSendKeyDictFilePath(), \ self.GetServerIP(), \ self.GetServerPort(), \ self.GetVersionNo(), \ self.GetIOThreadNum(), \ self.GetAIThreadNum(), \ self.GetUsePsyco(), \ self.GetAISleepPeriod(), \ ) __gGameConfig = None def ReadConfig( filename ): global __gGameConfig if not __gGameConfig: __gGameConfig = GameConfig( filename ) def GetConfig(): global __gGameConfig return __gGameConfig if __name__ == '__main__': FilePath = "..\Configuration\Config.ini" SectionName = "GameConfig" ReadConfig( FilePath ) config = GetConfig() print config