first stage of dynamic UI

This commit is contained in:
Mike
2019-11-07 22:48:42 -05:00
parent d164850def
commit 9fafc0b228
8 changed files with 141 additions and 22 deletions

View File

@@ -2,6 +2,7 @@ class Config:
"""Main System Configuration"""
def __init__(self):
self.book_path = "books/"
self.TITLE = "pyShelf E-Book Server"
self.book_shelf = "data/shelf.json"
self.catalogue_db = "data/catalogue.db"
self.file_array = [

102
lib/display.py Normal file
View File

@@ -0,0 +1,102 @@
#!/usr/bin/python
import cgi
import sys
sys.path.insert(0, '../')
from config import Config
class Frontend():
"""Dynamic frontend display functions"""
def __init__(self, dimensions=[0, 0]):
"""
:param dimensions: array containing screen size [x, y]
"""
self.dimensions = dimensions
self.TITLE = Config().TITLE
def html_Headers(self):
"""
HTML headers
:returns _head: HTML render of page headers
"""
_head = """
<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
<meta http-equiv=\"X-UA-Compatible\" content=\"IE-edge\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
<link type=\"text/css\" rel=\"stylesheet\" href=\"/css/main.css\" />
<title>%s</title>
</head>
""" % self.TITLE
return _head
def app_Headers(self):
"""
App specific headers
:returns _head: HTML render of application specific headers
"""
_head = """
<body>
<div id=\"app\">
<div class=\"app_header\">
<h1 class=\"app_hdr shadow\">pyShelf</h1>
<h2> class=\"app_subhdr shadow\">Open Source E-book Server</h2>
</div>
"""
return _head
def app_body(self, nav, shelf):
"""
Main interface body, and navigation
:param nav: nav[] system navigation list
:param shelf: shelf[0{path:"",title:"",cover:"",author:""}]
:returns _body: HTML render of page body
"""
_body = """
<div class=\"app_body\">
<div class=\"left_col\">
%s
</div>
<div class=\"shelf\">
<div class=\"shelf_contents\">
%s
</div>
</div>
</div>
""" %(nav, shelf)
return _body
def app_footer(self):
"""
Main interface footer; Closes HTML
:returns _footer: HTML render of page footer
"""
_footer = """
<div class=\"app_footer\">
<div class=\"python_logo\">
<img src=\"/img/py.png\" id=\"python_logo\" />
</div>
</div>
</div>
</body>
</html>
"""
return _footer
def compile(self, nav, shelf):
"""
Compiles user interface
:returns _ui: Compiled HTML for page layout
"""
_head = self.html_Headers() + self.app_Headers()
_body = self.app_body(nav, shelf)
_foot = self.app_footer()
try:
_ui = _head + _body + _foot
return _ui
except Exception as e:
return e

View File

@@ -65,11 +65,15 @@ class RequestHandler(BaseHTTPRequestHandler):
try: serve_file.close()
except Exception: pass
class BookDisplay:
"""All functions related to displaying book information in the HTML UI"""
def __init__(self):
"""Initialize class variables"""
"""
Initialize class variables
:return: None
"""
self.books_per_page = None
self.current_page = 0
self.thumbnail_size = [200, 300]
@@ -77,22 +81,39 @@ class BookDisplay:
self.total_pages = None
def nextPage(self):
"""Goto next book page"""
"""
Goto next book page
:return: new current_page
"""
self.current_page += 1
return self.current_page
def previousPage(self):
"""
Goto previous book page
:return: new current_page
"""
self.current_page -= 1
return self.current_page
def booksPerPage(self, screen_size):
"""
Set books per page
:param screen_size: Array containing x,y pixel sizes
:return: self.books_per_page
"""
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 Frontend"""
"""
HTTP server functions required to display e-books
"""
def __init__(self):
# TODO Get server Ip Address
self.server_address = ('', 8000)
@@ -107,17 +128,8 @@ class BookServer:
else:
self.close_prompt()
def run(self, test=0):
def run(self):
"""Start HTTP Server"""
self.httpd = HTTPServer(self.server_address, self.handler)
if test != 0:
try:
self.httpd.serve_forever()
self.httpd.handle_request()
self.close()
return True
except Exception:
return False
try:
print("Server running @ http://127.0.0.1:8000")
self.httpd.serve_forever()

View File

@@ -5,12 +5,14 @@ from config import Config
from lib.library import Catalogue
from lib.pyShelf import InitFiles
from lib.pyShelf import BookServer
from lib.pyShelf import BookDisplay
from lib.display import Frontend
# sys.path.insert(1, 'lib/')
config = Config() # Get configuration settings
InitFiles(config.file_array) # Initialize file system
Catalogue = Catalogue() # Open the Catalogue
UI = Frontend()
Server = BookServer()
# new_books = Catalogue.new_files()
Catalogue.import_books() # Filter Your books

View File

@@ -1 +0,0 @@
raelon@golumnsec.2706:1573068976

View File

@@ -1 +0,0 @@
raelon@golumnsec.2706:1573068976

View File

@@ -1 +0,0 @@
raelon@golumnsec.2706:1573068976

View File

@@ -2,16 +2,21 @@ import sys
import unittest
from lib.pyShelf import BookDisplay, BookServer
from lib.display import Frontend
sys.path.insert(0, '../lib')
sys.path.insert(1, '../')
class BookServerTest(unittest.TestCase):
def test_bookserver(self):
server = BookServer()
self.assertTrue(server.run())
def __init__(self):
self.dimensions = [900, 450]
def test_booksPerPage(self):
x, y = 900, 450
x, y = self.dimensions[0], self.dimensions[1]
self.assertGreater(BookDisplay().booksPerPage([x,y]), 0)
def test_Frontend(self):
x, y = self.dimensions[0], self.dimensions[1]
ui = Frontend([x, y]).compile("TestCase", "Test Shelf")
print(ui)
self.assertNotEqual(ui, False)