From d9bba14fccf3b7f691139ae6f44b86ffc42a8a05 Mon Sep 17 00:00:00 2001 From: Akashdeep Dhar Date: Jan 09 2022 02:42:03 +0000 Subject: Merge branch 'size-conversion-mod' into 'main' Modularize the format_partition_size function Closes #37 See merge request t0xic0der/obserware!65 --- diff --git a/obserware/sources/readers/__init__.py b/obserware/sources/readers/__init__.py index d3b2bd6..b9595ab 100644 --- a/obserware/sources/readers/__init__.py +++ b/obserware/sources/readers/__init__.py @@ -15,3 +15,26 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . """ + + +class memory_size: + def __init__(self): + self.petabyte = 1024 * 1024 * 1024 * 1024 * 1024 + self.terabyte = 1024 * 1024 * 1024 * 1024 + self.gigabyte = 1024 * 1024 * 1024 + self.megabyte = 1024 * 1024 + self.kilobyte = 1024 + + def format(self, size): + if size > self.petabyte: + return "%.2fPB" % (size / self.petabyte) + elif size > self.terabyte: + return "%.2fTB" % (size / self.terabyte) + elif size > self.gigabyte: + return "%.2fGB" % (size / self.gigabyte) + elif size > self.megabyte: + return "%.2fMB" % (size / self.megabyte) + elif size > self.kilobyte: + return "%.2fKB" % (size / self.kilobyte) + else: + return "%.2fB" % (size) diff --git a/obserware/sources/readers/lgptwind/provider.py b/obserware/sources/readers/lgptwind/provider.py index ca21207..dce75a7 100644 --- a/obserware/sources/readers/lgptwind/provider.py +++ b/obserware/sources/readers/lgptwind/provider.py @@ -19,26 +19,9 @@ along with this program. If not, see . import psutil -petabyte = 1024 * 1024 * 1024 * 1024 * 1024 -terabyte = 1024 * 1024 * 1024 * 1024 -gigabyte = 1024 * 1024 * 1024 -megabyte = 1024 * 1024 -kilobyte = 1024 +from obserware.sources.readers import memory_size - -def format_partition_size(size): - if size > petabyte: - return "%.2fPB" % (size / petabyte) - elif size > terabyte: - return "%.2fTB" % (size / terabyte) - elif size > gigabyte: - return "%.2fGB" % (size / gigabyte) - elif size > megabyte: - return "%.2fMB" % (size / megabyte) - elif size > kilobyte: - return "%.2fKB" % (size / kilobyte) - else: - return "%.2fB" % (size) +mmrysize = memory_size() def return_mainscreen_onetimed_statistics(): @@ -67,9 +50,9 @@ def return_mainscreen_onetimed_statistics(): "lgptnumb": partqant, "lgptdevc": indx.device, "lgptfutl": { - "free": format_partition_size(partfree), - "used": format_partition_size(partused), - "comp": format_partition_size(partcomp), + "free": mmrysize.format(partfree), + "used": mmrysize.format(partused), + "comp": mmrysize.format(partcomp), "perc": partperc, }, "lgptfsys": {"mtpt": indx.mountpoint, "fsys": indx.fstype}, diff --git a/obserware/sources/readers/mainwind/tab_network.py b/obserware/sources/readers/mainwind/tab_network.py index 2417f32..5d8ff6b 100644 --- a/obserware/sources/readers/mainwind/tab_network.py +++ b/obserware/sources/readers/mainwind/tab_network.py @@ -16,30 +16,14 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ + import time import psutil -petabyte = 1024 * 1024 * 1024 * 1024 * 1024 -terabyte = 1024 * 1024 * 1024 * 1024 -gigabyte = 1024 * 1024 * 1024 -megabyte = 1024 * 1024 -kilobyte = 1024 - +from obserware.sources.readers import memory_size -def format_partition_size(size): - if size > petabyte: - return "%.2fPB" % (size / petabyte) - elif size > terabyte: - return "%.2fTB" % (size / terabyte) - elif size > gigabyte: - return "%.2fGB" % (size / gigabyte) - elif size > megabyte: - return "%.2fMB" % (size / megabyte) - elif size > kilobyte: - return "%.2fKB" % (size / kilobyte) - else: - return "%.2fB" % (size) +mmrysize = memory_size() def return_global_network_rate(): @@ -48,10 +32,8 @@ def return_global_network_rate(): nxntiocf = psutil.net_io_counters(pernic=False) retndata = { "bytes": { - "recv": "%s/s" - % format_partition_size(nxntiocf.bytes_recv - pvntiocf.bytes_recv), - "sent": "%s/s" - % format_partition_size(nxntiocf.bytes_sent - pvntiocf.bytes_sent), + "recv": "%s/s" % mmrysize.format(nxntiocf.bytes_recv - pvntiocf.bytes_recv), + "sent": "%s/s" % mmrysize.format(nxntiocf.bytes_sent - pvntiocf.bytes_sent), }, "packets": { "recv": "%dPk/s" % (nxntiocf.packets_recv - pvntiocf.packets_recv), @@ -75,19 +57,19 @@ def return_pernic_threaded_statistics(): indx, netifsat[indx].isup, "%s/s" - % format_partition_size( + % mmrysize.format( nxntioct[indx].bytes_recv - pvntioct[indx].bytes_recv ), "%s/s" - % format_partition_size( + % mmrysize.format( nxntioct[indx].bytes_sent - pvntioct[indx].bytes_sent ), "%dPk/s" % int(nxntioct[indx].packets_recv - pvntioct[indx].packets_recv), "%dPk/s" % int(nxntioct[indx].packets_sent - pvntioct[indx].packets_sent), - "%s" % format_partition_size(nxntioct[indx].bytes_recv), - "%s" % format_partition_size(nxntioct[indx].bytes_sent), + "%s" % mmrysize.format(nxntioct[indx].bytes_recv), + "%s" % mmrysize.format(nxntioct[indx].bytes_sent), "%ldPk(s)" % nxntioct[indx].packets_recv, "%ldPk(s)" % nxntioct[indx].packets_sent, "%ld error(s)" % nxntioct[indx].errin, @@ -103,8 +85,8 @@ def return_mainscreen_threaded_statistics(): netiocnf = psutil.net_io_counters(pernic=False) retndata = { "bytes": { - "recv": "%s" % format_partition_size(netiocnf.bytes_recv), - "sent": "%s" % format_partition_size(netiocnf.bytes_sent), + "recv": "%s" % mmrysize.format(netiocnf.bytes_recv), + "sent": "%s" % mmrysize.format(netiocnf.bytes_sent), }, "packets": { "recv": "%ldPk(s)" % netiocnf.packets_recv, diff --git a/obserware/sources/readers/phptwind/provider.py b/obserware/sources/readers/phptwind/provider.py index 99b5833..079eec7 100644 --- a/obserware/sources/readers/phptwind/provider.py +++ b/obserware/sources/readers/phptwind/provider.py @@ -19,26 +19,9 @@ along with this program. If not, see . import psutil -petabyte = 1024 * 1024 * 1024 * 1024 * 1024 -terabyte = 1024 * 1024 * 1024 * 1024 -gigabyte = 1024 * 1024 * 1024 -megabyte = 1024 * 1024 -kilobyte = 1024 +from obserware.sources.readers import memory_size - -def format_partition_size(size): - if size > petabyte: - return "%.2fPB" % (size / petabyte) - elif size > terabyte: - return "%.2fTB" % (size / terabyte) - elif size > gigabyte: - return "%.2fGB" % (size / gigabyte) - elif size > megabyte: - return "%.2fMB" % (size / megabyte) - elif size > kilobyte: - return "%.2fKB" % (size / kilobyte) - else: - return "%.2fB" % (size) +mmrysize = memory_size() def return_mainscreen_onetimed_statistics(): @@ -59,9 +42,9 @@ def return_mainscreen_onetimed_statistics(): "phptnumb": partqant, "phptdevc": indx.device, "phptfutl": { - "free": format_partition_size(partfree), - "used": format_partition_size(partused), - "comp": format_partition_size(partcomp), + "free": mmrysize.format(partfree), + "used": mmrysize.format(partused), + "comp": mmrysize.format(partcomp), "perc": partperc, }, "phptfsys": {"mtpt": indx.mountpoint, "fsys": indx.fstype},