refactored packaging

This commit is contained in:
Lars Noack
2022-11-29 11:47:46 +01:00
parent f121ab99cc
commit 989830d679
16 changed files with 69 additions and 427 deletions

View File

@@ -4,6 +4,7 @@ import os
import logging
import json
import requests
from pkg_resources import resource_string
from . import song
from ..utils.shared import (
@@ -13,17 +14,16 @@ from ..utils.shared import (
logger = DATABASE_LOGGER
class Database:
def __init__(self, path_to_db: str, db_structure: str, db_structure_fallback: str, reset_anyways: bool = False):
def __init__(self, path_to_db: str, reset_anyways: bool = False):
self.path_to_db = path_to_db
self.connection = sqlite3.connect(self.path_to_db)
self.cursor = self.connection.cursor()
# init database
self.init_db(database_structure=db_structure, database_structure_fallback=db_structure_fallback,
reset_anyways=reset_anyways)
self.init_db(reset_anyways=reset_anyways)
def init_db(self, database_structure: str, database_structure_fallback: str, reset_anyways: bool = False):
def init_db(self, reset_anyways: bool = False):
# check if db exists
exists = True
try:
@@ -38,20 +38,11 @@ class Database:
if reset_anyways or not exists:
# reset the database if reset_anyways is true or if an error has been thrown previously.
logger.info("Creating/Reseting Database.")
logger.info(f"Reseting the database.")
if not os.path.exists(database_structure):
logger.info("database structure file doesn't exist yet, fetching from github")
r = requests.get(database_structure_fallback)
with open(database_structure, "w") as f:
f.write(r.text)
# read the file
with open(database_structure, "r") as database_structure_file:
query = database_structure_file.read()
self.cursor.executescript(query)
self.connection.commit()
query = resource_string("music_kraken", "static_files/temp_database_structure.sql").decode('utf-8')
self.cursor.executescript(query)
self.connection.commit()
def add_artist(
self,

View File

@@ -2,8 +2,6 @@ from .database import Database
from ..utils.shared import (
TEMP_DATABASE_PATH,
DATABASE_STRUCTURE_FILE,
DATABASE_STRUCTURE_FALLBACK,
DATABASE_LOGGER
)
@@ -12,7 +10,7 @@ logger = DATABASE_LOGGER
class TempDatabase(Database):
def __init__(self) -> None:
super().__init__(TEMP_DATABASE_PATH, DATABASE_STRUCTURE_FILE, DATABASE_STRUCTURE_FALLBACK, False)
super().__init__(TEMP_DATABASE_PATH, False)
temp_database = TempDatabase()