# -*- coding:utf-8 -*- import os, sys, time import shutil from ssat_sdk.utils import Environment from ConfigParser import * import thread try: Python_Home = sys.prefix # Server_CFG_Path = Python_Home + Environment.SDK_Relative_Path + u"config/server.cfg" Server_CFG_Path = Python_Home + Environment.SDK_Relative_Path + u"config/baidu_run.cfg" Server_CFG_Local_Path = Python_Home + Environment.SDK_Relative_Path + u"config/baidu.cfg" # Server_CFG_Path = "../config/server.cfg" Home_Init = True except KeyError, e: print "KeyError:", e.message class BaiduConfig(): ServiceModel = "service" def __init__(self): if not os.path.exists(Server_CFG_Path): print 'server_run.cfg文件不存在,复制server.cfg文件' shutil.copy(Server_CFG_Local_Path, Server_CFG_Path) print u"Server配置文件:", Server_CFG_Path self.CFGParser = ConfigParser() self.CFGParser.read(Server_CFG_Path) def writeCFG(self): self.CFGParser.write(open(Server_CFG_Path, "w")) def setBaiduCount(self,count): self.CFGParser.set("CurStatus", "Cur_Count", count) self.writeCFG() def getBaiduCount(self): return self.CFGParser.get("CurStatus", "Cur_Count") def getAppID(self): section = self.getBaiduCount() return self.CFGParser.get(section, "APP_ID") def getAPIKey(self): section = self.getBaiduCount() return self.CFGParser.get(section, "API_KEY") def getSecretKey(self): section = self.getBaiduCount() return self.CFGParser.get(section, "SECRET_KEY") def getRetCount(self): retCount = self.resetTime() if retCount is not None: return retCount section = self.getBaiduCount() retCount = self.CFGParser.getint(section, "ret_count") return retCount def resetTime(self): prevTime = self.CFGParser.getfloat("CurStatus", "reset_time") if prevTime == 0: timeLong = time.time() self.CFGParser.set("CurStatus", "reset_time", timeLong) self.writeCFG() prevGMT = time.gmtime(prevTime) prevYear = prevGMT.tm_year prevMonth = prevGMT.tm_mon prevDay = prevGMT.tm_mday curTime = time.time() curGMT = time.gmtime(curTime) curYear = curGMT.tm_year curMonth = curGMT.tm_mon curDay = curGMT.tm_mday if prevYear <> curYear or prevMonth <> curMonth or prevDay <> curDay: print "resetTime,prevGMT:",prevGMT print "resetTime,curGMT:",curGMT self.CFGParser.set("CurStatus", "reset_time", curTime) self.resetRetCount() return 500 def resetRetCount(self): countList = self.getBaiduList() for section in countList: self.CFGParser.set(section, "ret_count", 500) self.writeCFG() def subRetCount(self, count): section = self.getBaiduCount() retCount = self.getRetCount() retCount = retCount - count self.CFGParser.set(section, "ret_count", retCount) self.writeCFG() def getBaiduList(self): sections = self.CFGParser.sections() countList = [] for section in sections: if section.__contains__("count_"): countList.append(section) return countList def testCFG(): print "testCFG:" cfg = BaiduConfig() # print "getBaiduCount:", cfg.getBaiduCount() print "getRetCount:", cfg.getRetCount() # print cfg.getBaiduList() if __name__ == "__main__": thread.start_new_thread(testCFG,()) # thread.start_new_thread(testCFG,()) # thread.start_new_thread(testCFG,()) time.sleep(3)