]> git.ipfire.org Git - oddments/collecty.git/commitdiff
Open all files in a with statement block
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 20 Sep 2020 14:47:40 +0000 (14:47 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 20 Sep 2020 14:47:40 +0000 (14:47 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/collecty/plugins/interface.py
src/collecty/plugins/memory.py
src/collecty/plugins/processor.py

index b1dfe34fa22358f3870b858c3880c3b7d0558662..e63f06c6f337174213b3c94aef834e391c2bb3ee 100644 (file)
@@ -233,20 +233,10 @@ class InterfaceObject(base.Object):
                        path = os.path.join(interface_path, "statistics", file)
 
                        # Open file and read it's content.
-                       f = None
-                       try:
-                               f = open(path)
-
+                       with open(path) as f:
                                line = f.readline()
                                line = line.strip()
                                ret.append(line)
-                       except:
-                               ret.append("0")
-                               raise
-
-                       finally:
-                               if f:
-                                       f.close()
 
                return ret
 
index 01e4d66489e31de6abbd135462bca2789287ac5d..82791f6e65f661d395b80db2722290f2f613daf0 100644 (file)
@@ -90,11 +90,8 @@ class MemoryObject(base.Object):
                return "default"
 
        def collect(self):
-               f = None
-
-               try:
-                       f = open("/proc/meminfo")
-                       for line in f.readlines():
+               with open("/proc/meminfo") as f:
+                       for line in f:
                                if line.startswith("MemTotal:"):
                                        total = float(line.split()[1])
                                if line.startswith("MemFree:"):
@@ -121,9 +118,6 @@ class MemoryObject(base.Object):
                                ret.append("0")
 
                        return ret
-               finally:
-                       if f:
-                               f.close()
 
 
 class MemoryPlugin(base.Plugin):
index bf37b75ee1ede8a99b13477679f7ba4ca9045666..11b3aa8550a92e1471dae6ef249060a693a7c408 100644 (file)
@@ -109,12 +109,8 @@ class ProcessorObject(base.Object):
                """
                        Reads the CPU usage.
                """
-               f = None
-
-               try:
-                       f = open("/proc/stat")
-
-                       for line in f.readlines():
+               with open("/proc/stat") as f:
+                       for line in f:
                                if not line.startswith("cpu"):
                                        continue
 
@@ -131,9 +127,6 @@ class ProcessorObject(base.Object):
                                        columns[6], # irq
                                        columns[7], # sirq
                                )
-               finally:
-                       if f:
-                               f.close()
 
 
 class ProcessorPlugin(base.Plugin):