summaryrefslogtreecommitdiffstats
path: root/py3status/modules/vnstat.py
diff options
context:
space:
mode:
authorUltrabug <ultrabug@gentoo.org>2014-12-11 23:11:49 +0100
committerUltrabug <ultrabug@gentoo.org>2014-12-11 23:11:49 +0100
commit7259ba0425540631bd393fd80c23164e49669d8b (patch)
treebef6b0020b770e9082b20a135a377b0c14d02f85 /py3status/modules/vnstat.py
parent4d004ace301bb57c9e3bdf9e995ec397a44e0056 (diff)
module vnstat QA and config
Diffstat (limited to 'py3status/modules/vnstat.py')
-rw-r--r--py3status/modules/vnstat.py153
1 files changed, 73 insertions, 80 deletions
diff --git a/py3status/modules/vnstat.py b/py3status/modules/vnstat.py
index 100138c..14c3a9c 100644
--- a/py3status/modules/vnstat.py
+++ b/py3status/modules/vnstat.py
@@ -1,10 +1,4 @@
# -*- coding: utf8 -*-
-
-from __future__ import division # python2 compatibility
-from time import time, sleep
-from subprocess import check_output
-
-
"""
Module for displaying vnstat's statistics.
REQUIRE external program called "vnstat" installed and configured to work.
@@ -13,65 +7,24 @@ REQUIRE external program called "vnstat" installed and configured to work.
@license Eclipse Public License
"""
-CACHED_TIME = 3*60 # update time (in seconds)
-POSITION = 0 # bar position
-STATISTICS_TYPE = "d" # d for daily, m for monthly
-
-"""
-Coloring rules.
-
-If value is bigger that dict key, status string will turn to color, specified in the value.
-Example:
-COLORING = {
- 800: "#dddd00",
- 900: "#dd0000",
-}
-(0 - 800: white, 800-900: yellow, >900 - red)
-"""
-COLORING = {}
-
-"""
-Format of status string.
-
-Placeholders:
- total - total
- up - upload
- down - download
-"""
-FORMAT = "{total}"
-
-PRECISION = 1 # amount of numbers after dot
-MULTIPLIER_TOP = 1024 # if value is greater, divide it with UNIT_MULTI and get next unit from UNITS
-LEFT_ALIGN = 0
-
-"""
-Format of total, up and down placeholders under FORMAT.
-As default, substitutes LEFT_ALIGN and PRECISION as %s and %s
-Placeholders:
- value - value (float)
- unit - unit (string)
-
-"""
-VALUE_FORMAT = "{value:%s.%sf} {unit}" % (LEFT_ALIGN, PRECISION)
-
-INITIAL_MULTI = 1024 # initial multiplier, if you want to get rid of first bytes, set to 1 to disable
-UNIT_MULTI = 1024 # value to divide if rate is greater than MULTIPLIER_TOP
-UNITS = ["kb", "mb", "gb", "tb", ] # list of units, first one - value/INITIAL_MULTI, second - value/1024, third - value/1024^2, etc...
+from __future__ import division # python2 compatibility
+from time import time, sleep
+from subprocess import check_output
-def get_stat():
+def get_stat(statistics_type):
"""
Get statistics from devfile in list of lists of words
"""
def filter_stat():
for x in check_output(["vnstat", "--dumpdb"]).decode("utf-8").splitlines():
- if x.startswith("{};0;".format(STATISTICS_TYPE)):
+ if x.startswith("{};0;".format(statistics_type)):
return x
try:
type, number, ts, rxm, txm, rxk, txk, fill = filter_stat().split(";")
except OSError as e:
- print("Looks like you have'nt installed or configured vnstat!")
+ print("Looks like you haven't installed or configured vnstat!")
raise e
except ValueError:
raise RuntimeError("vnstat returned wrong output, maybe it's configured wrong or module is outdated")
@@ -79,57 +32,97 @@ def get_stat():
up = (int(txm) * 1024 + int(txk)) * 1024
down = (int(rxm) * 1024 + int(rxk)) * 1024
- return {"up": up,
- "down": down,
- "total": up+down, }
+ return {
+ "up": up,
+ "down": down,
+ "total": up+down
+ }
-def divide_and_format(value):
- """
- Divide a value and return formatted string
+class Py3status:
"""
- value /= INITIAL_MULTI
- for i, unit in enumerate(UNITS):
- if value > MULTIPLIER_TOP:
- value /= UNIT_MULTI
- else:
- break
+ Coloring rules.
- return VALUE_FORMAT.format(value=value, unit=unit)
+ If value is bigger that dict key, status string will turn to color, specified in the value.
+ Example:
+ coloring = {
+ 800: "#dddd00",
+ 900: "#dd0000",
+ }
+ (0 - 800: white, 800-900: yellow, >900 - red)
+ Format of status string.
+
+ Placeholders:
+ total - total
+ up - upload
+ down - download
+ """
+
+ # available configuration parameters
+ cache_timeout = 180
+ coloring = {}
+ format = "{total}"
+ initial_multi = 1024 # initial multiplier, if you want to get rid of first bytes, set to 1 to disable
+ left_align = 0
+ multiplier_top = 1024 # if value is greater, divide it with unit_multi and get next unit from units
+ precision = 1
+ statistics_type = "d" # d for daily, m for monthly
+ unit_multi = 1024 # value to divide if rate is greater than multiplier_top
-class Py3status:
def __init__(self, *args, **kwargs):
- self.last_stat = get_stat()
+ """
+ Format of total, up and down placeholders under FORMAT.
+ As default, substitutes left_align and precision as %s and %s
+ Placeholders:
+ value - value (float)
+ unit - unit (string)
+ """
+ self.last_stat = get_stat(self.statistics_type)
self.last_time = time()
self.last_interface = None
+ self.value_format = "{value:%s.%sf} {unit}" % (self.left_align, self.precision)
+ self.units = ["kb", "mb", "gb", "tb", ] # list of units, first one - value/initial_multi, second - value/1024, third - value/1024^2, etc...
+
+ def _divide_and_format(self, value):
+ """
+ Divide a value and return formatted string
+ """
+ value /= self.initial_multi
+ for i, unit in enumerate(self.units):
+ if value > self.multiplier_top:
+ value /= self.unit_multi
+ else:
+ break
+
+ return self.value_format.format(value=value, unit=unit)
- def currentSpeed(self, json, i3status_config):
- stat = get_stat()
+ def currentSpeed(self, i3s_output_list, i3s_config):
+ stat = get_stat(self.statistics_type)
color = None
- keys = list(COLORING.keys())
+ keys = list(self.coloring.keys())
keys.sort()
for k in keys:
if stat["total"] < k * 1024 * 1024:
break
else:
- color = COLORING[k]
+ color = self.coloring[k]
response = {
- 'transformed': True,
- 'full_text': FORMAT.format(
- total=divide_and_format(stat['total']),
- up=divide_and_format(stat['up']),
- down=divide_and_format(stat['down']),),
- 'name': 'vnstat',
- 'cached_until': time() + CACHED_TIME,
+ 'cached_until': time() + self.cache_timeout,
+ 'full_text': self.format.format(
+ total=self._divide_and_format(stat['total']),
+ up=self._divide_and_format(stat['up']),
+ down=self._divide_and_format(stat['down']),
+ ),
+ 'transformed': True
}
if color:
response["color"] = color
- return POSITION, response
+ return response
if __name__ == "__main__":
x = Py3status()