1
0
Fork 0
baconscript/bacon/script.py

82 lines
2.7 KiB
Python
Raw Normal View History

2020-11-23 12:31:44 -05:00
"""comment.py: Hog 4 comment macro interpreter and OSC bridge."""
2018-10-17 20:56:59 -04:00
import configparser
2018-10-21 14:14:56 -04:00
import logging
2020-11-25 09:38:24 -05:00
from typing import Dict
2018-10-17 20:56:59 -04:00
2019-11-18 13:21:36 -05:00
from antlr4 import CommonTokenStream, InputStream, ParseTreeWalker
from antlr4.error.ErrorListener import ErrorListener
2018-10-17 20:56:59 -04:00
from pythonosc import udp_client
2019-11-18 13:21:36 -05:00
from .commentmacro.CommentMacroLexer import CommentMacroLexer
from .commentmacro.CommentMacroParser import CommentMacroParser
from .OscListener import OscCommentMacroListener
2022-05-12 19:16:44 -04:00
__all__ = [
"SyntaxErrorListener",
"load_config",
"comment",
]
2018-10-17 20:56:59 -04:00
class SyntaxErrorListener(ErrorListener):
2020-11-23 12:31:44 -05:00
"""An error listener that raises SyntaxError exceptions."""
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
2020-11-25 09:38:24 -05:00
raise SyntaxError("line {}:{} {}".format(line, column, msg))
2020-11-25 09:38:24 -05:00
def load_config(file: str = 'server.cfg') -> Dict[int, object]:
2020-11-23 12:31:44 -05:00
"""Load an ini style configuration file."""
2019-11-18 22:23:51 -05:00
# empty server dictionary
2020-11-25 09:38:24 -05:00
servers: Dict[int, object] = {}
2019-11-17 12:36:11 -05:00
2019-11-18 22:23:51 -05:00
# open config file
config = configparser.ConfigParser(allow_no_value=True)
config.read(file)
2019-11-17 12:36:11 -05:00
2019-11-18 22:23:51 -05:00
# set up each hog device
for name in config.get('network', 'hogs').split(','):
try:
# move to config section
server = config[name.strip().strip('\"')]
# read settings
2020-11-25 09:38:24 -05:00
addr = server.get("ip", fallback="127.0.0.1")
port = server.getint("port", fallback=7001)
net = server.getint("net", fallback=1)
2019-11-18 22:23:51 -05:00
# osc clients are added to the dictionary with the net # as the key
2020-11-23 12:31:44 -05:00
logging.info("Adding Hog device at net# %d", str(net))
servers[net] = udp_client.SimpleUDPClient(addr, port)
except KeyError as exception:
print('Error configuring net#', net, exception)
2019-11-18 22:23:51 -05:00
continue
return servers
2019-11-17 13:04:54 -05:00
2019-11-17 13:05:13 -05:00
# init reusable walker and listener
2020-11-25 09:38:24 -05:00
WALKER: ParseTreeWalker = ParseTreeWalker()
LISTENER: OscCommentMacroListener = OscCommentMacroListener()
2018-10-29 01:40:56 -04:00
2018-10-17 20:56:59 -04:00
2020-11-25 09:38:24 -05:00
def comment(text: str) -> None:
2020-11-23 12:31:44 -05:00
"""Process comment macro input."""
2019-11-17 13:05:13 -05:00
# force upper case
2019-11-17 11:48:55 -05:00
text = text.upper()
2019-11-17 13:05:13 -05:00
# generate text stream
input_stream = InputStream(text)
2019-11-17 13:05:13 -05:00
# lex the text stream
2018-10-17 20:56:59 -04:00
lexer = CommentMacroLexer(input_stream)
2019-11-17 13:05:13 -05:00
# get token stream from lexer
stream = CommonTokenStream(lexer)
2019-11-17 13:05:13 -05:00
# parse the token steam
2018-10-17 20:56:59 -04:00
parser = CommentMacroParser(stream)
2019-11-17 13:05:13 -05:00
# attach an error handler to the parser
parser._listeners.append(SyntaxErrorListener())
try:
2019-11-17 13:05:13 -05:00
# get tree from parser
tree = parser.prog()
2019-11-17 13:05:13 -05:00
# walk the tree
2020-11-23 12:31:44 -05:00
WALKER.walk(LISTENER, tree)
except SyntaxError as exception:
logging.debug(exception) # antlr internal listener prints the error
# # log it to the debug logging anyway