-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
74 lines (66 loc) · 2.67 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import common as c
from FunClass import FunClass
import json
# 记住最后一次执行的方法的下标
gt_lastExeFunIndex="0"
l=[]
l.append(FunClass("createFunFormCurl","【把cur命令结果生成函数到文件】"))
def remove_empty_values(json_str):
data = json.loads(json_str)
# 使用递归函数去除空值
def remove_empty(d):
if isinstance(d, dict):
return {k: remove_empty(v) for k, v in d.items() if v or (isinstance(v, list) and len(v) > 0)}
if isinstance(d, list):
return [remove_empty(v) for v in d if v or (isinstance(v, list) and len(v) > 0)]
return d
return json.dumps(remove_empty(data))
def createFunFormCurl():
with open("draft", 'r') as file:
lines = file.readlines()
url=''
method='get'
postData={}
for i, line in enumerate(lines):
if '--data-raw' in lines[i]:
method='post'
postDataStr=lines[i].replace("--data-raw '","").replace("}' \\","}").strip()
postData=json.loads(postDataStr)
# postDataStr=postDataStr.replace("null","None").replace("true","True")
# postDataStr=json.dumps(postData, indent=4, ensure_ascii=False)
elif 'curl' in lines[i]:
url=lines[i].replace("curl ","").replace("\' \\","").strip()
inde=url.index("/nisg")+5
t= len(url)
url=url[inde:t]
# mName = input("请输入生成的方法名称:")
mName ="aaa"
# print(mName)
# print(url)
# print(method)
# print(postDataStr)
print("def "+mName+"():")
if 'get'== method:
print(" c.pget(\""+url+"\")")
elif 'post'== method:
print(" # postData="+postDataStr.replace("null","None").replace("true","True"))
print(" postData="+remove_empty_values(postDataStr))
print(" c.ppost(\""+url+"\",postData)")
# =================
#如果gt_lastExeFunIndex有值,那么就以gt_lastExeFunIndex为执行目标
#如果没有,那么提示输入,按输入执行,并更新gt_lastExeFunIndex的值
funlist=''
if len(gt_lastExeFunIndex)==0:
for index,f in enumerate(l):
print(str(index)+". "+f.toString())
funlist = input("请输入需要执行的方法序号,多个以英文逗号隔开:")
if len(funlist)>0:
c.put("util.py","gt_lastExeFunIndex",funlist) #记住下标,下次直接回车使用
else:
funlist=gt_lastExeFunIndex
indexArr=funlist.split(",")
for index,f in enumerate(l):
if str(index) in indexArr:
print('执行方法=> '+f.name)
func = globals()[f.name]
func()