1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
13 # Ensure strings with ' like fzf query strings get double-quoted:
14 def represent_str(self
, data
):
16 return self
.represent_scalar("tag:yaml.org,2002:str", data
, style
='"')
17 return self
.represent_scalar("tag:yaml.org,2002:str", data
)
20 yaml
.SafeDumper
.add_representer(str, represent_str
)
24 def __init__(self
, path
):
30 if not self
._presets
and os
.path
.isfile(self
.path
):
31 with
open(self
.path
) as fh
:
32 self
._presets
= yaml
.safe_load(fh
) or {}
36 def __contains__(self
, name
):
37 return name
in self
.presets
39 def __getitem__(self
, name
):
40 return self
.presets
[name
]
43 return len(self
.presets
)
48 return yaml
.safe_dump(self
.presets
, default_flow_style
=False)
52 print("no presets found")
57 if "EDITOR" not in os
.environ
:
59 "error: must set the $EDITOR environment variable to use --edit-presets"
63 subprocess
.call(shlex
.split(os
.environ
["EDITOR"]) + [self
.path
])
65 def save(self
, name
, **data
):
66 self
.presets
[name
] = data
68 with
open(self
.path
, "w") as fh
:
73 def __init__(self
, *paths
):
74 """Helper class for dealing with multiple preset files."""
75 self
.handlers
= [PresetHandler(p
) for p
in paths
]
77 def __contains__(self
, name
):
78 return any(name
in handler
for handler
in self
.handlers
)
80 def __getitem__(self
, name
):
81 for handler
in self
.handlers
:
87 return sum(len(h
) for h
in self
.handlers
)
91 k
: v
for handler
in self
.handlers
for k
, v
in handler
.presets
.items()
93 return yaml
.safe_dump(all_presets
, default_flow_style
=False)
97 print("no presets found")
100 for handler
in self
.handlers
:
104 [""] + val
.splitlines() + [""]
105 ) # indent all lines by 2 spaces
106 print("Presets from {}:".format(handler
.path
))