1
0
Fork 0
baconscript/comment.py

88 lines
2.8 KiB
Python
Raw Normal View History

2018-10-17 20:56:59 -04:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""comment.py: Hog 4 comment macro interpreter and OSC bridge."""
__author__ = "Kevin Matz"
__copyright__ = "Copyright 2018, Company 235, LLC"
__credits__ = ["Kevin Matz"]
__license__ = "MIT"
2018-10-21 14:22:02 -04:00
__version__ = "3.9"
2018-10-17 20:56:59 -04:00
__maintainer__ = "Kevin Matz"
__email__ = "kevin@company235.com"
__status__ = "Prototype"
import antlr4
import configparser
2018-10-21 14:14:56 -04:00
import logging
2018-10-17 20:56:59 -04:00
import sys
from antlr4.error.ErrorListener import ErrorListener
2018-10-17 20:56:59 -04:00
from CommentMacroLexer import CommentMacroLexer
from CommentMacroParser import CommentMacroParser
2018-10-20 15:55:24 -04:00
from OscCommentMacroListener import OscCommentMacroListener
2018-10-17 20:56:59 -04:00
from pythonosc import udp_client
# 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)
2018-10-21 14:14:56 -04:00
# setup logging
2018-10-28 18:27:25 -04:00
logger = logging.getLogger(__name__)
2018-10-21 14:14:56 -04:00
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler() # create console handler and
ch.setLevel(logging.INFO) # set console log level to INFO
logger.addHandler(ch) # add console log handler
# TODO: refactor this to support multiple net#s
2018-10-17 20:56:59 -04:00
config = configparser.ConfigParser(allow_no_value=True)
2018-10-21 14:14:56 -04:00
config.read('server.cfg') # open config file
2018-10-21 22:15:40 -04:00
servers = {} # init an empty dictionary
2018-10-21 14:14:56 -04:00
server = config['hog4'] # section of config file
net = server.getint("net", 1) # default to net #1
logger.info("Adding Hog device at net# " + str(net))
# osc clients are added to the dictionary with the net # as the key
2018-10-21 22:15:40 -04:00
servers[net] = udp_client.SimpleUDPClient(server.get("ip", "10.0.0.1"),
server.getint("port", 6600))
2018-10-17 20:56:59 -04:00
2018-10-29 01:40:56 -04:00
oscMacro = OscCommentMacroListener(servers)
walker = antlr4.ParseTreeWalker()
2018-10-17 20:56:59 -04:00
def comment(text):
input_stream = antlr4.InputStream(text)
lexer = CommentMacroLexer(input_stream)
stream = antlr4.CommonTokenStream(lexer)
parser = CommentMacroParser(stream)
parser._listeners.append(SyntaxErrorListener())
try:
tree = parser.prog()
walker.walk(oscMacro, tree)
except SyntaxError as e:
logger.debug(e) # antlr internal listener prints the error
# # log it to the debug logger anyway
2018-10-17 20:56:59 -04:00
if __name__ == '__main__':
if len(sys.argv) > 1:
2018-10-21 14:14:56 -04:00
logger.debug("found macro at argv[1]")
2018-10-17 20:56:59 -04:00
comment(sys.argv[1])
else:
import readline # for input history and line editing
2018-10-17 20:56:59 -04:00
while True:
try:
text = input("comment# ")
2018-10-28 17:12:12 -04:00
except (KeyboardInterrupt, EOFError):
2018-10-25 15:02:04 -04:00
text = 'exit'
print(text)
2018-10-17 20:56:59 -04:00
if text == 'exit':
2018-10-25 18:16:48 -04:00
break
2018-10-21 14:14:56 -04:00
else:
comment(text)