From 04bb38ff174cb98bf49474bfb5a41d27050a5510 Mon Sep 17 00:00:00 2001 From: nicodebo Date: Fri, 21 Jul 2017 00:40:30 +0200 Subject: Fix albumratings does not exist The first time rating an album, a FileNotFoundError is raised because ~/.config/clerk/albumratings.json does not exists: - Moved the file opening (i.e. load_fastlist) inside the 'try' block and added an except clause, to catch the FileNotFoundError exception and process it accordingly. - Moved the action of saving the json track database and setting the sticker into a 'finally' block in order not to duplicate lines of code in the different blocks of the try/except. - Replaced the 'artist' tag with the 'albumartist' one. --- clerk_helper | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'clerk_helper') diff --git a/clerk_helper b/clerk_helper index 47e3bb2..ee4fa88 100755 --- a/clerk_helper +++ b/clerk_helper @@ -89,19 +89,20 @@ def prune_fastlist(fastlist, mpdcachefile): return (new, newhdata) def rateAlbum(args): - fastlist = load_fastlist(os.getenv('HOME')+'/.config/clerk/albumratings.json') try: + fastlist = load_fastlist(os.getenv('HOME')+'/.config/clerk/albumratings.json') entry = get_entry(fastlist, args.artist, args.album, args.date) entry["rating"] = args.rating - save_fastlist(os.getenv('HOME')+'/.config/clerk/albumratings.json', fastlist) - uri = client.find('albumartist', args.artist, 'album', args.album, 'date', args.date, 'track', os.getenv('track'), 'disc', os.getenv('disc')) - for i in uri: - client.sticker_set("song", i['file'], "albumrating", args.rating) except KeyError: entry = {'albumartist': args.artist, 'album': args.album, 'date': args.date, 'disc': os.getenv('disc'), 'track': os.getenv('track'), 'rating': args.rating} append_entry(fastlist, entry) + except FileNotFoundError: + fastlist = ([], {}) + entry = {'albumartist': args.artist, 'album': args.album, 'date': args.date, 'disc': os.getenv('disc'), 'track': os.getenv('track'), 'rating': args.rating} + fastlist[0].append(entry) + finally: save_fastlist(os.getenv('HOME')+'/.config/clerk/albumratings.json', fastlist) - uri = client.find('artist', args.artist, 'album', args.album, 'date', args.date, 'track', os.getenv('track'), 'disc', os.getenv('disc')) + uri = client.find('albumartist', args.artist, 'album', args.album, 'date', args.date, 'track', os.getenv('track'), 'disc', os.getenv('disc')) for i in uri: client.sticker_set("song", i['file'], "albumrating", args.rating) -- cgit v1.2.3-24-g4f1b