-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdvds-sql.py
executable file
·299 lines (265 loc) · 10.1 KB
/
dvds-sql.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import datetime
import os
import sqlite3
import sys
import tempfile
import xml.etree.ElementTree
import xml.parsers.expat
import xml.sax.saxutils
import Console
import Util
DISPLAY_LIMIT = 20
def main():
functions = dict(a=add_dvd, e=edit_dvd, l=list_dvds,
d=list_directors, r=remove_dvd, i=import_,
x=export, q=quit)
filename = os.path.join(os.path.dirname(__file__), "dvds.sdb")
db = None
try:
db = connect(filename)
action = ""
while True:
count = dvd_count(db)
print("\nDVDs ({0})".format(os.path.basename(filename)))
if action != "l" and 1 <= count < DISPLAY_LIMIT:
list_dvds(db)
else:
print("{0} dvd{1}".format(count, Util.s(count)))
print()
menu = ("(A)dd (E)dit (L)ist (D)irectors (R)emove "
"(I)mport e(X)port (Q)uit"
if count else "(A)dd (I)mport (Q)uit")
valid = frozenset("adelrixq" if count else "aiq")
action = Console.get_menu_choice(menu, valid,
"l" if count else "a", True)
functions[action](db)
finally:
if db is not None:
db.close()
def connect(filename):
create = not os.path.exists(filename)
db = sqlite3.connect(filename)
if create:
cursor = db.cursor()
cursor.execute("CREATE TABLE directors ("
"id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, "
"name TEXT UNIQUE NOT NULL)")
cursor.execute("CREATE TABLE dvds ("
"id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, "
"title TEXT NOT NULL, "
"year INTEGER NOT NULL, "
"duration INTEGER NOT NULL, "
"director_id INTEGER NOT NULL, "
"FOREIGN KEY (director_id) REFERENCES directors)")
db.commit()
return db
def add_dvd(db):
title = Console.get_string("Title", "title")
if not title:
return
director = Console.get_string("Director", "director")
if not director:
return
year = Console.get_integer("Year", "year", minimum=1896,
maximum=datetime.date.today().year)
duration = Console.get_integer("Duration (minutes)", "minutes",
minimum=0, maximum=60*48)
director_id = get_and_set_director(db, director)
cursor = db.cursor()
cursor.execute("INSERT INTO dvds "
"(title, year, duration, director_id) "
"VALUES (?, ?, ?, ?)",
(title, year, duration, director_id))
db.commit()
def get_and_set_director(db, director):
director_id = get_director_id(db, director)
if director_id is not None:
return director_id
cursor = db.cursor()
cursor.execute("INSERT INTO directors (name) VALUES (?)",
(director,))
db.commit()
return get_director_id(db, director)
def get_director_id(db, director):
cursor = db.cursor()
cursor.execute("SELECT id FROM directors WHERE name=?",
(director,))
fields = cursor.fetchone()
return fields[0] if fields is not None else None
def edit_dvd(db):
title, identity = find_dvd(db, "edit")
if title is None:
return
title = Console.get_string("Title", "title", title)
if not title:
return
cursor = db.cursor()
cursor.execute("SELECT dvds.year, dvds.duration, directors.name "
"FROM dvds, directors "
"WHERE dvds.director_id = directors.id AND "
"dvds.id=:id", dict(id=identity))
year, duration, director = cursor.fetchone()
director = Console.get_string("Director", "director", director)
if not director:
return
year = Console.get_integer("Year", "year", year, 1896,
datetime.date.today().year)
duration = Console.get_integer("Duration (minutes)", "minutes",
duration, minimum=0, maximum=60*48)
director_id = get_and_set_director(db, director)
cursor.execute("UPDATE dvds SET title=:title, year=:year, "
"duration=:duration, director_id=:director_id "
"WHERE id=:identity", locals())
db.commit()
def list_dvds(db):
cursor = db.cursor()
sql = ("SELECT dvds.title, dvds.year, dvds.duration, "
"directors.name FROM dvds, directors "
"WHERE dvds.director_id = directors.id")
start = None
if dvd_count(db) > DISPLAY_LIMIT:
start = Console.get_string("List those starting with "
"[Enter=all]", "start")
sql += " AND dvds.title LIKE ?"
sql += " ORDER BY dvds.title"
print()
if start is None:
cursor.execute(sql)
else:
cursor.execute(sql, (start + "%",))
for record in cursor:
print("{0[0]} ({0[1]}) {0[2]} minutes, by {0[3]}".format(
record))
def list_directors(db):
cursor = db.cursor()
cursor.execute("SELECT COUNT(*) FROM directors")
count = cursor.fetchone()[0]
sql = "SELECT name FROM directors"
start = None
if count > DISPLAY_LIMIT:
start = Console.get_string("List those starting with "
"[Enter=all]", "start")
sql += " WHERE name LIKE ?"
sql += " ORDER BY name"
print()
if start is None:
cursor.execute(sql)
else:
cursor.execute(sql, (start + "%",))
for fields in cursor:
print(fields[0])
def remove_dvd(db):
title, identity = find_dvd(db, "remove")
if title is None:
return
ans = Console.get_bool("Remove {0}?".format(title), "no")
if ans:
cursor = db.cursor()
cursor.execute("DELETE FROM dvds WHERE id=?", (identity,))
db.commit()
def import_(db):
filename = Console.get_string("Import from", "filename")
if not filename:
return
try:
tree = xml.etree.ElementTree.parse(filename)
except (EnvironmentError,
xml.parsers.expat.ExpatError) as err:
print("ERROR:", err)
return
cursor = db.cursor()
cursor.execute("DELETE FROM directors")
cursor.execute("DELETE FROM dvds")
for element in tree.findall("dvd"):
get_and_set_director(db, element.get("director"))
for element in tree.findall("dvd"):
try:
year = int(element.get("year"))
duration = int(element.get("duration"))
title = element.text.strip()
director_id = get_director_id(db, element.get("director"))
cursor.execute("INSERT INTO dvds "
"(title, year, duration, director_id) "
"VALUES (?, ?, ?, ?)",
(title, year, duration, director_id))
except ValueError as err:
db.rollback()
print("ERROR:", err)
break
else:
db.commit()
count = dvd_count(db)
print("Imported {0} dvd{1}".format(count, Util.s(count)))
def dvd_count(db):
cursor = db.cursor()
cursor.execute("SELECT COUNT(*) FROM dvds")
return cursor.fetchone()[0]
def export(db):
TITLE, YEAR, DURATION, DIRECTOR = range(4)
filename = os.path.join(tempfile.gettempdir(), "dvds.xml")
cursor = db.cursor()
cursor.execute("SELECT dvds.title, dvds.year, dvds.duration, "
"directors.name FROM dvds, directors "
"WHERE dvds.director_id = directors.id "
"ORDER BY dvds.title ")
try:
with open(filename, "w", encoding="utf8") as fh:
fh.write('<?xml version="1.0" encoding="UTF-8"?>\n')
fh.write("<dvds>\n")
for record in cursor:
fh.write('<dvd year="{0}" duration="{1}" '
'director={2}>'.format(
record[YEAR], record[DURATION],
xml.sax.saxutils.quoteattr(record[DIRECTOR])))
fh.write(xml.sax.saxutils.escape(record[TITLE]))
fh.write("</dvd>\n")
fh.write("</dvds>\n")
except EnvironmentError as err:
print(err)
count = dvd_count(db)
print("exported {0} dvd{1} to {2}".format(
count, Util.s(count), filename))
def quit(db):
if db is not None:
count = dvd_count(db)
db.commit()
db.close()
print("Saved {0} dvd{1}".format(count, Util.s(count)))
sys.exit()
def find_dvd(db, message):
message = "(Start of) title to " + message
cursor = db.cursor()
while True:
start = Console.get_string(message, "title")
if not start:
return (None, None)
cursor.execute("SELECT title, id FROM dvds "
"WHERE title LIKE ? ORDER BY title",
(start + "%",))
records = cursor.fetchall()
if len(records) == 0:
print("There are no dvds starting with", start)
continue
elif len(records) == 1:
return records[0]
elif len(records) > DISPLAY_LIMIT:
print("Too many dvds ({0}) start with {1}; try entering "
"more of the title".format(len(records), start))
continue
else:
for i, record in enumerate(records):
print("{0}: {1}".format(i + 1, record[0]))
which = Console.get_integer("Number (or 0 to cancel)",
"number", minimum=1, maximum=len(records))
return records[which - 1] if which != 0 else (None, None)
main()