From ccb98490d4e8c3d948e94b800deaac549db9b89a Mon Sep 17 00:00:00 2001 From: juga0 Date: Wed, 31 Jan 2018 14:43:32 +0000 Subject: [PATCH] Add config file parsing --- bwscanner/scanner.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/bwscanner/scanner.py b/bwscanner/scanner.py index a720fa6..500b1ee 100755 --- a/bwscanner/scanner.py +++ b/bwscanner/scanner.py @@ -3,6 +3,7 @@ import time import click +import ConfigParser from twisted.internet import reactor from bwscanner.attacher import connect_to_tor @@ -12,6 +13,7 @@ BWSCAN_VERSION = '0.0.1' +APP_NAME = 'bwscanner' class ScanInstance(object): @@ -26,12 +28,27 @@ def __repr__(self): return '' % self.data_dir +def read_config(data_dir): + cfg = os.path.join(data_dir, 'config.ini') + print('reading config %s' % cfg) + parser = ConfigParser.RawConfigParser() + parser.read([cfg]) + # FIXME: handle section names + section = 'default' + return dict(parser.items(section)) + + +CONTEXT_SETTINGS = dict( + default_map=read_config(os.environ.get("BWSCANNER_DATADIR", + click.get_app_dir(APP_NAME))) +) + pass_scan = click.make_pass_decorator(ScanInstance) -@click.group() +@click.group(context_settings=CONTEXT_SETTINGS) @click.option('--data-dir', type=click.Path(), - default=os.environ.get("BWSCANNER_DATADIR", click.get_app_dir('bwscanner')), + default=os.environ.get("BWSCANNER_DATADIR", click.get_app_dir(APP_NAME)), help='Directory where bwscan should stores its measurements and ' 'other data.') @click.option('-l', '--loglevel', help='The logging level the scanner will use (default: info)', @@ -53,6 +70,7 @@ def cli(ctx, data_dir, loglevel, logfile, launch_tor, circuit_build_timeout): # Create the data directory if it doesn't exist data_dir = os.path.abspath(data_dir) ctx.obj = ScanInstance(data_dir) + print(ctx.obj) if not os.path.isdir(ctx.obj.measurement_dir): os.makedirs(ctx.obj.measurement_dir)