-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathselector.py
58 lines (52 loc) · 1.72 KB
/
selector.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
import os
from utils import papers_to_list
class Selector:
def __init__(self, fields=None, options=None, start_idx=0):
self.idx = start_idx
self.fields = fields if fields is not None else ['title', 'abstract']
if options is None:
self.options = {
'y':{
'desc':'yes',
'fn':lambda paper, selected_papers:selected_papers.append(paper)
},
'n':{
'desc':'no'
}
}
else:
self.options = options
self.options['e'] = {'desc':'exit'}
def __call__(self, papers):
return self.select(papers)
def select(self, papers):
os.system('clear') # i only support unix based systems
papers_list = papers_to_list(papers)
selected_papers = []
while self.idx<len(papers_list):
paper = papers_list[self.idx]
self.print_paper(paper)
decision = self.handle_options(paper, selected_papers)
if decision=='e': # e will be exit
print()
break
self.idx+=1
return selected_papers
def print_paper(self, paper):
paper_str = ''
paper_str+='\n' + '-'*os.get_terminal_size().columns + '\n'
paper_str+=f'Paper {self.idx}\n'
for field in self.fields:
paper_str+=f'{field.upper()}: {paper[field]}\n'
print(f'\r{paper_str}', end='')
def handle_options(self, paper, selected_papers):
options_str = ''
for option, option_dict in self.options.items():
options_str+=f"{option}: {option_dict['desc']} "
decision = input(options_str)
while decision not in self.options.keys():
print("Invalid input!")
decision = input(options_str)
if self.options[decision].get('fn') is not None:
self.options[decision]['fn'](paper, selected_papers)
return decision