mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Add 'File' parameter type.
Accepts filenames and loads file contents as parameter value.
This commit is contained in:
committed by
Jason Gerard DeRose
parent
da58b0cc75
commit
566a3cb972
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user