forked from claraj/book_wishlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastore.py
207 lines (149 loc) · 5.34 KB
/
datastore.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
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
import os
from book import Book
import files as file
import json
import valid
import datetime
DATA_DIR = 'data'
BOOKS_FILE_NAME = os.path.join(DATA_DIR, 'wishlist.txt')
COUNTER_FILE_NAME = os.path.join(DATA_DIR, 'counter.txt')
Current_Date = datetime.datetime.now()
book_list = []
counter = 0
def setup():
""" Read book info from file, if file exists. """
global counter
# reads them BOOKS_FILE_NAME file and passes make_book_list() as a calllback
file.read(BOOKS_FILE_NAME, cb=make_book_list)
try:
# reads COUNTER_FILE_NAME and loads it into memory
counter = file.read(COUNTER_FILE_NAME)
if not counter:
counter = 0
except IOError:
counter = len(book_list)
def shutdown():
"""Save all data to a file - one for books, one for the current counter value, for persistent storage"""
output_data = make_output_data()
file.write(DATA_DIR, BOOKS_FILE_NAME, output_data)
file.write(DATA_DIR, COUNTER_FILE_NAME, counter)
def get_books(**kwargs):
""" Return books from data store. With no arguments, returns everything. """
global book_list
if len(kwargs) == 0:
return book_list
if 'read' in kwargs:
if len(kwargs) == 1:
read_books = [book for book in book_list if book.read == kwargs['read']]
return read_books
else:
matching_books = [book for book in book_list if kwargs['string'].lower() in book.title.lower() or
kwargs['string'].lower() in book.author.lower()]
return matching_books
else:
if len(kwargs) == 1:
matching_books = [book for book in book_list if kwargs['string'].lower() in book.title.lower() or
kwargs['string'].lower() in book.author.lower()]
return matching_books
else:
unread_books = [book for book in book_list if book.read != kwargs['read']]
return unread_books
def get_book(book_id):
""" gets individual book by id """
global book_list
for book in book_list:
if valid.is_number(book_id):
if book.id == int(book_id):
return book
else:
if book_id in book.author or book_id in book_id.title:
return book
print("No book found with id {}.".format(book_id))
def update_book(updated_book):
""" updates an individual book """
global book_list
for book_index in range(len(book_list)):
if book_list[book_index - 1].id == updated_book.id:
book_list.pop(book_index - 1)
book_list.append(updated_book)
def add_book(book):
""" Add to db, set id value, return Book """
global book_list
book.id = generate_id()
book_list.append(book)
def check_book(new_book):
""" See if there is a duplicate book """
global book_list
for book in book_list:
if book.title == new_book.title:
if book.author == new_book.author:
return True
return False
def delete_book(book):
""" Delete book from db, and update """
global book_list
book_list.remove(book)
print("Book hella deleted!")
def sort_list(books):
""" Now that I sorted the books, we need to put the list in order """
global book_list
if books == "author":
order_of_book = sorted(book_list, key=lambda book: book.author.lower())
elif books == "title":
order_of_book = sorted(book_list, key=lambda book: book.getSortTitle())
else:
print('You did not use the right option man.....')
book_list = order_of_book
return book_list
def generate_id():
global counter
counter += 1
return counter
def set_read(book_id, read):
"""Update book with given book_id to read.
Return True if book is found in DB and update
is made, False otherwise."""
global book_list
for book in book_list:
if book.id == book_id:
book.read = True
book.date = "%d/%d/%d" %(Current_Date.month, Current_Date.day, Current_Date.year)
return True
return False # return False if book id is not found
def make_book_list(string_from_file):
""" turn the string from the file into a list of Book objects"""
global book_list
if len(string_from_file) > 0:
book_json = json.loads(string_from_file)
for book in book_json:
if 'stars_str' in book:
stars_str = book["stars_str"]
else:
stars_str = ""
if 'date' in book:
date = book["date"]
else:
date = ""
book_list.append(
Book(
title=book["title"],
author=book["author"],
read=bool(book["read"]),
id=int(book["id"]),
date=date,
stars=int(book["stars"]),
stars_str=stars_str)
)
def make_output_data(b_list=None):
""" create a string containing all data on books, for writing to output file"""
if not b_list:
global book_list
lst = book_list
else:
lst = b_list
output_data = []
for book in lst:
output_str = book.getJSON()
output_data.append(output_str)
all_books_string = "[ " + ', '.join(output_data) + " ]"
return all_books_string