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.
This commit is contained in:
James Muscat 2015-04-14 14:30:26 +01:00
parent 43ff4cda3a
commit 46292e3c30
3 changed files with 33 additions and 18 deletions

View File

@ -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')

View File

@ -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')

View File

@ -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)