|
1 | 1 | import re |
2 | 2 | import ast |
3 | 3 | import json |
| 4 | +import yaml # Requires pyyaml |
4 | 5 |
|
5 | 6 |
|
6 | 7 | def _analyze_ast(contents): |
@@ -183,28 +184,33 @@ def _read_file(file): |
183 | 184 | def _parse_py_file(cap_file): |
184 | 185 | all_code = _read_file(cap_file) |
185 | 186 | capabilities = _analyze_ast(all_code) |
186 | | - |
187 | 187 | if not capabilities: |
188 | 188 | capabilities = _analyze_manual(all_code) |
189 | | - |
190 | 189 | return capabilities |
191 | 190 |
|
192 | 191 |
|
193 | 192 | def _parse_json_file(cap_file): |
194 | 193 | all_code = _read_file(cap_file) |
195 | | - |
196 | 194 | return json.loads(all_code) |
197 | 195 |
|
198 | 196 |
|
| 197 | +def _parse_yaml_file(cap_file): |
| 198 | + all_code = _read_file(cap_file) |
| 199 | + return yaml.safe_load(all_code) |
| 200 | + |
| 201 | + |
199 | 202 | def get_desired_capabilities(cap_file): |
200 | 203 | if cap_file.endswith(".py"): |
201 | 204 | capabilities = _parse_py_file(cap_file) |
202 | 205 | elif cap_file.endswith(".json"): |
203 | 206 | capabilities = _parse_json_file(cap_file) |
| 207 | + elif (cap_file.endswith(".yml") or cap_file.endswith(".yaml")): |
| 208 | + capabilities = _parse_yaml_file(cap_file) |
204 | 209 | else: |
205 | | - raise Exception("\n\n`%s` is not a Python or JSON file!\n" % cap_file) |
206 | | - |
| 210 | + raise Exception( |
| 211 | + '\n\n`%s` must end in ".py", ".json", ".yml", or ".yaml"!\n' |
| 212 | + % cap_file |
| 213 | + ) |
207 | 214 | if len(capabilities.keys()) == 0: |
208 | 215 | raise Exception("Unable to parse desired capabilities file!") |
209 | | - |
210 | 216 | return capabilities |
0 commit comments