forked from jamesoff/simplemonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvconfig.py
40 lines (32 loc) · 1.34 KB
/
envconfig.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
import os
import re
from ConfigParser import ConfigParser
class EnvironmentAwareConfigParser(ConfigParser):
"""A subclass of ConfigParser which allows %env:VAR% interpolation via the
get method."""
r = re.compile('%env:([a-zA-Z0-9_]+)%')
def read(self, filenames):
result = ConfigParser.read(self, filenames)
for section in self.sections():
original_section = section
matches = self.r.search(section)
while matches:
env_key = matches.group(1)
if env_key in os.environ:
section = section.replace(matches.group(0), os.environ[env_key])
matches = self.r.search(section)
if section != original_section:
self.add_section(section)
for (option, value) in self.items(original_section):
self.set(section, option, value)
self.remove_section(original_section)
return result
def get(self, *args, **kwargs):
result = ConfigParser.get(self, *args, **kwargs)
matches = self.r.search(result)
while matches:
env_key = matches.group(1)
if env_key in os.environ:
result = result.replace(matches.group(0), os.environ[env_key])
matches = self.r.search(result)
return result