From b239a0b3d2dcbde0495750903dc2e69067d48ebf Mon Sep 17 00:00:00 2001 From: ybnd Date: Thu, 4 Jun 2020 11:15:34 +0200 Subject: [PATCH 1/2] Fix item == None issues when writing lyrics ReST * Skip ReST writing & sphinx info messages if query doesn't yield anything * `writerest` into `appendrest` and `writerest`, don't call `writerest(item=None)` to flush state at the end. --- beetsplug/lyrics.py | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index 71e1d4ef3..3e03bd224 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -770,7 +770,8 @@ class LyricsPlugin(plugins.BeetsPlugin): write = ui.should_write() if opts.writerest: self.writerest_indexes(opts.writerest) - for item in lib.items(ui.decargs(args)): + items = lib.items(ui.decargs(args)) + for item in items: if not opts.local_only and not self.config['local']: self.fetch_item_lyrics( lib, item, write, @@ -780,10 +781,10 @@ class LyricsPlugin(plugins.BeetsPlugin): if opts.printlyr: ui.print_(item.lyrics) if opts.writerest: - self.writerest(opts.writerest, item) - if opts.writerest: - # flush last artist - self.writerest(opts.writerest, None) + self.appendrest(opts.writerest, item) + if opts.writerest and len(items) > 0: + # flush last artist & write to ReST + self.writerest(opts.writerest) ui.print_(u'ReST files generated. to build, use one of:') ui.print_(u' sphinx-build -b html %s _build/html' % opts.writerest) @@ -795,26 +796,21 @@ class LyricsPlugin(plugins.BeetsPlugin): cmd.func = func return [cmd] - def writerest(self, directory, item): - """Write the item to an ReST file + def appendrest(self, directory, item): + """Append the item to an ReST file This will keep state (in the `rest` variable) in order to avoid writing continuously to the same files. """ - if item is None or slug(self.artist) != slug(item.albumartist): - if self.rest is not None: - path = os.path.join(directory, 'artists', - slug(self.artist) + u'.rst') - with open(path, 'wb') as output: - output.write(self.rest.encode('utf-8')) - self.rest = None - if item is None: - return + if slug(self.artist) != slug(item.albumartist): + # Write current file and start a new one ~ item.albumartist + self.writerest(directory) self.artist = item.albumartist.strip() self.rest = u"%s\n%s\n\n.. contents::\n :local:\n\n" \ % (self.artist, u'=' * len(self.artist)) + if self.album != item.album: tmpalbum = self.album = item.album.strip() if self.album == '': @@ -826,6 +822,16 @@ class LyricsPlugin(plugins.BeetsPlugin): u'~' * len(title_str), block) + def writerest(self, directory): + """Write self.rest to a ReST file + """ + if self.rest is not None and self.artist is not None: + path = os.path.join(directory, 'artists', + slug(self.artist) + u'.rst') + with open(path, 'wb') as output: + output.write(self.rest.encode('utf-8')) + + def writerest_indexes(self, directory): """Write conf.py and index.rst files necessary for Sphinx From d1f3d664d75edb1f681264a2d578ddec21ea6d4f Mon Sep 17 00:00:00 2001 From: ybnd Date: Thu, 4 Jun 2020 16:43:42 +0200 Subject: [PATCH 2/2] Implement comments & add to changelog --- beetsplug/lyrics.py | 3 +-- docs/changelog.rst | 3 +++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index 3e03bd224..05d3ea53f 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -782,7 +782,7 @@ class LyricsPlugin(plugins.BeetsPlugin): ui.print_(item.lyrics) if opts.writerest: self.appendrest(opts.writerest, item) - if opts.writerest and len(items) > 0: + if opts.writerest and items: # flush last artist & write to ReST self.writerest(opts.writerest) ui.print_(u'ReST files generated. to build, use one of:') @@ -831,7 +831,6 @@ class LyricsPlugin(plugins.BeetsPlugin): with open(path, 'wb') as output: output.write(self.rest.encode('utf-8')) - def writerest_indexes(self, directory): """Write conf.py and index.rst files necessary for Sphinx diff --git a/docs/changelog.rst b/docs/changelog.rst index 21cbd1217..691a42de5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -207,6 +207,9 @@ Fixes: * :doc:`/plugins/lyrics`: Adapt the Genius backend to changes in markup to reduce the scraping failure rate. :bug:`3535` :bug:`3594` +* :doc:`/plugins/lyrics`: Fix crash when writing ReST files for a query without + results or fetched lyrics + :bug:`2805` For plugin developers: