Minor modernizing of i3wrapper
This commit is contained in:
parent
f54f8bd5a8
commit
a500a63a53
@ -1,54 +1,56 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import subprocess
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
__all__ = ["_mem", "_backlight", "_hwtemp"]
|
||||||
|
|
||||||
_BITS = []
|
_BITS = []
|
||||||
|
|
||||||
|
|
||||||
def _bit(idx):
|
def _bit(idx):
|
||||||
def wrapper(func):
|
def wrapper(func):
|
||||||
_BITS.append([idx, func.__name__.replace('_', ''), func])
|
_BITS.append([idx, func.__name__.replace("_", ""), func])
|
||||||
return func
|
return func
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
@_bit(6)
|
@_bit(6)
|
||||||
def _mem():
|
def _mem():
|
||||||
with open('/proc/meminfo') as meminfo_fp:
|
with open("/proc/meminfo") as meminfo_fp:
|
||||||
attrs = {}
|
attrs = {}
|
||||||
for line in meminfo_fp.readlines():
|
for line in meminfo_fp.readlines():
|
||||||
parts = line.split(':', 2)
|
parts = line.split(":", 2)
|
||||||
attrs[parts[0].strip()] = parts[1].strip()
|
attrs[parts[0].strip()] = parts[1].strip()
|
||||||
avail = int(attrs['MemAvailable'].split()[0])
|
avail = int(attrs["MemAvailable"].split()[0])
|
||||||
total = int(attrs['MemTotal'].split()[0])
|
total = int(attrs["MemTotal"].split()[0])
|
||||||
used = int(((total - avail) / total) * 100)
|
used = int(((total - avail) / total) * 100)
|
||||||
color = None
|
color = None
|
||||||
if used > 75:
|
if used > 75:
|
||||||
color = '#ff0000'
|
color = "#ff0000"
|
||||||
if used < 25:
|
if used < 25:
|
||||||
color = '#00ff00'
|
color = "#00ff00"
|
||||||
return 'M:{}%'.format(used), color
|
return "M:{}%".format(used), color
|
||||||
|
|
||||||
|
|
||||||
@_bit(0)
|
@_bit(0)
|
||||||
def _backlight(dev_dir='/sys/class/backlight/intel_backlight'):
|
def _backlight(dev_dir="/sys/class/backlight/intel_backlight"):
|
||||||
with open('{}/brightness'.format(dev_dir)) as br_fp:
|
with open("{}/brightness".format(dev_dir)) as br_fp:
|
||||||
with open('{}/max_brightness'.format(dev_dir)) as mb_fp:
|
with open("{}/max_brightness".format(dev_dir)) as mb_fp:
|
||||||
value = int(br_fp.read().strip())
|
value = int(br_fp.read().strip())
|
||||||
max_value = int(mb_fp.read().strip())
|
max_value = int(mb_fp.read().strip())
|
||||||
pct = int(float(float(value) / float(max_value)) * 100)
|
pct = int(float(float(value) / float(max_value)) * 100)
|
||||||
color = None
|
color = None
|
||||||
if pct >= 75:
|
if pct >= 75:
|
||||||
color = '#ffaa00'
|
color = "#ffaa00"
|
||||||
if pct <= 25:
|
if pct <= 25:
|
||||||
color = '#5599ff'
|
color = "#5599ff"
|
||||||
return '☼:{}%'.format(pct), color
|
return "☼:{}%".format(pct), color
|
||||||
|
|
||||||
|
|
||||||
THINKFAN_CONF = '/etc/thinkfan.conf'
|
THINKFAN_CONF = "/etc/thinkfan.conf"
|
||||||
ACPI_IBM_FAN = '/proc/acpi/ibm/fan'
|
ACPI_IBM_FAN = "/proc/acpi/ibm/fan"
|
||||||
|
|
||||||
|
|
||||||
@_bit(7)
|
@_bit(7)
|
||||||
@ -58,40 +60,40 @@ def _hwtemp(conf=THINKFAN_CONF, fan=ACPI_IBM_FAN):
|
|||||||
|
|
||||||
temps = []
|
temps = []
|
||||||
|
|
||||||
with open('/etc/thinkfan.conf') as tfc_fp:
|
with open("/etc/thinkfan.conf") as tfc_fp:
|
||||||
for line in tfc_fp.readlines():
|
for line in tfc_fp.readlines():
|
||||||
if not line.startswith('hwmon '):
|
if not line.startswith("hwmon "):
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
with open(line.split(' ')[1].strip()) as temp_fp:
|
with open(line.split(" ")[1].strip()) as temp_fp:
|
||||||
temps.append(float(temp_fp.read()))
|
temps.append(float(temp_fp.read()))
|
||||||
except (OSError, IOError) as exc:
|
except (OSError, IOError) as exc:
|
||||||
sys.stderr.write(str(exc) + '\n')
|
sys.stderr.write(str(exc) + "\n")
|
||||||
|
|
||||||
avg_temp = float(sum(temps)) / len(temps) / 1000.0
|
avg_temp = float(sum(temps)) / len(temps) / 1000.0
|
||||||
color = None
|
color = None
|
||||||
|
|
||||||
fan_level = 'unset'
|
fan_level = "unset"
|
||||||
try:
|
try:
|
||||||
with open(fan) as fan_fp:
|
with open(fan) as fan_fp:
|
||||||
for line in fan_fp.readlines():
|
for line in fan_fp.readlines():
|
||||||
if not line.startswith('level:\t\t'):
|
if not line.startswith("level:\t\t"):
|
||||||
continue
|
continue
|
||||||
fan_level = int(line.replace('level:\t\t', ''))
|
fan_level = int(line.replace("level:\t\t", ""))
|
||||||
except (OSError, IOError) as exc:
|
except (OSError, IOError) as exc:
|
||||||
sys.stderr.write(str(exc) + '\n')
|
sys.stderr.write(str(exc) + "\n")
|
||||||
|
|
||||||
if avg_temp > 75:
|
if avg_temp > 75:
|
||||||
color = '#ff0000'
|
color = "#ff0000"
|
||||||
if avg_temp >= 50:
|
if avg_temp >= 50:
|
||||||
color = '#ffaa00'
|
color = "#ffaa00"
|
||||||
if avg_temp <= 25:
|
if avg_temp <= 25:
|
||||||
color = '#5599ff'
|
color = "#5599ff"
|
||||||
return 'T:{:.1f}°C L:{}'.format(avg_temp, fan_level), color
|
return "T:{:.1f}°C L:{}".format(avg_temp, fan_level), color
|
||||||
|
|
||||||
|
|
||||||
def _print_line(message):
|
def _print_line(message):
|
||||||
sys.stdout.write(message + '\n')
|
sys.stdout.write(message + "\n")
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
@ -110,9 +112,9 @@ def main(bits=_BITS):
|
|||||||
_print_line(_read_line())
|
_print_line(_read_line())
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
line, prefix = _read_line(), ''
|
line, prefix = _read_line(), ""
|
||||||
if line.startswith(','):
|
if line.startswith(","):
|
||||||
line, prefix = line[1:], ','
|
line, prefix = line[1:], ","
|
||||||
|
|
||||||
loaded = json.loads(line)
|
loaded = json.loads(line)
|
||||||
for idx, name, func in bits:
|
for idx, name, func in bits:
|
||||||
@ -127,11 +129,11 @@ def main(bits=_BITS):
|
|||||||
|
|
||||||
loaded.insert(idx, record)
|
loaded.insert(idx, record)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
sys.stderr.write(str(exc) + '\n')
|
sys.stderr.write(str(exc) + "\n")
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
|
|
||||||
_print_line(prefix+json.dumps(loaded))
|
_print_line(prefix + json.dumps(loaded))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user