[totem/wip/hadess/re-enable-pylint: 3/10] opensubtitles: Fix some pylint warnings




commit b434b003b655b79827c3eabd9386d4a73521e7ea
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Jun 23 16:12:11 2022 +0200

    opensubtitles: Fix some pylint warnings
    
    src/plugins/opensubtitles/hash.py:44:19: C0209: Formatting a regular string which could be a f-string 
(consider-using-f-string)
    src/plugins/opensubtitles/hash.py:24:18: R1732: Consider using 'with' for resource-allocating operations 
(consider-using-with)•

 src/plugins/opensubtitles/hash.py | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
---
diff --git a/src/plugins/opensubtitles/hash.py b/src/plugins/opensubtitles/hash.py
index f28341658..4a145b46d 100644
--- a/src/plugins/opensubtitles/hash.py
+++ b/src/plugins/opensubtitles/hash.py
@@ -21,26 +21,24 @@ def hash_file (name):
     if filesize < 65536 * 2:
         return SIZE_ERROR, 0
 
-    file_handle = open (file_to_hash.get_path (), "rb")
+    with file_handle = open (file_to_hash.get_path (), "rb"):
+        for _ in range(int(65536 / bytesize)):
+            buf = file_handle.read (bytesize)
+            (l_value,) = struct.unpack (longlongformat, buf)
+            file_hash += l_value
+            file_hash = file_hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number
 
-    for _ in range(int(65536 / bytesize)):
-        buf = file_handle.read (bytesize)
-        (l_value,) = struct.unpack (longlongformat, buf)
-        file_hash += l_value
-        file_hash = file_hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number
+        file_handle.seek (max (0, filesize - 65536), os.SEEK_SET)
 
-    file_handle.seek (max (0, filesize - 65536), os.SEEK_SET)
+        if file_handle.tell() != max (0, filesize - 65536):
+            return SEEK_ERROR, 0
 
-    if file_handle.tell() != max (0, filesize - 65536):
-        return SEEK_ERROR, 0
+        for _ in range(int(65536/bytesize)):
+            buf = file_handle.read (bytesize)
+            (l_value,) = struct.unpack (longlongformat, buf)
+            file_hash += l_value
+            file_hash = file_hash & 0xFFFFFFFFFFFFFFFF
 
-    for _ in range(int(65536/bytesize)):
-        buf = file_handle.read (bytesize)
-        (l_value,) = struct.unpack (longlongformat, buf)
-        file_hash += l_value
-        file_hash = file_hash & 0xFFFFFFFFFFFFFFFF
-
-    file_handle.close ()
-    returnedhash = "%016x" % file_hash
+    returnedhash = f"{file_hash:016x}"
 
     return returnedhash, filesize


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