Implemented new logging functions, refactored to suit.

This commit is contained in:
th3r00t
2020-08-25 13:59:06 -04:00
parent 59719c19f2
commit f2729e05db
9 changed files with 59 additions and 69 deletions

View File

@@ -1,5 +1,6 @@
import json
import pathlib
from loguru import logger
@@ -8,49 +9,49 @@ class Config:
Main System Configuration
"""
_fp = "config.json"
def __init__(self, root):
"""
Initialize main configuration options
"""
_cp = pathlib.Path.joinpath(root, self._fp)
_data = self.open_file(_cp)
self._fp = "config.json"
self._cp = pathlib.Path.joinpath(root, self._fp)
self._data = self.open_file()
self.root = root
self.logger = self.get_logger()
self.book_path = _data["BOOKPATH"]
self.TITLE = _data["TITLE"]
self.VERSION = _data["VERSION"]
self.book_path = self._data["BOOKPATH"]
self.TITLE = self._data["TITLE"]
self.VERSION = self._data["VERSION"]
self.TITLE = self.TITLE + " ver " + self.VERSION
self.book_shelf = _data["BOOKSHELF"]
self.catalogue_db = _data["DATABASE"]
self.user = _data["USER"]
self.password = _data["PASSWORD"]
self.db_host = _data["DB_HOST"]
self.db_port = _data["DB_PORT"]
self.book_shelf = self._data["BOOKSHELF"]
self.catalogue_db = self._data["DATABASE"]
self.user = self._data["USER"]
self.password = self._data["PASSWORD"]
self.db_host = self._data["DB_HOST"]
self.db_port = self._data["DB_PORT"]
self.file_array = [
self.book_shelf,
]
self.auto_scan = True
self.allowed_hosts = _data["ALLOWED_HOSTS"]
self.db_user = _data["USER"]
self.db_pass = _data["PASSWORD"]
self.SECRET = _data["SECRET"]
self.allowed_hosts = self._data["ALLOWED_HOSTS"]
self.db_user = self._data["USER"]
self.db_pass = self._data["PASSWORD"]
self.SECRET = self._data["SECRET"]
def get_logger(self):
_logger = logger
_logger.add(pathlib.PurePath(self.root, 'data','pyShelf_{time}.log'), rotation="10 MB", loop=None)
_logger.add(pathlib.PurePath(self.root, 'data','pyShelf_{time}.log'),
rotation="10 MB", enqueue=True, colorize=True)
return _logger
@staticmethod
def open_file(_cp):
def open_file(self):
"""
Opens config.json and reads in configuration options
"""
with open(str(_cp), "r") as read_file:
with open(str(self._cp), "r") as read_file:
data = json.load(read_file)
return data
def django_secret(self, _data):
def django_secret(self):
pass

View File

@@ -6,7 +6,6 @@ import re
import zipfile
from bs4 import BeautifulSoup
from mobi import Mobi
from .api_hooks import DuckDuckGo
@@ -14,7 +13,6 @@ from .config import Config
from .storage import Storage
class Catalogue:
"""
Decodes book metadata for storage
@@ -50,8 +48,6 @@ class Catalogue:
self.file_list.append(self.scan_folder(_path))
else:
self.file_list.append(_path)
self.config.logger.info(_path)
print(_path+"\n")
def filter_books(self):
"""
@@ -65,7 +61,7 @@ class Catalogue:
try:
self.books = list(filter(regx.search, filter(None, self.file_list)))
except TypeError as e:
print(e)
self.config.logger.error(e)
"""
for book in self.books:
self._book_list_expanded[book] = self.process_by_filetype(book)
@@ -73,8 +69,6 @@ class Catalogue:
"""
def process_by_filetype(self, book):
print(str(book), end='\r', flush=True)
if book.endswith(".epub"):
epub = self.process_epub(book)
return self.extract_metadata_epub(epub)
@@ -210,6 +204,7 @@ class Catalogue:
# ftags = ftags.replace(" ", ",")
except KeyError:
ftags = None
return [
title,
author,
@@ -286,7 +281,6 @@ class Catalogue:
db.insert_book(book)
inserted = db.commit()
if inserted is not True:
print(inserted)
if input("Continue ? y/n") == "y":
pass
self.config.logger.error("Failed storing {} in database".format(str(book)))
pass
db.close()

View File

@@ -1,8 +1,10 @@
#!/usr/bin/env python3
import asyncio
import os
import time
import asyncio
import websockets
from .config import Config
from .storage import Storage
@@ -11,16 +13,11 @@ class InitFiles:
"""First run file creation operations"""
def __init__(self, file_array):
print("Checking for program files")
for _pointer in file_array:
time.sleep(1)
if not os.path.isfile(_pointer):
self.CreateFile(_pointer)
print("%s created" % _pointer)
else:
print("%s present" % _pointer)
time.sleep(1)
print("File check complete.")
def CreateFile(self, _pointer):
"""

