def change_para_html(self, html_cont, data): if data == "": return html_cont replace_data = self.html_replace_data(data) for data in replace_data: if "=" not in data: raise Exception("error format,not found =") try: if ":=" not in data: raise Exception("error format") sval = get_var(data.split(":=")[1]) name = data.split(":=")[0] # 对参数做num,str区分 数字:222222, str:'222222' if "," in sval: sval = sval.split(",") if sval != "" and isinstance(sval, str): if Core.is_numeric(sval): sval = int(sval) elif "\'" in sval: sval = sval.replace("\'", "") elif sval.startswith("[") or sval.startswith("{"): sval = Core.eval_str(sval) # 对入参类型为bool, null的处理 if sval == 'false': sval = False elif sval == 'true': sval = True elif sval == 'null' or sval == "": sval = None scmd = '''html_cont[%s]="%s" ''' % (name, sval) html_cont[name] = sval except Exception, e: raise Exception("error after %s, %s" % (scmd, str(e)))
def rh_html_check_result(self, input, response_json): data = str(input).strip().replace("\n", "").replace("”", "\"").replace("‘", "\'").replace("’", "\'")\ # .replace(";", ";") if data is None: raise Exception("error format,prepareResult is none.") flag = False # eval()对特殊值处理 # null = "" # true = True # false = False # 返回结果为list[]或者 json格式 比较 if response_json.startswith("[") or response_json.startswith("{"): response_json = Core.eval_str(response_json) # str转成list cmp_result = Core.rh_check_result(input, response_json) if "[success]" in cmp_result: flag = True else: return cmp_result # 返回结果为text else: except_message = data.split(":=")[1] if response_json.__contains__(except_message): flag = True else: return "[fail]:response data do not contains %s." % data if flag: return "[success]:response data contains %s." % data
def change_para_json(self, json_cont, data): if data == "": return json_cont # try: # t_js = json.loads(data) # for ixe in t_js.keys(): # t_ixv = t_js.get(ixe) # if isinstance(t_ixv, (basestring, dict, list)): # t_ixv = get_var(t_ixv) # json_cont[ixe] = t_ixv # return json_cont # except: replace_data = self.json_replace_data(data) for data in replace_data: if "=" not in data: raise Exception("error format,not found =") try: if ":=" not in data: raise Exception("error format") sval = get_var(data.split(":=")[1]) name = data.split(":=")[0] # sval = sval.replace("'", "\"") # 对参数做num,str区分 数字:222222, str:'222222' if sval != "": if sval.startswith("'") and sval.endswith("'"): sval = str(sval[1:-1]) elif Core.is_numeric(sval): if "." in sval: sval = float(sval) else: sval = int(sval) # 支持替换成空list或dict: a=[]; a={} elif sval.startswith("[") or sval.startswith("{"): # elif sval.replace(" ", "") == "[]" or sval.replace(" ", "") == "{}": sval = Core.eval_str(sval) # 嵌套json替换 obj = "" for i in name.split("."): if Core.is_numeric(i): obj = "%s[%s]" % (obj, i) else: obj = "%s['%s']" % (obj, i) # 对值为为bool, null的处理 if sval == 'false': sval = False elif sval == 'true': sval = True elif sval == 'null' or sval == "": # 若需要传 {"A":null} 则可以用默认参数或者 A:=null 或者 A:= sval = None elif sval == "\"\"": # 若需要传 {"A":""} 则替换值时需这样处理 A:=\"\" sval = "" # 字符串处理 scmd = "%s%s = sval" % ("json_cont", obj) exec scmd except Exception, e: raise Exception("error occur %s" % scmd)
def dict_handle(self, dict_data): """ 该方法处理json嵌套中,嵌套的json和list是str类型。则需要eval处理。 eg: {"a": "[]"} --> {"a": []} :param dict_data: :return: """ for k, v in dict_data.items(): if type(v) in [ types.StringType, types.UnicodeType ] and (str(v).startswith("[") or str(v).startswith("{")): v1 = Core.eval_str(v) dict_data[k] = v1 else: pass return dict_data
def get_nestdict_value(self, keytag, dict_data): if type(dict_data) not in [types.ListType, types.DictType]: # 处理返回值为 "{}" ,"[]"情况 # dict_data = json.loads(dict_data) dict_data = Core.eval_str(dict_data) # 效果同上 # 处理 "a": "[]" 情况 dict_data = self.dict_handle(dict_data) sname = keytag.strip() obj = scmd = realval = "" for i in sname.split("."): if Core.is_numeric(i): obj = "%s[%s]" % (obj, i) else: if 'str(' in i: i = i[i.find('(') + 1:-1] obj = "%s['%s']" % (obj, i) scmd = "%s%s" % ("dict_data", obj) try: realval = eval(scmd) except: return "[Failed]:cmd change error,eval(%s)" % scmd return realval