From 46292e3c30dd5958e53c3dbb72c8b7c831b64a6c Mon Sep 17 00:00:00 2001 From: James Muscat Date: Tue, 14 Apr 2015 14:30:26 +0100 Subject: [PATCH] Don't corrupt the commandline unless we're asked to. The hacky approach caused problems with anything else trying to use argparse (e.g. nosetests). This approach only tries to parse the commandline if we need to e.g. within Relay/Gateway's main() function. --- src/eddn/Gateway.py | 25 ++++++++++++++++--------- src/eddn/Relay.py | 3 ++- src/eddn/conf/Settings.py | 23 +++++++++++++++-------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/eddn/Gateway.py b/src/eddn/Gateway.py index 1676924..643db0c 100644 --- a/src/eddn/Gateway.py +++ b/src/eddn/Gateway.py @@ -2,6 +2,7 @@ Contains the necessary ZeroMQ socket and a helper function to publish market data to the Announcer daemons. """ +import argparse import gevent import hashlib import logging @@ -10,7 +11,7 @@ import urlparse import zlib import zmq.green as zmq from datetime import datetime -from eddn.conf.Settings import Settings +from eddn.conf.Settings import Settings, loadConfig from eddn.Validator import Validator, ValidationSeverity from gevent import monkey @@ -22,23 +23,26 @@ logger = logging.getLogger(__name__) # This socket is used to push market data out to the Announcers over ZeroMQ. context = zmq.Context() sender = context.socket(zmq.PUB) -# Get the list of transports to bind from settings. This allows us to PUB -# messages to multiple announcers over a variety of socket types -# (UNIX sockets and/or TCP sockets). -for binding in Settings.GATEWAY_SENDER_BINDINGS: - sender.bind(binding) validator = Validator() -for schemaRef, schemaFile in Settings.GATEWAY_JSON_SCHEMAS.iteritems(): - validator.addSchemaResource(schemaRef, schemaFile) - # This import must be done post-monkey-patching! from eddn.StatsCollector import StatsCollector statsCollector = StatsCollector() statsCollector.start() +def configure(): + # Get the list of transports to bind from settings. This allows us to PUB + # messages to multiple announcers over a variety of socket types + # (UNIX sockets and/or TCP sockets). + for binding in Settings.GATEWAY_SENDER_BINDINGS: + sender.bind(binding) + + for schemaRef, schemaFile in Settings.GATEWAY_JSON_SCHEMAS.iteritems(): + validator.addSchemaResource(schemaRef, schemaFile) + + def push_message(string_message): """ Spawned as a greenlet to push messages (strings) through ZeroMQ. @@ -193,6 +197,9 @@ class MalformedUploadError(Exception): def main(): + loadConfig() + + configure() run(host='0.0.0.0', port=8080, server='gevent') diff --git a/src/eddn/Relay.py b/src/eddn/Relay.py index c0c8b07..6ef59b6 100644 --- a/src/eddn/Relay.py +++ b/src/eddn/Relay.py @@ -13,7 +13,7 @@ import gevent import simplejson import zmq.green as zmq from bottle import get, run as bottle_run -from eddn.conf.Settings import Settings +from eddn.conf.Settings import Settings, loadConfig from gevent import monkey monkey.patch_all() @@ -76,6 +76,7 @@ class Relay(Thread): def main(): + loadConfig() r = Relay() r.start() bottle_run(host='0.0.0.0', port=9090, server='gevent') diff --git a/src/eddn/conf/Settings.py b/src/eddn/conf/Settings.py index e345b29..6393bf1 100644 --- a/src/eddn/conf/Settings.py +++ b/src/eddn/conf/Settings.py @@ -4,8 +4,8 @@ Created on 15 Nov 2014 @author: james ''' -import simplejson import argparse +import simplejson from eddn._version import __version__ as version @@ -32,8 +32,8 @@ class _Settings(object): GATEWAY_IP_KEY_SALT = None GATEWAY_JSON_SCHEMAS = { - "http://schemas.elite-markets.net/eddn/commodity/1": "../schemas/commodity-v0.1.json", - "http://schemas.elite-markets.net/eddn/commodity/1/test": "../schemas/commodity-v0.1.json" + "http://schemas.elite-markets.net/eddn/commodity/1": "schemas/commodity-v0.1.json", + "http://schemas.elite-markets.net/eddn/commodity/1/test": "schemas/commodity-v0.1.json" } def loadFrom(self, fileName): @@ -47,9 +47,16 @@ class _Settings(object): Settings = _Settings() -parser = argparse.ArgumentParser() -parser.add_argument("-c", "--config", nargs="?", default=None) -args = parser.parse_args() -if args.config: - Settings.loadFrom(args.config) +def loadConfig(): + ''' + Loads in a settings file specified on the commandline if one has been specified. + A convenience method if you don't need other things specified as commandline + options. Otherwise, point the filename to Settings.loadFrom(). + ''' + parser = argparse.ArgumentParser() + parser.add_argument("-c", "--config", nargs="?", default=None) + args = parser.parse_args() + + if args.config: + Settings.loadFrom(args.config)