sites

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

commit f3a35d30b5783954eb493e39102d3515e63850a8
parent f0aa82c45f960474f0384e135959da7841058743
Author: Wojciech Gac <wgac@wp.pl>
Date:   Fri,  8 Oct 2010 10:16:08 +0200

Slight correction of the text formatting.
Diffstat:
Mwmii.suckless.org/code_snippets/python/battery_indicator.md | 124++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mwmii.suckless.org/code_snippets/python/memory_usage_indicator.md | 29+++++++++++++----------------
2 files changed, 73 insertions(+), 80 deletions(-)

diff --git a/wmii.suckless.org/code_snippets/python/battery_indicator.md b/wmii.suckless.org/code_snippets/python/battery_indicator.md @@ -12,70 +12,66 @@ 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 -` -### -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) + ### -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!!!' -` - + @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 @@ -9,21 +9,18 @@ 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 + ### -` -### -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' -` + @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'