Fix jack_control for flake8 compatibility

dbus/jack_control:
Remove unused imports for os and traceback.print_exc.
Fix indentation, whitespace and line length issues across the entire
file.
Simplify printing in `print_help()` by calling `print()` only once.
This commit is contained in:
David Runge 2022-04-13 22:29:23 +02:00 committed by Filipe Coelho
parent b5ca229866
commit 5aa5861af0
1 changed files with 78 additions and 55 deletions

View File

@ -1,6 +1,10 @@
#!/usr/bin/python3 #!/usr/bin/python3
from __future__ import print_function from __future__ import print_function
import dbus
import sys
name_base = 'org.jackaudio' name_base = 'org.jackaudio'
control_interface_name = name_base + '.JackControl' control_interface_name = name_base + '.JackControl'
configure_interface_name = name_base + '.Configure' configure_interface_name = name_base + '.Configure'
@ -8,18 +12,14 @@ service_name = name_base + '.service'
control_iface = None control_iface = None
configure_iface = None configure_iface = None
import sys
import os
from traceback import print_exc
import dbus
def bool_convert(str_value): def bool_convert(str_value):
if str_value == "0" or str_value.lower() in ["false","off","no","(null)"]: if str_value == "0" or str_value.lower() in ["false", "off", "no", "(null)"]:
return False return False
return bool(str_value) return bool(str_value)
def dbus_type_to_python_type(dbus_value): def dbus_type_to_python_type(dbus_value):
t = type(dbus_value) t = type(dbus_value)
if t == dbus.Boolean: if t == dbus.Boolean:
@ -28,12 +28,13 @@ def dbus_type_to_python_type(dbus_value):
return int(dbus_value) return int(dbus_value)
return dbus_value return dbus_value
def python_type_to_jackdbus_type(value, type_char): def python_type_to_jackdbus_type(value, type_char):
type_char = str(type_char) type_char = str(type_char)
if type_char == "b": if type_char == "b":
return bool_convert(value); return bool_convert(value)
elif type_char == "y": elif type_char == "y":
return dbus.Byte(ord(value)); return dbus.Byte(ord(value))
elif type_char == "i": elif type_char == "i":
return dbus.Int32(value) return dbus.Int32(value)
elif type_char == "u": elif type_char == "u":
@ -41,6 +42,7 @@ def python_type_to_jackdbus_type(value, type_char):
return value return value
def dbus_typesig_to_type_string(type_char): def dbus_typesig_to_type_string(type_char):
type_char = str(type_char) type_char = str(type_char)
if type_char == 'i': if type_char == 'i':
@ -57,18 +59,19 @@ def dbus_typesig_to_type_string(type_char):
print('err: unknown dbus typesig "%s"' % type_char) print('err: unknown dbus typesig "%s"' % type_char)
return None # throw exception here? return None # throw exception here?
def get_parameters(iface, path): def get_parameters(iface, path):
params = iface.GetParametersInfo(path) params = iface.GetParametersInfo(path)
#print params # print params
for param in params: for param in params:
typestr = dbus_typesig_to_type_string(param[0]) typestr = dbus_typesig_to_type_string(param[0])
name = param[1] name = param[1]
#print name # print name
descr = param[2] descr = param[2]
#print descr # print descr
isset, default, value = iface.GetParameterValue(path + [name]) isset, default, value = iface.GetParameterValue(path + [name])
#print typestr # print typestr
if bool(isset): if bool(isset):
isset = "set" isset = "set"
else: else:
@ -76,38 +79,44 @@ def get_parameters(iface, path):
value = dbus_type_to_python_type(value) value = dbus_type_to_python_type(value)
default = dbus_type_to_python_type(default) default = dbus_type_to_python_type(default)
print("%20s: %s (%s:%s:%s:%s)" %(name, descr, typestr, isset, default, value)) print("%20s: %s (%s:%s:%s:%s)" % (name, descr, typestr, isset, default, value))
def print_help(): def print_help():
print("Usage: %s [command] [command] ..." % os.path.basename(sys.argv[0])) help_output = (
print("Commands:") "Usage: jack_control [command] [command] ...\n"
print(" shell - execute commands from stdin until End Of File (Ctrl+D in terminal)") "Commands:\n"
print(" exit - exit jack dbus service (stops jack server if currently running)") " shell - execute commands from stdin until End Of File (Ctrl+D in terminal)\n"
print(" help - print this help text") " exit - exit jack dbus service (stops jack server if currently running)\n"
print(" status - check whether jack server is started, return value is 0 if running and 1 otherwise") " help - print this help text\n"
print(" start - start jack server if not currently started") " status - check whether jack server is started, "
print(" stop - stop jack server if currently started") "return value is 0 if running and 1 otherwise\n"
print(" sm - switch master to currently selected driver") " start - start jack server if not currently started\n"
print(" dl - get list of available drivers") " stop - stop jack server if currently started\n"
print(" dg - get currently selected driver") " sm - switch master to currently selected driver\n"
print(" ds <driver> - select driver") " dl - get list of available drivers\n"
print(" dp - get parameters of currently selected driver") " dg - get currently selected driver\n"
print(" dpd <param> - get long description for driver parameter") " ds <driver> - select driver\n"
print(" dps <param> <value> - set driver parameter") " dp - get parameters of currently selected driver\n"
print(" dpr <param> - reset driver parameter to its default value") " dpd <param> - get long description for driver parameter\n"
print(" asd <driver> - add slave driver") " dps <param> <value> - set driver parameter\n"
print(" rsd <driver> - remove slave driver") " dpr <param> - reset driver parameter to its default value\n"
print(" il - get list of available internals") " asd <driver> - add slave driver\n"
print(" ip <name> - get parameters of given internal") " rsd <driver> - remove slave driver\n"
print(" ipd <name> <param> - get long description for internal parameter") " il - get list of available internals\n"
print(" ips <name> <param> <value> - set internal parameter") " ip <name> - get parameters of given internal\n"
print(" ipr <name> <param> - reset internal parameter to its default value") " ipd <name> <param> - get long description for internal parameter\n"
print(" iload <name> - load internal") " ips <name> <param> <value> - set internal parameter\n"
print(" iunload <name> - unload internal") " ipr <name> <param> - reset internal parameter to its default value\n"
print(" ep - get engine parameters") " iload <name> - load internal\n"
print(" epd <param> - get long description for engine parameter") " iunload <name> - unload internal\n"
print(" eps <param> <value> - set engine parameter") " ep - get engine parameters\n"
print(" epr <param> - reset engine parameter to its default value") " epd <param> - get long description for engine parameter\n"
" eps <param> <value> - set engine parameter\n"
" epr <param> - reset engine parameter to its default value\n"
)
print(help_output)
def maybe_print_param_constraint(iface, param): def maybe_print_param_constraint(iface, param):
is_range, is_strict, is_fake, values = iface.GetParameterConstraint(param) is_range, is_strict, is_fake, values = iface.GetParameterConstraint(param)
@ -124,13 +133,14 @@ def maybe_print_param_constraint(iface, param):
max_len = 0 max_len = 0
for value in values: for value in values:
if len(str(value[0])) > max_len: if len(str(value[0])) > max_len:
max_len = len(str(value[0])) max_len = len(str(value[0]))
for value in values: for value in values:
print(("%*s'%s' - %s" % (1 + max_len - len(str(value[0])), "", str(value[0]), str(value[1])))) print(("%*s'%s' - %s" % (1 + max_len - len(str(value[0])), "", str(value[0]), str(value[1]))))
def parse_argv(argv): def parse_argv(argv):
global control_iface, configure_iface global control_iface, configure_iface
# check arguments # check arguments
index = 0 index = 0
while index < len(argv): while index < len(argv):
@ -162,7 +172,9 @@ def parse_argv(argv):
print("Automatically activated") print("Automatically activated")
elif arg == 'dl': elif arg == 'dl':
print("--- drivers list") print("--- drivers list")
is_range, is_strict, is_fake_values, values = configure_iface.GetParameterConstraint(['engine', 'driver']) is_range, is_strict, is_fake_values, values = configure_iface.GetParameterConstraint(
['engine', 'driver']
)
for value in values: for value in values:
print(value[1]) print(value[1])
elif arg == 'dg': elif arg == 'dg':
@ -270,7 +282,10 @@ def parse_argv(argv):
get_parameters(configure_iface, ['internals', internal_name]) get_parameters(configure_iface, ['internals', internal_name])
elif arg == 'ipd': elif arg == 'ipd':
if index + 1 >= len(argv): if index + 1 >= len(argv):
return (1, "get internal parameter long description command requires internal and parameter name arguments") return (
1,
"get internal parameter long description command requires internal and parameter name arguments"
)
name = argv[index] name = argv[index]
index += 1 index += 1
@ -293,8 +308,13 @@ def parse_argv(argv):
print("--- internal param set \"%s\" -> \"%s\"" % (param, value)) print("--- internal param set \"%s\" -> \"%s\"" % (param, value))
type_char, name, short_descr, long_descr = configure_iface.GetParameterInfo(['internals', internal_name, param]) type_char, name, short_descr, long_descr = configure_iface.GetParameterInfo(
configure_iface.SetParameterValue(['internals', internal_name, param], python_type_to_jackdbus_type(value, type_char)) ['internals', internal_name, param]
)
configure_iface.SetParameterValue(
['internals', internal_name, param],
python_type_to_jackdbus_type(value, type_char),
)
elif arg == 'ipr': elif arg == 'ipr':
if index + 1 >= len(argv): if index + 1 >= len(argv):
return (1, "reset internal parameter command requires internal and parameter name arguments") return (1, "reset internal parameter command requires internal and parameter name arguments")
@ -315,7 +335,7 @@ def parse_argv(argv):
name = argv[index] name = argv[index]
index += 1 index += 1
result = control_iface.LoadInternal(name) control_iface.LoadInternal(name)
elif arg == 'iunload': elif arg == 'iunload':
print("--- unload internal") print("--- unload internal")
@ -324,7 +344,7 @@ def parse_argv(argv):
name = argv[index] name = argv[index]
index += 1 index += 1
result = control_iface.UnloadInternal(name) control_iface.UnloadInternal(name)
elif arg == 'asd': elif arg == 'asd':
print("--- add slave driver") print("--- add slave driver")
@ -333,7 +353,7 @@ def parse_argv(argv):
name = argv[index] name = argv[index]
index += 1 index += 1
result = control_iface.AddSlaveDriver(name) control_iface.AddSlaveDriver(name)
elif arg == 'rsd': elif arg == 'rsd':
print("--- remove slave driver") print("--- remove slave driver")
@ -342,16 +362,17 @@ def parse_argv(argv):
name = argv[index] name = argv[index]
index += 1 index += 1
result = control_iface.RemoveSlaveDriver(name) control_iface.RemoveSlaveDriver(name)
elif arg == 'help': elif arg == 'help':
print_help() print_help()
else: else:
return (0, "Unknown command '%s'" % arg) return (0, "Unknown command '%s'" % arg)
except dbus.DBusException as e: except dbus.DBusException as e:
return (1, "DBus exception: %s" % str(e)) return (1, "DBus exception: %s" % str(e))
return (0, "") return (0, "")
def shell(): def shell():
from click.parser import split_arg_string from click.parser import split_arg_string
from sys import stdin from sys import stdin
@ -360,11 +381,12 @@ def shell():
cmd = stdin.readline() cmd = stdin.readline()
if (cmd == ''): if (cmd == ''):
break break
s,t = parse_argv(split_arg_string(cmd)) s, t = parse_argv(split_arg_string(cmd))
print("%i: %s" % (s, t), flush=True) print("%i: %s" % (s, t), flush=True)
except: except Exception:
break break
def main(): def main():
global control_iface, configure_iface global control_iface, configure_iface
@ -387,5 +409,6 @@ def main():
print(t) print(t)
return s return s
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())