mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
Started reworking CLI class into cli plugin
This commit is contained in:
parent
9efda29d60
commit
db0168f7af
6
ipa
6
ipa
@ -26,9 +26,7 @@ The CLI functionality is implemented in ipalib/cli.py
|
||||
"""
|
||||
|
||||
import sys
|
||||
from ipalib import api
|
||||
from ipalib.cli import CLI
|
||||
from ipalib import api, cli
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli = CLI(api, sys.argv[1:])
|
||||
sys.exit(cli.run())
|
||||
cli.run(api)
|
||||
|
@ -95,6 +95,13 @@ class Connectible(Backend):
|
||||
|
||||
class Executioner(Backend):
|
||||
|
||||
|
||||
def create_context(self, ccache=None, client_ip=None):
|
||||
if self.env.in_server:
|
||||
self.Backend.ldap.connect(ccache=ccache)
|
||||
else:
|
||||
self.Backend.xmlclient.connect(ccache=ccache)
|
||||
|
||||
def execute(self, name, *args, **options):
|
||||
error = None
|
||||
try:
|
||||
|
212
ipalib/cli.py
212
ipalib/cli.py
@ -392,40 +392,43 @@ class textui(backend.Backend):
|
||||
self.print_error(_('Cancelled.'))
|
||||
|
||||
|
||||
class help(frontend.Application):
|
||||
'''Display help on a command.'''
|
||||
class help(frontend.Command):
|
||||
"""
|
||||
Display help for a command or topic.
|
||||
"""
|
||||
|
||||
takes_args = ['command?']
|
||||
takes_args = [Bytes('command?')]
|
||||
|
||||
def run(self, command):
|
||||
def finalize(self):
|
||||
self.__topics = dict(
|
||||
(to_cli(c.name), c) for c in self.Command()
|
||||
)
|
||||
super(help, self).finalize()
|
||||
|
||||
def run(self, key):
|
||||
textui = self.Backend.textui
|
||||
if command is None:
|
||||
if key is None:
|
||||
self.print_commands()
|
||||
return
|
||||
key = str(command)
|
||||
if key not in self.application:
|
||||
name = from_cli(key)
|
||||
if name not in self.Command:
|
||||
raise HelpError(topic=key)
|
||||
cmd = self.application[key]
|
||||
print 'Purpose: %s' % cmd.doc
|
||||
self.application.build_parser(cmd).print_help()
|
||||
|
||||
def print_commands(self):
|
||||
std = set(self.Command) - set(self.Application)
|
||||
print '\nStandard IPA commands:'
|
||||
for key in sorted(std):
|
||||
cmd = self.api.Command[key]
|
||||
self.print_cmd(cmd)
|
||||
print '\nSpecial CLI commands:'
|
||||
for cmd in self.api.Application():
|
||||
self.print_cmd(cmd)
|
||||
mcl = self.get_mcl()
|
||||
for cmd in self.api.Command():
|
||||
print ' %s %s' % (
|
||||
to_cli(cmd.name).ljust(mcl),
|
||||
cmd.doc,
|
||||
)
|
||||
print '\nUse the --help option to see all the global options'
|
||||
print ''
|
||||
|
||||
def print_cmd(self, cmd):
|
||||
print ' %s %s' % (
|
||||
to_cli(cmd.name).ljust(self.application.mcl),
|
||||
cmd.doc,
|
||||
)
|
||||
def get_mcl(self):
|
||||
return max(len(k) for k in self.Command)
|
||||
|
||||
|
||||
class console(frontend.Application):
|
||||
@ -499,22 +502,26 @@ cli_application_commands = (
|
||||
)
|
||||
|
||||
|
||||
class KWCollector(object):
|
||||
def __init__(self):
|
||||
object.__setattr__(self, '_KWCollector__d', {})
|
||||
class Collector(object):
|
||||
def __init__(self, **extra):
|
||||
object.__setattr__(self, '_Collector__options', {})
|
||||
object.__setattr__(self, '_Collector__extra', frozenset(extra))
|
||||
for (key, value) in extra.iteritems():
|
||||
object.__setattr__(self, key, value)
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if name in self.__d:
|
||||
v = self.__d[name]
|
||||
if type(v) is tuple:
|
||||
value = v + (value,)
|
||||
else:
|
||||
value = (v, value)
|
||||
self.__d[name] = value
|
||||
if name not in self.__extra:
|
||||
if name in self.__options:
|
||||
v = self.__options[name]
|
||||
if type(v) is tuple:
|
||||
value = v + (value,)
|
||||
else:
|
||||
value = (v, value)
|
||||
self.__options[name] = value
|
||||
object.__setattr__(self, name, value)
|
||||
|
||||
def __todict__(self):
|
||||
return dict(self.__d)
|
||||
return dict(self.__options)
|
||||
|
||||
|
||||
class CLI(object):
|
||||
@ -819,3 +826,146 @@ class CLI(object):
|
||||
def __getitem__(self, key):
|
||||
assert self.__d is not None, 'you must call finalize() first'
|
||||
return self.__d[key]
|
||||
|
||||
|
||||
class cli(backend.Executioner):
|
||||
"""
|
||||
Backend plugin for executing from command line interface.
|
||||
"""
|
||||
|
||||
def run(self, argv):
|
||||
if len(argv) == 0:
|
||||
self.Command.help()
|
||||
return
|
||||
(key, argv) = (argv[0], argv[1:])
|
||||
cmd = self.get_command(key)
|
||||
(kw, collector) = self.parse(cmd, argv)
|
||||
if collector._interactive:
|
||||
self.prompt_interactively(cmd, kw, collector)
|
||||
self.create_context()
|
||||
|
||||
def prompt_interactively(self, cmd, kw, collector):
|
||||
"""
|
||||
Interactively prompt for missing or invalid values.
|
||||
|
||||
By default this method will only prompt for *required* Param that
|
||||
have a missing or invalid value. However, if
|
||||
``CLI.options.prompt_all`` is True, this method will prompt for any
|
||||
params that have a missing or required values, even if the param is
|
||||
optional.
|
||||
"""
|
||||
for param in cmd.params():
|
||||
if param.password or param.autofill:
|
||||
continue
|
||||
elif param.name not in kw:
|
||||
if not param.required and not collector._prompt_all:
|
||||
continue
|
||||
default = param.get_default(**kw)
|
||||
error = None
|
||||
while True:
|
||||
if error is not None:
|
||||
print '>>> %s: %s' % (param.cli_name, error)
|
||||
raw = self.Backend.textui.prompt(param.cli_name, default)
|
||||
try:
|
||||
value = param(raw, **kw)
|
||||
if value is not None:
|
||||
kw[param.name] = value
|
||||
break
|
||||
except errors.ValidationError, e:
|
||||
error = e.error
|
||||
|
||||
def get_command(self, key):
|
||||
name = from_cli(key)
|
||||
if name not in self.Command:
|
||||
raise CommandError(name=key)
|
||||
return self.Command[name]
|
||||
|
||||
def parse(self, cmd, argv):
|
||||
parser = self.build_parser(cmd)
|
||||
(collector, args) = parser.parse_args(argv,
|
||||
Collector(_prompt_all=False, interactive=True)
|
||||
)
|
||||
options = collector.__todict__()
|
||||
kw = cmd.args_options_2_params(*args, **options)
|
||||
return (dict(self.parse_iter(cmd, kw)), collector)
|
||||
|
||||
# FIXME: Move decoding to Command, use same regardless of request source
|
||||
def parse_iter(self, cmd, kw):
|
||||
"""
|
||||
Decode param values if appropriate.
|
||||
"""
|
||||
for (key, value) in kw.iteritems():
|
||||
param = cmd.params[key]
|
||||
if isinstance(param, Bytes):
|
||||
yield (key, value)
|
||||
else:
|
||||
yield (key, self.Backend.textui.decode(value))
|
||||
|
||||
def build_parser(self, cmd):
|
||||
parser = optparse.OptionParser(
|
||||
usage=' '.join(self.usage_iter(cmd))
|
||||
)
|
||||
if len(cmd.params) > 0:
|
||||
parser.add_option('-a', dest='_prompt_all', action='store_true',
|
||||
help='Prompt for all values interactively')
|
||||
parser.add_option('-n', dest='_interactive', action='store_false',
|
||||
help="Don\'t prompt for any values interactively")
|
||||
for option in cmd.options():
|
||||
kw = dict(
|
||||
dest=option.name,
|
||||
help=option.doc,
|
||||
)
|
||||
if option.password:
|
||||
kw['action'] = 'store_true'
|
||||
elif option.type is bool:
|
||||
if option.default is True:
|
||||
kw['action'] = 'store_false'
|
||||
else:
|
||||
kw['action'] = 'store_true'
|
||||
else:
|
||||
kw['metavar'] = metavar=option.__class__.__name__.upper()
|
||||
o = optparse.make_option('--%s' % to_cli(option.cli_name), **kw)
|
||||
parser.add_option(o)
|
||||
return parser
|
||||
|
||||
def usage_iter(self, cmd):
|
||||
yield 'Usage: %%prog [global-options] %s' % to_cli(cmd.name)
|
||||
for arg in cmd.args():
|
||||
if arg.password:
|
||||
continue
|
||||
name = to_cli(arg.cli_name).upper()
|
||||
if arg.multivalue:
|
||||
name = '%s...' % name
|
||||
if arg.required:
|
||||
yield name
|
||||
else:
|
||||
yield '[%s]' % name
|
||||
|
||||
|
||||
cli_plugins = (
|
||||
cli,
|
||||
textui,
|
||||
help,
|
||||
)
|
||||
|
||||
|
||||
def run(api):
|
||||
try:
|
||||
argv = api.bootstrap_with_global_options(context='cli')
|
||||
for klass in cli_plugins:
|
||||
api.register(klass)
|
||||
api.load_plugins()
|
||||
api.finalize()
|
||||
api.Backend.cli.run(sys.argv[1:])
|
||||
sys.exit()
|
||||
except KeyboardInterrupt:
|
||||
print ''
|
||||
api.log.info('operation aborted')
|
||||
sys.exit()
|
||||
except PublicError, e:
|
||||
error = e
|
||||
except Exception, e:
|
||||
api.log.exception('%s: %s', e.__class__.__name__, str(e))
|
||||
error = InternalError()
|
||||
api.log.error(error.strerror)
|
||||
sys.exit(error.errno)
|
||||
|
@ -122,7 +122,7 @@ class Search(frontend.Method):
|
||||
"""
|
||||
|
||||
|
||||
class CrudBackend(backend.Backend):
|
||||
class CrudBackend(backend.Connectible):
|
||||
"""
|
||||
Base class defining generic CRUD backend API.
|
||||
"""
|
||||
|
@ -33,6 +33,7 @@ import logging
|
||||
import os
|
||||
from os import path
|
||||
import subprocess
|
||||
import optparse
|
||||
import errors2
|
||||
from config import Env
|
||||
import util
|
||||
@ -575,12 +576,37 @@ class API(DictProxy):
|
||||
handler.setLevel(logging.INFO)
|
||||
log.addHandler(handler)
|
||||
|
||||
def bootstrap_with_global_options(self, options=None, context=None):
|
||||
if options is None:
|
||||
parser = util.add_global_options()
|
||||
(options, args) = parser.parse_args(
|
||||
list(s.decode('utf-8') for s in sys.argv[1:])
|
||||
def add_global_options(self, parser=None, context=None):
|
||||
"""
|
||||
Add global options to an optparse.OptionParser instance.
|
||||
"""
|
||||
if parser is None:
|
||||
parser = optparse.OptionParser()
|
||||
parser.disable_interspersed_args()
|
||||
parser.add_option('-e', dest='env', metavar='KEY=VAL', action='append',
|
||||
help='Set environment variable KEY to VAL',
|
||||
)
|
||||
parser.add_option('-c', dest='conf', metavar='FILE',
|
||||
help='Load configuration from FILE',
|
||||
)
|
||||
parser.add_option('-d', '--debug', action='store_true',
|
||||
help='Produce full debuging output',
|
||||
)
|
||||
parser.add_option('-v', '--verbose', action='store_true',
|
||||
help='Produce more verbose output',
|
||||
)
|
||||
if context == 'cli':
|
||||
parser.add_option('-a', '--prompt-all', action='store_true',
|
||||
help='Prompt for all values interactively'
|
||||
)
|
||||
parser.add_option('-n', '--no-prompt', action='store_false',
|
||||
help="Don\'t prompt for values interactively"
|
||||
)
|
||||
return parser
|
||||
|
||||
def bootstrap_with_global_options(self, parser=None, context=None):
|
||||
parser = self.add_global_options(parser, context)
|
||||
(options, args) = parser.parse_args()
|
||||
overrides = {}
|
||||
if options.env is not None:
|
||||
assert type(options.env) is list
|
||||
@ -600,6 +626,7 @@ class API(DictProxy):
|
||||
if context is not None:
|
||||
overrides['context'] = context
|
||||
self.bootstrap(**overrides)
|
||||
return args
|
||||
|
||||
def load_plugins(self):
|
||||
"""
|
||||
@ -686,6 +713,7 @@ class API(DictProxy):
|
||||
|
||||
for p in plugins.itervalues():
|
||||
p.instance.finalize()
|
||||
assert islocked(p.instance) is True
|
||||
object.__setattr__(self, '_API__finalized', True)
|
||||
tuple(PluginInfo(p) for p in plugins.itervalues())
|
||||
object.__setattr__(self, 'plugins',
|
||||
|
@ -120,6 +120,7 @@ def add_global_options(parser=None):
|
||||
"""
|
||||
if parser is None:
|
||||
parser = optparse.OptionParser()
|
||||
parser.disable_interspersed_args()
|
||||
parser.add_option('-e', dest='env', metavar='KEY=VAL', action='append',
|
||||
help='Set environment variable KEY to VAL',
|
||||
)
|
||||
|
@ -41,6 +41,12 @@ class ldap(CrudBackend):
|
||||
self.dn = _ldap.dn
|
||||
super(ldap, self).__init__()
|
||||
|
||||
def create_connection(self, ccache=None):
|
||||
return 'The LDAP connection.'
|
||||
|
||||
def destroy_connection(self):
|
||||
pass
|
||||
|
||||
def make_user_dn(self, uid):
|
||||
"""
|
||||
Construct user dn from uid.
|
||||
|
Loading…
Reference in New Issue
Block a user