Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
Add config file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
juga0 committed Jan 31, 2018
1 parent 943e003 commit a82db14
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions bwscanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time

import click
import ConfigParser
from twisted.internet import reactor

from bwscanner.attacher import connect_to_tor
Expand All @@ -12,6 +13,7 @@


BWSCAN_VERSION = '0.0.1'
APP_NAME = 'bwscanner'


class ScanInstance(object):
Expand All @@ -21,17 +23,33 @@ class ScanInstance(object):
def __init__(self, data_dir):
self.data_dir = data_dir
self.measurement_dir = os.path.join(data_dir, 'measurements')
self.tor_dir = os.path.join(data_dir, 'tordata')

def __repr__(self):
return '<BWScan %r>' % 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)',
Expand All @@ -53,6 +71,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)
Expand Down

0 comments on commit a82db14

Please sign in to comment.