diff --git a/bookapp.py b/bookapp.py index d2284c6..43c7161 100644 --- a/bookapp.py +++ b/bookapp.py @@ -4,20 +4,71 @@ DB = BookDB() +def resolve_path(path): + funcs = { + '': books, + 'book': book, + } + + path = path.strip('/').split('/') + + func_name = path[0] + args = path[1:] + + try: + func = funcs[func_name] + except KeyError: + raise NameError + + return func, args + def book(book_id): - return "

a book with id %s

" % book_id + entry = DB.title_info(book_id) + if not entry: + raise NameError + page = """ +

{title}

+ + + + +
Author{author}
Publisher{publisher}
ISBN{isbn}
+Back to the list +""" + + return page.format(**entry) def books(): - return "

a list of books

" + book_list = DB.titles() + body = ["

Book Index

"] + item_template = '
  • {title}
  • ' + for book in book_list: + body.append(item_template.format(**book)) + return '\n'.join(body) def application(environ, start_response): - status = "200 OK" + # status = "200 OK" headers = [('Content-type', 'text/html')] - start_response(status, headers) - return ["

    No Progress Yet

    ".encode('utf8')] + try: + path=environ.get('PATH_INFO',None) + if not path: + raise NameError + func, args = resolve_path(path) + body = func(*args) + status = "200 OK" + except NameError: + status = "404 Not Found" + body = "

    Not Found

    " + except: + status = "500 Internal Server Error" + body = "

    Vague Server Error