config: Add in default support
This allows us to have default attributes unless the user has specified one
already for that attribute. Useful to make MDT a bit more general.
Change-Id: I4225b540ecf32bd5a50145b0752d40f469e8f80f
diff --git a/mdt/config.py b/mdt/config.py
index db101d0..4634e8f 100644
--- a/mdt/config.py
+++ b/mdt/config.py
@@ -5,6 +5,9 @@
CONFIG_KEYSDIR = os.path.join(CONFIG_BASEDIR, "keys")
CONFIG_ATTRDIR = os.path.join(CONFIG_BASEDIR, "attribs")
+DEFAULT_USERNAME = "mendel"
+DEFAULT_SSH_COMMAND = "ssh"
+
class Config:
def __init__(self):
self.ensureConfigDirExists()
@@ -17,12 +20,13 @@
if not os.path.exists(CONFIG_ATTRDIR):
os.makedirs(CONFIG_ATTRDIR, mode=0o700)
- def getAttribute(self, name):
+ def getAttribute(self, name, default=None):
path = os.path.join(CONFIG_ATTRDIR, name)
if os.path.exists(path):
with open(path, "r") as fp:
return fp.readline().rstrip()
- return None
+
+ return default
def setAttribute(self, name, value):
path = os.path.join(CONFIG_ATTRDIR, name)
@@ -34,10 +38,20 @@
if os.path.exists(path):
os.unlink(path)
- def defaultDeviceName(self, devicename=None):
+ def preferredDevice(self, devicename=None):
if not devicename:
- return self.getAttribute("devicename")
- self.setAttribute("devicename", devicename)
+ return self.getAttribute("preferred-device")
+ self.setAttribute("preferred-device", devicename)
+
+ def username(self, username=None):
+ if not username:
+ return self.getAttribute("username", DEFAULT_USERNAME)
+ self.setAttribute("username", username)
+
+ def sshCommand(self, command=None):
+ if not command:
+ return self.getAttribute("ssh-command", DEFAULT_SSH_COMMAND)
+ self.setAttribute("ssh-command", command)
def getKey(self, keyname):
path = os.path.join(CONFIG_KEYSDIR, keyname)
diff --git a/mdt/devices.py b/mdt/devices.py
index 8aac28a..b1e85eb 100644
--- a/mdt/devices.py
+++ b/mdt/devices.py
@@ -6,14 +6,14 @@
class DevicesCommand:
def __init__(self):
self.discoverer = Discoverer()
- self.devicename = Config().defaultDeviceName()
+ self.device = Config().preferredDevice()
def run(self, args):
sleep(1)
print('Devices found:')
discoveries = self.discoverer.discoveries
for host, address in discoveries.items():
- if self.devicename and host == self.devicename:
+ if self.device and host == self.device:
print('{0}\t\t({1},default)'.format(host, address))
else:
print('{0}\t\t({1})'.format(host, address))