"recess": "/usr/local/bin/recess",
// Path to lessc
"lessc": "/usr/local/bin/lessc",
+ // Path to your favourite editor
+ "editor": "/usr/bin/vim",
// Debug level of MDK. 'debug', 'info', 'warning', 'error' or 'critical'.
"debug": "info",
http://github.com/FMCorz/mdk
"""
+import logging
from lib.command import Command
+from lib.tools import launchEditor, yesOrNo
+from lib.config import ConfigObject, ConfigFileCouldNotBeLoaded
class ConfigCommand(Command):
'help': 'the action to perform',
'metavar': 'action',
'sub-commands': {
+ 'edit': (
+ {
+ 'help': 'opens the config file in an editor'
+ },
+ []
+ ),
'flatlist': (
{
'help': 'flat list of the settings'
if args.action == 'list':
self.dictDisplay(self.C.get(), 0)
+ elif args.action == 'edit':
+ f = self.C.userFile
+ success = None
+
+ while True:
+ tmpfile = launchEditor(filepath=f, suffix='.json')
+ co = ConfigObject()
+ try:
+ co.loadFromFile(tmpfile)
+ except ConfigFileCouldNotBeLoaded:
+ success = False
+ logging.error('I could not load the file, you probably screwed up the JSON...')
+ if yesOrNo('Would you like to continue editing? If not the changes will be discarded.'):
+ f = tmpfile
+ continue
+ else:
+ break
+ else:
+ success = True
+ break
+
+ if success:
+ with open(tmpfile, 'r') as new:
+ with open(self.C.userFile, 'w') as current:
+ current.write(new.read())
+
elif args.action == 'flatlist':
self.flatDisplay(self.C.get())
print 'Checking dependencies'
hasErrors = False
- for k in ['git', 'php', 'java', 'recess', 'lessc']:
+ for k in ['git', 'php', 'java', 'recess', 'lessc', 'editor']:
path = self.C.get(k)
if not path or not os.path.isfile(path):
print ' The path to \'%s\' is invalid: %s' % (k, path)
import json
import re
import copy
-from exceptions import ConfigFileCouldNotBeLoaded, ConfigFileNotFound, ConfigFileCouldNotBeSaved
class ConfigObject(object):
"""Set a new setting"""
super(Conf, self).set(name, value)
self.save()
+
+
+class ConfigFileNotFound(Exception):
+ pass
+
+
+class ConfigFileCouldNotBeLoaded(Exception):
+ pass
+
+
+class ConfigFileCouldNotBeSaved(Exception):
+ pass
pass
-class ConfigFileNotFound(Exception):
- pass
-
-
-class ConfigFileCouldNotBeLoaded(Exception):
- pass
-
-
-class ConfigFileCouldNotBeSaved(Exception):
- pass
-
-
class ConflictInScriptName(Exception):
pass
import getpass
import logging
import hashlib
+import tempfile
+from lib.config import Conf
+C = Conf()
def yesOrNo(q):
while True:
def get_current_user():
"""Attempt to get the currently logged in user"""
username = 'root'
- import os
try:
username = os.getlogin()
except OSError:
return username
+def launchEditor(filepath=None, suffix='.tmp'):
+ """Launchs up an editor
+
+ If filepath is passed, the content of the file is used to populate the editor.
+
+ This returns the path to the saved file.
+ """
+ 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])
+ return tmpfile.name
+
+
def md5file(filepath):
"""Return the md5 sum of a file
This is terribly memory inefficient!"""