stable/utils.py

83 lines
2.2 KiB
Python
Raw Normal View History

2017-05-12 17:11:56 +00:00
import json
import logging
from jsonschema import validate
2017-05-14 12:14:16 +00:00
from wrapt import synchronized
2017-05-12 17:11:56 +00:00
logger = logging.getLogger(__name__)
2017-05-14 12:14:16 +00:00
_cur_conf = None
2017-05-12 17:11:56 +00:00
# Required json-schema for user specified config
_conf_schema = {
'type': 'object',
'properties': {
2017-09-08 05:49:50 +00:00
'max_open_trades': {'type': 'integer', 'minimum': 1},
'stake_currency': {'type': 'string'},
'stake_amount': {'type': 'number', 'minimum': 0.0005},
'dry_run': {'type': 'boolean'},
'minimal_roi': {
'type': 'object',
'patternProperties': {
'^[0-9.]+$': {'type': 'number'}
},
'minProperties': 1
},
'poloniex': {'$ref': '#/definitions/exchange'},
'bittrex': {'$ref': '#/definitions/exchange'},
'telegram': {
'type': 'object',
'properties': {
'enabled': {'type': 'boolean'},
'token': {'type': 'string'},
'chat_id': {'type': 'string'},
},
'required': ['enabled', 'token', 'chat_id']
}
},
'definitions': {
'exchange': {
'type': 'object',
'properties': {
'enabled': {'type': 'boolean'},
'key': {'type': 'string'},
'secret': {'type': 'string'},
'pair_whitelist': {
'type': 'array',
'items': {'type': 'string'},
'uniqueItems': True
}
},
'required': ['enabled', 'key', 'secret', 'pair_whitelist']
}
},
'anyOf': [
{'required': ['poloniex']},
{'required': ['bittrex']}
],
'required': [
'max_open_trades',
'stake_currency',
'stake_amount',
'dry_run',
'minimal_roi',
'telegram'
]
}
2017-05-14 12:14:16 +00:00
@synchronized
2017-09-01 19:11:46 +00:00
def get_conf(filename: str='config.json') -> dict:
2017-05-12 17:11:56 +00:00
"""
Loads the config into memory validates it
and returns the singleton instance
2017-05-12 17:11:56 +00:00
:return: dict
"""
2017-05-14 12:14:16 +00:00
global _cur_conf
if not _cur_conf:
2017-08-27 13:40:27 +00:00
with open(filename) as file:
_cur_conf = json.load(file)
validate(_cur_conf, _conf_schema)
2017-05-14 12:14:16 +00:00
return _cur_conf