unify vol/bri set/get; unify dependency check

This commit is contained in:
Piotr Miller
2021-03-03 15:59:42 +01:00
parent 0501274f9d
commit 0e8acbe166
5 changed files with 58 additions and 69 deletions

View File

@@ -17,10 +17,13 @@ controls_list = []
config_dir = ""
app_dirs = []
dependencies = {
"pyalsa": False,
"netifaces": False,
"amixer": False
commands = {
"pamixer": False,
"wlr-randr": False,
"light": False,
"playerctl": False,
"systemctl": False,
"netifaces": False
}
icons_path = "" # "icons_light", "icons_dark" or "" (GTK icons)

View File

@@ -1539,7 +1539,7 @@ class EditorWrapper(object):
self.ctrl_comp_volume.set_active("volume" in settings["components"])
self.ctrl_comp_switcher = builder.get_object("output-switcher")
self.ctrl_comp_switcher.set_sensitive(is_command("pactl"))
self.ctrl_comp_switcher.set_sensitive(is_command("pamixer"))
self.ctrl_comp_switcher.set_active(settings["output-switcher"])
self.ctrl_comp_net = builder.get_object("ctrl-comp-net")

View File

@@ -167,7 +167,8 @@ def instantiate_content(panel, container, content_list, icons_path=""):
def main():
common.config_dir = get_config_dir()
common.defaults = get_defaults()
check_commands()
print("Dependencies check:", common.commands)
parser = argparse.ArgumentParser()
parser.add_argument("-c",
@@ -195,12 +196,6 @@ def main():
args = parser.parse_args()
try:
from pyalsa import alsamixer
common.dependencies["pyalsa"] = True
except:
print("pylsa module not found, will try amixer")
global restart_cmd
restart_cmd = "nwg-panel -c {} -s {}".format(args.config, args.style)
@@ -219,8 +214,6 @@ def main():
common.app_dirs = get_app_dirs()
common.dependencies["amixer"] = is_command("amixer")
config_file = os.path.join(common.config_dir, args.config)
copy_files(os.path.join(dir_name, "icons_light"), os.path.join(common.config_dir, "icons_light"))

View File

@@ -13,14 +13,12 @@ from gi.repository import Gtk, Gdk, GLib, GdkPixbuf, GtkLayerShell
from nwg_panel.tools import check_key, get_brightness, set_brightness, get_volume, set_volume, get_battery, \
get_interface, update_image, bt_service_enabled, bt_on, bt_name, is_command, list_sinks, toggle_mute
from nwg_panel.common import dependencies
from nwg_panel.common import commands
try:
"""try:
import netifaces
dependencies["netifaces"] = True
except ModuleNotFoundError:
pass
pass"""
class Controls(Gtk.EventBox):
@@ -102,7 +100,7 @@ class Controls(Gtk.EventBox):
box.pack_start(self.vol_label, False, False, 0)
if "net" in self.settings["components"] and self.settings["net-interface"]:
if dependencies["netifaces"]:
if commands["netifaces"]:
box.pack_start(self.net_image, False, False, 4)
if self.net_label:
box.pack_start(self.net_label, False, False, 0)
@@ -141,7 +139,7 @@ class Controls(Gtk.EventBox):
except Exception as e:
print(e)
if "volume" in self.settings["components"] and dependencies["pyalsa"] or dependencies["amixer"]:
if "volume" in self.settings["components"] and commands["pamixer"]:
try:
value, muted = get_volume()
GLib.idle_add(self.update_volume, value, muted)
@@ -275,7 +273,7 @@ class PopupWindow(Gtk.Window):
check_key(settings, "output-switcher", False)
self.sinks = []
if is_command("pamixer") and settings["output-switcher"]:
if commands["pamixer"] and settings["output-switcher"]:
self.sinks = list_sinks()
self.connect("show", self.refresh_sinks)
@@ -336,7 +334,7 @@ class PopupWindow(Gtk.Window):
inner_hbox.pack_start(self.bri_scale, True, True, 5)
add_sep = True
if "volume" in settings["components"] and dependencies["pyalsa"] or dependencies["amixer"]:
if "volume" in settings["components"] and commands["pamixer"]:
inner_hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
v_box.pack_start(inner_hbox, False, False, 6)
@@ -362,7 +360,7 @@ class PopupWindow(Gtk.Window):
self.vol_scale.connect("value-changed", self.set_vol)
inner_hbox.pack_start(self.vol_scale, True, True, 5)
if is_command("pamixer") and settings["output-switcher"]:
if commands["pamixer"] and settings["output-switcher"]:
pactl_eb = Gtk.EventBox()
image = Gtk.Image()
pactl_eb.add(image)
@@ -374,7 +372,7 @@ class PopupWindow(Gtk.Window):
add_sep = True
if is_command("pamixer") and settings["output-switcher"]:
if commands["pamixer"] and settings["output-switcher"]:
self.sink_box = SinkBox()
pactl_eb.connect('button-press-event', self.sink_box.switch_visibility)
v_box.pack_start(self.sink_box, False, False, 0)
@@ -383,7 +381,7 @@ class PopupWindow(Gtk.Window):
sep = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
v_box.pack_start(sep, True, True, 10)
if "net" in settings["components"] and dependencies["netifaces"] and settings["net-interface"]:
if "net" in settings["components"] and commands["netifaces"] and settings["net-interface"]:
event_box = Gtk.EventBox()
if "net" in settings["commands"] and settings["commands"]["net"]:
event_box.connect("enter_notify_event", self.on_enter_notify_event)
@@ -587,7 +585,7 @@ class PopupWindow(Gtk.Window):
def refresh(self):
if self.get_visible():
if "net" in self.settings["components"] and dependencies["netifaces"]:
if "net" in self.settings["components"] and commands["netifaces"]:
ip_addr = get_interface(self.settings["net-interface"])
icon_name = "network-wired-symbolic" if ip_addr else "network-wired-disconnected-symbolic"

View File

@@ -133,28 +133,6 @@ def get_config_dir():
return config_dir
def get_defaults():
file = os.path.join(local_dir(), "defaults")
if os.path.isfile(file):
defaults = load_json(file)
missing = False
if "master" not in defaults:
defaults["master"] = "Master"
missing = True
if missing:
save_json(defaults, file)
return defaults
else:
defaults = {
"master": "Master"
}
save_json(defaults, file)
return defaults
def copy_files(src_dir, dst_dir, restore=False):
src_files = os.listdir(src_dir)
for file in src_files:
@@ -245,7 +223,7 @@ def list_outputs(sway=False, silent=False):
elif os.getenv('WAYLAND_DISPLAY') is not None:
if not silent:
print("Running on Wayland, but not sway")
if is_command("wlr-randr"):
if nwg_panel.common.commands["wlr-randr"]:
lines = subprocess.check_output("wlr-randr", shell=True).decode("utf-8").strip().splitlines()
if lines:
name, w, h, x, y = None, None, None, None, None
@@ -309,10 +287,21 @@ def is_command(cmd):
return False
def check_commands():
for key in nwg_panel.common.commands:
nwg_panel.common.commands[key] = is_command(key)
try:
import netifaces
nwg_panel.common.commands["netifaces"] = True
except ModuleNotFoundError:
pass
def get_volume():
vol = 0
muted = False
if is_command("pamixer"):
if nwg_panel.common.commands["pamixer"]:
try:
vol = int(cmd2string("pamixer --get-volume"))
except Exception as e:
@@ -325,14 +314,14 @@ def get_volume():
# the command above returns the 'disabled` status w/ CalledProcessError, exit status 1
pass
else:
eprint("Required 'pamixer' command not found")
eprint("Couldn't get volume, 'pamixer' not found")
return vol, muted
def list_sinks():
sinks = []
if is_command("pamixer"):
if nwg_panel.common.commands["pamixer"]:
try:
output = cmd2string("pamixer --list-sinks")
if output:
@@ -345,46 +334,52 @@ def list_sinks():
except Exception as e:
eprint(e)
else:
eprint("Required 'pamixer' command not found")
eprint("Couldn't list sinks, 'pamixer' not found")
return sinks
def toggle_mute(*args):
if is_command("pamixer"):
if nwg_panel.common.commands["pamixer"]:
vol, muted = get_volume()
if muted:
subprocess.call("pamixer -u".split())
else:
subprocess.call("pamixer -m".split())
else:
eprint("Required 'pamixer' command not found")
eprint("Couldn't toggle mute, 'pamixer' not found")
def set_volume(slider):
percent = int(slider.get_value())
if is_command("pamixer"):
if nwg_panel.common.commands["pamixer"]:
subprocess.call("pamixer --set-volume {}".format(percent).split())
else:
eprint("Required 'pamixer' command not found")
eprint("Couldn't set volume, 'pamixer' not found")
def get_brightness():
brightness = 0
try:
output = cmd2string("light -G")
brightness = int(round(float(output), 0))
except:
pass
if nwg_panel.common.commands["light"]:
try:
output = cmd2string("light -G")
brightness = int(round(float(output), 0))
except:
pass
else:
eprint("Couldn't get brightness, 'light' not found")
return brightness
def set_brightness(slider):
value = slider.get_value()
res = subprocess.call("{} {}".format("light -S", value), shell=True)
if res != 0:
print("Couldn't set brightness, is 'light' installed?")
value = int(slider.get_value())
if value == 0:
value = 1
if nwg_panel.common.commands["light"]:
subprocess.call("{} {}".format("light -S", value).split())
else:
eprint("Required 'light' command not found")
def get_battery():
@@ -437,7 +432,7 @@ def get_interface(name):
def player_status():
status = "install playerctl"
if is_command("playerctl"):
if nwg_panel.common.commands["playerctl"]:
try:
status = cmd2string("playerctl status 2>&1")
except:
@@ -547,7 +542,7 @@ def bt_name():
def bt_service_enabled():
result, enabled, active = False, False, False
if is_command("systemctl"):
if nwg_panel.common.commands["systemctl"]:
try:
enabled = subprocess.check_output("systemctl is-enabled bluetooth.service", shell=True).decode(
"utf-8").strip() == "enabled"