From 1de7cefe7200082d843fc380d788c3a8b494f3e2 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sat, 5 Feb 2022 18:42:35 +0000 Subject: [PATCH 1/2] Start configurable note colours --- .idea/dataSources.xml | 12 ++++ .../inspectionProfiles/profiles_settings.xml | 1 + .idea/sqldialects.xml | 6 ++ alembic.ini | 3 +- app/model.py | 55 +++++++++++++++---- app/playlists.py | 9 ++- .../a5aada49f2fc_add_notecolours_table.py | 36 ++++++++++++ 7 files changed, 103 insertions(+), 19 deletions(-) create mode 100644 .idea/dataSources.xml create mode 100644 .idea/sqldialects.xml create mode 100644 migrations/versions/a5aada49f2fc_add_notecolours_table.py diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..b255d30 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,12 @@ + + + + + mariadb + true + org.mariadb.jdbc.Driver + jdbc:mariadb://localhost:3306/musicmuster_dev + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml index 105ce2d..dd4c951 100644 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -1,5 +1,6 @@ + diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..63772a3 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/alembic.ini b/alembic.ini index 569ba00..d744355 100644 --- a/alembic.ini +++ b/alembic.ini @@ -39,8 +39,7 @@ prepend_sys_path = . # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = mysql+mysqldb://songdb:songdb@localhost/musicmuster - +sqlalchemy.url = mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_dev [post_write_hooks] # post_write_hooks defines scripts or Python functions that are run diff --git a/app/model.py b/app/model.py index b16bc25..fe0e82d 100644 --- a/app/model.py +++ b/app/model.py @@ -38,6 +38,36 @@ Session = sessionmaker(bind=engine) # Database classes +class NoteColours(Base): + __tablename__ = 'notecolours' + + id = Column(Integer, primary_key=True, autoincrement=True) + substring = Column(String(256), index=False) + hexcolour = Column(String(6), index=False) + enabled = Column(Boolean, default=True, index=False) + is_regex = Column(Boolean, default=False, index=False) + is_casesensitive = Column(Boolean, default=False, index=False) + + def __repr__(self): + return ( + f"" + ) + + @staticmethod + def get_colour(session, text): + """ + Parse text and return colour string if match, else None + + Currently ignore is_regex and is_casesensitive + """ + + for rec in session.query(NoteColours).all(): + if rec.substring in text: + return '#' + rec.hexcolour + + return None + + class Notes(Base): __tablename__ = 'notes' @@ -115,7 +145,7 @@ class Playdates(Base): last_played = session.query(Playdates.lastplayed).filter( (Playdates.track_id == track_id) - ).order_by(Playdates.lastplayed.desc()).first() + ).order_by(Playdates.lastplayed.desc()).first() if last_played: return last_played[0] else: @@ -221,11 +251,11 @@ class Playlists(Base): return ( session.query(Playlists) - .filter( + .filter( (Playlists.loaded == False) | # noqa E712 (Playlists.loaded == None) ) - .order_by(Playlists.last_used.desc()) + .order_by(Playlists.last_used.desc()) ).all() @staticmethod @@ -242,8 +272,8 @@ class Playlists(Base): return ( session.query(Playlists) - .filter(Playlists.loaded == True) # noqa E712 - .order_by(Playlists.last_used.desc()) + .filter(Playlists.loaded == True) # noqa E712 + .order_by(Playlists.last_used.desc()) ).all() def get_notes(self): @@ -253,8 +283,8 @@ class Playlists(Base): def get_playlist(session, playlist_id): return ( session.query(Playlists) - .filter( - Playlists.id == playlist_id # noqa E712 + .filter( + Playlists.id == playlist_id # noqa E712 ) ).one() @@ -320,7 +350,7 @@ class PlaylistTracks(Base): f"to_playlist_id={to_playlist_id})" ) max_row = session.query(func.max(PlaylistTracks.row)).filter( - PlaylistTracks.playlist_id == to_playlist_id).scalar() + PlaylistTracks.playlist_id == to_playlist_id).scalar() if max_row is None: # Destination playlist is empty; use row 0 new_row = 0 @@ -454,6 +484,7 @@ class Tracks(Base): f"" ) + # Not currently used 1 June 2021 # @staticmethod # def get_note(session, id): @@ -579,16 +610,16 @@ class Tracks(Base): def search_artists(session, text): return ( session.query(Tracks) - .filter(Tracks.artist.ilike(f"%{text}%")) - .order_by(Tracks.title) + .filter(Tracks.artist.ilike(f"%{text}%")) + .order_by(Tracks.title) ).all() @staticmethod def search_titles(session, text): return ( session.query(Tracks) - .filter(Tracks.title.ilike(f"%{text}%")) - .order_by(Tracks.title) + .filter(Tracks.title.ilike(f"%{text}%")) + .order_by(Tracks.title) ).all() @staticmethod diff --git a/app/playlists.py b/app/playlists.py index 81e373f..4a64db2 100644 --- a/app/playlists.py +++ b/app/playlists.py @@ -20,7 +20,7 @@ from datetime import datetime, timedelta from helpers import get_relative_date, open_in_audacity from log import DEBUG, ERROR from model import ( - Notes, Playdates, Playlists, PlaylistTracks, Session, Settings, Tracks + Notes, Playdates, Playlists, PlaylistTracks, Session, Settings, Tracks, NoteColours ) from songdb import create_track_from_file, update_meta @@ -1040,11 +1040,10 @@ class PlaylistTab(QTableWidget): if row_time: next_start_time = row_time # Set colour - note_colour = Config.COLOUR_NOTES_PLAYLIST note_text = self.item(row, self.COL_TITLE).text() - for colour_token in Config.NOTE_COLOURS.keys(): - if note_text.lower().startswith(colour_token.lower()): - note_colour = Config.NOTE_COLOURS[colour_token] + note_colour = NoteColours.get_colour(session, note_text) + if not note_colour: + note_colour = Config.COLOUR_NOTES_PLAYLIST self._set_row_colour( row, QColor(note_colour) ) diff --git a/migrations/versions/a5aada49f2fc_add_notecolours_table.py b/migrations/versions/a5aada49f2fc_add_notecolours_table.py new file mode 100644 index 0000000..77490de --- /dev/null +++ b/migrations/versions/a5aada49f2fc_add_notecolours_table.py @@ -0,0 +1,36 @@ +"""Add NoteColours table + +Revision ID: a5aada49f2fc +Revises: 2cc37d3cf07f +Create Date: 2022-02-05 17:34:54.880473 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'a5aada49f2fc' +down_revision = '2cc37d3cf07f' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('notecolours', + sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), + sa.Column('substring', sa.String(length=256), nullable=True), + sa.Column('hexcolour', sa.String(length=6), nullable=True), + sa.Column('enabled', sa.Boolean(), nullable=True), + sa.Column('is_regex', sa.Boolean(), nullable=True), + sa.Column('is_casesensitive', sa.Boolean(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('notecolours') + # ### end Alembic commands ### From 53899b3a240fbdc2fe85dd0257bb2860e4a06f1c Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Sat, 5 Feb 2022 21:32:41 +0000 Subject: [PATCH 2/2] Manage note colours from database --- app/model.py | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/app/model.py b/app/model.py index fe0e82d..ef6a4f8 100644 --- a/app/model.py +++ b/app/model.py @@ -1,6 +1,8 @@ #!/usr/bin/python3 import os.path +import re + import sqlalchemy from datetime import datetime @@ -44,9 +46,10 @@ class NoteColours(Base): id = Column(Integer, primary_key=True, autoincrement=True) substring = Column(String(256), index=False) hexcolour = Column(String(6), index=False) - enabled = Column(Boolean, default=True, index=False) + enabled = Column(Boolean, default=True, index=True) is_regex = Column(Boolean, default=False, index=False) is_casesensitive = Column(Boolean, default=False, index=False) + order = Column(Integer, index=True) def __repr__(self): return ( @@ -61,9 +64,26 @@ class NoteColours(Base): Currently ignore is_regex and is_casesensitive """ - for rec in session.query(NoteColours).all(): - if rec.substring in text: - return '#' + rec.hexcolour + for rec in ( + session.query(NoteColours) + .filter(NoteColours.enabled == True) + .order_by(NoteColours.order) + .all() + ): + if rec.is_regex: + if rec.is_casesensitive: + p = re.compile(rec.substring) + else: + p = re.compile(rec.substring, re.IGNORECASE) + if p.match(text): + return '#' + rec.hexcolour + else: + if rec.is_casesensitive: + if rec.substring in text: + return '#' + rec.hexcolour + else: + if rec.substring.lower() in text.lower(): + return '#' + rec.hexcolour return None