]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/util/configutil.py
Refactor tahoe.cfg handling to configutil.
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / util / configutil.py
1
2 from ConfigParser import SafeConfigParser
3
4
5 def get_config(tahoe_cfg):
6     config = SafeConfigParser()
7     f = open(tahoe_cfg, "rb")
8     try:
9         # Skip any initial Byte Order Mark. Since this is an ordinary file, we
10         # don't need to handle incomplete reads, and can assume seekability.
11         if f.read(3) != '\xEF\xBB\xBF':
12             f.seek(0)
13         config.readfp(f)
14     finally:
15         f.close()
16     return config
17
18 def set_config(config, section, option, value):
19     if not config.has_section(section):
20         config.add_section(section)
21     config.set(section, option, value)
22     assert config.get(section, option) == value
23
24 def write_config(tahoe_cfg, config):
25     f = open(tahoe_cfg, "wb")
26     try:
27         config.write(f)
28     finally:
29         f.close()