1
0
Fork 0
baconscript/bacon/script.py

76 lines
2.4 KiB
Python
Raw Normal View History

2019-11-18 13:21:36 -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
2018-10-17 20:56:59 -04:00
import sys
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
2018-10-17 20:56:59 -04:00
# define an error listener that raises SyntaxError exceptions
class SyntaxErrorListener(ErrorListener):
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
raise SyntaxError("line "+str(line)+":"+str(column)+" "+msg)
2019-11-18 22:23:51 -05:00
def loadConfig(file='server.cfg'):
# empty server dictionary
servers = {}
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
ip = server.get("ip", "10.0.0.100")
port = server.getint("port", 7001)
net = server.getint("net", 1)
# osc clients are added to the dictionary with the net # as the key
logging.info("Adding Hog device at net# " + str(net))
servers[net] = udp_client.SimpleUDPClient(ip, port)
except KeyError as e:
print('Error configuring net#', net, e)
continue
return servers
2019-11-17 13:04:54 -05:00
2019-11-17 13:05:13 -05:00
# init reusable walker and listener
walker = ParseTreeWalker()
2019-11-18 22:23:51 -05:00
listener = OscCommentMacroListener()
2018-10-29 01:40:56 -04:00
2018-10-17 20:56:59 -04:00
2019-11-17 13:05:13 -05:00
# main handler for processing input
2018-10-17 20:56:59 -04:00
def comment(text):
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
walker.walk(listener, tree)
except SyntaxError as e:
2019-11-18 20:07:48 -05:00
logging.debug(e) # antlr internal listener prints the error
2019-11-18 22:23:51 -05:00
# # log it to the debug logging anyway