-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.py
71 lines (53 loc) · 2.06 KB
/
config.py
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
"""This module wraps a local config file.
It is motivated by the need for host-specific paths to the Stanford Parser,
but may see other uses as well. A usage example:
>>> from config import get_config
>>> config = get_config('My section')
>>> stored_value = config['stored_value_key']
Config file: Section 'My section' does not have key 'stored_value_key'
Please enter the wanted value of key 'stored_value_key'
> 12345
Key 'stored_value_key' set to value '12345' in config file. Proceeding.
>>> stored_value
'12345'
"""
import ConfigParser
CONFIG_FILE = 'local_config'
class ConfigSection(object):
def __init__(self, section):
config = ConfigParser.ConfigParser()
config.optionxform = str
config.read(CONFIG_FILE)
if not config.has_section(section):
config.add_section(section)
self.section = section
self.config = config
def __getitem__(self, key):
if not self.config.has_option(self.section, key):
print ("Config file: Section '{}' does not have key '{}'").format(self.section, key)
question = "Please enter the wanted value of key '{}'\n> ".format(key)
value = raw_input(question)
self.config.set(self.section, key, value)
write(self.config)
print "Key '{}' set to value '{}' in config file. Proceeding.".format(key, value)
return self.config.get(self.section, key)
def get_config(section):
"""Returns a dict of configuration keys, values
If section is not defined in the config file, it takes
the user through creating that section.
Section can be a module name, or whatever makes sense when you create it.
"""
return ConfigSection(section)
def write(config):
with open(CONFIG_FILE, 'w') as f:
config.write(f)
def ask_and_insert_item(config, section, key):
"""Ask the user for a value to put in config's 'section'
under 'key':
Paraphrased mental model:
config[
section[
key:value (answer to 'question')
]
]
"""