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/.
14 def __init__(self
, path
):
20 if not self
._presets
and os
.path
.isfile(self
.path
):
21 with
open(self
.path
) as fh
:
22 self
._presets
= yaml
.safe_load(fh
) or {}
26 def __contains__(self
, name
):
27 return name
in self
.presets
29 def __getitem__(self
, name
):
30 return self
.presets
[name
]
33 return len(self
.presets
)
38 return yaml
.safe_dump(self
.presets
, default_flow_style
=False)
42 print("no presets found")
47 if "EDITOR" not in os
.environ
:
49 "error: must set the $EDITOR environment variable to use --edit-presets"
53 subprocess
.call(shlex
.split(os
.environ
["EDITOR"]) + [self
.path
])
55 def save(self
, name
, **data
):
56 self
.presets
[name
] = data
58 with
open(self
.path
, "w") as fh
:
63 def __init__(self
, *paths
):
64 """Helper class for dealing with multiple preset files."""
65 self
.handlers
= [PresetHandler(p
) for p
in paths
]
67 def __contains__(self
, name
):
68 return any(name
in handler
for handler
in self
.handlers
)
70 def __getitem__(self
, name
):
71 for handler
in self
.handlers
:
77 return sum(len(h
) for h
in self
.handlers
)
81 k
: v
for handler
in self
.handlers
for k
, v
in handler
.presets
.items()
83 return yaml
.safe_dump(all_presets
, default_flow_style
=False)
87 print("no presets found")
90 for handler
in self
.handlers
:
94 [""] + val
.splitlines() + [""]
95 ) # indent all lines by 2 spaces
96 print("Presets from {}:".format(handler
.path
))