WIP V3: remove functions, formatting

This commit is contained in:
Keith Edmunds 2023-11-16 00:08:12 +00:00
parent a8aa157484
commit 2861511f1f
7 changed files with 136 additions and 155 deletions

View File

@ -217,4 +217,5 @@ class TrackSequence:
now = PlaylistTrack()
previous = PlaylistTrack()
track_sequence = TrackSequence()

View File

@ -44,12 +44,12 @@ class Config(object):
COLUMN_NAME_TITLE = "Title"
DBFS_SILENCE = -50
DEBUG_FUNCTIONS: List[Optional[str]] = []
DEBUG_MODULES: List[Optional[str]] = ['dbconfig']
DEBUG_MODULES: List[Optional[str]] = ["dbconfig"]
DEFAULT_COLUMN_WIDTH = 200
DISPLAY_SQL = False
EPOCH = datetime.datetime(1970, 1, 1)
ERRORS_FROM = ['noreply@midnighthax.com']
ERRORS_TO = ['kae@midnighthax.com']
ERRORS_FROM = ["noreply@midnighthax.com"]
ERRORS_TO = ["kae@midnighthax.com"]
FADE_CURVE_BACKGROUND = "lightyellow"
FADE_CURVE_FOREGROUND = "blue"
FADE_CURVE_MS_BEFORE_FADE = 5000
@ -71,11 +71,11 @@ class Config(object):
LOG_LEVEL_STDERR = logging.ERROR
LOG_LEVEL_SYSLOG = logging.DEBUG
LOG_NAME = "musicmuster"
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25)
MAIL_SERVER = os.environ.get('MAIL_SERVER') or "woodlands.midnighthax.com"
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None
MAIL_PASSWORD = os.environ.get("MAIL_PASSWORD")
MAIL_PORT = int(os.environ.get("MAIL_PORT") or 25)
MAIL_SERVER = os.environ.get("MAIL_SERVER") or "woodlands.midnighthax.com"
MAIL_USERNAME = os.environ.get("MAIL_USERNAME")
MAIL_USE_TLS = os.environ.get("MAIL_USE_TLS") is not None
MAX_IMPORT_MATCHES = 5
MAX_INFO_TABS = 5
MAX_MISSING_FILES_TO_REPORT = 10
@ -86,7 +86,7 @@ class Config(object):
OBS_PASSWORD = "auster"
OBS_PORT = 4455
PLAY_SETTLE = 500000
ROOT = os.environ.get('ROOT') or "/home/kae/music"
ROOT = os.environ.get("ROOT") or "/home/kae/music"
ROWS_FROM_ZERO = True
IMPORT_DESTINATION = os.path.join(ROOT, "Singles")
SCROLL_TOP_MARGIN = 3

View File

@ -489,7 +489,9 @@ class PlaylistRows(Base):
session.flush()
@staticmethod
def delete_rows(session: scoped_session, playlist_id: int, row_numbers: List[int]) -> None:
def delete_rows(
session: scoped_session, playlist_id: int, row_numbers: List[int]
) -> None:
"""
Delete passed rows in given playlist.
"""
@ -497,7 +499,7 @@ class PlaylistRows(Base):
session.execute(
delete(PlaylistRows).where(
PlaylistRows.playlist_id == playlist_id,
PlaylistRows.plr_rownum.in_(row_numbers)
PlaylistRows.plr_rownum.in_(row_numbers),
)
)

View File

