|
| 1 | +update to new API in ruamel.yaml version 0.18 |
| 2 | +see https://github.com/debbiemarkslab/EVcouplings/pull/333 |
| 3 | +author: Alex Domingo (Vrije Universiteit Brussel) |
| 4 | +diff --git a/evcouplings/utils/config.py b/evcouplings/utils/config.py |
| 5 | +index fe8fb67..163f31f 100644 |
| 6 | +--- a/evcouplings/utils/config.py |
| 7 | ++++ b/evcouplings/utils/config.py |
| 8 | +@@ -10,7 +10,8 @@ Authors: |
| 9 | + Thomas A. Hopf |
| 10 | + """ |
| 11 | + |
| 12 | +-import ruamel.yaml as yaml |
| 13 | ++from ruamel.yaml import YAML |
| 14 | ++from ruamel.yaml.error import MarkedYAMLError |
| 15 | + |
| 16 | + |
| 17 | + class MissingParameterError(Exception): |
| 18 | +@@ -25,7 +26,7 @@ class InvalidParameterError(Exception): |
| 19 | + """ |
| 20 | + |
| 21 | + |
| 22 | +-def parse_config(config_str, preserve_order=False): |
| 23 | ++def parse_config(config_str): |
| 24 | + """ |
| 25 | + Parse a configuration string |
| 26 | + |
| 27 | +@@ -33,29 +34,25 @@ def parse_config(config_str, preserve_order=False): |
| 28 | + ---------- |
| 29 | + config_str : str |
| 30 | + Configuration to be parsed |
| 31 | +- preserve_order : bool, optional (default: True) |
| 32 | +- Preserve formatting of input configuration |
| 33 | +- string |
| 34 | + |
| 35 | + Returns |
| 36 | + ------- |
| 37 | + dict |
| 38 | + Configuration dictionary |
| 39 | + """ |
| 40 | ++ # uses round-trip loader by default, preserving order |
| 41 | ++ yaml = YAML(typ='safe', pure=True) |
| 42 | + try: |
| 43 | +- if preserve_order: |
| 44 | +- return yaml.load(config_str, Loader=yaml.RoundTripLoader) |
| 45 | +- else: |
| 46 | +- return yaml.safe_load(config_str) |
| 47 | +- except yaml.parser.ParserError as e: |
| 48 | ++ return yaml.load(config_str) |
| 49 | ++ except MarkedYAMLError as e: |
| 50 | + raise InvalidParameterError( |
| 51 | + "Could not parse input configuration. " |
| 52 | + "Formatting mistake in config file? " |
| 53 | +- "See ParserError above for details." |
| 54 | ++ "See MarkedYAMLError above for details." |
| 55 | + ) from e |
| 56 | + |
| 57 | + |
| 58 | +-def read_config_file(filename, preserve_order=False): |
| 59 | ++def read_config_file(filename): |
| 60 | + """ |
| 61 | + Read and parse a configuration file. |
| 62 | + |
| 63 | +@@ -70,7 +67,7 @@ def read_config_file(filename, preserve_order=False): |
| 64 | + Configuration dictionary |
| 65 | + """ |
| 66 | + with open(filename) as f: |
| 67 | +- return parse_config(f, preserve_order) |
| 68 | ++ return parse_config(f) |
| 69 | + |
| 70 | + |
| 71 | + def write_config_file(out_filename, config): |
| 72 | +@@ -84,14 +81,11 @@ def write_config_file(out_filename, config): |
| 73 | + config : dict |
| 74 | + Config data that will be written to file |
| 75 | + """ |
| 76 | +- if isinstance(config, yaml.comments.CommentedBase): |
| 77 | +- dumper = yaml.RoundTripDumper |
| 78 | +- else: |
| 79 | +- dumper = yaml.Dumper |
| 80 | +- |
| 81 | ++ yaml = YAML(typ='safe', pure=True) |
| 82 | ++ yaml.default_flow_style = False |
| 83 | + with open(out_filename, "w") as f: |
| 84 | + f.write( |
| 85 | +- yaml.dump(config, Dumper=dumper, default_flow_style=False) |
| 86 | ++ yaml.dump(config) |
| 87 | + ) |
| 88 | + |
| 89 | + |
0 commit comments