Permalink
Browse files

Migrate code to json API

  • Loading branch information...
samj1912 committed Jul 13, 2017
1 parent e35eff8 commit a79d05efa7d07cc87788d94f91df040928c3f67d
Showing with 109 additions and 117 deletions.
  1. +21 −22 picard/album.py
  2. +2 −2 picard/cluster.py
  3. +21 −21 picard/collection.py
  4. +1 −1 picard/disc.py
  5. +10 −10 picard/file.py
  6. +24 −26 picard/metadata.py
  7. +20 −24 picard/releasegroup.py
  8. +3 −4 picard/track.py
  9. +7 −7 picard/ui/cdlookup.py
View
@@ -37,7 +37,7 @@
from picard.util.textencoding import asciipunct
from picard.cluster import Cluster
from picard.collection import add_release_to_user_collections
-from picard.mbxml import (
+from picard.mbjson import (
release_group_to_metadata,
release_to_metadata,
medium_to_metadata,
@@ -101,31 +101,30 @@ def get_album_artists(self):
"""Returns the list of album artists (as AlbumArtist objects)"""
return self._album_artists
- def _parse_release(self, document):
+ def _parse_release(self, release_node):
log.debug("Loading release %r ...", self.id)
self._tracks_loaded = False
-
- release_node = document.metadata[0].release[0]
- if release_node.id != self.id:
- self.tagger.mbid_redirects[self.id] = release_node.id
- album = self.tagger.albums.get(release_node.id)
+ release_id = release_node['id']
+ if release_id != self.id:
+ self.tagger.mbid_redirects[self.id] = release_id
+ album = self.tagger.albums.get(release_id)
if album:
- log.debug("Release %r already loaded", release_node.id)
+ log.debug("Release %r already loaded", release_id)
album.match_files(self.unmatched_files.files)
album.update()
self.tagger.remove_album(self)
return False
else:
del self.tagger.albums[self.id]
- self.tagger.albums[release_node.id] = self
- self.id = release_node.id
+ self.tagger.albums[release_id] = self
+ self.id = release_id
# Get release metadata
m = self._new_metadata
m.length = 0
- rg_node = release_node.release_group[0]
- rg = self.release_group = self.tagger.get_release_group_by_id(rg_node.id)
+ rg_node = release_node['release-group']
+ rg = self.release_group = self.tagger.get_release_group_by_id(rg_node['id'])
rg.loaded_albums.add(self.id)
rg.refcount += 1
@@ -144,7 +143,7 @@ def _parse_release(self, document):
if config.setting['convert_punctuation']:
m.apply_func(asciipunct)
- m['totaldiscs'] = release_node.medium_list[0].count
+ m['totaldiscs'] = len(release_node['media'])
# Add album to collections
add_release_to_user_collections(release_node)
@@ -223,7 +222,7 @@ def _finalize_loading(self, error):
if hasattr(self._new_metadata, "_djmix_ars"):
djmix_ars = self._new_metadata._djmix_ars
- for medium_node in self._release_node.medium_list[0].medium:
+ for medium_node in self._release_node['media']:
mm = Metadata()
mm.copy(self._new_metadata)
medium_to_metadata(medium_node, mm)
@@ -232,21 +231,21 @@ def _finalize_loading(self, error):
for dj in djmix_ars.get(mm["discnumber"], []):
mm.add("djmixer", dj)
- if "pregap" in medium_node.children:
+ if "pregap" in medium_node:
discpregap = True
absolutetracknumber += 1
- track = self._finalize_loading_track(medium_node.pregap[0], mm, artists, va, absolutetracknumber, discpregap)
+ track = self._finalize_loading_track(medium_node['pregap'], mm, artists, va, absolutetracknumber, discpregap)
track.metadata['~pregap'] = "1"
- tracklist_node = medium_node.track_list[0]
- track_count = int(tracklist_node.count)
+ tracklist_node = medium_node['tracks']
+ track_count = int(medium_node['track-count'])
if track_count:
- for track_node in tracklist_node.track:
+ for track_node in tracklist_node:
absolutetracknumber += 1
track = self._finalize_loading_track(track_node, mm, artists, va, absolutetracknumber, discpregap)
- if "data_track_list" in medium_node.children:
- for track_node in medium_node.data_track_list[0].track:
+ if "data-tracks" in medium_node:
+ for track_node in medium_node['data-tracks']:
absolutetracknumber += 1
track = self._finalize_loading_track(track_node, mm, artists, va, absolutetracknumber, discpregap)
track.metadata['~datatrack'] = "1"
@@ -309,7 +308,7 @@ def _finalize_loading(self, error):
self._after_load_callbacks = []
def _finalize_loading_track(self, track_node, metadata, artists, va, absolutetracknumber, discpregap):
- track = Track(track_node.recording[0].id, self)
+ track = Track(track_node['id'], self)
self._new_tracks.append(track)
# Get track metadata
View
@@ -167,7 +167,7 @@ def _lookup_finished(self, document, http, error):
self.lookup_task = None
try:
- releases = document.metadata[0].release_list[0].release
+ releases = document['releases']
except (AttributeError, IndexError):
releases = None
@@ -201,7 +201,7 @@ def _lookup_finished(self, document, http, error):
mparms,
timeout=3000
)
- self.tagger.move_files_to_album(self.files, match[1].id)
+ self.tagger.move_files_to_album(self.files, match[1]['id'])
def lookup_metadata(self):
"""Try to identify the cluster using the existing metadata."""
View
@@ -106,23 +106,24 @@ def request_finished(document, reply, error):
echo=log.error
)
return
- collection_list = document.metadata[0].collection_list[0]
- if "collection" in collection_list.children:
+ if document and "collection" in document:
+ collection_list = document['collections']
new_collections = set()
- for node in collection_list.collection:
- if node.attribs.get("entity_type") != "release":
+ for node in collection_list:
+ if node["entity-type"] != "release":
continue
- new_collections.add(node.id)
- collection = user_collections.get(node.id)
+ node_id = node['id']
+ new_collections.add(node_id)
+ collection = user_collections.get(node_id)
if collection is None:
- user_collections[node.id] = Collection(node.id, node.name[0].text, node.release_list[0].count)
+ user_collections[node_id] = Collection(node_id, node['name'], node['release-count'])
else:
- collection.name = node.name[0].text
- collection.size = int(node.release_list[0].count)
+ collection.name = node['name']
+ collection.size = int(node['release-count'])
- for id in set(user_collections.keys()) - new_collections:
- del user_collections[id]
+ for collection_id in set(user_collections.keys()) - new_collections:
+ del user_collections[collection_id]
if callback:
callback()
@@ -136,14 +137,13 @@ def request_finished(document, reply, error):
def add_release_to_user_collections(release_node):
"""Add album to collections"""
# Check for empy collection list
- if ("collection_list" in release_node.children and
- "collection" in release_node.collection_list[0].children):
+ if "collections" in release_node:
+ release_id = release_node['id']
username = config.persist["oauth_username"].lower()
- for node in release_node.collection_list[0].collection:
- if node.editor[0].text.lower() == username:
- if node.id not in user_collections:
- user_collections[node.id] = \
- Collection(node.id, node.name[0].text, node.release_list[0].count)
- user_collections[node.id].releases.add(release_node.id)
- log.debug("Adding release %r to %r" %
- (release_node.id, user_collections[node.id]))
+ for node in release_node['collections']:
+ node_id = node['id']
+ if node['editor'].lower() == username:
+ if node_id not in user_collections:
+ user_collections[node_id] = Collection(node_id, node['name'], node['release-count'])
+ user_collections[node_id].releases.add(release_id)
+ log.debug("Adding release %r to %r", release_id, user_collections[node_id])
View
@@ -60,7 +60,7 @@ def _lookup_finished(self, document, http, error):
log.error("%r", http.errorString())
else:
try:
- releases = document.metadata[0].disc[0].release_list[0].release
+ releases = document['releases']
except (AttributeError, IndexError):
log.error(traceback.format_exc())
View
@@ -572,15 +572,16 @@ def _lookup_finished(self, lookuptype, document, http, error):
if self.state == File.REMOVED:
return
-
+ if error:
+ log.error(document)
try:
- m = document.metadata[0]
if lookuptype == "metadata":
- tracks = m.recording_list[0].recording
+ tracks = document['recordings']
elif lookuptype == "acoustid":
- tracks = m.acoustid[0].recording_list[0].recording
- except (AttributeError, IndexError):
+ tracks = document['recordings']
+ except Exception as e:
tracks = None
+ log.debug(e)
# no matches
if not tracks:
@@ -596,7 +597,6 @@ def _lookup_finished(self, lookuptype, document, http, error):
match = sorted((self.metadata.compare_to_track(
track, self.comparison_weights) for track in tracks),
reverse=True, key=itemgetter(0))[0]
-
if lookuptype != 'acoustid':
threshold = config.setting['file_lookup_threshold']
if match[0] < threshold:
@@ -616,12 +616,12 @@ def _lookup_finished(self, lookuptype, document, http, error):
rg, release, track = match[1:]
if lookuptype == 'acoustid':
- self.tagger.acoustidmanager.add(self, track.id)
+ self.tagger.acoustidmanager.add(self, track['id'])
if release:
- self.tagger.get_release_group_by_id(rg.id).loaded_albums.add(release.id)
- self.tagger.move_file_to_track(self, release.id, track.id)
+ self.tagger.get_release_group_by_id(rg['id']).loaded_albums.add(release['id'])
+ self.tagger.move_file_to_track(self, release['id'], track['id'])
else:
- self.tagger.move_file_to_nat(self, track.id, node=track)
+ self.tagger.move_file_to_nat(self, track['id'], node=track)
def lookup_metadata(self):
"""Try to identify the file using the existing metadata."""
View
@@ -25,7 +25,7 @@
linear_combination_of_weights,
)
from picard.util.tags import PRESERVED_TAGS
-from picard.mbxml import artist_credit_from_node
+from picard.mbjson import artist_credit_from_node
from picard.util.imagelist import ImageList
MULTI_VALUED_JOINER = '; '
@@ -119,14 +119,13 @@ def compare_to_release(self, release, weights):
def compare_to_release_parts(self, release, weights):
parts = []
-
if "album" in self:
- b = release.title[0].text
+ b = release['title']
parts.append((similarity2(self["album"], b), weights["album"]))
if "albumartist" in self and "albumartist" in weights:
a = self["albumartist"]
- b = artist_credit_from_node(release.artist_credit[0])[0]
+ b = artist_credit_from_node(release['artist-credit'])[0]
parts.append((similarity2(a, b), weights["albumartist"]))
if "totaltracks" in self:
@@ -135,10 +134,11 @@ def compare_to_release_parts(self, release, weights):
except ValueError:
pass
else:
- if "title" in weights:
- b = int(release.medium_list[0].medium[0].track_list[0].count)
- else:
- b = int(release.medium_list[0].track_count[0].text)
+ if 'media' in release:
+ if "title" in weights:
+ b = int(release['media'][0]['track-count'])
+ else:
+ b = int(release['media'][0]['track-count'])
score = 0.0 if a > b else 0.3 if a < b else 1.0
parts.append((score, weights["totaltracks"]))
@@ -148,9 +148,9 @@ def compare_to_release_parts(self, release, weights):
total_countries = len(preferred_countries)
if total_countries:
score = 0.0
- if "country" in release.children:
+ if "country" in release:
try:
- i = preferred_countries.index(release.country[0].text)
+ i = preferred_countries.index(release['country'])
score = float(total_countries - i) / float(total_countries)
except ValueError:
pass
@@ -160,10 +160,10 @@ def compare_to_release_parts(self, release, weights):
if total_formats:
score = 0.0
subtotal = 0
- for medium in release.medium_list[0].medium:
- if "format" in medium.children:
+ for medium in release['media']:
+ if "format" in medium:
try:
- i = preferred_formats.index(medium.format[0].text)
+ i = preferred_formats.index(medium['format'])
score += float(total_formats - i) / float(total_formats)
except ValueError:
pass
@@ -174,41 +174,40 @@ def compare_to_release_parts(self, release, weights):
if "releasetype" in weights:
type_scores = dict(config.setting["release_type_scores"])
- if 'release_group' in release.children and 'type' in release.release_group[0].attribs:
- release_type = release.release_group[0].type
+ if 'release-group' in release and 'primary-type' in release['release-group']:
+ release_type = release['release-group']['primary-type']
score = type_scores.get(release_type, type_scores.get('Other', 0.5))
else:
score = 0.0
parts.append((score, weights["releasetype"]))
- rg = QObject.tagger.get_release_group_by_id(release.release_group[0].id)
- if release.id in rg.loaded_albums:
+ rg = QObject.tagger.get_release_group_by_id(release['release-group']['id'])
+ if release['id'] in rg.loaded_albums:
parts.append((1.0, 6))
return parts
def compare_to_track(self, track, weights):
parts = []
-
if 'title' in self:
a = self['title']
- b = track.title[0].text
+ b = track['title']
parts.append((similarity2(a, b), weights["title"]))
if 'artist' in self:
a = self['artist']
- b = artist_credit_from_node(track.artist_credit[0])[0]
+ b = artist_credit_from_node(track['artist-credit'])[0]
parts.append((similarity2(a, b), weights["artist"]))
a = self.length
- if a > 0 and 'length' in track.children:
- b = int(track.length[0].text)
+ if a > 0 and 'length' in track:
+ b = int(track['length'])
score = 1.0 - min(abs(a - b), 30000) / 30000.0
parts.append((score, weights["length"]))
releases = []
- if "release_list" in track.children and "release" in track.release_list[0].children:
- releases = track.release_list[0].release
+ if "releases" in track:
+ releases = track['releases']
if not releases:
sim = linear_combination_of_weights(parts)
@@ -219,9 +218,8 @@ def compare_to_track(self, track, weights):
release_parts = self.compare_to_release_parts(release, weights)
sim = linear_combination_of_weights(parts + release_parts)
if sim > result[0]:
- rg = release.release_group[0] if "release_group" in release.children else None
+ rg = release['release-group'] if "release-group" in release else None
result = (sim, rg, release, track)
-
return result
def copy(self, other):
Oops, something went wrong.

0 comments on commit a79d05e

Please sign in to comment.