#coding:utf8 import requests import json from Action import * import sys; reload(sys); sys.setdefaultencoding('utf8'); class BackgroundConfig(): ''' 根据post的参数保存成配置文件的形式 ''' def __init__(self): self.config_string = "" self.CONFIG_DIC = {} #存储全局配置文件的信息 self.load_global_config("global_config.txt") def load_global_config(self,engine_config_file): config = open(engine_config_file,'r') for line in config: if line.strip() == "": continue name,value = line.split("=") self.CONFIG_DIC[name] = value.strip() def save_background_config(self,parameters): flag = 1 try: userName = parameters["userName"].strip() configList = parameters["configList"] for config in configList: currentPremiseList = config["currentPremiseList"] reply = config["reply"].strip() if config["reply"] != "" else " " nextStep = config["nextStep"].strip() historyPremiseList = config["historyPremiseList"] if nextStep == "" : flag = 2 break if len(currentPremiseList) == 1 and currentPremiseList[0]["keywords"] == "": flag = 3 break curPreStr = ','.join([currentPremise["actionName"]+"="+currentPremise["keywords"] for currentPremise in currentPremiseList]) if historyPremiseList != [] and historyPremiseList[0]["keywords"] != "": hisPreStr = ','.join([historyPremise["actionName"]+"="+historyPremise["keywords"] for historyPremise in historyPremiseList]) else: hisPreStr = "" if hisPreStr == "": config_line = curPreStr + "| |"+"match|=>|"+reply+"|nextStep="+nextStep+'\n' else: config_line = curPreStr + "|"+hisPreStr+"|match|=>|"+reply+"|nextStep="+nextStep+'\n' self.config_string += config_line print self.config_string except KeyError: flag = 2 return flag def post_bg_config(self,userName): url= self.CONFIG_DIC["config_post_url"] payload= {'userName':userName,'config_string':self.config_string} ret= requests.post(url,data=payload) if(ret.status_code == 200): return 1 elif(ret.status_code == 111): return 2 else: return 4