|
4 | 4 | import logging |
5 | 5 | import os |
6 | 6 | import os.path |
7 | | -import re |
8 | 7 |
|
9 | | -import yaml |
| 8 | +from satosa.exception import SATOSAConfigurationError |
| 9 | +from satosa.yaml import load as yaml_load |
| 10 | +from satosa.yaml import YAMLError |
10 | 11 |
|
11 | | -from .exception import SATOSAConfigurationError |
12 | 12 |
|
13 | 13 | logger = logging.getLogger(__name__) |
14 | 14 |
|
@@ -145,69 +145,11 @@ def _load_yaml(self, config_file): |
145 | 145 | :param config_file: config to load. Can be file path or yaml string |
146 | 146 | :return: Loaded config |
147 | 147 | """ |
148 | | - # Tag to indicate environment variable: !ENV |
149 | | - tag_env = '!ENV' |
150 | | - |
151 | | - # Pattern for environment variable: ${word} |
152 | | - pattern_env = re.compile('.*?\${(\w+)}.*?') |
153 | | - |
154 | | - yaml.SafeLoader.add_implicit_resolver(tag_env, pattern_env, None) |
155 | | - |
156 | | - def constructor_env_variables(loader, node): |
157 | | - """ |
158 | | - Extracts the environment variable from the node's value. |
159 | | - :param yaml.Loader loader: the yaml loader |
160 | | - :param node: the current node in the yaml |
161 | | - :return: value of the environment variable |
162 | | - """ |
163 | | - value = loader.construct_scalar(node) |
164 | | - match = pattern_env.findall(value) |
165 | | - if match: |
166 | | - new_value = value |
167 | | - for m in match: |
168 | | - new_value = new_value.replace('${' + m + '}', |
169 | | - os.environ.get(m, m)) |
170 | | - return new_value |
171 | | - return value |
172 | | - |
173 | | - yaml.SafeLoader.add_constructor(tag_env, constructor_env_variables) |
174 | | - |
175 | | - # Tag to indicate file pointed to by environment variable: !ENVFILE |
176 | | - tag_env_file = '!ENVFILE' |
177 | | - |
178 | | - # Pattern for environment variable: $(word) |
179 | | - pattern_env_file = re.compile('.*?\$\((\w+)\).*?') |
180 | | - |
181 | | - yaml.SafeLoader.add_implicit_resolver(tag_env_file, |
182 | | - pattern_env_file, None) |
183 | | - |
184 | | - def constructor_envfile_variables(loader, node): |
185 | | - """ |
186 | | - Extracts the environment variable from the node's value. |
187 | | - :param yaml.Loader loader: the yaml loader |
188 | | - :param node: the current node in the yaml |
189 | | - :return: value read from file pointed to by environment variable |
190 | | - """ |
191 | | - value = loader.construct_scalar(node) |
192 | | - match = pattern_env_file.findall(value) |
193 | | - if match: |
194 | | - new_value = value |
195 | | - for m in match: |
196 | | - path = os.environ.get(m, '') |
197 | | - if os.path.exists(path): |
198 | | - with open(path, 'r') as f: |
199 | | - new_value = new_value.replace('$(' + m + ')', |
200 | | - f.read().strip()) |
201 | | - return new_value |
202 | | - return value |
203 | | - |
204 | | - yaml.SafeLoader.add_constructor(tag_env_file, |
205 | | - constructor_envfile_variables) |
206 | 148 |
|
207 | 149 | try: |
208 | 150 | with open(os.path.abspath(config_file)) as f: |
209 | | - return yaml.safe_load(f.read()) |
210 | | - except yaml.YAMLError as exc: |
| 151 | + return yaml_load(f.read()) |
| 152 | + except YAMLError as exc: |
211 | 153 | logger.error("Could not parse config as YAML: {}".format(exc)) |
212 | 154 | if hasattr(exc, 'problem_mark'): |
213 | 155 | mark = exc.problem_mark |
|
0 commit comments