Skip to content

Commit 1b90b1b

Browse files
committed
Add saving to playbook feature
1 parent 41f7ea3 commit 1b90b1b

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

src/arguments/utility.py

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from collections import defaultdict
1111
from simple_term_menu import TerminalMenu
1212
import webbrowser
13+
import time
1314

1415
from .error import SearchError
1516
from .save import SaveSearchResults
@@ -33,13 +34,79 @@
3334

3435
console = Console()
3536

37+
class Playbook():
38+
def __init__(self):
39+
self.linux_path = "/home/{}/Documents/dynamic".format(os.getenv('USER'))
40+
self.windows_path = repr("c:\\Users\\{}\\My Documents".format(os.getenv('USERNAME')))
41+
self.unix_path = "." # Add mac support here
42+
self.file_name = 'dynamic_playbook.json'
43+
44+
@property
45+
def playbook_path(self):
46+
if(sys.platform=='linux'):
47+
return os.path.join(self.linux_path, self.file_name)
48+
elif(sys.platform=='win32'):
49+
return os.path.join(self.windows_path, self.file_name)
50+
51+
@property
52+
def playbook_content(self):
53+
try:
54+
with open(self.playbook_path, 'r') as playbook:
55+
return json.load(playbook)
56+
except FileNotFoundError:
57+
os.makedirs(os.path.dirname(self.playbook_path), exist_ok=True)
58+
with open(self.playbook_path, 'w') as playbook:
59+
playbook.write('[]')
60+
return self.playbook_content
61+
62+
@playbook_content.setter
63+
def playbook_content(self, value):
64+
"""
65+
Saves playbook in the following format
66+
[
67+
{
68+
time: unix timestamp
69+
question_id: 123456,
70+
question_title: 'question_title',
71+
question_link: 'link',
72+
answer_body: 'body of the answer'
73+
},
74+
...
75+
]
76+
"""
77+
if type(value) == list:
78+
with open(self.playbook_path, 'w') as playbook:
79+
json.dump(value, playbook, ensure_ascii=False)
80+
pass
81+
else:
82+
raise ValueError("value should be of type list")
83+
84+
def add_to_playbook(self, stackoverflow_object, question_id):
85+
"""
86+
Receives a QuestionsPanelStackoverflow object and
87+
saves data of a particular question into playbook
88+
"""
89+
for question in stackoverflow_object.questions_data:
90+
if(int(question[1])==int(question_id)):
91+
content = self.playbook_content
92+
content.append({
93+
'time_of_creation': time.time(),
94+
'question_id': int(question_id),
95+
'question_title': question[0],
96+
'question_link': question[2],
97+
'answer_body': stackoverflow_object.answer_data[int(question_id)]
98+
})
99+
self.playbook_content = content
100+
36101
class QuestionsPanelStackoverflow():
37102
def __init__(self):
38103
self.questions_data = [] # list( list( question_title, question_id, question_link )... )
39104
self.answer_data = defaultdict(lambda: False) # dict( question_id:list( body, link )) corresponding to self.questions_data
40105
self.line_color = "bold red"
41106
self.heading_color = "bold blue"
42107
self.utility = Utility()
108+
self.playbook = Playbook()
109+
self.log = open('logs.txt', 'w')
43110

44111
def populate_question_data(self, questions_list):
45112
"""
@@ -107,7 +174,7 @@ def navigate_questions_panel(self):
107174
console.print("[yellow] Use arrow keys to navigate. 'q' or 'Esc' to quit. 'Enter' to open in a browser")
108175
console.print()
109176
options = ["|".join(map(str, question)) for question in self.questions_data]
110-
question_menu = TerminalMenu(options, preview_command=self.return_formatted_ans, preview_size=0.75, )
177+
question_menu = TerminalMenu(options, preview_command=self.return_formatted_ans, preview_size=0.75, accept_keys=('p', 'enter'))
111178
quitting = False
112179
while not(quitting):
113180
options_index = question_menu.show()
@@ -116,7 +183,11 @@ def navigate_questions_panel(self):
116183
except Exception:
117184
return
118185
else:
119-
webbrowser.open(question_link)
186+
if(question_menu.chosen_accept_key == 'enter'):
187+
webbrowser.open(question_link)
188+
elif(question_menu.chosen_accept_key == 'p'):
189+
self.playbook.add_to_playbook(self, self.questions_data[options_index][1])
190+
pass
120191

121192
def display_panel(self, questions_list):
122193
self.populate_question_data(questions_list)

0 commit comments

Comments
 (0)