2017-08-17 15:14:07 +00:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
|
|
|
import os
|
2017-10-27 18:58:34 +00:00
|
|
|
import subprocess
|
2017-08-17 15:14:07 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
import yaml
|
2017-08-17 15:14:07 +00:00
|
|
|
from mozboot.util import get_state_dir
|
|
|
|
|
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
class PresetHandler(object):
|
|
|
|
config_path = os.path.join(get_state_dir(), "try_presets.yml")
|
2017-08-17 15:14:07 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
def __init__(self):
|
|
|
|
self._presets = {}
|
2017-08-17 15:14:07 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
@property
|
|
|
|
def presets(self):
|
|
|
|
if not self._presets and os.path.isfile(self.config_path):
|
|
|
|
with open(self.config_path, 'r') as fh:
|
|
|
|
self._presets = yaml.safe_load(fh) or {}
|
2017-08-17 15:14:07 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
return self._presets
|
2017-08-17 15:14:07 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
def __contains__(self, name):
|
|
|
|
return name in self.presets
|
2017-08-17 15:14:07 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
def __getitem__(self, name):
|
|
|
|
return self.presets[name]
|
2017-08-17 15:14:07 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
def __str__(self):
|
|
|
|
return yaml.safe_dump(self.presets, default_flow_style=False)
|
2017-08-17 15:14:07 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
def list(self):
|
|
|
|
if not self.presets:
|
|
|
|
print("no presets found")
|
|
|
|
else:
|
|
|
|
print(self)
|
2017-10-27 18:58:34 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
def edit(self):
|
|
|
|
if 'EDITOR' not in os.environ:
|
|
|
|
print("error: must set the $EDITOR environment variable to use --edit-presets")
|
|
|
|
return
|
2017-10-27 18:58:34 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
subprocess.call([os.environ['EDITOR'], self.config_path])
|
2017-08-17 15:14:07 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
def save(self, name, **data):
|
|
|
|
self.presets[name] = data
|
2017-08-17 15:14:07 +00:00
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
with open(self.config_path, "w") as fh:
|
|
|
|
fh.write(str(self))
|
2017-08-17 15:14:07 +00:00
|
|
|
|
|
|
|
|
2019-02-25 19:47:29 +00:00
|
|
|
presets = PresetHandler()
|