lemonbar: Find and launch all modules at runtime

This commit is contained in:
kuben
2020-01-08 23:41:54 +01:00
committed by Jakub Fojt
parent c63af798fe
commit 827870bc01
2 changed files with 66 additions and 55 deletions

View File

@@ -2,6 +2,7 @@ import threading
import subprocess
import contextlib
import time, os, signal, getpass
import sys, inspect, re
import i3_lemonbar_config as config
import i3_lemonbar_common as common
@@ -85,6 +86,7 @@ def format_load(data, module, alert):
return (d_v, u_v)
class DateTimeModule(LemonModule):
def __init__(self):
super().__init__()
@@ -157,11 +159,11 @@ class DateTimeModule(LemonModule):
self.dt_thread.secs_mode = not self.dt_thread.secs_mode
self.dt_thread.wake()
class ConkyFastModule(LemonModule):
conky_config_str = """
conky.config = {
def conky_config(update_interval=5):
return """
conky.config = {{
background=false,
update_interval=1,
update_interval={},
total_run_times=0,
override_utf8_locale=true,
short_units=true,
@@ -170,10 +172,12 @@ conky.config = {
out_to_x=false,
if_up_strictness='address',
format_human_readable=true,
}
"""
if os.uname()[1] == 'kubaDesktop':
conky_config = conky_config_str + """
}}
""".format(update_interval)
class ConkyFastModule(LemonModule):
if common.hostname() == 'kubaDesktop':
conky_config = conky_config(update_interval=1) + """
conky.text = [[
${exec ~/.i3/lemonbar/get_vol.sh} \
down down\
@@ -184,7 +188,7 @@ ${else}down down${endif}\
]]
"""
else:
conky_config = conky_config_str + """
conky_config = conky_config(update_interval=2) + """
conky.text = [[
${exec ~/.i3/lemonbar/get_vol.sh} \
${if_up wlp3s0}${downspeedf wlp3s0} ${upspeedf wlp3s0}\
@@ -244,22 +248,8 @@ ${else}down down${endif}\
common.kill_on_unfocus.append(p.pid)
class ConkySlowModule(LemonModule):
conky_config_str = """
conky.config = {
background=false,
update_interval=5,
total_run_times=0,
override_utf8_locale=true,
short_units=true,
uppercase=false,
out_to_console=true,
out_to_x=false,
if_up_strictness='address',
format_human_readable=true,
}
"""
if os.uname()[1] == 'kubaDesktop':
conky_config = conky_config_str + """
if common.hostname() == 'kubaDesktop':
conky_config = conky_config() + """
conky.text = [[
${cpu} \
${mem} \
@@ -271,7 +261,7 @@ ${exec /home/kuba/.i3/scripts/lang.sh show}
]]
"""
else:
conky_config = conky_config_str + """
conky_config = conky_config() + """
conky.text = [[
${cpu} \
${mem} \
@@ -546,6 +536,9 @@ class i3Module(LemonModule):
break
class ScreenModule(LemonModule):
"""
@ignore host kubaArch-Desktop
"""
# Start, stop and send commands to screen instance
# Start detached, in UTF-8 mode. Log to fifo
@@ -648,39 +641,54 @@ class PowerOptionsModule(LemonModule):
, ' Abort (Esc) | System (l) lock, (e) logout, (s) suspend, (h) hibernate'
, ', (r) reboot, (Shift+s) shutdown'])
def do_action(keyword):
global m_datetime, m_conky_fast, m_conky_slow, m_i3ws
def filter_ignored_modules(module_classes):
re_ignore = re.compile(r'\s*@ignore host (\S+)')
hostname = common.hostname()
m_i3ws.do_action(keyword)
m_datetime.do_action(keyword)
m_conky_slow.do_action(keyword)
m_conky_fast.do_action(keyword)
m_screen.do_action(keyword)
m_power.do_action(keyword)
filtered = module_classes.copy()
for module in module_classes:
doc = module.__doc__
# Search docstring for exceptions based on hostname
if not doc:
continue
for line in doc.split('\n'):
m = re_ignore.match(line)
if m:
ignored = m.group(1)
if ignored == hostname:
common.logger.debug('Ignoring module {} on host {}'.format(module.__name__, hostname))
filtered.remove(module)
continue
return filtered
def get_active_modules():
clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)
modules = []
for name, cls in clsmembers:
if LemonModule in inspect.getmro(cls) and cls is not LemonModule:
modules.append(cls)
modules = filter_ignored_modules(modules)
return modules
def do_action(keyword):
global all_modules
for module in all_modules:
module.do_action(keyword)
def start_all():
global m_datetime, m_conky_fast, m_conky_slow, m_i3ws, m_screen, m_power
global all_modules
m_i3ws = i3Module()
m_datetime = DateTimeModule()
m_conky_slow = ConkySlowModule()
m_conky_fast = ConkyFastModule()
m_screen = ScreenModule()
m_power = PowerOptionsModule()
all_modules = []
for cls in get_active_modules():
all_modules.append(cls())
m_i3ws.start()
m_datetime.start()
m_conky_slow.start()
m_conky_fast.start()
m_screen.start()
m_power.start()
for module in all_modules:
module.start()
def stop_all():
global m_datetime, m_conky_fast, m_conky_slow, m_i3ws, m_screen, m_power
m_i3ws.stop()
m_datetime.stop()
m_conky_slow.stop()
m_conky_fast.stop()
m_screen.stop()
m_power.stop()
global all_modules
for module in all_modules:
module.stop()