From a0575f8aae110857bbe84d416d137859a4c6446a Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Tue, 15 Apr 2014 15:56:23 +0800 Subject: [PATCH] Guessing the editor from the environment --- config-dist.json | 4 ++-- lib/commands/doctor.py | 22 ++++++++++++++++++++-- lib/tools.py | 18 +++++++++++++++++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/config-dist.json b/config-dist.json index 1a48f46..5211cc3 100644 --- a/config-dist.json +++ b/config-dist.json @@ -171,8 +171,8 @@ "recess": "/usr/local/bin/recess", // Path to lessc "lessc": "/usr/local/bin/lessc", - // Path to your favourite editor - "editor": "/usr/bin/vim", + // Path to your favourite editor. Set to null to guess it from the System environment. + "editor": null, // Debug level of MDK. 'debug', 'info', 'warning', 'error' or 'critical'. "debug": "info", diff --git a/lib/commands/doctor.py b/lib/commands/doctor.py index 42b0b79..5112f11 100644 --- a/lib/commands/doctor.py +++ b/lib/commands/doctor.py @@ -25,9 +25,10 @@ http://github.com/FMCorz/mdk import os import shutil import imp +import subprocess from lib import git from lib.command import Command -from lib.tools import mkdir +from lib.tools import mkdir, resolveEditor class DoctorCommand(Command): @@ -213,8 +214,9 @@ class DoctorCommand(Command): print 'Checking dependencies' + # Check binaries. hasErrors = False - for k in ['git', 'php', 'java', 'recess', 'lessc', 'editor']: + for k in ['git', 'php', 'java', 'recess', 'lessc']: path = self.C.get(k) if not path or not os.path.isfile(path): print ' The path to \'%s\' is invalid: %s' % (k, path) @@ -222,6 +224,7 @@ class DoctorCommand(Command): if hasErrors and args.fix: print ' Please manually fix the paths in your config file' + # Check PIP modules. with open(os.path.join(os.path.dirname(__file__), '..', '..', 'requirements.txt'), 'r') as f: hasErrors = False for line in f: @@ -236,6 +239,21 @@ class DoctorCommand(Command): if hasErrors and args.fix: print ' Try running \'pip -r requirements.txt\' from MDK\'s installation directory' + # Checking editor. + editor = resolveEditor() + if editor: + try: + # Check if it is callable. + proc = subprocess.Popen(editor, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + proc.kill() + except OSError: + editor = None + + if not editor: + print ' Could not resolve the path to your editor' + if args.fix: + print ' Set $EDITOR, /usr/bin/editor, or use: mdk config set editor [path]' + def directories(self, args): """Check that the directories are valid""" diff --git a/lib/tools.py b/lib/tools.py index cbfcda9..0ebad54 100644 --- a/lib/tools.py +++ b/lib/tools.py @@ -106,11 +106,14 @@ def launchEditor(filepath=None, suffix='.tmp'): This returns the path to the saved file. """ + editor = resolveEditor() + if not editor: + raise Exception('Could not locate the editor') with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmpfile: with open(filepath, 'r') as f: tmpfile.write(f.read()) tmpfile.flush() - subprocess.call([C.get('editor'), tmpfile.name]) + subprocess.call([editor, tmpfile.name]) return tmpfile.name @@ -156,6 +159,19 @@ def process(cmd, cwd=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE): return (proc.returncode, out, err) +def resolveEditor(): + """Try to resolve the editor that the user would want to use. + This does actually checks if it is executable""" + editor = C.get('editor') + if not editor: + editor = os.environ.get('EDITOR') + if not editor: + editor = os.environ.get('VISUAL') + if not editor and os.path.isfile('/usr/bin/editor'): + editor = '/usr/bin/editor' + return editor + + def downloadProcessHook(count, size, total): """Hook to report the downloading a file using urllib.urlretrieve""" if count <= 0: -- 2.11.0