2017-08-17 18:14:07 +03: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
|
|
|
|
|
2019-02-23 02:25:54 +03:00
|
|
|
import ConfigParser
|
2017-08-17 18:14:07 +03:00
|
|
|
import os
|
2017-10-27 21:58:34 +03:00
|
|
|
import subprocess
|
2019-02-23 02:25:54 +03:00
|
|
|
from collections import defaultdict
|
2017-08-17 18:14:07 +03:00
|
|
|
|
2019-02-25 22:47:29 +03:00
|
|
|
import yaml
|
2017-08-17 18:14:07 +03:00
|
|
|
from mozboot.util import get_state_dir
|
|
|
|
|
|
|
|
|
2019-02-25 22:47:29 +03:00
|
|
|
class PresetHandler(object):
|
2017-08-17 18:14:07 +03:00
|
|
|
|
2019-02-28 21:26:03 +03:00
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
2019-02-25 22:47:29 +03:00
|
|
|
self._presets = {}
|
2017-08-17 18:14:07 +03:00
|
|
|
|
2019-02-25 22:47:29 +03:00
|
|
|
@property
|
|
|
|
def presets(self):
|
2019-02-28 21:26:03 +03:00
|
|
|
if not self._presets and os.path.isfile(self.path):
|
|
|
|
with open(self.path, 'r') as fh:
|
2019-02-25 22:47:29 +03:00
|
|
|
self._presets = yaml.safe_load(fh) or {}
|
2017-08-17 18:14:07 +03:00
|
|
|
|
2019-02-25 22:47:29 +03:00
|
|
|
return self._presets
|
2017-08-17 18:14:07 +03:00
|
|
|
|
2019-02-25 22:47:29 +03:00
|
|
|
def __contains__(self, name):
|
|
|
|
return name in self.presets
|
2017-08-17 18:14:07 +03:00
|
|
|
|
2019-02-25 22:47:29 +03:00
|
|
|
def __getitem__(self, name):
|
|
|
|
return self.presets[name]
|
2017-08-17 18:14:07 +03:00
|
|
|
|
2019-02-25 22:47:29 +03:00
|
|
|
def __str__(self):
|
|
|
|
return yaml.safe_dump(self.presets, default_flow_style=False)
|
2017-08-17 18:14:07 +03:00
|
|
|
|
2019-02-25 22:47:29 +03:00
|
|
|
def list(self):
|
|
|
|
if not self.presets:
|
|
|
|
print("no presets found")
|
|
|
|
else:
|
|
|
|
print(self)
|
2017-10-27 21:58:34 +03:00
|
|
|
|
2019-02-25 22:47:29 +03: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 21:58:34 +03:00
|
|
|
|
2019-02-28 21:26:03 +03:00
|
|
|
subprocess.call([os.environ['EDITOR'], self.path])
|
2017-08-17 18:14:07 +03:00
|
|
|
|
2019-02-25 22:47:29 +03:00
|
|
|
def save(self, name, **data):
|
|
|
|
self.presets[name] = data
|
2017-08-17 18:14:07 +03:00
|
|
|
|
2019-02-28 21:26:03 +03:00
|
|
|
with open(self.path, "w") as fh:
|
2019-02-25 22:47:29 +03:00
|
|
|
fh.write(str(self))
|
2017-08-17 18:14:07 +03:00
|
|
|
|
|
|
|
|
2019-02-28 21:26:03 +03:00
|
|
|
presets = PresetHandler(os.path.join(get_state_dir(), "try_presets.yml"))
|
2019-02-23 02:25:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
def migrate_old_presets():
|
|
|
|
"""Move presets from the old `autotry.ini` format to the new
|
|
|
|
`try_presets.yml` one.
|
|
|
|
"""
|
|
|
|
from .selectors.syntax import AutoTry, SyntaxParser
|
|
|
|
old_preset_path = os.path.join(get_state_dir(), 'autotry.ini')
|
2019-02-28 21:26:03 +03:00
|
|
|
if os.path.isfile(presets.path) or not os.path.isfile(old_preset_path):
|
2019-02-23 02:25:54 +03:00
|
|
|
return
|
|
|
|
|
2019-02-28 21:26:03 +03:00
|
|
|
print("migrating saved presets from '{}' to '{}'".format(old_preset_path, presets.path))
|
2019-02-23 02:25:54 +03:00
|
|
|
config = ConfigParser.ConfigParser()
|
|
|
|
config.read(old_preset_path)
|
|
|
|
|
|
|
|
unknown = defaultdict(list)
|
|
|
|
for section in config.sections():
|
|
|
|
for name, value in config.items(section):
|
|
|
|
kwargs = {}
|
|
|
|
if section == 'fuzzy': # try fuzzy
|
|
|
|
kwargs['query'] = [value]
|
2019-02-26 22:33:03 +03:00
|
|
|
kwargs['selector'] = 'fuzzy'
|
2019-02-23 02:25:54 +03:00
|
|
|
|
|
|
|
elif section == 'try': # try syntax
|
|
|
|
parser = SyntaxParser()
|
|
|
|
kwargs = vars(parser.parse_args(AutoTry.split_try_string(value)))
|
|
|
|
kwargs = {k: v for k, v in kwargs.items() if v != parser.get_default(k)}
|
2019-02-26 22:33:03 +03:00
|
|
|
kwargs['selector'] = 'syntax'
|
2019-02-23 02:25:54 +03:00
|
|
|
|
|
|
|
else:
|
|
|
|
unknown[section].append("{} = {}".format(name, value))
|
|
|
|
continue
|
|
|
|
|
2019-02-26 22:33:03 +03:00
|
|
|
presets.save(name, **kwargs)
|
2019-02-23 02:25:54 +03:00
|
|
|
|
|
|
|
os.remove(old_preset_path)
|
|
|
|
|
|
|
|
if unknown:
|
|
|
|
for section, values in unknown.items():
|
|
|
|
print("""
|
|
|
|
warning: unknown section '{}', the following presets were not migrated:
|
|
|
|
{}
|
|
|
|
""".format(section, '\n '.join(values)).lstrip())
|