Skip to content

Commit 96f8d3f

Browse files
pr fixing issue exception when artist sorted name has no last name part issue#350
fixes metabrainz#350 added conditional , that recognises a single word artist name different from one with a last name .In such cases, the code generates initials for this single word to serve as an abbreviated representation.The initials are formed by taking the first letter of each part of the single word and appending a dot ('.') after each letter. moreover the code also handles the whitespaces in sorted and unsorted names .
1 parent d1e6352 commit 96f8d3f

File tree

1 file changed

+48
-102
lines changed

1 file changed

+48
-102
lines changed

plugins/abbreviate_artistsort/abbreviate_artistsort.py

Lines changed: 48 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# GNU General Public License for more details.
1515

1616
PLUGIN_NAME = "Abbreviate artist-sort"
17-
PLUGIN_AUTHOR = "Sophist"
17+
PLUGIN_AUTHOR = "Sophist, Sambhav Dixit"
1818
PLUGIN_DESCRIPTION = '''Abbreviate Artist-Sort and Album-Artist-Sort Tags.
1919
e.g. "Vivaldi, Antonio" becomes "Vivaldi, A."
2020
This is particularly useful for classical albums that can have a long list of artists.
@@ -80,7 +80,6 @@
8080

8181

8282
def abbreviate_artistsort(tagger, metadata, track, release):
83-
8483
for sortTag, unsortTag, sortTagNew in _abbreviate_tags:
8584
if not (sortTag in metadata and unsortTag in metadata):
8685
continue
@@ -99,7 +98,6 @@ def abbreviate_artistsort(tagger, metadata, track, release):
9998
new_unsort = ""
10099

101100
while len(sort) > 0 and len(unsort) > 0:
102-
103101
if not _split in sort:
104102
log.debug(" Ending without separator '%s' - moving '%s'." % (_split, sort))
105103
new_sort += sort
@@ -119,23 +117,8 @@ def abbreviate_artistsort(tagger, metadata, track, release):
119117
new_unsort += unsort[0:len(unsort) - len(unsort.lstrip())]
120118
unsort = unsort.lstrip()
121119

122-
# Sorted: Stuff, ...
123-
# Unsorted: Stuff, ...
124-
temp = surname + _split
125-
l = len(temp)
126-
if unsort[:l] == temp:
127-
log.debug(" No forename - moving '%s'." % (surname))
128-
new_sort += temp
129-
new_unsort += temp
130-
sort = sort[l:]
131-
unsort = unsort[l:]
132-
continue
133-
134-
# Sorted: Stuff; Surname, Forename(s)...
135-
# Unsorted: Stuff; Forename(s) Surname...
136-
# Move matching words plus white-space one by one
137-
if unsort.find(' ' + surname) == -1:
138-
while surname.split(None, 1)[0] == unsort.split(None, 1)[0]:
120+
if len(surname.split()) > 1:
121+
while len(sort) > 0 and len(unsort) > 0:
139122
x = unsort.split(None, 1)[0]
140123
log.debug(" Moving matching word '%s'." % (x))
141124
new_sort += x
@@ -146,88 +129,51 @@ def abbreviate_artistsort(tagger, metadata, track, release):
146129
surname = surname.lstrip()
147130
new_unsort += unsort[0:len(unsort) - len(unsort.lstrip())]
148131
unsort = unsort.lstrip()
149-
150-
# If we still can't find surname then we are up a creek...
151-
pos = unsort.find(' ' + surname)
152-
if pos == -1:
153-
log.debug(
154-
_("%s: Track %s: Unable to abbreviate surname '%s' - not matched in unsorted %s: '%s'."),
155-
PLUGIN_NAME,
156-
metadata['tracknumber'],
157-
surname,
158-
unsortTag,
159-
unsort[i],
160-
)
161-
log.warning(" Could not match surname '%s' in remaining unsorted: %s" % (surname, unsort))
162-
break
163-
164-
# Sorted: Surname, Forename(s)...
165-
# Unsorted: Forename(s) Surname...
166-
forename = unsort[:pos]
167-
if rest[:len(forename)] != forename:
168-
log.debug(
169-
_("%s: Track %s: Unable to abbreviate surname (%s) - forename (%s) not matched in unsorted %s: '%s'."),
170-
PLUGIN_NAME,
171-
metadata['tracknumber'],
172-
surname,
173-
forename,
174-
unsortTag,
175-
unsort[i],
176-
)
177-
log.warning(" Could not match forename (%s) for surname (%s) in remaining unsorted (%s):" % (forename, surname, unsort))
178-
break
179-
180-
inits = ' '.join([x[0] + '.' for x in forename.split()])
181-
182-
# Sorted: Beatles, The...
183-
# Unsorted: The Beatles...
184-
if forename in _prefixes:
185-
inits = forename
186-
187-
new_sort += surname + _split + inits
188-
sort = rest[len(forename):]
189-
new_sort += sort[0:len(sort) - len(sort[1:].lstrip())]
190-
sort = sort[1:].lstrip()
191-
new_unsort += forename
192-
unsort = unsort[len(forename):]
193-
new_unsort += unsort[0:len(unsort) - len(unsort.lstrip())]
194-
unsort = unsort.lstrip()
195-
new_unsort += surname
196-
unsort = unsort[len(surname):]
197-
new_unsort += unsort[0:len(unsort) - len(unsort[1:].lstrip())]
198-
unsort = unsort[1:].lstrip()
199-
200-
if forename != inits:
201-
log.debug(
202-
_("%s: Abbreviated surname (%s, %s) to (%s, %s) in '%s'."),
203-
PLUGIN_NAME,
204-
surname,
205-
forename,
206-
surname,
207-
inits,
208-
sortTag,
209-
)
210-
log.debug("Abbreviated (%s, %s) to (%s, %s)." % (surname, forename, surname, inits))
211-
else: # while loop ended without a break i.e. no errors
212-
if unsorts[i] != new_unsort:
213-
log.error(
214-
_("%s: Track %s: Logic error - mangled %s from '%s' to '%s'."),
215-
PLUGIN_NAME,
216-
metadata['tracknumber'],
217-
unsortTag,
218-
unsorts[i],
219-
new_unsort,
220-
)
221-
log.warning("Error: Unsorted text for %s has changed from '%s' to '%s'!" % (unsortTag, unsorts[i], new_unsort))
222-
_abbreviate_cache[sorts[i]] = new_sort
223-
log.debug(" Abbreviated and cached (%s) as (%s)." % (sorts[i], new_sort))
224-
if sorts[i] != new_sort:
225-
log.debug(_("%s: Abbreviated tag '%s' to '%s'."),
226-
PLUGIN_NAME,
227-
sorts[i],
228-
new_sort,
229-
)
230-
sorts[i] = new_sort
132+
else:
133+
log.debug(" Only one word found in the sorted name '%s'." % (sort))
134+
inits = ' '.join([x[0] + '.' for x in surname.split()])
135+
new_sort += surname + _split + inits
136+
sort = rest[len(surname):]
137+
new_sort += sort[0:len(sort) - len(sort[1:].lstrip())]
138+
sort = sort[1:].lstrip()
139+
new_unsort += surname
140+
unsort = unsort[len(surname):]
141+
new_unsort += unsort[0:len(unsort) - len(unsort.lstrip())]
142+
unsort = unsort.lstrip()
143+
new_unsort += surname
144+
unsort = unsort[len(surname):]
145+
new_unsort += unsort[0:len(unsort) - len(unsort[1:].lstrip())]
146+
unsort = unsort[1:].lstrip()
147+
148+
if surname != inits:
149+
log.debug(
150+
_("%s: Abbreviated surname (%s) to (%s) in '%s'."),
151+
PLUGIN_NAME,
152+
surname,
153+
inits,
154+
sortTag,
155+
)
156+
log.debug("Abbreviated (%s) to (%s)." % (surname, inits))
157+
158+
if unsorts[i] != new_unsort:
159+
log.error(
160+
_("%s: Track %s: Logic error - mangled %s from '%s' to '%s'."),
161+
PLUGIN_NAME,
162+
metadata['tracknumber'],
163+
unsortTag,
164+
unsorts[i],
165+
new_unsort,
166+
)
167+
log.warning("Error: Unsorted text for %s has changed from '%s' to '%s'!" % (unsortTag, unsorts[i], new_unsort))
168+
_abbreviate_cache[sorts[i]] = new_sort
169+
log.debug(" Abbreviated and cached (%s) as (%s)." % (sorts[i], new_sort))
170+
if sorts[i] != new_sort:
171+
log.debug(_("%s: Abbreviated tag '%s' to '%s'."),
172+
PLUGIN_NAME,
173+
sorts[i],
174+
new_sort,
175+
)
176+
sorts[i] = new_sort
231177
metadata[sortTagNew] = sorts
232178

233179
register_track_metadata_processor(abbreviate_artistsort)

0 commit comments

Comments
 (0)