@ -740,10 +740,7 @@ class Window(QMainWindow, Ui_MainWindow):
times a second; this function has much better resolution.
"""
if (
track_sequence.now.track_id is None
or track_sequence.now.start_time is None
):
if track_sequence.now.track_id is None or track_sequence.now.start_time is None:
return 0
now = datetime.now()
@ -900,8 +897,7 @@ class Window(QMainWindow, Ui_MainWindow):
plrs_to_move = [
plr
for plr in playlistrows
if plr.id
not in [track_sequence.now.plr_id, track_sequence.next.plr_id]
if plr.id not in [track_sequence.now.plr_id, track_sequence.next.plr_id]
]
rows_to_delete = [

View File

@ -726,7 +726,9 @@ class PlaylistModel(QAbstractTableModel):
# Check to see whether any rows in track_sequence have moved
if track_sequence.previous.plr_rownum in row_map:
track_sequence.previous.plr_rownum = row_map[track_sequence.previous.plr_rownum]
track_sequence.previous.plr_rownum = row_map[
track_sequence.previous.plr_rownum
]
if track_sequence.now.plr_rownum in row_map:
track_sequence.now.plr_rownum = row_map[track_sequence.now.plr_rownum]
if track_sequence.next.plr_rownum in row_map:
@ -937,7 +939,7 @@ class PlaylistModel(QAbstractTableModel):
Sort selected rows by artist
"""
self.sort_by_attribute(row_numbers, 'artist')
self.sort_by_attribute(row_numbers, "artist")
def sort_by_attribute(self, row_numbers: List[int], attr_name: str) -> None:
"""
@ -949,8 +951,8 @@ class PlaylistModel(QAbstractTableModel):
# interested in
shortlist_rows = {k: self.playlist_rows[k] for k in row_numbers}
sorted_list = [
plr.plr_rownum for plr in
sorted(shortlist_rows.values(), key=attrgetter(attr_name))
plr.plr_rownum
for plr in sorted(shortlist_rows.values(), key=attrgetter(attr_name))
]
self.move_rows(sorted_list, min(sorted_list))
@ -959,21 +961,21 @@ class PlaylistModel(QAbstractTableModel):
Sort selected rows by duration
"""
self.sort_by_attribute(row_numbers, 'duration')
self.sort_by_attribute(row_numbers, "duration")
def sort_by_lastplayed(self, row_numbers: List[int]) -> None:
"""
Sort selected rows by lastplayed
"""
self.sort_by_attribute(row_numbers, 'lastplayed')
self.sort_by_attribute(row_numbers, "lastplayed")
def sort_by_title(self, row_numbers: List[int]) -> None:
"""
Sort selected rows by title
"""
self.sort_by_attribute(row_numbers, 'title')
self.sort_by_attribute(row_numbers, "title")
def supportedDropActions(self) -> Qt.DropAction:
return Qt.DropAction.MoveAction | Qt.DropAction.CopyAction
@ -999,7 +1001,10 @@ class PlaylistModel(QAbstractTableModel):
continue
# Set start time for next row if we have a current track
if row_number == track_sequence.next.plr_rownum and track_sequence.now.end_time:
if (
row_number == track_sequence.next.plr_rownum
and track_sequence.now.end_time
):
stend.start_time = track_sequence.now.end_time
stend.end_time = stend.start_time + timedelta(milliseconds=prd.duration)
next_start_time = stend.end_time

View File

