From 409580462ebc36c336b32504e35c99353874d771 Mon Sep 17 00:00:00 2001 From: Caner Candan <candan@info.univ-angers.fr> Date: Tue, 14 Jan 2014 19:58:45 +0100 Subject: [PATCH] + added config support --- .gitignore | 2 ++ config.json-dist | 5 +++++ ucoin.py | 25 ++++++++++++++++--------- ucoin/__init__.py | 16 +++++++++------- 4 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 config.json-dist diff --git a/.gitignore b/.gitignore index ded60678..f81a36b3 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ nosetests.xml .mr.developer.cfg .project .pydevproject + +config.json diff --git a/config.json-dist b/config.json-dist new file mode 100644 index 00000000..6c2fc58b --- /dev/null +++ b/config.json-dist @@ -0,0 +1,5 @@ +{ + "host": "localhost", + "port": 8081, + "auth": false +} diff --git a/ucoin.py b/ucoin.py index ca58844c..78a68d31 100755 --- a/ucoin.py +++ b/ucoin.py @@ -20,9 +20,7 @@ from parser import Parser from pprint import pprint import ucoin - -URL = 'http://mycurrency.candan.fr:8081' -AUTH = False +import json def action_peering(): pprint(ucoin.ucg.Peering().get()) @@ -38,15 +36,24 @@ def action_transactions(): if __name__ == '__main__': parser = Parser(description='ucoin client.', verbose='error') - parser.add_argument('--peering', '-p', help='get peering', - action='store_const', dest='action', const=action_peering) + parser.add_argument('--peering', '-p', help='get peering', action='store_const', dest='action', const=action_peering) + parser.add_argument('--amendments', '-a', help='get amendments list', action='store_const', dest='action', const=action_amendments) + parser.add_argument('--transactions', '-t', help='get transactions list', action='store_const', dest='action', const=action_transactions) - parser.add_argument('--amendments', '-a', help='get amendments list', - action='store_const', dest='action', const=action_amendments) + parser.add_argument('--user', '-u', help='set the pgp user') + parser.add_argument('--host', '-H', help='set the server host', default='localhost') + parser.add_argument('--port', '-P', help='set the server port', type=int, default=8081) - parser.add_argument('--transactions', '-t', help='get transactions list', - action='store_const', dest='action', const=action_transactions) + parser.add_argument('--config', '-c', help='set a config file', default='config.json') args = parser() + ucoin.settings.update(args.__dict__) + + try: + with open(args.config) as f: + ucoin.settings.update(json.load(f)) + except FileNotFoundError: + pass + if args.action: args.action() diff --git a/ucoin/__init__.py b/ucoin/__init__.py index b49f3ca2..3f8e92c7 100644 --- a/ucoin/__init__.py +++ b/ucoin/__init__.py @@ -24,26 +24,28 @@ __nonsense__ = 'uCoin' import requests -URL = 'http://mycurrency.candan.fr:8081' -AUTH = False +settings = { + 'host': 'localhost', + 'port': 8081, + 'auth': False, +} class API: """APIRequest is a class used as an interface. The intermediate derivated classes are the modules and the leaf classes are the API requests.""" - def __init__(self, module, url=URL, auth=AUTH): + def __init__(self, module): """ Asks a module in order to create the url used then by derivated classes. Arguments: - `module`: module name - - `url`: url defining the host and port of the server - - `auth`: enables to get multipart/signed messages. """ - self.url = '%s/%s' % (url, module) + self.url = 'http://%s:%d/%s' % (settings['host'], settings['port'], module) self.headers = {} - if auth: self.headers['Accept'] = 'multipart/signed' + if settings['auth']: + self.headers['Accept'] = 'multipart/signed' def reverse_url(self, path): """ -- GitLab