From d5b7ce64da65dcacd99207bcdf11fc6f2703d879 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 14 Nov 2015 13:05:06 -0800 Subject: [PATCH] Move EDITOR discovery to a util function --- beets/ui/commands.py | 2 +- beets/util/__init__.py | 42 +++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/beets/ui/commands.py b/beets/ui/commands.py index fa2fc84af..307945a03 100644 --- a/beets/ui/commands.py +++ b/beets/ui/commands.py @@ -1473,7 +1473,7 @@ def config_edit(): An empty config file is created if no existing config file exists. """ path = config.user_config_path() - editor = os.environ.get('EDITOR') + editor = util.editor_command() try: if not os.path.isfile(path): open(path, 'w+').close() diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 61e2f4ac8..a2a5688b5 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -736,28 +736,36 @@ def open_anything(): return base_cmd -def interactive_open(targets, command=None): - """Open the files in `targets` by `exec`ing a new command. (The new - program takes over, and Python execution ends: this does not fork a - subprocess.) +def editor_command(): + """Get a command for opening a text file. - If `command` is provided, use it. Otherwise, use an OS-specific - command (from `open_anything`) to open the file. + Use the `EDITOR` environment variable by default. If it is not + present, fall back to `open_anything()`, the platform-specific tool + for opening files in general. + """ + editor = os.environ.get('EDITOR') + if editor: + return editor + return open_anything() + + +def interactive_open(targets, command): + """Open the files in `targets` by `exec`ing a new `command`, given + as a Unicode string. (The new program takes over, and Python + execution ends: this does not fork a subprocess.) Can raise `OSError`. """ - if command: - command = command.encode('utf8') - try: - command = [c.decode('utf8') - for c in shlex.split(command)] - except ValueError: # Malformed shell tokens. - command = [command] - command.insert(0, command[0]) # for argv[0] - else: - base_cmd = open_anything() - command = [base_cmd, base_cmd] + # Split the command string into its arguments. + command = command.encode('utf8') + try: + command = [c.decode('utf8') + for c in shlex.split(command)] + except ValueError: # Malformed shell tokens. + command = [command] + command.insert(0, command[0]) # for argv[0] + # Add the explicit arguments. command += targets return os.execlp(*command)