diff options
author | Tomas Babej <tbabej@redhat.com> | 2013-05-09 15:36:41 +0200 |
---|---|---|
committer | Martin Kosek <mkosek@redhat.com> | 2013-06-05 12:50:29 +0200 |
commit | 89ffaf411d4cfbf7cc7861439b8cde7ea8ba3ff7 (patch) | |
tree | 8d132728b2f16a750c6d6c0f89dfa2a2e7f640bd /ipalib/frontend.py | |
parent | 8984e3e10582c3802edeb03d22d8b2c78381a31c (diff) | |
download | freeipa-89ffaf411d4cfbf7cc7861439b8cde7ea8ba3ff7.tar.gz freeipa-89ffaf411d4cfbf7cc7861439b8cde7ea8ba3ff7.tar.xz freeipa-89ffaf411d4cfbf7cc7861439b8cde7ea8ba3ff7.zip |
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
Diffstat (limited to 'ipalib/frontend.py')
-rw-r--r-- | ipalib/frontend.py | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/ipalib/frontend.py b/ipalib/frontend.py index 0331dc52b..427f68235 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. |