-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
144 lines (112 loc) · 4.73 KB
/
search.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
'''Module containing the search functionality of the application'''
# Import Python standard libraries
import re
from datetime import datetime
# Import custom classes and helper methods
import constants
from menu import Menu
from utils import Utilities
from validation import Validation
from entry import Entry
class Search:
'''This class contains all methods for searching the worklog
and displaying the results'''
def __init__(self):
self.utils = Utilities()
self.validation = Validation()
self.entry = Entry()
self.menu = Menu()
self.results = list()
def search(self, query_text, search_type=''):
'''Method for searching the worklog
This method takes the text to ask the user for input
and the type of search to conduct.'''
while True:
self.utils.clear_screen()
query = input(query_text).lower()
worklog = self.utils.read_file()
if search_type == 'date':
if not self.validation.is_valid_date(query):
continue
elif search_type == 'time_spent':
if not self.validation.is_valid_number(query):
continue
elif search_type == 'exact_match' or search_type == 'regex':
if not self.validation.is_valid_input(query):
continue
elif search_type == 'date_range':
if not self.validation.is_valid_date_range(query.split(', ')):
continue
search = re.compile(r'{}'.format(query), re.I)
for line in worklog:
if search_type == 'date' or search_type == 'time_spent':
if search.match(line[search_type]):
self.results.append(line)
elif search_type == 'exact_match' or search_type == 'regex':
if search.findall(line['title']) \
or search.findall(line['notes']):
self.results.append(line)
elif search_type == 'date_range':
real_date = datetime.strptime(
line['date'], '%m/%d/%Y')
date_range = query.split(', ')
if (datetime.strptime(date_range[0], '%m/%d/%Y') <=
real_date and
datetime.strptime(date_range[1], '%m/%d/%Y') >=
real_date):
self.results.append(line)
if not self.results:
print("-------------------------------\n")
print('No results found...')
input('Press Enter to try again.')
continue
else:
self.display_search_results()
break
def display_search_results(self):
'''This method displays the results of a user search
if any are found.'''
index = 0
while True:
num_results = len(self.results)
entry = self.results[index]
self.utils.clear_screen()
print('\n{} Search Results Found'.format(num_results))
print("-------------------------------\n")
print('title: {}'.format(entry['title']))
print('date: {}'.format(entry['date']))
print('time spent: {}'.format(entry['time_spent']))
print('notes: {}'.format(entry['notes']))
print("-------------------------------")
print('Result {}/{}'.format((index + 1), num_results))
print("-------------------------------\n")
choice = input(
"Choose an action: "
"[N]ext, "
"[P]revious, "
"[E]dit, "
"[D]elete, "
"[S]earch Menu: ").lower()
if not self.validation.is_valid_input(choice, menu='npeds'):
self.utils.clear_screen()
continue
if choice == 'n':
if index != (len(self.results) - 1):
index += 1
else:
index = 0
elif choice == 'p':
if index != 0:
index -= 1
else:
index = (len(self.results) - 1)
elif choice == 'e' or choice == 's' or choice == 'd':
if choice == 'e':
self.entry.update_current_entry(
entry['id'], edit_mode='edit')
if choice == 'd':
self.entry.update_current_entry(
entry['id'], edit_mode='delete')
self.menu.display(constants.SEARCH_MENU)
self.results = list()
break