delay loading config file
This commit is contained in:
parent
9532b1c003
commit
457e53109c
|
@ -31,8 +31,8 @@ def beautify_lisp_string(in_string):
|
||||||
|
|
||||||
|
|
||||||
class OscCommentMacroListener(CommentMacroListener):
|
class OscCommentMacroListener(CommentMacroListener):
|
||||||
def __init__(self, servers):
|
def __init__(self):
|
||||||
self.osc = HogNet(servers)
|
self.osc = None
|
||||||
|
|
||||||
def exitWait(self, ctx: CommentMacroParser.WaitContext):
|
def exitWait(self, ctx: CommentMacroParser.WaitContext):
|
||||||
logging.info("Waiting " + str(ctx.number().value) + " seconds.")
|
logging.info("Waiting " + str(ctx.number().value) + " seconds.")
|
||||||
|
|
|
@ -3,7 +3,7 @@ from pythonosc import osc_message_builder
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
class HogNet():
|
class HogNet:
|
||||||
# button state constants
|
# button state constants
|
||||||
buttonDOWN = 1
|
buttonDOWN = 1
|
||||||
buttonUP = 0
|
buttonUP = 0
|
||||||
|
|
|
@ -20,41 +20,35 @@ class SyntaxErrorListener(ErrorListener):
|
||||||
raise SyntaxError("line "+str(line)+":"+str(column)+" "+msg)
|
raise SyntaxError("line "+str(line)+":"+str(column)+" "+msg)
|
||||||
|
|
||||||
|
|
||||||
# empty server dictionary
|
def loadConfig(file='server.cfg'):
|
||||||
servers = {}
|
# empty server dictionary
|
||||||
|
servers = {}
|
||||||
|
|
||||||
# open config file
|
# open config file
|
||||||
config = configparser.ConfigParser(allow_no_value=True)
|
config = configparser.ConfigParser(allow_no_value=True)
|
||||||
config.read('server.cfg')
|
config.read(file)
|
||||||
|
|
||||||
# set up each hog device
|
# set up each hog device
|
||||||
for name in config.get('network', 'hogs').split(','):
|
for name in config.get('network', 'hogs').split(','):
|
||||||
try:
|
try:
|
||||||
# move to config section
|
# move to config section
|
||||||
server = config[name.strip().strip('\"')]
|
server = config[name.strip().strip('\"')]
|
||||||
# read settings
|
# read settings
|
||||||
ip = server.get("ip", "10.0.0.100")
|
ip = server.get("ip", "10.0.0.100")
|
||||||
port = server.getint("port", 7001)
|
port = server.getint("port", 7001)
|
||||||
net = server.getint("net", 1)
|
net = server.getint("net", 1)
|
||||||
# osc clients are added to the dictionary with the net # as the key
|
# osc clients are added to the dictionary with the net # as the key
|
||||||
logging.info("Adding Hog device at net# " + str(net))
|
logging.info("Adding Hog device at net# " + str(net))
|
||||||
servers[net] = udp_client.SimpleUDPClient(ip, port)
|
servers[net] = udp_client.SimpleUDPClient(ip, port)
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
print('Error configuring net#', net, e)
|
print('Error configuring net#', net, e)
|
||||||
continue
|
continue
|
||||||
|
return servers
|
||||||
|
|
||||||
# clear temp variables off the stack
|
|
||||||
try:
|
|
||||||
del ip, port, net
|
|
||||||
del server, config
|
|
||||||
except NameError as e:
|
|
||||||
print('failed to release memory', e)
|
|
||||||
|
|
||||||
|
|
||||||
# init reusable walker and listener
|
# init reusable walker and listener
|
||||||
walker = ParseTreeWalker()
|
walker = ParseTreeWalker()
|
||||||
listener = OscCommentMacroListener(servers)
|
listener = OscCommentMacroListener()
|
||||||
|
|
||||||
|
|
||||||
# main handler for processing input
|
# main handler for processing input
|
||||||
|
@ -78,4 +72,4 @@ def comment(text):
|
||||||
walker.walk(listener, tree)
|
walker.walk(listener, tree)
|
||||||
except SyntaxError as e:
|
except SyntaxError as e:
|
||||||
logging.debug(e) # antlr internal listener prints the error
|
logging.debug(e) # antlr internal listener prints the error
|
||||||
# # log it to the debug logging anyway
|
# # log it to the debug logging anyway
|
||||||
|
|
6
bs.py
6
bs.py
|
@ -6,13 +6,17 @@
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import sys
|
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
|
# setup logging
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
|
listener.osc = HogNet(loadBsConfig('server.cfg'))
|
||||||
|
|
||||||
# handle user input if run directly
|
# handle user input if run directly
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
|
|
Loading…
Reference in New Issue