Begynt å implementere kommandoene for å vise bøker.
This commit is contained in:
parent
f39cbbe957
commit
dcb6982d24
202
cli/worblehat.py
202
cli/worblehat.py
@ -7,64 +7,183 @@ import pgdb
|
||||
from fileformat import read_actionlist, write_actionlist
|
||||
from util import *
|
||||
|
||||
connection = pgdb.connect(database='oysteini_pbb2',
|
||||
user='oysteini_pbb',
|
||||
password='lio5Aide',
|
||||
host='postgres.pvv.ntnu.no');
|
||||
# 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)
|
||||
# c = connection.cursor()
|
||||
# c.execute('SELECT * from book')
|
||||
# print fetchall_dict(c)
|
||||
|
||||
def show(ids, commit_format=False, tmp_file=False):
|
||||
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',
|
||||
user='oysteini_pbb',
|
||||
password='lio5Aide',
|
||||
host='postgres.pvv.ntnu.no')
|
||||
return connection
|
||||
|
||||
def get_by_id(connection, id):
|
||||
c = connection.cursor()
|
||||
q_book = 'SELECT * FROM book WHERE isbn=%(id)s OR id=%(id)s'
|
||||
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_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
|
||||
|
||||
def list_cmd(what):
|
||||
def list_categories(connection):
|
||||
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
|
||||
|
||||
def search_person(search_strings):
|
||||
def search_person(connection, search_strings):
|
||||
pass
|
||||
|
||||
def commit(filename=None):
|
||||
def commit(connection, filename=None):
|
||||
pass
|
||||
|
||||
def edit(ids):
|
||||
def edit(connection, ids):
|
||||
pass
|
||||
|
||||
def map_cmd(shelfname=None, category=None):
|
||||
def map_cmd(connection, shelfname=None, category=None):
|
||||
pass
|
||||
|
||||
commands = { 'show':
|
||||
{ 'args': [('ids', (1,None))],
|
||||
'options': ['commit_format', 'tmp_file'],
|
||||
'fun': show },
|
||||
{ 'args': [('ids', (1,None))],
|
||||
'options': ['commit_format', 'tmp_file'],
|
||||
'fun': show,
|
||||
'use_db': True },
|
||||
'list':
|
||||
{ 'args': [('what', (1,1))],
|
||||
'options': [],
|
||||
'fun': list_cmd },
|
||||
'fun': list_cmd,
|
||||
'use_db': True },
|
||||
'search':
|
||||
{ 'args': [('search_strings', (1,None))],
|
||||
'options': ['search_description'],
|
||||
'fun': search_book },
|
||||
{ 'args': [('search_strings', (1,None))],
|
||||
'options': ['search_description'],
|
||||
'fun': search_book,
|
||||
'use_db': True },
|
||||
'search-person':
|
||||
{ 'args': [('search_strings', (1,None))],
|
||||
'options': [],
|
||||
'fun': search_person },
|
||||
{ 'args': [('search_strings', (1,None))],
|
||||
'options': [],
|
||||
'fun': search_person,
|
||||
'use_db': True },
|
||||
'commit':
|
||||
{ 'args': [('filename', (0,1))],
|
||||
'options': [],
|
||||
'fun': commit },
|
||||
{ 'args': [('filename', (0,1))],
|
||||
'options': [],
|
||||
'fun': commit,
|
||||
'use_db': True },
|
||||
'edit':
|
||||
{ 'args': [('ids', (1,None))],
|
||||
'options': [],
|
||||
'fun': edit },
|
||||
{ 'args': [('ids', (1,None))],
|
||||
'options': [],
|
||||
'fun': edit,
|
||||
'use_db': True },
|
||||
'map':
|
||||
{ 'args': [('shelfname', (0,1)), ('category', (0,1))],
|
||||
'options': [],
|
||||
'fun': map_cmd }
|
||||
{ 'args': [('shelfname', (0,1)), ('category', (0,1))],
|
||||
'options': [],
|
||||
'fun': map_cmd,
|
||||
'use_db': True }
|
||||
}
|
||||
|
||||
flags = { 'commit_format':
|
||||
@ -165,7 +284,16 @@ def invoke_command(command, args):
|
||||
cmd_decl = commands[command]
|
||||
cmd_decl['fun'](**args)
|
||||
|
||||
cmdline_parsed = parse_cmdline(sys.argv[1:])
|
||||
print 'command line parsed to:', cmdline_parsed
|
||||
invoke_command(cmdline_parsed['command'],
|
||||
cmdline_parsed['args'])
|
||||
def main(argv):
|
||||
cmdline_parsed = parse_cmdline(argv[1:])
|
||||
#print 'command line parsed to:', cmdline_parsed
|
||||
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)
|
||||
|
||||
|
Reference in New Issue
Block a user