Added basic documentation to all backend objects

This commit is contained in:
Mike Young
2019-12-01 12:35:40 -05:00
parent c5ad0a2bb7
commit 9dc1acf6b9
13 changed files with 159 additions and 33 deletions

View File

@@ -32,6 +32,9 @@ class Config:
self.auto_scan = True
def open_file(self, _cp):
"""
Opens config.json and reads in configuration options
"""
with open(str(_cp), "r") as read_file:
data = json.load(read_file)
return data

View File

@@ -15,9 +15,9 @@ from .storage import Storage
class Catalogue:
"""Decodes and stores book information"""
"""Step One: filter_books"""
"""
Decodes book metadata for storage
"""
def __init__(self, config):
self.file_list = []
@@ -33,6 +33,9 @@ class Catalogue:
self.config = config
def scan_folder(self, _path=None):
"""
Scan folder by _path, allows recurisive scanning
"""
if _path is not None:
folder = _path
elif os.path.isdir(str(self.root_dir) + "/" + self.book_folder):
@@ -48,11 +51,10 @@ class Catalogue:
def filter_books(self):
"""
Scan book folder recursively for epub files
filter_books(0) -> Catalogue.books
filter_books(1) -> self.books[]
:param ret: 0 -> create class property -> dump json
:param ret: 1 -> create & return class property
Calls scan_folder and filters out book files
Proceeds to call process_book
:returns self._book_list_expanded: json string containing all book metadata
"""
self.scan_folder()
regx = re.compile(r"\.epub")
@@ -111,14 +113,23 @@ class Catalogue:
return book_details
def extract_content(self, book_zip, book):
"""
Opens epub as zip file filters then stores as list any files matching opf_regx
"""
content = book_zip.open(list(filter(self.opf_regx.search, book["files"]))[0])
return content
def extract_cover_html(self, book_zip, book):
"""
Opens epub as zip file filters then stores as list any files matching html_regx
"""
cover = book_zip.open(list(filter(self.html_regx.search, book["files"]))[0])
return cover
def extract_cover_image(self, book_zip, book):
"""
Opens epub as zip file filters then stores as list any files matching cover_regx
"""
cover = book_zip.open(list(filter(self.cover_regx.search, book["files"]))[0])
try:
cover = book_zip.read(cover.name)
@@ -127,6 +138,9 @@ class Catalogue:
return False
def compare_shelf_current(self):
"""
Calls storage system, gets list of books stored and compares against files on disk
"""
db = Storage(self.db_pointer, self.config)
stored = db.book_paths_list()
closed = db.close()
@@ -142,6 +156,11 @@ class Catalogue:
return c
def import_books(self, list=None):
"""
Main entry point for import operations.
Gets a list of new files via compare_shelf_current.
Iterates over list and inserts new books into database.
"""
book_list = self.compare_shelf_current()
db = Storage(self.db_pointer, self.config)
for book in book_list:

View File

@@ -25,7 +25,9 @@ class InitFiles:
print("File check complete.")
def CreateFile(self, _pointer):
"""Create the file"""
"""
Checks if file exists and creates it if not
"""
if not os.path.isdir(os.path.split(_pointer)[0]):
os.mkdir(os.path.split(_pointer)[0])
f = open(_pointer, "w+")
@@ -52,6 +54,7 @@ class BookDisplay:
def nextPage(self):
"""
## TODO Remove me
Goto next book page
:return: new current_page
"""
@@ -60,6 +63,7 @@ class BookDisplay:
def previousPage(self):
"""
## TODO Remove me
Goto previous book page
:return: new current_page
"""
@@ -68,8 +72,8 @@ class BookDisplay:
def booksPerPage(self, screen_size):
"""
## TODO Remove me
Set books per page
:param screen_size: Array containing x,y pixel sizes
:return: self.books_per_page
"""

View File

@@ -69,6 +69,9 @@ class Storage:
return False
def book_paths_list(self):
"""
Get file paths from database for comparison to system files
"""
q = """SELECT file_name FROM books"""
x = self.cursor.execute(q)
try:
@@ -78,6 +81,9 @@ class Storage:
return x
def commit(self):
"""
Commit database transactions
"""
try:
self.db.commit()
return True
@@ -85,5 +91,8 @@ class Storage:
return e
def close(self):
"""
Close database connection
"""
self.db.close()
return True

View File

@@ -11,6 +11,10 @@ sys.path.append(os.path.abspath("."))
def execute_scan(root):
"""
Main scan execution
:param root: Project root. Required to properly execute program. Sends to configuration.
"""
_t1 = time.time()
config = Config(root) # Get configuration settings
InitFiles(config.file_array) # Initialize file system

BIN
src/db.sqlite3 vendored

Binary file not shown.

View File

@@ -7,24 +7,29 @@ class Migration(migrations.Migration):
initial = True
dependencies = [
]
dependencies = []
operations = [
migrations.CreateModel(
name='Books',
name="Books",
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('author', models.CharField(max_length=255, null=True)),
('categories', models.CharField(max_length=255, null=True)),
('cover', models.BinaryField(editable=True, null=True)),
('pages', models.IntegerField(null=True)),
('progress', models.IntegerField(null=True)),
('file_name', models.CharField(max_length=255)),
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("title", models.CharField(max_length=255)),
("author", models.CharField(max_length=255, null=True)),
("categories", models.CharField(max_length=255, null=True)),
("cover", models.BinaryField(editable=True, null=True)),
("pages", models.IntegerField(null=True)),
("progress", models.IntegerField(null=True)),
("file_name", models.CharField(max_length=255)),
],
options={
'db_table': 'books',
},
options={"db_table": "books",},
),
]

34
src/interface/models.py Executable file
View File

@@ -0,0 +1,34 @@
from django.db import models
# Create your models here.
class Books(models.Model):
"""
pyShelfs Book Database class
:param title: Book title
:param author: Author
:param categories: Categories <-- Not implemented
:param cover: Cover image BinaryField
:param pages: # of pages <-- Not implemented
:param progress: Reader percentage <-- Not implented
:param file_name: Path to book
"""
class Meta:
db_table = "books"
def __str__(self):
return self.title
title = models.CharField(max_length=255)
author = models.CharField(max_length=255, null=True)
categories = models.CharField(max_length=255, null=True)
cover = models.BinaryField(null=True, editable=True)
pages = models.IntegerField(null=True)
progress = models.IntegerField(null=True)
file_name = models.CharField(max_length=255, null=False)
def get_absolute_url(self):
"""Returns the url to access a particular instance of MyModelName."""
return reverse("model-detail-view", args=[str(self.id)])