@ -284,13 +284,6 @@ class PlaylistTab(QTableView):
return index.row()
return None
def get_selected_playlistrow_ids(self) -> list:
"""
Return a list of PlaylistRow ids of the selected rows
"""
return [self._get_row_plr_id(a) for a in self._get_selected_rows()]
# def lookup_row_in_songfacts(self) -> None:
# """
# If there is a selected row and it is a track row,
@ -736,20 +729,6 @@ class PlaylistTab(QTableView):
if self._get_row_track_id(check_row):
break
def _open_in_audacity(self, row_number: int) -> None:
"""Open track in Audacity. Audacity must be already running"""
track_path = self._get_row_track_path(row_number)
if not track_path:
log.error(
f"{self.playlist_id=} "
f"playlists._open_in_audactity({row_number=}): "
"track_path not set"
)
return
open_in_audacity(track_path)
def _rescan(self, row_number: int) -> None:
"""Rescan track"""
@ -757,42 +736,42 @@ class PlaylistTab(QTableView):
model.rescan_track(row_number)
self.clear_selection()
def _reset_next(self, old_plrid: int, new_plrid: int) -> None:
"""
Called when set_next_track_signal signal received.
# def _reset_next(self, old_plrid: int, new_plrid: int) -> None:
# """
# Called when set_next_track_signal signal received.
Actions required:
- If old_plrid points to this playlist:
- Remove existing next track
- If new_plrid points to this playlist:
- Set track as next
- Display row as next track
- Update start/stop times
"""
# Actions required:
# - If old_plrid points to this playlist:
# - Remove existing next track
# - If new_plrid points to this playlist:
# - Set track as next
# - Display row as next track
# - Update start/stop times
# """
with Session() as session:
# Get plrs
old_plr = new_plr = None
if old_plrid:
old_plr = session.get(PlaylistRows, old_plrid)
# with Session() as session:
# # Get plrs
# old_plr = new_plr = None
# if old_plrid:
# old_plr = session.get(PlaylistRows, old_plrid)
# Unmark next track
if old_plr and old_plr.playlist_id == self.playlist_id:
self._set_row_colour_default(old_plr.plr_rownum)
# # Unmark next track
# if old_plr and old_plr.playlist_id == self.playlist_id:
# self._set_row_colour_default(old_plr.plr_rownum)
# Mark next track
if new_plrid:
new_plr = session.get(PlaylistRows, new_plrid)
if not new_plr:
log.error(f"_reset_next({new_plrid=}): plr not found")
return
if new_plr.playlist_id == self.playlist_id:
self._set_row_colour_next(new_plr.plr_rownum)
# # Mark next track
# if new_plrid:
# new_plr = session.get(PlaylistRows, new_plrid)
# if not new_plr:
# log.error(f"_reset_next({new_plrid=}): plr not found")
# return
# if new_plr.playlist_id == self.playlist_id:
# self._set_row_colour_next(new_plr.plr_rownum)
# Update start/stop times
self._update_start_end_times(session)
# # Update start/stop times
# self._update_start_end_times(session)
self.clear_selection()
# self.clear_selection()
def _run_subprocess(self, args):
"""Run args in subprocess"""
@ -827,66 +806,68 @@ class PlaylistTab(QTableView):
scroll_item = self.item(top_row, 0)
self.scrollToItem(scroll_item, QAbstractItemView.ScrollHint.PositionAtTop)
def _search(self, next: bool = True) -> None:
"""
Select next/previous row containg self.search_string. Start from
top selected row if there is one, else from top.
# def _search(self, next: bool = True) -> None:
# """
# Select next/previous row containg self.search_string. Start from
# top selected row if there is one, else from top.
Wrap at last/first row.
"""
# Wrap at last/first row.
# """
if not self.search_text:
return
# if not self.search_text:
# return
selected_row = self._get_selected_row()
if next:
if selected_row is not None and selected_row < self.rowCount() - 1:
starting_row = selected_row + 1
else:
starting_row = 0
else:
if selected_row is not None and selected_row > 0:
starting_row = selected_row - 1
else:
starting_row = self.rowCount() - 1
# selected_row = self._get_selected_row()
# if next:
# if selected_row is not None and selected_row < self.rowCount() - 1:
# starting_row = selected_row + 1
# else:
# starting_row = 0
# else:
# if selected_row is not None and selected_row > 0:
# starting_row = selected_row - 1
# else:
# starting_row = self.rowCount() - 1
wrapped = False
match_row = None
row_number = starting_row
needle = self.search_text.lower()
while True:
# Check for match in title, artist or notes
title = self._get_row_title(row_number)
if title and needle in title.lower():
match_row = row_number
break
artist = self._get_row_artist(row_number)
if artist and needle in artist.lower():
match_row = row_number
break
note = self._get_row_note(row_number)
if note and needle in note.lower():
match_row = row_number
break
if next:
row_number += 1
if wrapped and row_number >= starting_row:
break
if row_number >= self.rowCount():
row_number = 0
wrapped = True
else:
row_number -= 1
if wrapped and row_number <= starting_row:
break
if row_number < 0:
row_number = self.rowCount() - 1
wrapped = True
# wrapped = False
# match_row = None
# row_number = starting_row
# needle = self.search_text.lower()
# while True:
# # Check for match in title, artist or notes
# title = self._get_row_title(row_number)
# if title and needle in title.lower():
# match_row = row_number
# break
# artist = self._get_row_artist(row_number)
# if artist and needle in artist.lower():
# match_row = row_number
# break
# note = self._get_row_note(row_number)
# if note and needle in note.lower():
# match_row = row_number
# break
# if next:
# row_number += 1
# if wrapped and row_number >= starting_row:
# break
# if row_number >= self.rowCount():
# row_number = 0
# wrapped = True
# else:
# row_number -= 1
# if wrapped and row_number <= starting_row:
# break
# if row_number < 0:
# row_number = self.rowCount() - 1
# wrapped = True
if match_row is not None:
self.selectRow(row_number)
# if match_row is not None:
# self.selectRow(row_number)
def selectionChanged(self, selected: QItemSelection, deselected: QItemSelection) -> None:
def selectionChanged(
self, selected: QItemSelection, deselected: QItemSelection
) -> None:
"""
Toggle drag behaviour according to whether rows are selected
"""
@ -927,29 +908,29 @@ class PlaylistTab(QTableView):
else:
self.setColumnWidth(column_number, Config.DEFAULT_COLUMN_WIDTH)
def _set_row_note_colour(self, session: scoped_session, row_number: int) -> None:
"""
Set row note colour
"""
# def _set_row_note_colour(self, session: scoped_session, row_number: int) -> None:
# """
# Set row note colour
# """
# Sanity check: this should be a track row and thus have a
# track associated
if not self._get_row_track_id(row_number):
if os.environ["MM_ENV"] == "PRODUCTION":
send_mail(
Config.ERRORS_TO,
Config.ERRORS_FROM,
"playlists:_set_row_note_colour() on header row",
stackprinter.format(),
)
# stackprinter.show(add_summary=True, style="darkbg")
print(f"playists:_set_row_note_colour() called on track row ({row_number=}")
return
# # Sanity check: this should be a track row and thus have a
# # track associated
# if not self._get_row_track_id(row_number):
# if os.environ["MM_ENV"] == "PRODUCTION":
# send_mail(
# Config.ERRORS_TO,
# Config.ERRORS_FROM,
# "playlists:_set_row_note_colour() on header row",
# stackprinter.format(),
# )
# # stackprinter.show(add_summary=True, style="darkbg")
# print(f"playists:_set_row_note_colour() called on track row ({row_number=}")
# return
# Set colour
note_text = self._get_row_note(row_number)
note_colour = NoteColours.get_colour(session, note_text)
self._set_cell_colour(row_number, ROW_NOTES, note_colour)
# # Set colour
# note_text = self._get_row_note(row_number)
# note_colour = NoteColours.get_colour(session, note_text)
# self._set_cell_colour(row_number, ROW_NOTES, note_colour)
def _span_cells(self, row: int, column: int, rowSpan: int, columnSpan: int) -> None:
"""

View File

@ -18,7 +18,6 @@ test_tracks = [
def create_model_with_tracks(session: scoped_session) -> "playlistmodel.PlaylistModel":
playlist = Playlists(session, "test playlist")
model = playlistmodel.PlaylistModel(playlist.id)
@ -228,14 +227,12 @@ def test_insert_header_row_middle(monkeypatch, session):
def test_create_model_with_tracks(monkeypatch, session):
monkeypatch.setattr(playlistmodel, "Session", session)
model = create_model_with_tracks(session)
assert len(model.playlist_rows) == len(test_tracks)
def test_timing_one_track(monkeypatch, session):
START_ROW = 0
END_ROW = 2
@ -267,4 +264,3 @@ def test_timing_one_track(monkeypatch, session):
# model.edit_role(model.rowCount(), playlistmodel.Col.NOTE.value, prd)
# == note_text
# )