[rhythmbox: 7/8] Close file handle when reading / saving queue cache



commit 6fd1a5118c9dab27889201f51d765a512c27fd18
Author: Philipp Wolfer <phil parolu io>
Date:   Fri Apr 12 10:14:36 2019 +0200

    Close file handle when reading / saving queue cache

 plugins/listenbrainz/queue.py | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
---
diff --git a/plugins/listenbrainz/queue.py b/plugins/listenbrainz/queue.py
index 6db4b7785..5790b85fb 100644
--- a/plugins/listenbrainz/queue.py
+++ b/plugins/listenbrainz/queue.py
@@ -1,4 +1,4 @@
-# Copyright (c) 2018 Philipp Wolfer <ph wolfer gmail com>
+# Copyright (c) 2018-2019 Philipp Wolfer <ph wolfer gmail com>
 #
 # Permission is hereby granted, free of charge, to any person obtaining
 # a copy of this software and associated documentation files (the
@@ -45,7 +45,7 @@ class ListenBrainzQueue:
 
     def add(self, listened_at, track):
         try:
-            # Try to submit immediatelly, and queue if it fails
+            # Try to submit immediately, and queue if it fails
             response = self.__client.listen(listened_at, track)
             if response.status in [401, 429] or response.status >= 500:
                 self._append(listened_at, track)
@@ -57,7 +57,8 @@ class ListenBrainzQueue:
         cache_file = self.get_cache_file_path()
         if os.path.exists(cache_file):
             logger.debug("Loading queue from %s", cache_file)
-            self.__queue = json.load(open(cache_file), object_hook=from_json)
+            with open(cache_file) as f:
+                self.__queue = json.load(f, object_hook=from_json)
 
     def save(self):
         cache_file = self.get_cache_file_path()
@@ -65,7 +66,8 @@ class ListenBrainzQueue:
         if not os.path.exists(cache_dir):
             os.makedirs(cache_dir)
         logger.debug("Saving queue to %s", cache_file)
-        json.dump(self.__queue, open(cache_file, 'w'), cls=QueueEncoder)
+        with open(cache_file, 'w') as f:
+            json.dump(self.__queue, f, cls=QueueEncoder)
 
     def _append(self, listened_at, track):
         logger.debug("Queuing for later submission %s: %s", listened_at, track)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]