Skip to content

Commit

Permalink
fix: Track audio_format is now a property and not a field
Browse files Browse the repository at this point in the history
Use some regex query on path if you wish to query for it. For now, I'd like to keep track properties like bitrate, audio_format, etc. as generated properties since they require a data migration. I'd like to do that data migration all at once if possible.
  • Loading branch information
jtpavlock committed Nov 1, 2022
1 parent 97e4611 commit c2aeda7
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 64 deletions.
53 changes: 0 additions & 53 deletions alembic/versions/4279789f5739_audio_format_non_null.py

This file was deleted.

26 changes: 26 additions & 0 deletions alembic/versions/880c5a2d80ed_remove_audio_format_field.py
@@ -0,0 +1,26 @@
"""remove audio_format field.
Revision ID: 880c5a2d80ed
Revises: bf49ac6805f7
Create Date: 2022-11-01 13:41:12.407819
"""
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "880c5a2d80ed"
down_revision = "bf49ac6805f7"
branch_labels = None
depends_on = None


def upgrade():
with op.batch_alter_table("track", schema=None) as batch_op:
batch_op.drop_column("audio_format")


def downgrade():
with op.batch_alter_table("track", schema=None) as batch_op:
batch_op.add_column(sa.Column("audio_format", sa.VARCHAR(), nullable=True))
2 changes: 1 addition & 1 deletion docs/developers/api/core.rst
Expand Up @@ -27,7 +27,7 @@ Library

.. automodule:: moe.library
:members:
:exclude-members: cache_ok, path, year, catalog_num, genre, original_year
:exclude-members: cache_ok, path, year, catalog_num, genre, original_year, audio_format
:show-inheritance:


Expand Down
1 change: 0 additions & 1 deletion docs/fields.rst
Expand Up @@ -14,7 +14,6 @@ Track Fields
* ``albumartist`` - album artist
* ``artist`` - track artist
* ``artists`` - track artists [#f1]_
* ``audio_format`` - aac, aiff, alac, ape, asf, dsf, flac, ogg, opus, mp3, mpc, wav, or wv
* ``disc`` - disc number
* ``genre`` - genre [#f1]_
* ``path`` - filesystem path of the track [#f3]_
Expand Down
14 changes: 10 additions & 4 deletions moe/library/track.py
Expand Up @@ -127,7 +127,6 @@ def read_custom_tags(
track_fields["artist"] = audio_file.artist
if audio_file.artists is not None:
track_fields["artists"] = set(audio_file.artists)
track_fields["audio_format"] = audio_file.type
track_fields["disc"] = audio_file.disc
if audio_file.genres is not None:
track_fields["genres"] = set(audio_file.genres)
Expand Down Expand Up @@ -338,7 +337,6 @@ class Track(LibItem, SABase, MetaTrack):
artists: Optional[set[str]] = cast(
Optional[set[str]], MutableSet.as_mutable(Column(SetType, nullable=True))
)
audio_format: str = cast(str, Column(String, nullable=False))
disc: int = cast(int, Column(Integer, nullable=False, default=1))
genres: Optional[set[str]] = cast(
Optional[set[str]], MutableSet.as_mutable(Column(SetType, nullable=True))
Expand Down Expand Up @@ -388,7 +386,6 @@ def __init__(

# set default values
self.artist = self.albumartist
self.audio_format = self.path.suffix

for key, value in kwargs.items():
setattr(self, key, value)
Expand Down Expand Up @@ -476,10 +473,19 @@ def from_file(cls: type[T], track_path: Path, album: Optional[Album] = None) ->
**track_fields,
)

@property
def audio_format(self) -> str:
"""Returns the audio format of the track.
One of ['aac', 'aiff', 'alac', 'ape', 'asf', 'dsf', 'flac', 'ogg', 'opus',
'mp3', 'mpc', 'wav', 'wv'].
"""
return mediafile.MediaFile(self.path).type

@property
def fields(self) -> set[str]:
"""Returns any editable, track-specific fields."""
return super().fields.union({"audio_format", "path"})
return super().fields.union({"path"})

def is_unique(self, other: "Track") -> bool:
"""Returns whether a track is unique in the library from ``other``."""
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Expand Up @@ -187,7 +187,6 @@ def track_factory(
title=title,
track_num=track_num,
disc=disc,
audio_format=kwargs.pop("audio_format", "mp3"),
**kwargs,
)

Expand Down
12 changes: 9 additions & 3 deletions tests/library/test_track.py
Expand Up @@ -239,12 +239,12 @@ def test_db_delete(self, tmp_session):

def test_overwrite(self):
"""Fields are overwritten if the option is given."""
track = track_factory(audio_format="1")
other_track = track_factory(audio_format="2")
track = track_factory(title="1")
other_track = track_factory(title="2")

track.merge(other_track, overwrite=True)

assert track.audio_format == "2"
assert track.title == "2"


class TestProperties:
Expand All @@ -266,6 +266,12 @@ def test_set_genre(self):
track.genre = "1; 2"
assert track.genres == {"1", "2"}

def test_audio_format(self):
"""We can get the audio format of a track."""
track = track_factory(exists=True)

assert track.audio_format == "mp3"


class TestListDuplicates:
"""List fields should not cause duplicate errors (just merge silently)."""
Expand Down
1 change: 0 additions & 1 deletion tests/plugins/test_write.py
Expand Up @@ -107,7 +107,6 @@ def test_write_tags(self, tmp_config):
assert new_track.albumartist == albumartist
assert new_track.artist == artist
assert new_track.artists == artists
assert new_track.audio_format == "mp3"
assert new_track.disc == disc
assert new_track.genres == genres
assert new_track.title == title
Expand Down

0 comments on commit c2aeda7

Please sign in to comment.