From 820598b1b8c7433332584616074ff87e506c67e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Sun, 30 Aug 2020 12:11:48 -0300 Subject: [PATCH 1/4] Fix memory module tests --- bumblebee_status/modules/core/memory.py | 35 ++++++++++++++++--------- tests/modules/core/test_memory.py | 1 - 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/bumblebee_status/modules/core/memory.py b/bumblebee_status/modules/core/memory.py index 82de769..9b4a3ab 100644 --- a/bumblebee_status/modules/core/memory.py +++ b/bumblebee_status/modules/core/memory.py @@ -41,18 +41,8 @@ class Module(core.module.Module): return self._format.format(**self._mem) def update(self): - data = {} - with open("/proc/meminfo", "r") as f: - for line in f: - tmp = re.split(r"[:\s]+", line) - value = int(tmp[1]) - if tmp[2] == "kB": - value = value * 1024 - if tmp[2] == "mB": - value = value * 1024 * 1024 - if tmp[2] == "gB": - value = value * 1024 * 1024 * 1024 - data[tmp[0]] = value + data = self.__parse_meminfo() + if "MemAvailable" in data: used = data["MemTotal"] - data["MemAvailable"] else: @@ -78,5 +68,26 @@ class Module(core.module.Module): return "warning" return None + def __parse_meminfo(self): + data = {} + with open("/proc/meminfo", "r") as f: + # https://bugs.python.org/issue32933 + while True: + line = f.readline() + + if line == '': + break + + tmp = re.split(r"[:\s]+", line) + value = int(tmp[1]) + if tmp[2] == "kB": + value = value * 1024 + if tmp[2] == "mB": + value = value * 1024 * 1024 + if tmp[2] == "gB": + value = value * 1024 * 1024 * 1024 + data[tmp[0]] = value + + return data # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/modules/core/test_memory.py b/tests/modules/core/test_memory.py index b133c52..9f52032 100644 --- a/tests/modules/core/test_memory.py +++ b/tests/modules/core/test_memory.py @@ -30,7 +30,6 @@ def meminfo_mock( ('Slab', slab) ] - for i, (key, value) in enumerate(states): data.append('{}: {} kB'.format(key, value)) From 6f6f3cedd930103f9f4bfd8b740dce43ce80422d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Sun, 30 Aug 2020 12:28:48 -0300 Subject: [PATCH 2/4] Improve meminfo parse logic --- bumblebee_status/modules/core/memory.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/bumblebee_status/modules/core/memory.py b/bumblebee_status/modules/core/memory.py index 9b4a3ab..a4015ff 100644 --- a/bumblebee_status/modules/core/memory.py +++ b/bumblebee_status/modules/core/memory.py @@ -72,12 +72,7 @@ class Module(core.module.Module): data = {} with open("/proc/meminfo", "r") as f: # https://bugs.python.org/issue32933 - while True: - line = f.readline() - - if line == '': - break - + for line in f.readlines(): tmp = re.split(r"[:\s]+", line) value = int(tmp[1]) if tmp[2] == "kB": From 49de0e520b8c879111096c00d1c7abba77ea1fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Sun, 30 Aug 2020 12:37:58 -0300 Subject: [PATCH 3/4] Reduce code cognitive complexity --- bumblebee_status/modules/core/memory.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/bumblebee_status/modules/core/memory.py b/bumblebee_status/modules/core/memory.py index a4015ff..c9ecf44 100644 --- a/bumblebee_status/modules/core/memory.py +++ b/bumblebee_status/modules/core/memory.py @@ -74,15 +74,22 @@ class Module(core.module.Module): # https://bugs.python.org/issue32933 for line in f.readlines(): tmp = re.split(r"[:\s]+", line) - value = int(tmp[1]) - if tmp[2] == "kB": - value = value * 1024 - if tmp[2] == "mB": - value = value * 1024 * 1024 - if tmp[2] == "gB": - value = value * 1024 * 1024 * 1024 + value = self.__parse_value(tmp) + data[tmp[0]] = value return data + def __parse_value(self, data): + value = int(data[1]) + + if data[2] == "kB": + value = value * 1024 + if data[2] == "mB": + value = value * 1024 * 1024 + if data[2] == "gB": + value = value * 1024 * 1024 * 1024 + + return value + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 From 0c8d682d62b1305df5643e12cabfc50ea5ed8a94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Sun, 30 Aug 2020 12:49:33 -0300 Subject: [PATCH 4/4] Add unit tests --- tests/modules/core/test_memory.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tests/modules/core/test_memory.py b/tests/modules/core/test_memory.py index 9f52032..4787395 100644 --- a/tests/modules/core/test_memory.py +++ b/tests/modules/core/test_memory.py @@ -15,6 +15,7 @@ def memory_widget(module): def meminfo_mock( total, available, + unit = 'kB', free = 0, buffers = 0, cached = 0, @@ -31,7 +32,7 @@ def meminfo_mock( ] for i, (key, value) in enumerate(states): - data.append('{}: {} kB'.format(key, value)) + data.append('{}: {} {}'.format(key, value, unit)) return '\n'.join(data) @@ -118,3 +119,25 @@ class TestMemory(TestCase): assert widget.full_text() == '50.0%' assert module.state(widget) == None + + + @mock.patch('builtins.open', mock.mock_open(read_data=meminfo_mock(8196, 4096, 'mB'))) + def test_mb_unit(self): + module = build_module() + module.update() + + widget = memory_widget(module) + + assert widget.full_text() == '4.00GiB/8.00GiB (50.02%)' + assert module.state(widget) == None + + @mock.patch('builtins.open', mock.mock_open(read_data=meminfo_mock(2, 1, 'gB'))) + def test_gb_unit(self): + module = build_module() + module.update() + + widget = memory_widget(module) + + assert widget.full_text() == '1.00GiB/2.00GiB (50.00%)' + assert module.state(widget) == None +