From 9ecf706d63ce61652cf2230f82bf78a93737cde0 Mon Sep 17 00:00:00 2001 From: Rob Weber Date: Tue, 31 Dec 2019 10:49:16 -0600 Subject: [PATCH] use contextlib --- resources/lib/backup.py | 5 ++--- resources/lib/scheduler.py | 5 ++--- resources/lib/vfs.py | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/resources/lib/backup.py b/resources/lib/backup.py index 74103f8..2947b0b 100644 --- a/resources/lib/backup.py +++ b/resources/lib/backup.py @@ -508,9 +508,8 @@ class XbmcBackup: else: self.xbmc_vfs.put(path + "xbmcbackup.val", xbmc.translatePath(utils.data_dir() + "xbmcbackup_restore.val")) - vFile = xbmcvfs.File(xbmc.translatePath(utils.data_dir() + "xbmcbackup_restore.val"), 'r') - jsonString = vFile.read() - vFile.close() + with xbmcvfs.File(xbmc.translatePath(utils.data_dir() + "xbmcbackup_restore.val"), 'r') as vFile: + jsonString = vFile.read() # delete after checking xbmcvfs.delete(xbmc.translatePath(utils.data_dir() + "xbmcbackup_restore.val")) diff --git a/resources/lib/scheduler.py b/resources/lib/scheduler.py index 8147309..2642da3 100644 --- a/resources/lib/scheduler.py +++ b/resources/lib/scheduler.py @@ -132,9 +132,8 @@ class BackupScheduler: utils.log("scheduler will run again on " + utils.getRegionalTimestamp(datetime.fromtimestamp(self.next_run), ['dateshort', 'time'])) # write the next time to a file - fh = xbmcvfs.File(self.next_run_path, 'w') - fh.write(str(self.next_run)) - fh.close() + with xbmcvfs.File(self.next_run_path, 'w') as fh: + fh.write(str(self.next_run)) # only show when not in silent mode if(progress_mode != 2): diff --git a/resources/lib/vfs.py b/resources/lib/vfs.py index 806ea54..16c1efb 100644 --- a/resources/lib/vfs.py +++ b/resources/lib/vfs.py @@ -86,9 +86,8 @@ class XBMCFileSystem(Vfs): return xbmcvfs.exists(aFile) def fileSize(self, filename): - f = xbmcvfs.File(filename) - result = f.size() / 1024 # bytes to kilobytes - f.close() + with xbmcvfs.File(filename) as f: + result = f.size() / 1024 # bytes to kilobytes return result