Skip to content

Commit 5ce2d1c

Browse files
committed
Replace Ref constants with RefType enum
1 parent 20cfd72 commit 5ce2d1c

File tree

4 files changed

+52
-52
lines changed

4 files changed

+52
-52
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ classifiers = [
1414
]
1515
dynamic = ["version"]
1616
dependencies = [
17-
"mopidy >= 4.0.0a1",
17+
"mopidy >= 4.0.0a3",
1818
"pykka >= 4",
1919
"setuptools >= 66",
2020
"uritools >= 4",

src/mopidy_local/library.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import uritools
66
from mopidy import backend, models
7-
from mopidy.models import Ref, SearchResult
7+
from mopidy.models import Ref, RefType, SearchResult
88
from mopidy.types import Uri
99

1010
from . import Extension, schema
@@ -52,11 +52,11 @@ def load(self):
5252
def lookup(self, uri):
5353
try:
5454
if uri.startswith("local:album"):
55-
return list(schema.lookup(self._connect(), Ref.ALBUM, uri))
55+
return list(schema.lookup(self._connect(), RefType.ALBUM, uri))
5656
if uri.startswith("local:artist"):
57-
return list(schema.lookup(self._connect(), Ref.ARTIST, uri))
57+
return list(schema.lookup(self._connect(), RefType.ARTIST, uri))
5858
if uri.startswith("local:track"):
59-
return list(schema.lookup(self._connect(), Ref.TRACK, uri))
59+
return list(schema.lookup(self._connect(), RefType.TRACK, uri))
6060
msg = "Invalid lookup URI"
6161
raise ValueError(msg) # noqa: TRY301
6262
except Exception as e:
@@ -127,27 +127,27 @@ def _connect(self):
127127
return self._connection
128128

129129
def _browse_album(self, uri, order=("disc_no", "track_no", "name")):
130-
return schema.browse(self._connect(), Ref.TRACK, order, album=uri)
130+
return schema.browse(self._connect(), RefType.TRACK, order, album=uri)
131131

132132
def _browse_artist(self, uri, order=("type", "name COLLATE NOCASE")):
133133
with self._connect() as c:
134-
albums = schema.browse(c, Ref.ALBUM, order, albumartist=uri)
134+
albums = schema.browse(c, RefType.ALBUM, order, albumartist=uri)
135135
refs = schema.browse(c, order=order, artist=uri)
136136
album_uris, tracks = {ref.uri for ref in albums}, []
137137
for ref in refs:
138-
if ref.type == Ref.ALBUM and ref.uri not in album_uris:
138+
if ref.type == RefType.ALBUM and ref.uri not in album_uris:
139139
albums.append(
140140
Ref.directory(
141141
uri=uritools.uricompose(
142142
"local",
143143
None,
144144
"directory",
145-
dict(type=Ref.TRACK, album=ref.uri, artist=uri), # noqa: C408
145+
dict(type=RefType.TRACK, album=ref.uri, artist=uri), # noqa: C408
146146
),
147147
name=ref.name,
148148
),
149149
)
150-
elif ref.type == Ref.TRACK:
150+
elif ref.type == RefType.TRACK:
151151
tracks.append(ref)
152152
else:
153153
logger.debug("Skipped SQLite browse result %s", ref.uri)
@@ -168,29 +168,29 @@ def _browse_directory(self, uri, order=("type", "name COLLATE NOCASE")):
168168

169169
# Fix #38: keep sort order of album tracks; this also applies
170170
# to composers and performers
171-
if type_ == Ref.TRACK and "album" in query:
171+
if type_ == RefType.TRACK and "album" in query:
172172
order = ("disc_no", "track_no", "name")
173-
if type_ == Ref.ARTIST and self._config["use_artist_sortname"]:
173+
if type_ == RefType.ARTIST and self._config["use_artist_sortname"]:
174174
order = ("coalesce(sortname, name) COLLATE NOCASE",)
175175
roles = role or ("artist", "albumartist") # TODO: re-think 'roles'...
176176

177177
refs = []
178178
for ref in schema.browse(self._connect(), type_, order, role=roles, **query):
179-
if ref.type == Ref.TRACK or (not query and not role):
179+
if ref.type == RefType.TRACK or (not query and not role):
180180
refs.append(ref)
181-
elif ref.type == Ref.ALBUM:
181+
elif ref.type == RefType.ALBUM:
182182
refs.append(
183183
Ref.directory(
184184
uri=uritools.uricompose(
185185
"local",
186186
None,
187187
"directory",
188-
dict(query, type=Ref.TRACK, album=ref.uri),
188+
dict(query, type=RefType.TRACK, album=ref.uri),
189189
),
190190
name=ref.name,
191191
),
192192
)
193-
elif ref.type == Ref.ARTIST:
193+
elif ref.type == RefType.ARTIST:
194194
refs.append(
195195
Ref.directory(
196196
uri=uritools.uricompose(

src/mopidy_local/schema.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
import sqlite3
66

7-
from mopidy.models import Album, Artist, Image, Ref, Track
7+
from mopidy.models import Album, Artist, Image, Ref, RefType, Track
88

99
_IMAGE_SIZE_RE = re.compile(r".*-(\d+)x(\d+)\.(?:png|gif|jpeg)$")
1010

@@ -22,28 +22,28 @@
2222
_BROWSE_QUERIES = {
2323
None: f"""
2424
SELECT CASE WHEN album.uri IS NULL THEN
25-
'{Ref.TRACK}' ELSE '{Ref.ALBUM}' END AS type,
25+
'{RefType.TRACK}' ELSE '{RefType.ALBUM}' END AS type,
2626
coalesce(album.uri, track.uri) AS uri,
2727
coalesce(album.name, track.name) AS name
2828
FROM track LEFT OUTER JOIN album ON track.album = album.uri
2929
WHERE %s
3030
GROUP BY coalesce(album.uri, track.uri)
3131
ORDER BY %s
3232
""", # noqa: S608
33-
Ref.ALBUM: f"""
34-
SELECT '{Ref.ALBUM}' AS type, uri AS uri, name AS name
33+
RefType.ALBUM: f"""
34+
SELECT '{RefType.ALBUM}' AS type, uri AS uri, name AS name
3535
FROM album
3636
WHERE %s
3737
ORDER BY %s
3838
""", # noqa: S608
39-
Ref.ARTIST: f"""
40-
SELECT '{Ref.ARTIST}' AS type, uri AS uri, name AS name
39+
RefType.ARTIST: f"""
40+
SELECT '{RefType.ARTIST}' AS type, uri AS uri, name AS name
4141
FROM artist
4242
WHERE %s
4343
ORDER BY %s
44-
""", # noqa: S608
45-
Ref.TRACK: f"""
46-
SELECT '{Ref.TRACK}' AS type, uri AS uri, name AS name
44+
""", # noqa: S608
45+
RefType.TRACK: f"""
46+
SELECT '{RefType.TRACK}' AS type, uri AS uri, name AS name
4747
FROM track
4848
WHERE %s
4949
ORDER BY %s
@@ -61,7 +61,7 @@
6161
"performer": "track.performers = ?",
6262
"max-age": "track.last_modified >= (strftime('%s', 'now') - ?) * 1000",
6363
},
64-
Ref.ARTIST: {
64+
RefType.ARTIST: {
6565
"role": {
6666
"albumartist": """EXISTS (
6767
SELECT * FROM album WHERE album.artists = artist.uri
@@ -77,7 +77,7 @@
7777
)""",
7878
},
7979
},
80-
Ref.ALBUM: {
80+
RefType.ALBUM: {
8181
"albumartist": "artists = ?",
8282
"artist": """? IN (
8383
SELECT artists FROM track WHERE album = album.uri
@@ -101,7 +101,7 @@
101101
AND last_modified >= (strftime('%s', 'now') - ?) * 1000
102102
)""",
103103
},
104-
Ref.TRACK: {
104+
RefType.TRACK: {
105105
"album": "album = ?",
106106
"albumartist": """? IN (
107107
SELECT artists FROM album WHERE uri = track.album
@@ -116,13 +116,13 @@
116116
}
117117

118118
_LOOKUP_QUERIES = {
119-
Ref.ALBUM: """
119+
RefType.ALBUM: """
120120
SELECT * FROM tracks WHERE album_uri = ?
121121
""",
122-
Ref.ARTIST: """
122+
RefType.ARTIST: """
123123
SELECT * FROM tracks WHERE ? IN (artist_uri, albumartist_uri)
124124
""",
125-
Ref.TRACK: """
125+
RefType.TRACK: """
126126
SELECT * FROM tracks WHERE uri = ?
127127
""",
128128
}

tests/test_schema.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33
from uuid import UUID
44

5-
from mopidy.models import Album, Artist, Ref, Track
5+
from mopidy.models import Album, Artist, Ref, RefType, Track
66

77
from mopidy_local import schema
88

@@ -133,26 +133,26 @@ def test_dates(self):
133133
def test_lookup_track(self):
134134
with self.connection as c:
135135
for track in self.tracks:
136-
result = schema.lookup(c, Ref.TRACK, track.uri)
136+
result = schema.lookup(c, RefType.TRACK, track.uri)
137137
assert [track] == list(result)
138138

139139
def test_lookup_album(self):
140140
with self.connection as c:
141-
result = schema.lookup(c, Ref.ALBUM, self.albums[0].uri)
141+
result = schema.lookup(c, RefType.ALBUM, self.albums[0].uri)
142142
assert [self.tracks[2]] == list(result)
143143

144-
result = schema.lookup(c, Ref.ALBUM, self.albums[1].uri)
144+
result = schema.lookup(c, RefType.ALBUM, self.albums[1].uri)
145145
assert [self.tracks[3]] == list(result)
146146

147-
result = schema.lookup(c, Ref.ALBUM, self.albums[2].uri)
147+
result = schema.lookup(c, RefType.ALBUM, self.albums[2].uri)
148148
assert [self.tracks[4]] == list(result)
149149

150150
def test_lookup_artist(self):
151151
with self.connection as c:
152-
result = schema.lookup(c, Ref.ARTIST, self.artists[0].uri)
152+
result = schema.lookup(c, RefType.ARTIST, self.artists[0].uri)
153153
assert [self.tracks[1], self.tracks[3]] == list(result)
154154

155-
result = schema.lookup(c, Ref.ARTIST, self.artists[1].uri)
155+
result = schema.lookup(c, RefType.ARTIST, self.artists[1].uri)
156156
assert [self.tracks[4]] == list(result)
157157

158158
@unittest.SkipTest # TODO: check indexed search
@@ -208,30 +208,30 @@ def ref(artist):
208208
return Ref.artist(name=artist.name, uri=artist.uri)
209209

210210
with self.connection as c:
211-
assert list(map(ref, self.artists)) == schema.browse(c, Ref.ARTIST)
211+
assert list(map(ref, self.artists)) == schema.browse(c, RefType.ARTIST)
212212
assert list(map(ref, self.artists)) == schema.browse(
213213
c,
214-
Ref.ARTIST,
214+
RefType.ARTIST,
215215
role=["artist", "albumartist"],
216216
)
217217
assert list(map(ref, self.artists[0:1])) == schema.browse(
218218
c,
219-
Ref.ARTIST,
219+
RefType.ARTIST,
220220
role="artist",
221221
)
222222
assert list(map(ref, self.artists[0:1])) == schema.browse(
223223
c,
224-
Ref.ARTIST,
224+
RefType.ARTIST,
225225
role="composer",
226226
)
227227
assert list(map(ref, self.artists[0:1])) == schema.browse(
228228
c,
229-
Ref.ARTIST,
229+
RefType.ARTIST,
230230
role="performer",
231231
)
232232
assert list(map(ref, self.artists)) == schema.browse(
233233
c,
234-
Ref.ARTIST,
234+
RefType.ARTIST,
235235
role="albumartist",
236236
)
237237

@@ -240,15 +240,15 @@ def ref(album):
240240
return Ref.album(name=album.name, uri=album.uri)
241241

242242
with self.connection as c:
243-
assert list(map(ref, self.albums)) == schema.browse(c, Ref.ALBUM)
243+
assert list(map(ref, self.albums)) == schema.browse(c, RefType.ALBUM)
244244
assert list(map(ref, [])) == schema.browse(
245245
c,
246-
Ref.ALBUM,
246+
RefType.ALBUM,
247247
artist=self.artists[0].uri,
248248
)
249249
assert list(map(ref, self.albums[1:2])) == schema.browse(
250250
c,
251-
Ref.ALBUM,
251+
RefType.ALBUM,
252252
albumartist=self.artists[0].uri,
253253
)
254254

@@ -257,25 +257,25 @@ def ref(track):
257257
return Ref.track(name=track.name, uri=track.uri)
258258

259259
with self.connection as c:
260-
assert list(map(ref, self.tracks)) == schema.browse(c, Ref.TRACK)
260+
assert list(map(ref, self.tracks)) == schema.browse(c, RefType.TRACK)
261261
assert list(map(ref, self.tracks[1:2])) == schema.browse(
262262
c,
263-
Ref.TRACK,
263+
RefType.TRACK,
264264
artist=self.artists[0].uri,
265265
)
266266
assert list(map(ref, self.tracks[2:3])) == schema.browse(
267267
c,
268-
Ref.TRACK,
268+
RefType.TRACK,
269269
album=self.albums[0].uri,
270270
)
271271
assert list(map(ref, self.tracks[3:4])) == schema.browse(
272272
c,
273-
Ref.TRACK,
273+
RefType.TRACK,
274274
albumartist=self.artists[0].uri,
275275
)
276276
assert list(map(ref, self.tracks[4:5])) == schema.browse(
277277
c,
278-
Ref.TRACK,
278+
RefType.TRACK,
279279
composer=self.artists[0].uri,
280280
performer=self.artists[0].uri,
281281
)

0 commit comments

Comments
 (0)