diff options
-rw-r--r-- | ipalib/__init__.py | 2 | ||||
-rw-r--r-- | ipalib/cli.py | 35 | ||||
-rw-r--r-- | ipalib/parameters.py | 12 |
3 files changed, 47 insertions, 2 deletions
diff --git a/ipalib/__init__.py b/ipalib/__init__.py index d263d98a..e96c75d5 100644 --- a/ipalib/__init__.py +++ b/ipalib/__init__.py @@ -870,7 +870,7 @@ from frontend import Command, LocalOrRemote from frontend import Object, Method, Property from crud import Create, Retrieve, Update, Delete, Search from parameters import DefaultFrom, Bool, Flag, Int, Float, Bytes, Str, Password,List -from parameters import BytesEnum, StrEnum, GeneralizedTime +from parameters import BytesEnum, StrEnum, GeneralizedTime, File from errors import SkipPluginModule # We can't import the python uuid since it includes ctypes which makes diff --git a/ipalib/cli.py b/ipalib/cli.py index 7913512f..c1128a7c 100644 --- a/ipalib/cli.py +++ b/ipalib/cli.py @@ -38,7 +38,7 @@ import plugable import util from errors import PublicError, CommandError, HelpError, InternalError, NoSuchNamespaceError, ValidationError, NotFound from constants import CLI_TAB -from parameters import Password, Bytes +from parameters import Password, Bytes, File from request import ugettext as _ @@ -718,6 +718,7 @@ class cli(backend.Executioner): kw = self.parse(cmd, argv) if self.env.interactive: self.prompt_interactively(cmd, kw) + self.load_files(cmd, kw) try: result = self.execute(name, **kw) if callable(cmd.output_for_cli): @@ -824,6 +825,38 @@ class cli(backend.Executioner): except ValidationError, e: error = e.error + def load_files(self, cmd, kw): + """ + Load files from File parameters. + + This has to be done after all required parameters have been read + (i.e. after prompt_interactively has or would have been called) + AND before they are passed to the command. This is because: + 1) we need to be sure no more files are going to be added + 2) we load files from the machine where the command was executed + 3) the webUI will use a different way of loading files + """ + for p in cmd.params(): + if isinstance(p, File): + if p.name in kw: + try: + f = open(kw[p.name], 'r') + raw = f.read() + f.close() + except IOError, e: + raise ValidationError( + name=to_cli(p.cli_name), + error='%s: %s:' % (kw[p.name], e[1]) + ) + elif p.stdin_if_missing: + try: + raw = sys.stdin.read() + except IOError, e: + raise ValidationErro( + name=to_cli(p.cli_name), error=e[1] + ) + kw[p.name] = self.Backend.textui.decode(raw) + cli_plugins = ( cli, diff --git a/ipalib/parameters.py b/ipalib/parameters.py index 819a158f..0630915c 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -1254,6 +1254,18 @@ class List(Param): return +class File(Str): + """ + File parameter type. + + Accepts file names and loads their content into the parameter value. + """ + kwargs = Str.kwargs + ( + # valid for CLI, other backends (e.g. webUI) can ignore this + ('stdin_if_missing', bool, False), + ) + + class GeneralizedTime(Str): """ Generalized time parameter type. |