-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigargumentparser.py
More file actions
72 lines (61 loc) · 2.38 KB
/
configargumentparser.py
File metadata and controls
72 lines (61 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# ----------------------------------------------------
# Electromagnetic Mining Array (EMMA)
# Copyright 2018, Pieter Robyns
# ----------------------------------------------------
import argparse
import configparser
import logging
import os
logger = logging.getLogger(__name__)
def _config_string_to_type(string):
if string.lower() in ('true', 'false'):
return bool(string)
elif len(string) < 1:
return None
elif string[0] in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'):
try:
if '.' in string:
return float(string)
else:
return int(string)
except ValueError:
return None
class ConfigArgumentParser(argparse.ArgumentParser):
"""
Wrapper class for ArgumentParser that uses an additional config file for overriding default arguments. This results
in the following override priorities: default arguments < config arguments < CLI arguments.
"""
def __init__(self, *args, config_path='settings.conf', config_section='DEFAULT', **kwargs):
self.config_path = config_path
self.config_section = config_section
self.emma_conf = {}
super().__init__(*args, **kwargs)
if os.path.exists(self.config_path):
settings = configparser.ConfigParser()
settings.read(self.config_path)
emma_conf_tuples = settings.items(self.config_section)
for k, v in emma_conf_tuples:
self.emma_conf[k] = _config_string_to_type(v)
else:
logger.warning("%s does not exist; ignoring" % self.config_path)
def _remove_prefix_chars(self, string):
"""
Does conversion from '--foo-bar' to 'foo_bar'.
:param string:
:return:
"""
result = string.lstrip(self.prefix_chars)
result = result.replace('-', '_')
return result
def add_argument(self, *args, **kwargs):
"""
Modified add_argument that overrides the 'default' parameter with the value in the config file if present.
:param args:
:param kwargs:
:return:
"""
for arg in args:
arg_key = self._remove_prefix_chars(arg)
if arg_key in self.emma_conf:
kwargs['default'] = self.emma_conf[arg_key] # Override default with value from config file
super().add_argument(*args, **kwargs)