From 4c041ef7ffd2c208a46a9fe00cb32e60316f8758 Mon Sep 17 00:00:00 2001 From: Hellow2 Date: Thu, 2 Mar 2023 07:59:53 +0100 Subject: [PATCH] tried fixing shit --- src/metal_archives.py | 13 +++--- src/music_kraken/__init__.py | 19 ++------- src/music_kraken/database/data_models.py | 4 +- src/music_kraken/database/database.py | 47 ++++++++++++++++++---- src/music_kraken/database/temp_database.py | 9 ++--- src/music_kraken/database/write.py | 2 +- src/music_kraken/objects/__init__.py | 1 + src/music_kraken/objects/collection.py | 2 +- src/music_kraken/objects/parents.py | 2 +- src/music_kraken/objects/song.py | 2 +- src/music_kraken/objects/source.py | 5 +-- 11 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/metal_archives.py b/src/metal_archives.py index d0a1d4f..8016d35 100644 --- a/src/metal_archives.py +++ b/src/metal_archives.py @@ -1,15 +1,14 @@ -from music_kraken import ( +from music_kraken.objects import ( Song, - Database, - Artist, - Album + ) + from music_kraken.pages import ( EncyclopaediaMetallum ) - +""" test_db = Database("test.db") # test_db.reset() @@ -64,3 +63,7 @@ print_song(song) artist = song.main_artist_list[0] artist = EncyclopaediaMetallum.fetch_artist_details(artist, flat=False) print_artist(artist) +""" + +results = EncyclopaediaMetallum.search_by_query("#a only smile") +print(results) diff --git a/src/music_kraken/__init__.py b/src/music_kraken/__init__.py index 4e405e1..9d053e2 100644 --- a/src/music_kraken/__init__.py +++ b/src/music_kraken/__init__.py @@ -6,6 +6,7 @@ import logging import os from . import ( + objects, database, pages ) @@ -37,33 +38,21 @@ logging.getLogger("musicbrainzngs").setLevel(logging.WARNING) musicbrainzngs.set_useragent("metadata receiver", "0.1", "https://github.com/HeIIow2/music-downloader") # define the most important values and function for import in the __init__ file -Song = database.Song -Artist = database.Artist -Source = database.Source -SourceTypes = database.SourceTypes -SourcePages = database.SourcePages -Target = database.Target -Lyrics = database.Lyrics -Album = database.Album -MusicObject = database.MusicObject - -ID3Timestamp = database.ID3Timestamp - cache = database.cache Database = database.Database -def get_options_from_query(query: str) -> List[MusicObject]: +def get_options_from_query(query: str) -> List[objects.MusicObject]: options = [] for MetadataPage in pages.MetadataPages: options.extend(MetadataPage.search_by_query(query=query)) return options -def get_options_from_option(option: MusicObject) -> List[MusicObject]: +def get_options_from_option(option: objects.MusicObject) -> List[objects.MusicObject]: for MetadataPage in pages.MetadataPages: option = MetadataPage.fetch_details(option, flat=False) return option.get_options() -def print_options(options: List[MusicObject]): +def print_options(options: List[objects.MusicObject]): print("\n".join([f"{str(j).zfill(2)}: {i.get_option_string()}" for j, i in enumerate(options)])) def cli(): diff --git a/src/music_kraken/database/data_models.py b/src/music_kraken/database/data_models.py index 6b029a2..7deff35 100644 --- a/src/music_kraken/database/data_models.py +++ b/src/music_kraken/database/data_models.py @@ -108,8 +108,8 @@ class Source(BaseModel): url: str = CharField() content_type: str = CharField() - content_id: int = IntegerField() - content: ForeignKeyField = ForeignKeyField('self', backref='content_items', null=True) + content_id: int = CharField() + # content: ForeignKeyField = ForeignKeyField('self', backref='content_items', null=True) @property def content_object(self) -> Union[Song, Album, Artist]: diff --git a/src/music_kraken/database/database.py b/src/music_kraken/database/database.py index 6d3e533..d20e793 100644 --- a/src/music_kraken/database/database.py +++ b/src/music_kraken/database/database.py @@ -1,6 +1,7 @@ # Standard library from typing import Optional, Union, List from enum import Enum +from playhouse.migrate import * # third party modules from peewee import ( @@ -12,9 +13,9 @@ from peewee import ( # own modules from . import ( data_models, - write, - objects, + write ) +from .. import objects class DatabaseType(Enum): @@ -76,6 +77,18 @@ class Database: raise ValueError("Invalid database type specified.") + + @property + def migrator(self) -> SchemaMigrator: + if self.db_type == DatabaseType.SQLITE: + return SqliteMigrator(self.database) + + if self.db_type == DatabaseType.MYSQL: + return MySQLMigrator(self.database) + + if self.db_type == DatabaseType.POSTGRESQL: + return PostgresqlMigrator(self.database) + def initialize_database(self): """ Connect to the database @@ -84,20 +97,38 @@ class Database: """ self.database = self.create_database() self.database.connect() + + migrator = self.migrator + + for model in data_models.ALL_MODELS: + model = model.Use(self.database) + + if self.database.table_exists(model): + migration_operations = [ + migrator.add_column( + "some field", field[0], field[1] + ) + for field in model._meta.fields.items() + ] + + migrate(*migration_operations) + else: + self.database.create_tables([model], safe=True) - self.database.create_tables(data_models.ALL_MODELS, safe=True) + #self.database.create_tables([model.Use(self.database) for model in data_models.ALL_MODELS], safe=True) """ upgrade old databases. If a collumn has been added in a new version this adds it to old Tables, without deleting the data in legacy databases """ + for model in data_models.ALL_MODELS: - for field_name, field_obj in model._meta.fields.items(): - # check if the field exists in the database - if not self.database.table_column_exists(model._meta.db_table, field_name): - # add the missing column to the table - self.database.add_column(model._meta.db_table, field_name, field_obj) + model = model.Use(self.database) + + + + print(model._meta.fields) def push(self, database_object: objects.MusicObject): """ diff --git a/src/music_kraken/database/temp_database.py b/src/music_kraken/database/temp_database.py index 048b1bd..2879ffa 100644 --- a/src/music_kraken/database/temp_database.py +++ b/src/music_kraken/database/temp_database.py @@ -1,4 +1,4 @@ -from .old_database import Database +from .database import Database, DatabaseType from ..utils.shared import ( TEMP_DATABASE_PATH, @@ -9,11 +9,8 @@ logger = DATABASE_LOGGER class TempDatabase(Database): - def __init__(self, reset_on_start: bool = True) -> None: - super().__init__(TEMP_DATABASE_PATH) - - if reset_on_start: - self.reset() + def __init__(self) -> None: + super().__init__(db_type=DatabaseType.SQLITE, db_name=TEMP_DATABASE_PATH) temp_database = TempDatabase() diff --git a/src/music_kraken/database/write.py b/src/music_kraken/database/write.py index 73e78d8..fc29027 100644 --- a/src/music_kraken/database/write.py +++ b/src/music_kraken/database/write.py @@ -9,7 +9,7 @@ from peewee import ( Model ) -from . import objects +from .. import objects from . import data_models # just a Type for type hintung. You can't do anything with it. diff --git a/src/music_kraken/objects/__init__.py b/src/music_kraken/objects/__init__.py index 2f923e6..dcdb1e2 100644 --- a/src/music_kraken/objects/__init__.py +++ b/src/music_kraken/objects/__init__.py @@ -12,6 +12,7 @@ MusicObject = parents.DatabaseObject ID3Mapping = metadata.Mapping ID3Timestamp = metadata.ID3Timestamp +Source = source.Source SourceTypes = source.SourceTypes SourcePages = source.SourcePages SourceAttribute = source.SourceAttribute diff --git a/src/music_kraken/objects/collection.py b/src/music_kraken/objects/collection.py index 9ea2ee0..73471a4 100644 --- a/src/music_kraken/objects/collection.py +++ b/src/music_kraken/objects/collection.py @@ -1,7 +1,7 @@ from typing import List from .source import SourceAttribute -from src.music_kraken.utils import string_processing +from ..utils import string_processing class Collection: diff --git a/src/music_kraken/objects/parents.py b/src/music_kraken/objects/parents.py index 8ee1f58..d2d3781 100644 --- a/src/music_kraken/objects/parents.py +++ b/src/music_kraken/objects/parents.py @@ -1,7 +1,7 @@ from typing import Optional import uuid -from src.music_kraken.utils.shared import ( +from ..utils.shared import ( SONG_LOGGER as LOGGER ) diff --git a/src/music_kraken/objects/song.py b/src/music_kraken/objects/song.py index 7f4e855..d1a05aa 100644 --- a/src/music_kraken/objects/song.py +++ b/src/music_kraken/objects/song.py @@ -7,7 +7,7 @@ from .metadata import ( ID3Timestamp, MetadataAttribute ) -from src.music_kraken.utils.shared import ( +from ..utils.shared import ( MUSIC_DIR, DATABASE_LOGGER as LOGGER ) diff --git a/src/music_kraken/objects/source.py b/src/music_kraken/objects/source.py index d884545..3425132 100644 --- a/src/music_kraken/objects/source.py +++ b/src/music_kraken/objects/source.py @@ -3,8 +3,7 @@ from typing import List, Dict from .metadata import Mapping, MetadataAttribute from .parents import ( - DatabaseObject, - SongAttribute, + DatabaseObject ) @@ -48,7 +47,7 @@ class SourcePages(Enum): return homepage_map[attribute] -class Source(DatabaseObject, SongAttribute, MetadataAttribute): +class Source(DatabaseObject, MetadataAttribute): """ create somehow like that ```python