diff options
author | Pavel Zuna <pzuna@redhat.com> | 2009-11-05 16:15:47 +0100 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2009-11-06 16:05:31 -0700 |
commit | 566a3cb9722233b4ee92ef55db5a04dc0f26ee9d (patch) | |
tree | 0215e8e5f942e2038a9ca45c5b68859dcea60fe1 /ipalib/cli.py | |
parent | da58b0cc75ffd59e34729d3caedaa715d8dd2584 (diff) | |
download | freeipa-566a3cb9722233b4ee92ef55db5a04dc0f26ee9d.tar.gz freeipa-566a3cb9722233b4ee92ef55db5a04dc0f26ee9d.tar.xz freeipa-566a3cb9722233b4ee92ef55db5a04dc0f26ee9d.zip |
Add 'File' parameter type.
Accepts filenames and loads file contents as parameter value.
Diffstat (limited to 'ipalib/cli.py')
-rw-r--r-- | ipalib/cli.py | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/ipalib/cli.py b/ipalib/cli.py index 7913512f9..c1128a7c4 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, |