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

@@ -26,6 +26,9 @@ class bar_mode(Enum):
mode = bar_mode.normal mode = bar_mode.normal
# Helper functions # Helper functions
def hostname():
return os.uname()[1]
ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]') ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')
def strip_ansi_unicode(s): def strip_ansi_unicode(s):
# ANSI escape sequences are for colors in terminal and similar # ANSI escape sequences are for colors in terminal and similar

View File

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