From 457e53109c93d3cb917016c57d3ef83b27e3f028 Mon Sep 17 00:00:00 2001 From: Kevin Matz Date: Mon, 18 Nov 2019 22:23:51 -0500 Subject: [PATCH] delay loading config file --- bacon/OscListener.py | 4 ++-- bacon/hog4.py | 2 +- bacon/script.py | 54 ++++++++++++++++++++------------------------ bs.py | 6 ++++- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/bacon/OscListener.py b/bacon/OscListener.py index 127279f..df99b20 100644 --- a/bacon/OscListener.py +++ b/bacon/OscListener.py @@ -31,8 +31,8 @@ def beautify_lisp_string(in_string): class OscCommentMacroListener(CommentMacroListener): - def __init__(self, servers): - self.osc = HogNet(servers) + def __init__(self): + self.osc = None def exitWait(self, ctx: CommentMacroParser.WaitContext): logging.info("Waiting " + str(ctx.number().value) + " seconds.") diff --git a/bacon/hog4.py b/bacon/hog4.py index 30b1977..bc72663 100644 --- a/bacon/hog4.py +++ b/bacon/hog4.py @@ -3,7 +3,7 @@ from pythonosc import osc_message_builder from time import sleep -class HogNet(): +class HogNet: # button state constants buttonDOWN = 1 buttonUP = 0 diff --git a/bacon/script.py b/bacon/script.py index b6df350..235f91e 100644 --- a/bacon/script.py +++ b/bacon/script.py @@ -20,41 +20,35 @@ class SyntaxErrorListener(ErrorListener): raise SyntaxError("line "+str(line)+":"+str(column)+" "+msg) -# empty server dictionary -servers = {} +def loadConfig(file='server.cfg'): + # empty server dictionary + servers = {} -# open config file -config = configparser.ConfigParser(allow_no_value=True) -config.read('server.cfg') + # open config file + config = configparser.ConfigParser(allow_no_value=True) + config.read(file) -# 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 - - -# clear temp variables off the stack -try: - del ip, port, net - del server, config -except NameError as e: - print('failed to release memory', e) + # 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 # init reusable walker and listener walker = ParseTreeWalker() -listener = OscCommentMacroListener(servers) +listener = OscCommentMacroListener() # main handler for processing input @@ -78,4 +72,4 @@ def comment(text): walker.walk(listener, tree) except SyntaxError as e: logging.debug(e) # antlr internal listener prints the error -# # log it to the debug logging anyway +# # log it to the debug logging anyway diff --git a/bs.py b/bs.py index 6351a1d..02aebaa 100755 --- a/bs.py +++ b/bs.py @@ -6,13 +6,17 @@ import logging import sys -from bacon.script import comment +from bacon.script import comment, listener +from bacon.script import loadConfig as loadBsConfig +from bacon.hog4 import HogNet # setup logging logger = logging.getLogger() logger.setLevel(logging.DEBUG) +listener.osc = HogNet(loadBsConfig('server.cfg')) + # handle user input if run directly if __name__ == '__main__': if len(sys.argv) > 1: