sites

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit f0aa82c45f960474f0384e135959da7841058743
parent 4f5f9d9f3f9d8e2865fde5f58786e241ae9617d5
Author: Wojciech Gac <wgac@wp.pl>
Date:   Mon,  4 Oct 2010 01:42:01 +0200

Added two items to python code snippets: memory and battery indicator for wmii.
Diffstat:
Awmii.suckless.org/code_snippets/python/battery_indicator.md | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Awmii.suckless.org/code_snippets/python/memory_usage_indicator.md | 29+++++++++++++++++++++++++++++
2 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/wmii.suckless.org/code_snippets/python/battery_indicator.md b/wmii.suckless.org/code_snippets/python/battery_indicator.md @@ -0,0 +1,81 @@ +Battery Indicator +================= + +This is a simple battery indicator. Depending on the value of `from_acpi` +it either uses the system command `acpi` or processes data from the `/proc` +filesystem. To use it place the following code in your `wmiirc_local.py` file. +Please note, that monitors in wmii are sorted +alphabetically by their function names, so you might want to change the name as +you see fit. + +The `trem` function is a helper function used to convert seconds to the format +`hh:mm:ss`. `info_path` and `state_path` point to virtual files containing relevant +information regarding the battery. + + +` +### +import subprocess + + +from_acpi = False +#Read battery data from 'acpi' command. +#If set to 'False' read data from /proc filesystem +info_path = '/proc/acpi/battery/BAT0/info' +state_path = '/proc/acpi/battery/BAT0/state' +infostr = open(info_path) +statestr = open(state_path) + +def trem(sec): + """Convert number of seconds to the format hh:mm:ss""" + h = sec / 3600 + m = (sec - h * 3600) / 60 + s = (sec - h * 3600 - m * 60) + return '%02d:%02d:%02d' % (h, m, s) +### + +@defmonitor +def indicator(self): + if from_acpi: + inp = subprocess.Popen(['acpi'], shell = False, stdout = subprocess.PIPE) + out = inp.communicate() + return wmii.cache['normcolors'], out[0][:-1] + else: + info_raw = infostr.readlines() + state_raw = statestr.readlines() + infostr.seek(0) + statestr.seek(0) + info_list = [] + state_list = [] + for i in info_raw: + temp = i.split(':') + temp = map(str.strip, temp) + info_list.append(temp) + for i in state_raw: + temp = i.split(':') + temp = map(str.strip, temp) + state_list.append(temp) + info = dict(info_list) + state = dict(state_list) + if state['charging state'] == 'charging': + perc = float(state['remaining capacity'].split()[0])/\ + float(info['last full capacity'].split()[0])*100 + rem = int((float(info['last full capacity'].split()[0]) - \ + float(state['remaining capacity'].split()[0]))/\ + float(state['present rate'].split()[0])*3600) + res = 'Battery: Charging, %d%%, ' %perc\ + + trem(rem) + ' remaining' + return wmii.cache['normcolors'], str(res) + elif state['charging state'] == 'discharging': + perc = float(state['remaining capacity'].split()[0])/\ + float(info['last full capacity'].split()[0])*100 + rem = int(float(state['remaining capacity'].split()[0])/\ + float(state['present rate'].split()[0])*3600) + return wmii.cache['normcolors'], 'Battery: Discharging, %d%%, ' %perc\ + + trem(rem) + ' remaining' + elif state['charging state'] == 'charged': + return wmii.cache['normcolors'], 'Battery: Fully Charged' + else: + return wmii.cache['focuscolors'], 'Battery: UNRECOGNIZED STATE!!!' +` + diff --git a/wmii.suckless.org/code_snippets/python/memory_usage_indicator.md b/wmii.suckless.org/code_snippets/python/memory_usage_indicator.md @@ -0,0 +1,29 @@ +Memory Usage Indicator +====================== + +This is a simple memory usage indicator based on information obtainable from +the `free` system command. + +It was written to work with wmii-3.9 and higher. To use it you have to put it +in your `wmiirc_local.py` file. Please note, that monitors in wmii are sorted +alphabetically by their function names, so you might want to change the name as +you see fit. + + +` +### +import subprocess +### + +@defmonitor +def fmem(self): + inp = subprocess.Popen(['free'], shell = False, stdout = subprocess.PIPE) + out = inp.communicate() + ram = int(out[0].splitlines()[2].split()[2])/1024 + maxram = int(out[0].splitlines()[1].split()[1])/1024 + swap = int(out[0].splitlines()[3].split()[2])/1024 + maxswap = int(out[0].splitlines()[3].split()[1])/1024 + return wmii.cache['normcolors'], 'RAM: ' + str(ram) + '/' + str(maxram) +\ + ' MB' + ' | SWAP: ' + str(swap) + '/' + str(maxswap) + ' MB' +` +