-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
161 lines (136 loc) · 4.74 KB
/
cli.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
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
import click
import requests
from pyfiglet import Figlet, figlet_format
from termcolor import cprint
#from get_error import main as get_error
from get_error import run_command
from search_answers import ask
from colorama import init
import csv
import io
__author__ = "Team 2 Sprint-4"
@click.group()
def main():
"""
CLI for querying StackExchange API
"""
init(convert=True)
cprint(figlet_format('AskFlow CLI', font='slant'), "cyan")
@main.command()
@click.argument('query')
def search(query):
#python cli.py search "python error_test.py"
#Should run Arlyn's error extraction scripts and return the error search terms
#Get the command string from the first argument
command_string = query
#Run the program to check error on
op, err = run_command(command_string)
error_message = err.decode("utf-8").strip().split("\r\n")[-1]
#print(error_message)
#If there's an error message, get the relevant info to search
if error_message:
relevant_info = error_message.split(":") #will split error into error type, and error info
#Print all relevant search info
#print("Relevant information for search: ")
#print(relevant_info)
a=ask(relevant_info)
answers=a.get_answer()
#Print the first answer
print_answer(1, answers[0])
print("To show next answer type 'python cli.py next'")
return(relevant_info)
else:
print("There was no error with the script")
def print_answer(answer_num, answer):
colors = ['green','magenta']
print('\n')
print("Answer #" + str(answer_num))
click.echo(click.style(f" ************** ", fg=colors[0]))
click.echo(click.style(answer, fg=colors[1]))
@main.command()
def next():
file = io.open('g4g.csv', encoding="utf-8")
reader = csv.DictReader(file)
on_next = False
case_list = []
#Create a dictionary from the file
for row in reader:
case_list.append(row)
#Find the next answer
result_to_return = ""
count=0
for row in case_list:
count+=1
if on_next:
row['Current'] = 'True'
on_next = False
result_to_return = row
break
if row['Current'] == 'True':
#Check if it's the last one
if count != len(case_list):
row['Current'] = 'False'
on_next = True
#If there's a next answer
if result_to_return != "":
#Print the next answer
print_answer(result_to_return['Number'], result_to_return['Answer'])
#print("Answer #" + result_to_return['Number'])
#print(result_to_return['Answer'])
#Set the next answer in the file
file = io.open('g4g.csv', 'w', newline ='', encoding="utf-8")
with file:
# identifying header
header = ['Number', 'Answer', 'Current']
writer = csv.DictWriter(file, fieldnames = header)
writer.writeheader()
# writing data row-wise into the csv file
for row in case_list:
writer.writerow(row)
else:
print("There is no next answer. Try to go to a previous answer with [python cli.py previous]")
# Go to previous answer
@main.command()
def previous():
file = io.open('g4g.csv', encoding="utf-8")
reader = csv.DictReader(file)
on_previous = False
case_list = []
#Create a dictionary from the file
for row in reader:
case_list.append(row)
#Find the next answer
result_to_return = ""
count = 0
for row in reversed(case_list):
count+=1
if on_previous:
row['Current'] = 'True'
on_previous = False
result_to_return = row
break
if row['Current'] == 'True':
#Check if it's the last one
if count != len(case_list):
row['Current'] = 'False'
on_previous = True
#If there's a next answer
if result_to_return != "":
#Print the next answer
print_answer(result_to_return['Number'], result_to_return['Answer'])
#print("Answer #" + result_to_return['Number'])
#print(result_to_return['Answer'])
#Set the next answer in the file
file = io.open('g4g.csv', 'w', newline ='', encoding="utf-8")
with file:
# identifying header
header = ['Number', 'Answer', 'Current']
writer = csv.DictWriter(file, fieldnames = header)
writer.writeheader()
# writing data row-wise into the csv file
for row in case_list:
writer.writerow(row)
else:
print("There is no previous answer. Try to go to the next answer with [python cli.py next]")
if __name__ == "__main__":
main()