Files
pyShelf/lib/storage.py
Jon Banafato 98c0a4fc3d Use isort
[isort](https://isort.readthedocs.io/en/latest/) sorts your Python
imports so you don't have to. This makes sure that imports are always
where they should be and prevents issues like duplicated imports and
merge conflicts. Using pre-commit, this can be done automatically
without any manual steps.

Depends on #9.
2019-11-09 10:45:01 -05:00

74 lines
2.1 KiB
Python

#!/usr/bin/python
import sqlite3
import sys
# sys.path.insert(1, '../')
from config import Config
db_pointer = Config().catalogue_db
class Storage:
"""Contains all methods for system storage"""
def __init__(self):
self.db_file = db_pointer
self.database()
self.create_tables()
def database(self):
"""Create database cursor"""
try:
self.db = sqlite3.connect(self.db_file)
self.cursor = self.db.cursor()
return True
except Exception as e:
return False
def create_tables(self):
"""Create table structure"""
q_check = "SELECT * FROM books"
q_create = '''CREATE TABLE books(title text, author text,
categories text, cover blob, pages int, progress int,
file_name text)'''
try:
self.cursor.execute(q_check)
except sqlite3.OperationalError as e:
self.cursor.execute(q_create)
def insert_book(self, book):
"""
Insert book in database
:returns: True if succeeds False if not
"""
q_x = '''SELECT title FROM books WHERE EXISTS(SELECT * from books WHERE `title` = ?)'''
q = '''INSERT INTO books (title, author, cover, file_name) values (?, ?, ?, ?)'''
try:
try: cover_image = book[2].data
except: cover_image = book[2]
x = self.cursor.execute(q_x, (book[0],))
try: len(x.fetchone()) > 0
except Exception:
if not book[2]: # If cover image is missing unset entry
cover_image = None
self.cursor.execute(q, (book[0], book[1], cover_image, book[3]))
return True
except Exception as e:
print(e)
return False
def book_paths_list(self):
q = '''SELECT file_name FROM books'''
x = self.cursor.execute(q)
try: x = x.fetchall()
except Exception: x = []
return x
def commit(self):
try: self.db.commit(); return True
except Exception as e: return e
def close(self):
self.db.close()
return True