View File

@@ -1,6 +1,7 @@
#!/usr/bin/python
import re
import datetime
import re
import psycopg2
@@ -34,7 +35,7 @@ class Storage:
set_perms.cursor.execute(_q)
set_perms.close()
except Exception as e:
print(e)
self.config.logger.error(e)
set_perms.close()
def create_tables(self):
@@ -71,11 +72,12 @@ class Storage:
book[4], # descr
book[5], # ident
book[6], # publisher
datetime.datetime.now(),
datetime.datetime.now(),
book[8], # rights
book[9], # tags
),
)
self.config.logger.info(book[0])
return True
except Exception as e:
if e.pgcode == '22007': # psycopg2's error code for invalid date
@@ -92,7 +94,7 @@ class Storage:
try:
x = self.cursor.fetchall()
except psycopg2.Error as e:
print(e)
self.config.logger.error(e)
x = []
return x
@@ -146,8 +148,9 @@ class Storage:
(collection, book_id_id) VALUES ('%s',%s)"""
% (_s, book[0])
)
self.config.logger.info("Collection {} Added".format(_s))
except Exception as e:
print(e)
self.config.logger.error(e)
_collections.append(_p)
self.db.commit()
self.close()

View File

@@ -11,13 +11,13 @@ from .lib.storage import Storage
sys.path.append(os.path.abspath("."))
def MakeCollections(root):
def MakeCollections(root, **kwargs):
_t1 = time.time()
config = Config(root) # Get configuration settings
try: config = kwargs['config']
except KeyError as e: config = Config(root)
# InitFiles(config.file_array) # Initialize file system
_storage = Storage(config)
_storage.make_collections()
_t2 = time.time()
scan_time = round(_t2 - _t1)
print("Collections Made.")
print("Time %s seconds" % scan_time)
config.logger.info("Collections made in {}".format(scan_time))

View File

@@ -11,18 +11,18 @@ from .lib.storage import Storage
sys.path.append(os.path.abspath("."))
def execute_scan(root):
def execute_scan(root, **kwargs):
"""
Main scan execution
:param root: Project root. Required to properly execute program. Sends to configuration.
"""
_t1 = time.time()
config = Config(root) # Get configuration settings
try: config = kwargs["config"];
except KeyError as e: config = Config(root) # Get configuration settings
InitFiles(config.file_array) # Initialize file system
Storage(config).check_ownership()
catalogue = Catalogue(config) # Open the Catalogue
catalogue.import_books()
_t2 = time.time()
scan_time = round(_t2 - _t1)
print("Scan Completed.")
print("Scan Time %s seconds" % scan_time)
config.logger.info("Scan Completed in {} seconds".format(scan_time))

View File

@@ -32,7 +32,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = CONFIG.SECRET
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = TEMPLATE_DEBUG = False
DEBUG = TEMPLATE_DEBUG = True
if DEBUG is True:
from pudb.remote import set_trace
ALLOWED_HOSTS = CONFIG.allowed_hosts