add get values to dsl example

This commit is contained in:
onyx-and-iris 2022-06-18 23:49:15 +01:00
parent 316a5bded5
commit 56d26d6bde

View File

@ -17,42 +17,40 @@ from pyparsing import (
class Parser: class Parser:
def __init__(self, vm): def __init__(self, vm):
self.vm = vm self.vm = vm
self.kls = Word(alphas) self.kls = Group(OneOrMore(Word(alphanums)))
self.token = Suppress("->") self.token = Suppress("->")
self.index = Word(nums) self.param = Word(alphanums)
self.action = Word(alphanums) self.value = Combine(
Optional("-") + Word(nums) + Optional(".") + Optional(Word(nums))
) | Group(OneOrMore(Word(alphanums)))
def parse(self, cmds): def parse(self, cmds):
event = ( event = (
self.kls self.kls
+ self.token + self.token
+ self.index + self.param
+ self.token
+ self.action
+ Optional(
Combine(
Optional("-") + Word(nums) + Optional(".") + Optional(Word(nums))
)
)
+ Optional(self.action)
+ Optional(self.token) + Optional(self.token)
+ Optional(Group(OneOrMore(Word(alphanums)))) + Optional(self.value)
) )
res = list()
for cmd in cmds: for cmd in cmds:
kls, index, param, val = event.parseString(cmd) if len(event.parseString(cmd)) == 2:
kls = "".join(kls) kls, param = event.parseString(cmd)
index = int(*index) target = getattr(self.vm, kls[0])[int(kls[-1])]
target = getattr(self.vm, kls)[index] res.append(getattr(target, param))
if val in ["off", "on"]: elif len(event.parseString(cmd)) == 3:
setattr(target, param, bool(["off", "on"].index(val))) kls, param, val = event.parseString(cmd)
if param in ["gain", "comp", "gate", "limit", "audibility"]: target = getattr(self.vm, kls[0])[int(kls[-1])]
setattr(target, param, float(val)) if "".join(val) in ["off", "on"]:
if param in ["label"]: setattr(target, param, bool(["off", "on"].index("".join(val))))
val = " ".join(val) elif param in ["gain", "comp", "gate", "limit", "audibility"]:
setattr(target, param, val) setattr(target, param, float("".join(val)))
elif param in ["label"]:
setattr(target, param, " ".join(val))
time.sleep(0.05) time.sleep(0.05)
return res
def main(cmds=None): def main(cmds=None):
@ -61,34 +59,43 @@ def main(cmds=None):
with voicemeeterlib.api(kind_id) as vm: with voicemeeterlib.api(kind_id) as vm:
parser = Parser(vm) parser = Parser(vm)
if cmds: if cmds:
parser.parse(cmds) res = parser.parse(cmds)
if res:
print(res)
else: else:
try: try:
while True: while True:
cmd = input("please enter command (Return to exit)\n") cmd = input("please enter command (Return to exit)\n")
if not cmd: if not cmd:
break break
parser.parse((cmd,)) res = parser.parse((cmd,))
if res:
print(res)
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
SystemExit(e) SystemExit(e)
if __name__ == "__main__": if __name__ == "__main__":
cmds = ( cmds = (
"strip -> 0 -> mute on", "strip 0 -> mute -> on",
"bus -> 0 -> mute on", "strip 0 -> mute",
"strip -> 0 -> mute off", "bus 0 -> mute -> on",
"bus -> 0 -> mute on", "strip 0 -> mute -> off",
"strip -> 3 -> solo on", "bus 0 -> mute -> on",
"strip -> 3 -> solo off", "strip 3 -> solo -> on",
"strip -> 1 -> A1 on", "strip 3 -> solo -> off",
"strip -> 1 -> A1 off", "strip 1 -> A1 -> on",
"bus -> 3 -> eq on", "strip 1 -> A1",
"bus -> 3 -> eq off", "strip 1 -> A1 -> off",
"strip -> 4 -> gain 1.2", "strip 1 -> A1",
"strip -> 0 -> gain -8.2", "bus 3 -> eq -> on",
"strip -> 1 -> label -> rode podmic", "bus 3 -> eq -> off",
"strip -> 2 -> limit -28", "strip 4 -> gain -> 1.2",
"strip 0 -> gain -> -8.2",
"strip 0 -> gain",
"strip 1 -> label -> rode podmic",
"strip 2 -> limit -> -28",
"strip 2 -> limit",
) )
# pass cmds to parse cmds, otherwise simply run main() to test stdin parsing # pass cmds to parse cmds, otherwise simply run main() to test stdin parsing