Add platform-specific open and edit commands at init

The main motivation is to provide sensible default to make the
commnand "just work" without needing to fiddle with the configuration
This commit is contained in:
Fabien Benureau 2015-12-06 08:41:33 +01:00
parent 14df0ad1cb
commit 789db93911
3 changed files with 39 additions and 0 deletions

View File

@ -41,6 +41,8 @@ def command(conf, args):
conf['main']['pubsdir'] = pubsdir conf['main']['pubsdir'] = pubsdir
conf['main']['docsdir'] = docsdir conf['main']['docsdir'] = docsdir
conf['main']['open_cmd'] = config.default_open_cmd()
conf['main']['edit_cmd'] = config.default_edit_cmd()
config.save_conf(conf) config.save_conf(conf)
Repository(conf, create=True) Repository(conf, create=True)

View File

@ -1 +1,2 @@
from .conf import get_confpath, load_default_conf, load_conf, save_conf, check_conf from .conf import get_confpath, load_default_conf, load_conf, save_conf, check_conf
from .conf import default_open_cmd, default_edit_cmd

View File

@ -1,4 +1,6 @@
import os import os
import platform
import shutil
import configobj import configobj
import validate import validate
@ -54,3 +56,37 @@ def save_conf(conf, path=None):
path = get_confpath(verify=False) path = get_confpath(verify=False)
with open(path, 'wb') as f: with open(path, 'wb') as f:
conf.write(outfile=f) conf.write(outfile=f)
def default_open_cmd():
"""Chooses the default command to open documents"""
if platform.system() == 'Darwin':
return 'open'
elif platform.system() == 'Linux':
return 'xdg-open'
elif platform.system() == 'Windows':
return 'start'
else:
return None
def which(cmd):
try:
return shutil.which(cmd) # available in python 3.3
except AttributeError:
for path in ['.'] + os.environ["PATH"].split(os.pathsep):
filepath = os.path.join(path.strip('"'), cmd)
if os.path.isfile(path) and os.access(path, os.X_OK):
return filepath
return None
def default_edit_cmd():
"""Find an available editor"""
if 'EDITOR' in os.environ:
return os.environ['EDITOR']
elif platform.system() == 'Darwin' or 'Linux':
for editor in ['vim', 'nano', 'emacs', 'vi']:
if which(editor) is not None:
return editor
elif platform.system() == 'Windows':
return 'Notepad.exe | Out-Null' # wait for notepad to close
else:
return None