From 89ffaf411d4cfbf7cc7861439b8cde7ea8ba3ff7 Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Thu, 9 May 2013 15:36:41 +0200 Subject: Add prompt_param method to avoid code duplication Extracted common code from ipalib/plugins/cli.py and ipalib/plugins/dns.py that provided way to prompt user for the value of specific attribute. Added prompt_param method to Command class in ipalib/frontend.py Done as part of https://fedorahosted.org/freeipa/ticket/3602 --- ipalib/frontend.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'ipalib/frontend.py') diff --git a/ipalib/frontend.py b/ipalib/frontend.py index 0331dc52..427f6823 100644 --- a/ipalib/frontend.py +++ b/ipalib/frontend.py @@ -22,19 +22,18 @@ Base classes for all front-end plugins. """ import re -import inspect from distutils import version from ipapython.version import API_VERSION from ipapython.ipa_log_manager import root_logger -from base import lock, check_name, NameSpace +from base import NameSpace from plugable import Plugin, is_production_mode -from parameters import create_param, parse_param_spec, Param, Str, Flag, Password +from parameters import create_param, Param, Str, Flag, Password from output import Output, Entry, ListOfEntries -from text import _, ngettext +from text import _ from errors import (ZeroArgumentError, MaxArgumentError, OverlapError, - RequiresRoot, VersionError, RequirementError, OptionError, InvocationError) -from constants import TYPE_ERROR + VersionError, OptionError, InvocationError, + ValidationError, ConversionError) from ipalib import messages @@ -560,6 +559,32 @@ class Command(HasParam): if name in params: yield(name, params[name]) + def prompt_param(self, param, default=None, optional=False, kw=dict(), + label=None): + """ + Prompts the user for the value of given parameter. + + Returns the parameter instance. + """ + + if label is None: + label = param.label + + while True: + raw = self.Backend.textui.prompt(label, default, optional=optional) + + # Backend.textui.prompt does not fill in the default value, + # we have to do it ourselves + if not raw.strip(): + raw = default + + try: + return param(raw, **kw) + except (ValidationError, ConversionError), e: + # Display error and prompt again + self.Backend.textui.print_prompt_attribute_error(unicode(label), + unicode(e.error)) + def normalize(self, **kw): """ Return a dictionary of normalized values. -- cgit