Projects/worblehat-old
Projects
/
worblehat-old
Archived
12
0
Fork 0

Begynt å implementere kommandoene for å vise bøker.

This commit is contained in:
Øystein Ingmar Skartsæterhagen 2011-10-08 11:38:13 +00:00
parent f39cbbe957
commit dcb6982d24
1 changed files with 165 additions and 37 deletions

View File

@ -7,64 +7,183 @@ import pgdb
from fileformat import read_actionlist, write_actionlist from fileformat import read_actionlist, write_actionlist
from util import * from util import *
# connection = pgdb.connect(database='oysteini_pbb2',
# user='oysteini_pbb',
# password='lio5Aide',
# host='postgres.pvv.ntnu.no');
# c = connection.cursor()
# c.execute('SELECT * from book')
# print fetchall_dict(c)
q_list_books = \
'SELECT isbn, book.id AS id, title, category, person.id AS person_id, ' \
' lastname, firstname ' \
'FROM book ' \
'LEFT JOIN bookperson ON book.isbn=bookperson.book ' \
'LEFT JOIN person ON bookperson.person=person.id'
def connect_to_db():
connection = pgdb.connect(database='oysteini_pbb2', connection = pgdb.connect(database='oysteini_pbb2',
user='oysteini_pbb', user='oysteini_pbb',
password='lio5Aide', password='lio5Aide',
host='postgres.pvv.ntnu.no'); host='postgres.pvv.ntnu.no')
return connection
def get_by_id(connection, id):
c = connection.cursor() c = connection.cursor()
c.execute('SELECT * from book') q_book = 'SELECT * FROM book WHERE isbn=%(id)s OR id=%(id)s'
print fetchall_dict(c) q_person = 'SELECT * FROM person WHERE id=%(id)s'
q_cat = 'SELECT * FROM category WHERE id=%(id)s'
for (typ,q) in [('book', q_book),
('person', q_person),
('category', q_cat)]:
c.execute(q, {'id': id})
if c.rowcount > 0:
d = fetchone_dict(c)
d['type'] = typ
return d
return None
def show(ids, commit_format=False, tmp_file=False): def show_book(book):
s = ''
if book['id']:
s += 'book %s %s\n' % (book['isbn'], book['id'])
else:
s += 'book %s\n' % book['isbn']
s += 'Title: %s\n' % book['title']
if book['subtitle']:
s += 'Subtitle: %s\n' % book['subtitle']
s += 'ISBN: %s\n' % book['isbn']
s += 'Persons:\n'
# TODO
if book['series']:
s += 'Part of series: %s %s\n' % (book['series'], book['series_title'])
s += 'Category: %s\n' % book['category']
if book['publisher']:
s += 'Publisher: %s\n' % book['publisher']
if book['published_year']:
s += 'Published year: %s\n' % book['published_year']
if book['edition']:
s += 'Edition: %s\n' % book['edition']
if book['pages']:
s += 'Number of pages: %s\n' % book['pages']
if book['description']:
s += ('Description:\n%s\n' %
'\n'.join(map(lambda line: ' '+line,
book['description'].split('\n'))))
return s
def show_person(person):
return ''
def show_category(category):
return ''
def show(connection, ids, commit_format=False, tmp_file=False):
objects = map(lambda id: get_by_id(connection, id), ids)
for i in range(len(ids)):
if not objects[i]:
objects[i] = 'No object with id %s.\n' % ids[i]
elif not commit_format:
show_funs = {'book': show_book,
'person': show_person,
'category': show_category}
show_fun = show_funs[objects[i]['type']]
objects[i] = show_fun(objects[i])
if commit_format:
output = write_actionlist(objects)
else:
output = '\n'.join(objects)
if tmp_file:
filename = write_tmpfile('.'.join(ids), output)
print filename
return filename
else:
print output.strip()
def list_books(connection):
c = connection.cursor()
c.execute(q_list_books)
#print fetchall_dict(c)
s = ''
last_isbn = None
for i in xrange(c.rowcount):
d = fetchone_dict(c)
if d['isbn'] != last_isbn:
if last_isbn != None:
s += '\n'
s += '%-13s %-10s %-40s' % (d['isbn'], str_or_empty(d['id']), d['title'])
if d['person_id']:
s += ' ' + d['person_id']
else:
s += ', ' + d['person_id']
last_isbn = d['isbn']
print s
def list_persons(connection):
pass pass
def list_cmd(what): def list_categories(connection):
pass pass
def search_book(search_strings, search_description=False): def list_cmd(connection, what):
funs = { 'book': list_books,
'person': list_persons,
'category': list_categories }
fun = funs[what]
fun(connection)
def search_book(connection, search_strings, search_description=False):
pass pass
def search_person(search_strings): def search_person(connection, search_strings):
pass pass
def commit(filename=None): def commit(connection, filename=None):
pass pass
def edit(ids): def edit(connection, ids):
pass pass
def map_cmd(shelfname=None, category=None): def map_cmd(connection, shelfname=None, category=None):
pass pass
commands = { 'show': commands = { 'show':
{ 'args': [('ids', (1,None))], { 'args': [('ids', (1,None))],
'options': ['commit_format', 'tmp_file'], 'options': ['commit_format', 'tmp_file'],
'fun': show }, 'fun': show,
'use_db': True },
'list': 'list':
{ 'args': [('what', (1,1))], { 'args': [('what', (1,1))],
'options': [], 'options': [],
'fun': list_cmd }, 'fun': list_cmd,
'use_db': True },
'search': 'search':
{ 'args': [('search_strings', (1,None))], { 'args': [('search_strings', (1,None))],
'options': ['search_description'], 'options': ['search_description'],
'fun': search_book }, 'fun': search_book,
'use_db': True },
'search-person': 'search-person':
{ 'args': [('search_strings', (1,None))], { 'args': [('search_strings', (1,None))],
'options': [], 'options': [],
'fun': search_person }, 'fun': search_person,
'use_db': True },
'commit': 'commit':
{ 'args': [('filename', (0,1))], { 'args': [('filename', (0,1))],
'options': [], 'options': [],
'fun': commit }, 'fun': commit,
'use_db': True },
'edit': 'edit':
{ 'args': [('ids', (1,None))], { 'args': [('ids', (1,None))],
'options': [], 'options': [],
'fun': edit }, 'fun': edit,
'use_db': True },
'map': 'map':
{ 'args': [('shelfname', (0,1)), ('category', (0,1))], { 'args': [('shelfname', (0,1)), ('category', (0,1))],
'options': [], 'options': [],
'fun': map_cmd } 'fun': map_cmd,
'use_db': True }
} }
flags = { 'commit_format': flags = { 'commit_format':
@ -165,7 +284,16 @@ def invoke_command(command, args):
cmd_decl = commands[command] cmd_decl = commands[command]
cmd_decl['fun'](**args) cmd_decl['fun'](**args)
cmdline_parsed = parse_cmdline(sys.argv[1:]) def main(argv):
print 'command line parsed to:', cmdline_parsed cmdline_parsed = parse_cmdline(argv[1:])
invoke_command(cmdline_parsed['command'], #print 'command line parsed to:', cmdline_parsed
cmdline_parsed['args']) command = cmdline_parsed['command']
args = cmdline_parsed['args']
if commands[cmdline_parsed['command']]['use_db']:
connection = connect_to_db()
args['connection'] = connection
invoke_command(command, args)
if __name__ == "__main__":
main(sys.argv)