|
21 | 21 |
|
22 | 22 | logger = create_logger() |
23 | 23 |
|
| 24 | +import yaml |
| 25 | + |
24 | 26 | from .errors import Error |
25 | 27 | from .fs import get_keys_dir, make_path_safe, slashify |
26 | | -from .argparsing import Action, ArgumentError, ArgumentTypeError |
| 28 | +from .argparsing import Action, ArgumentError, ArgumentTypeError, register_type |
27 | 29 | from .msgpack import Timestamp |
28 | 30 | from .time import OutputTimestamp, format_time, safe_timestamp |
29 | 31 | from .. import __version__ as borg_version |
@@ -236,10 +238,22 @@ def compressor(self): |
236 | 238 | elif self.name == "obfuscate": |
237 | 239 | return get_compressor(self.name, level=self.level, compressor=self.inner.compressor) |
238 | 240 |
|
| 241 | + def __str__(self): |
| 242 | + if self.name in ("none", "lz4"): |
| 243 | + return f"{self.name}" |
| 244 | + elif self.name in ("zlib", "lzma", "zstd", "zlib_legacy"): |
| 245 | + return f"{self.name},{self.level}" |
| 246 | + elif self.name == "auto": |
| 247 | + return f"auto,{self.inner}" |
| 248 | + elif self.name == "obfuscate": |
| 249 | + return f"obfuscate,{self.level},{self.inner}" |
| 250 | + else: |
| 251 | + raise ValueError(f"unsupported compression type: {self.name}") |
| 252 | + |
239 | 253 |
|
240 | 254 | def ChunkerParams(s): |
241 | | - if isinstance(s, tuple): |
242 | | - return s |
| 255 | + if isinstance(s, (list, tuple)): |
| 256 | + return tuple(s) |
243 | 257 | params = s.strip().split(",") |
244 | 258 | count = len(params) |
245 | 259 | if count == 0: |
@@ -714,6 +728,18 @@ def validator(text): |
714 | 728 | return validator |
715 | 729 |
|
716 | 730 |
|
| 731 | +# Register types with jsonargparse so they can be represented in config files |
| 732 | +# (e.g. for --print_config). Two things are needed: |
| 733 | +# 1. A YAML representer so yaml.safe_dump can serialize Location objects to strings. |
| 734 | +# 2. A jsonargparse register_type so it knows how to deserialize strings back to Location. |
| 735 | + |
| 736 | +yaml.SafeDumper.add_representer(Location, lambda dumper, loc: dumper.represent_str(loc.raw or "")) |
| 737 | +register_type(Location, serializer=lambda loc: loc.raw or "") |
| 738 | + |
| 739 | +yaml.SafeDumper.add_representer(CompressionSpec, lambda dumper, cs: dumper.represent_str(str(cs))) |
| 740 | +register_type(CompressionSpec) |
| 741 | + |
| 742 | + |
717 | 743 | def relative_time_marker_validator(text: str): |
718 | 744 | time_marker_regex = r"^\d+[ymwdHMS]$" |
719 | 745 | match = re.compile(time_marker_regex).search(text) |
|
0 commit comments