Massive code refactoring, and some test rewrites.

This commit is contained in:
Mike Young
2019-11-15 14:45:42 -05:00
parent e012436011
commit d7279a73a7
127 changed files with 3960 additions and 345 deletions

View File

@@ -1,15 +1,11 @@
#!/usr/bin/python
import mimetypes
import os
import zipfile
from http.server import BaseHTTPRequestHandler, HTTPServer
from config import Config
from lib.library import Catalogue
from lib.storage import Storage
from .config import Config
from .storage import Storage
config = Config()
Storage = Storage()
# config = Config()
# Storage = Storage()
class InitFiles:
@@ -29,47 +25,10 @@ class InitFiles:
f.close()
class RequestHandler(BaseHTTPRequestHandler):
"""Request Handler"""
def do_GET(self):
# TODO determine how to include stylesheets
self.send_response(200)
if self.path == '/':
self.path = '../static/index.html'
mimetype = 'text/html'
serve_file = open(self.path[1:]).read()
self.send_header('Content-type', mimetype)
self.end_headers()
self.wfile.write(bytes(serve_file, 'utf-8'))
elif self.path.split('.', 1)[1] == 'css':
self.path = '../static' + self.path
mimetype = 'text/css'
serve_file = open(self.path[1:]).read()
self.send_header('Content-type', mimetype)
self.end_headers()
self.wfile.write(bytes(serve_file, 'utf-8'))
elif self.path.endswith('.png'):
self.path = '../static' + self.path
mimetype = 'image/png'
serve_file = open(self.path[1:], 'rb')
# Important to rb read binary for images
self.send_header('Content-type', mimetype)
self.end_headers()
self.wfile.write(serve_file.read())
else:
self.send_response(404)
serve_file = "File Not Found"
mimetype = 'text/html'
self.send_header('Content-type', mimetype)
self.end_headers()
try: serve_file.close()
except Exception: pass
class BookDisplay:
"""All functions related to displaying book information in the HTML UI"""
def __init__(self):
def __init__(self, **kwargs):
"""
Initialize class variables
:return: None
@@ -79,6 +38,8 @@ class BookDisplay:
self.thumbnail_size = [200, 300]
self.thumbnail_scale = 1
self.total_pages = None
try: self.screen_size = kwargs['screen_size']
except Exception: self.screen_size = [900, 600]
def nextPage(self):
"""
@@ -105,45 +66,4 @@ class BookDisplay:
"""
x = (self.thumbnail_size[0] * self.thumbnail_scale) + 10
y = (self.thumbnail_size[1] * self.thumbnail_scale) + 10
self.books_per_page = int(screen_size[0]//x) * int(screen_size[1]//y)
return self.books_per_page
class BookServer:
"""
HTTP server functions required to display e-books
"""
def __init__(self):
self.server_address = ('', 8000)
self.handler = RequestHandler
self.httpd = HTTPServer(self.server_address, self.handler)
def close_prompt(self):
"""Prompt to close server"""
close = input("Close Server? y/n")
if close == 'y':
self.close()
return True
else:
self.close_prompt()
def run(self):
"""Start HTTP Server"""
try:
print("Server running @ http://127.0.0.1:8000")
self.httpd.serve_forever()
self.httpd.handle_request()
except KeyboardInterrupt:
print("Interrupt received, Closing Server")
self.close()
print("Server shutdown, Goodbye!")
return True
def close(self):
"""Stop HTTP Server"""
try:
self.httpd.server_close()
return True
except Exception:
return False
self.books_per_page = int(self.screen_size[0]//x) * int(self.screen_size[1]//y)