From 813b32502908baa66d42bb9221eafad92a3666ea Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Tue, 31 Oct 2023 08:15:24 +0000 Subject: [PATCH] Black reformatting, tidying --- app/helpers.py | 14 ++-- app/models.py | 148 ++++++++++++++++++------------------------- app/playlistmodel.py | 30 +++------ app/playlists.py | 1 - 4 files changed, 77 insertions(+), 116 deletions(-) diff --git a/app/helpers.py b/app/helpers.py index 2f1a0cc..fa7e214 100644 --- a/app/helpers.py +++ b/app/helpers.py @@ -155,23 +155,21 @@ def get_file_metadata(filepath: str) -> dict: # Get title, artist, bitrate, duration, path metadata: Dict[str, str | int | float] = get_tags(filepath) - metadata['mtime'] = os.path.getmtime(filepath) + metadata["mtime"] = os.path.getmtime(filepath) # Set start_gap, fade_at and silence_at audio = get_audio_segment(filepath) if not audio: - audio_values = dict( - start_gap=0, - fade_at=0, - silence_at=0 - ) + audio_values = dict(start_gap=0, fade_at=0, silence_at=0) else: audio_values = dict( start_gap=leading_silence(audio), - fade_at=int(round(fade_point(audio) / 1000, Config.MILLISECOND_SIGFIGS) * 1000), + fade_at=int( + round(fade_point(audio) / 1000, Config.MILLISECOND_SIGFIGS) * 1000 + ), silence_at=int( round(trailing_silence(audio) / 1000, Config.MILLISECOND_SIGFIGS) * 1000 - ) + ), ) metadata |= audio_values diff --git a/app/models.py b/app/models.py index b0429fe..0c6c0f6 100644 --- a/app/models.py +++ b/app/models.py @@ -134,14 +134,11 @@ class NoteColours(Base): if not text: return None - for rec in ( - session.scalars( - select(NoteColours) - .filter(NoteColours.enabled.is_(True)) - .order_by(NoteColours.order) - ) - .all() - ): + for rec in session.scalars( + select(NoteColours) + .filter(NoteColours.enabled.is_(True)) + .order_by(NoteColours.order) + ).all(): if rec.is_regex: flags = re.UNICODE if not rec.is_casesensitive: @@ -202,14 +199,11 @@ class Playdates(Base): def played_after(session: scoped_session, since: datetime) -> Sequence["Playdates"]: """Return a list of Playdates objects since passed time""" - return ( - session.scalars( - select(Playdates) - .where(Playdates.lastplayed >= since) - .order_by(Playdates.lastplayed) - ) - .all() - ) + return session.scalars( + select(Playdates) + .where(Playdates.lastplayed >= since) + .order_by(Playdates.lastplayed) + ).all() class Playlists(Base): @@ -285,42 +279,33 @@ class Playlists(Base): def get_all(cls, session: scoped_session) -> Sequence["Playlists"]: """Returns a list of all playlists ordered by last use""" - return ( - session.scalars( - select(cls) - .filter(cls.is_template.is_(False)) - .order_by(cls.tab.desc(), cls.last_used.desc()) - ) - .all() - ) + return session.scalars( + select(cls) + .filter(cls.is_template.is_(False)) + .order_by(cls.tab.desc(), cls.last_used.desc()) + ).all() @classmethod def get_all_templates(cls, session: scoped_session) -> Sequence["Playlists"]: """Returns a list of all templates ordered by name""" - return ( - session.scalars( - select(cls).filter(cls.is_template.is_(True)).order_by(cls.name) - ) - .all() - ) + return session.scalars( + select(cls).filter(cls.is_template.is_(True)).order_by(cls.name) + ).all() @classmethod def get_closed(cls, session: scoped_session) -> Sequence["Playlists"]: """Returns a list of all closed playlists ordered by last use""" - return ( - session.scalars( - select(cls) - .filter( - cls.tab.is_(None), - cls.is_template.is_(False), - cls.deleted.is_(False), - ) - .order_by(cls.last_used.desc()) + return session.scalars( + select(cls) + .filter( + cls.tab.is_(None), + cls.is_template.is_(False), + cls.deleted.is_(False), ) - .all() - ) + .order_by(cls.last_used.desc()) + ).all() @classmethod def get_open(cls, session: scoped_session) -> Sequence[Optional["Playlists"]]: @@ -328,10 +313,9 @@ class Playlists(Base): Return a list of loaded playlists ordered by tab order. """ - return ( - session.scalars(select(cls).where(cls.tab.is_not(None)).order_by(cls.tab)) - .all() - ) + return session.scalars( + select(cls).where(cls.tab.is_not(None)).order_by(cls.tab) + ).all() def mark_open(self, session: scoped_session, tab_index: int) -> None: """Mark playlist as loaded and used now""" @@ -433,12 +417,9 @@ class PlaylistRows(Base): def copy_playlist(session: scoped_session, src_id: int, dst_id: int) -> None: """Copy playlist entries""" - src_rows = ( - session.scalars( - select(PlaylistRows).filter(PlaylistRows.playlist_id == src_id) - ) - .all() - ) + src_rows = session.scalars( + select(PlaylistRows).filter(PlaylistRows.playlist_id == src_id) + ).all() for plr in src_rows: PlaylistRows( @@ -512,14 +493,11 @@ class PlaylistRows(Base): Ensure the row numbers for passed playlist have no gaps """ - plrs = ( - session.scalars( - select(PlaylistRows) - .where(PlaylistRows.playlist_id == playlist_id) - .order_by(PlaylistRows.plr_rownum) - ) - .all() - ) + plrs = session.scalars( + select(PlaylistRows) + .where(PlaylistRows.playlist_id == playlist_id) + .order_by(PlaylistRows.plr_rownum) + ).all() for i, plr in enumerate(plrs): plr.plr_rownum = i @@ -536,14 +514,11 @@ class PlaylistRows(Base): PlaylistRows objects """ - plrs = ( - session.scalars( - select(cls) - .where(cls.playlist_id == playlist_id, cls.id.in_(plr_ids)) - .order_by(cls.plr_rownum) - ) - .all() - ) + plrs = session.scalars( + select(cls) + .where(cls.playlist_id == playlist_id, cls.id.in_(plr_ids)) + .order_by(cls.plr_rownum) + ).all() return plrs @@ -581,14 +556,11 @@ class PlaylistRows(Base): have been played. """ - plrs = ( - session.scalars( - select(cls) - .where(cls.playlist_id == playlist_id, cls.played.is_(True)) - .order_by(cls.plr_rownum) - ) - .all() - ) + plrs = session.scalars( + select(cls) + .where(cls.playlist_id == playlist_id, cls.played.is_(True)) + .order_by(cls.plr_rownum) + ).all() return plrs @@ -626,21 +598,25 @@ class PlaylistRows(Base): have not been played. """ - plrs = ( - session.scalars( - select(cls) - .where( - cls.playlist_id == playlist_id, - cls.track_id.is_not(None), - cls.played.is_(False), - ) - .order_by(cls.plr_rownum) + plrs = session.scalars( + select(cls) + .where( + cls.playlist_id == playlist_id, + cls.track_id.is_not(None), + cls.played.is_(False), ) - .all() - ) + .order_by(cls.plr_rownum) + ).all() return plrs + @classmethod + def insert_row( + cls, session: scoped_session, playlist_id: int, new_row_number: int + ) -> "PlaylistRows": + cls.move_rows_down(session, playlist_id, new_row_number, 1) + return cls(session, playlist_id, new_row_number) + @staticmethod def move_rows_down( session: scoped_session, playlist_id: int, starting_row: int, move_by: int diff --git a/app/playlistmodel.py b/app/playlistmodel.py index 55e2685..b50655d 100644 --- a/app/playlistmodel.py +++ b/app/playlistmodel.py @@ -3,15 +3,12 @@ from enum import auto, Enum from sqlalchemy import bindparam, update from typing import List, Optional, TYPE_CHECKING -from dbconfig import scoped_session, Session - from PyQt6.QtCore import ( QAbstractTableModel, QModelIndex, Qt, QVariant, ) - from PyQt6.QtGui import ( QBrush, QColor, @@ -42,9 +39,6 @@ class Col(Enum): NOTE = auto() -HEADER_NOTES_COLUMN = 1 - - class PlaylistRowData: def __init__(self, plr: PlaylistRows) -> None: """ @@ -120,7 +114,11 @@ class PlaylistModel(QAbstractTableModel): ) def add_track( - self, playlist_id: int, track: Optional[Tracks], note: Optional[str] + self, + playlist_id: int, + new_row_number: int, + track_id: Optional[int], + note: Optional[str], ) -> None: """ Add track if it's for our playlist @@ -130,14 +128,12 @@ class PlaylistModel(QAbstractTableModel): if playlist_id != self.playlist_id: return - row_number = self.playlist.get_selected_row_number() - # Insert track if we have one - if track: - self.insert_track_row(row_number, track, note) + if track_id: + self.insert_track_row(new_row_number, track_id, note) # If we only have a note, add as a header row elif note: - self.insert_header_row(row_number, note) + self.insert_header_row(new_row_number, note) else: # No track, no note, no point return @@ -364,16 +360,8 @@ class PlaylistModel(QAbstractTableModel): else: new_row_number = row_number - # Move rows below new row down - stmt = ( - update(PlaylistRows) - .where(PlaylistRows.plr_rownum >= new_row_number) - .values({PlaylistRows.plr_rownum: PlaylistRows.plr_rownum + 1}) - ) - session.execute(stmt) - # Insert the new row and return it - return PlaylistRows(session, self.playlist_id, new_row_number) + return PlaylistRows.insert_row(session, self.playlist_id, new_row_number) def insert_track_row( self, row_number: Optional[int], track_id: int, text: Optional[str] diff --git a/app/playlists.py b/app/playlists.py index 6f5fb95..2443c05 100644 --- a/app/playlists.py +++ b/app/playlists.py @@ -6,7 +6,6 @@ import threading import obsws_python as obs # type: ignore -# from collections import namedtuple from datetime import datetime, timedelta from typing import Any, cast, List, Optional, Tuple, TYPE_CHECKING