-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.py
More file actions
216 lines (165 loc) · 5.99 KB
/
command.py
File metadata and controls
216 lines (165 loc) · 5.99 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import re
from quickcmd_color import QuickCmdColor
from chatgpt import ChatGPT
class Command(object):
def __init__(self, file="", name="", items=[]):
infos = {k: v for (k, v) in items}
self.command = infos.get("command", "")
self.set_workdir(infos.get("workdir", ""))
self.godir = infos.get("godir", "")
self.desc = infos.get("desc", "")
self.tip = infos.get("tip", "")
self.api_key = infos.get("api_key", "")
self.multi_line_question = infos.get("multi_line_question", False)
self.type = infos.get("type", "")
self.file = file
if self.type == "stock":
self.cmd_type = "stock"
prefix = "[STOCK] "
elif self.command:
prefix = "[CMD] "
self.cmd_type = "cmd"
elif self.godir:
self.cmd_type = "cd"
prefix = "[GOTO] "
elif self.tip:
self.cmd_type = "tip"
prefix = "[TIP] "
elif self.api_key:
self.cmd_type = "chatgpt"
prefix = "[ChatGPT] "
else:
sys.exit(f"Unknown command: {self.type}")
self.name = prefix + name
self.name = self.name.replace(" ", "-")
self.qcc = QuickCmdColor()
def get_type(self):
return self.cmd_type
def abs_path(self, path):
if path and path.startswith("~"):
# os.path.join(os.path.expanduser("~"), path[1:])
return os.path.expanduser("~") + path[1:]
return path
def script_path(self):
return os.path.dirname(os.path.realpath(__file__))
def set_field(self, key, value):
if key == "name":
self.name = value
elif key == "command":
self.command = value
elif key == "workdir":
self.workdir = self.abs_path(value)
elif key == "godir":
self.godir = self.abs_path(value)
elif key == "tip":
self.tip = value
def get_file(self):
return self.file
def get_name(self):
return self.name
def set_name(self, name):
self.name = name
def set_cmd(self, command):
self.command = command
def get_cmd(self):
return self.command
def set_workdir(self, workdir):
self.workdir = self.abs_path(workdir)
def set_godir(self, godir):
self.godir = godir
def get_godir(self):
return self.godir
def set_tip(self, tip):
self.tip = tip
def get_tip(self):
return self.tip
def complete(self):
"""
Completing the command
"""
if not self.command:
# not a command
return True
pt = re.compile(r'\${\w+}')
variables = pt.findall(self.command)
variables = set(variables)
for variable in variables:
m = re.match(r'^\${(\w+)}$', variable)
# m = re.match(r'^\${(\w+)}', variable)
name = m.group(1)
try:
self.qcc.purple_print(self.command)
value = self.qcc.green_input(r"input %s:" % (name))
self.command = self.command.replace(variable, value)
except KeyboardInterrupt:
return False
return True
def execute(self):
self.qcc.light_blue_print(self.tostring())
if self.cmd_type == "cmd":
if self.workdir and os.path.exists(self.workdir):
os.chdir(self.workdir)
os.system(self.command)
elif self.cmd_type == "cd":
godir = self.abs_path(self.godir)
os.system("echo '%s' > %s/../.qc.cd.path" %
(godir, self.script_path()))
elif self.cmd_type == "chatgpt":
if self.multi_line_question:
s = "[!] Please enter your question, ending with a new line containing only \"\F\": "
else:
s = "[!] Please enter your question: "
lines = []
while True:
line = self.qcc.yellow_input(s)
if not self.multi_line_question:
lines.append(line)
break
if s is not None:
s = None
if line == "\F" or line == "\\f":
break
lines.append(line)
self.chatgpt = ChatGPT(self.api_key)
self.qcc.yellow_print("[!] Sending Request to ChatGPT ...")
choices, total_tokens = self.chatgpt.ask_question("\n".join(lines))
i = 1
for answer in choices:
self.qcc.yellow_print("[+] Answer " + str(i) + ":")
message = answer.get('message', {})
content = message.get('content', None)
if content:
print(content)
i = i + 1
self.qcc.yellow_print("[!] Spent tokens: " + str(total_tokens))
def tostring(self):
s = "[%s]" % (self.name)
if self.cmd_type == "cmd":
s = "%s\n[+] Command = %s" % (s, self.command)
if self.workdir:
s = "%s\n[+] Workdir: %s" % (s, self.workdir)
elif self.cmd_type == "cd":
s = "%s\n[+] Goto: %s" % (s, self.godir)
elif self.cmd_type == "tip":
s = "%s\n[+] Tip = %s" % (s, self.tip)
elif self.cmd_type == "stock":
s = "%s\n[+] Type = Stock Analysis" % (s)
if self.file:
s = "%s\n[+] In file = %s" %(s, self.file)
return s
def fzf_str(self, index):
res = "{:0>3}:{}".format(index, self.name)
if self.command:
res = "{}: {}".format(res, self.command)
elif self.godir:
res = "{}; cd {}".format(res, self.godir)
elif self.tip:
res = "{}; tip: {}".format(res, self.tip)
elif self.cmd_type == "stock":
res = "{}: {}".format(res, "Perform stock analysis")
res = "{}\n".format(res)
return res