mirror of
https://github.com/beetbox/beets.git
synced 2026-01-04 06:53:27 +01:00
Merge pull request #1128 from geigerzaehler/edit-invalid-config
Allow invalid configuration files to be edited
This commit is contained in:
commit
d0e8be45a9
3 changed files with 47 additions and 20 deletions
|
|
@ -920,6 +920,13 @@ def _raw_main(args, lib=None):
|
|||
help=optparse.SUPPRESS_HELP)
|
||||
|
||||
options, subargs = parser.parse_global_options(args)
|
||||
|
||||
# Bypass _setup so that an invalid configuration does not prevent
|
||||
# the editor from starting.
|
||||
if subargs[0] == 'config' and ('-e' in subargs or '--edit' in subargs):
|
||||
from beets.ui.commands import config_edit
|
||||
return config_edit()
|
||||
|
||||
subcommands, plugins, lib = _setup(options, lib)
|
||||
parser.add_subcommand(*subcommands)
|
||||
|
||||
|
|
|
|||
|
|
@ -1485,32 +1485,38 @@ def config_func(lib, opts, args):
|
|||
|
||||
# Open in editor.
|
||||
elif opts.edit:
|
||||
path = config.user_config_path()
|
||||
|
||||
if 'EDITOR' in os.environ:
|
||||
editor = os.environ['EDITOR']
|
||||
args = [editor, editor, path]
|
||||
elif platform.system() == 'Darwin':
|
||||
args = ['open', 'open', '-n', path]
|
||||
elif platform.system() == 'Windows':
|
||||
# On windows we can execute arbitrary files. The os will
|
||||
# take care of starting an appropriate application
|
||||
args = [path, path]
|
||||
else:
|
||||
# Assume Unix
|
||||
args = ['xdg-open', 'xdg-open', path]
|
||||
|
||||
try:
|
||||
os.execlp(*args)
|
||||
except OSError:
|
||||
raise ui.UserError("Could not edit configuration. Please"
|
||||
"set the EDITOR environment variable.")
|
||||
config_edit()
|
||||
|
||||
# Dump configuration.
|
||||
else:
|
||||
print(config.dump(full=opts.defaults))
|
||||
|
||||
|
||||
def config_edit():
|
||||
"""Open a program to edit the user configuration.
|
||||
"""
|
||||
path = config.user_config_path()
|
||||
|
||||
if 'EDITOR' in os.environ:
|
||||
editor = os.environ['EDITOR']
|
||||
args = [editor, editor, path]
|
||||
elif platform.system() == 'Darwin':
|
||||
args = ['open', 'open', '-n', path]
|
||||
elif platform.system() == 'Windows':
|
||||
# On windows we can execute arbitrary files. The os will
|
||||
# take care of starting an appropriate application
|
||||
args = [path, path]
|
||||
else:
|
||||
# Assume Unix
|
||||
args = ['xdg-open', 'xdg-open', path]
|
||||
|
||||
try:
|
||||
os.execlp(*args)
|
||||
except OSError:
|
||||
raise ui.UserError("Could not edit configuration. Please"
|
||||
"set the EDITOR environment variable.")
|
||||
|
||||
|
||||
config_cmd = ui.Subcommand('config',
|
||||
help='show or edit the user configuration')
|
||||
config_cmd.parser.add_option(
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ from beets import config
|
|||
import _common
|
||||
from _common import unittest
|
||||
from helper import TestHelper, capture_stdout
|
||||
from beets.library import Library
|
||||
|
||||
|
||||
class ConfigCommandTest(unittest.TestCase, TestHelper):
|
||||
|
|
@ -105,6 +106,19 @@ class ConfigCommandTest(unittest.TestCase, TestHelper):
|
|||
self.assertIn('Could not edit configuration',
|
||||
str(user_error.exception.args[0]))
|
||||
|
||||
def test_edit_invalid_config_file(self):
|
||||
self.lib = Library(':memory:')
|
||||
with open(self.config_path, 'w') as file:
|
||||
file.write('invalid: [')
|
||||
config.clear()
|
||||
config._materialized = False
|
||||
|
||||
os.environ['EDITOR'] = 'myeditor'
|
||||
with patch('os.execlp') as execlp:
|
||||
self.run_command('config', '-e')
|
||||
execlp.assert_called_once_with(
|
||||
'myeditor', 'myeditor', self.config_path)
|
||||
|
||||
|
||||
def suite():
|
||||
return unittest.TestLoader().loadTestsFromName(__name__)
|
||||
|
|
|
|||
Loading…
Reference in a new issue