This patch introduces several changes to the _query_pool_dir method in
lnst.Controller.Wizard:
- Method was renamed to _check_and_query_pool_dir to better express nature of the method
- Method now uses 1 optional argument - pool_dir - if set to something else than None,
it checks if it's valid path to existing or creatable dir, if it is, uses it,
else queries user for a new path (which is also checked)
This method was reworked to prevent code duplication which would occur when mode for
virtual
machines would be introduced
Signed-off-by: Jiri Prochazka <jprochaz(a)redhat.com>
---
lnst/Controller/Wizard.py | 80 ++++++++++++++++++++---------------------------
1 file changed, 34 insertions(+), 46 deletions(-)
diff --git a/lnst/Controller/Wizard.py b/lnst/Controller/Wizard.py
index 51856e8..f579e5d 100644
--- a/lnst/Controller/Wizard.py
+++ b/lnst/Controller/Wizard.py
@@ -32,22 +32,7 @@ class Wizard:
""" Starts Wizard in an interactive mode
@param pool_dir Path to pool directory (optional)
"""
- if pool_dir is None:
- pool_dir = self._query_pool_dir()
- else:
- rv = self._check_path(pool_dir)
- if rv == PATH_IS_DIR_ACCESSIBLE:
- print("Pool directory set to '%s'" % pool_dir)
- elif rv == PATH_DOES_NOT_EXIST:
- sys.stderr.write("Path '%s' does not exist\n" %
pool_dir)
- pool_dir = self._query_pool_dir()
- elif rv == PATH_NOT_DIR:
- sys.stderr.write("Path '%s' exists but is not a
directory\n"
- % pool_dir)
- pool_dir = self._query_pool_dir()
- elif rv == PATH_IS_DIR_NOT_ACCESSIBLE:
- sys.stderr.write("Directory '%s' is not writable\n" %
pool_dir)
- pool_dir = self._query_pool_dir()
+ pool_dir = self._check_and_query_pool_dir(pool_dir)
while True:
hostname = self._query_hostname()
@@ -161,6 +146,39 @@ class Wizard:
except:
return False
+ def _check_and_query_pool_dir(self, pool_dir):
+ """ Queries user for pool directory
+ @param pool_dir Optional pool_dir which will be checked and used if OK
+ @return Valid (is writable by user) path to directory
+ """
+ while True:
+ if pool_dir is None:
+ pool_dir = raw_input("Enter path to a pool directory "
+ "(default: '%s'): " %
DefaultPoolDir)
+ if pool_dir == "":
+ pool_dir = DefaultPoolDir
+
+ pool_dir = os.path.expanduser(pool_dir)
+ rv = self._check_path(pool_dir)
+ if rv == PATH_IS_DIR_ACCESSIBLE:
+ print("Pool directory set to '%s'" % pool_dir)
+ return pool_dir
+ elif rv == PATH_DOES_NOT_EXIST:
+ sys.stderr.write("Path '%s' does not exist\n"
+ % pool_dir)
+ if self._query_dir_creation(pool_dir):
+ created_dir = self._create_dir(pool_dir)
+ if created_dir is not None:
+ return created_dir
+
+ elif rv == PATH_NOT_DIR:
+ sys.stderr.write("Path '%s' exists but is not a
directory\n"
+ % pool_dir)
+ elif rv == PATH_IS_DIR_NOT_ACCESSIBLE:
+ sys.stderr.write("Directory '%s' is not writable\n"
+ % pool_dir)
+ pool_dir = None
+
def _check_path(self, pool_dir):
""" Checks if path exists, is dir and is accessible by user
@param pool_dir Path to checked directory
@@ -330,36 +348,6 @@ class Wizard:
sys.stderr.write("Hostname '%s' is not translatable into a
"
"valid IP address\n" % hostname)
- def _query_pool_dir(self):
- """ Queries user for pool directory
- @return Valid (is writable by user) path to directory
- """
- while True:
- pool_dir = raw_input("Enter path to a pool directory "
- "(default: '%s'): " % DefaultPoolDir)
- if pool_dir == "":
- pool_dir = DefaultPoolDir
-
- pool_dir = os.path.expanduser(pool_dir)
- rv = self._check_path(pool_dir)
- if rv == PATH_IS_DIR_ACCESSIBLE:
- print("Pool directory set to '%s'" % pool_dir)
- return pool_dir
- elif rv == PATH_DOES_NOT_EXIST:
- sys.stderr.write("Path '%s' does not exist\n"
- % pool_dir)
- if self._query_dir_creation(pool_dir):
- created_dir = self._create_dir(pool_dir)
- if created_dir is not None:
- return created_dir
-
- elif rv == PATH_NOT_DIR:
- sys.stderr.write("Path '%s' exists but is not a
directory\n"
- % pool_dir)
- elif rv == PATH_IS_DIR_NOT_ACCESSIBLE:
- sys.stderr.write("Directory '%s' is not writable\n"
- % pool_dir)
-
def _query_port(self):
""" Queries user for port
@return Integer representing port
--
2.4.3