52: Got cli working against new framework

This commit is contained in:
Jason Gerard DeRose
2008-08-05 22:21:57 +00:00
parent 1fce1487f9
commit 159207514f
5 changed files with 77 additions and 66 deletions

39
ipa
View File

@@ -38,42 +38,35 @@ def _(msg):
def print_commands():
print 'Commands:'
m = api.max_cmd_len
for cmd in api.commands():
print ' %s %s' % (cmd.cli_name.ljust(m), cmd.get_doc(_))
for cmd in api.cmd:
print ' %s %s' % (str(cmd).ljust(m), cmd.get_doc(_))
def print_help(cmd):
print 'Help on %s' % cmd
def print_api():
print 'Commands:'
for cmd in api.commands():
print ' %s [%s]' % (cmd.name, cmd.loc)
print 'Objects:'
for obj in api.objects():
print ' %s [%s]' % (obj.name, obj.loc)
for meth in obj.methods():
print ' .%s() [%s]' % (meth.attr_name, meth.loc)
for prop in obj.properties():
print ' .%s [%s]' % (prop.attr_name, prop.loc)
print 'Stats:'
print ' %d commands' % len(api.commands)
print ' %d objects' % len(api.objects)
def print_ns(name):
ns = getattr(api, name)
print '%d %s:' % (len(ns), name)
m = max(len(i.name) for i in ns)
for i in ns:
print ' %s %r' % (i.name.ljust(m), i)
for n in ['cmd', 'obj', 'prop']:
print_ns(n)
print ''
if len(sys.argv) < 2:
print_commands()
print 'Usage: ipa COMMAND [OPTIONS]'
sys.exit(2)
cmd = sys.argv[1]
pcmd = cmd.replace('-', '_')
if cmd == '_api_':
name= sys.argv[1]
if name == '_api_':
print_api()
sys.exit()
elif pcmd not in api.commands:
elif name not in api.cmd:
print_commands()
print 'ipa: ERROR: unknown command %r' % cmd
print 'ipa: ERROR: unknown command %r' % name
sys.exit(2)
api.commands[pcmd]()
api.cmd[name]()

View File

@@ -92,7 +92,7 @@ class Plugin(object):
Returns a valid Python expression that could create this plugin
instance given the appropriate environment.
"""
return '%s.%s()' % (
return '%s.%s' % (
self.__class__.__module__,
self.__class__.__name__
)
@@ -296,7 +296,8 @@ class Registrar(object):
base.
"""
for base in self.__allowed:
yield (base, self.__d[base.__name__].values())
sub_d = self.__d[base.__name__]
yield (base, tuple(sub_d[k] for k in sorted(sub_d)))
class API(ReadOnly):

View File

@@ -21,116 +21,115 @@
Some example plugins.
"""
import crud
import base
import public
from run import api
# Hypothetical functional commands (not associated with any object):
class krbtest(base.Command):
class krbtest(public.cmd):
def get_doc(self, _):
return _('test your Kerberos ticket')
api.register(krbtest)
class discover(base.Command):
class discover(public.cmd):
def get_doc(self, _):
return _('discover IPA servers on network')
api.register(discover)
# Register some methods for the 'user' object:
class user__add(crud.Add):
class user_add(public.mthd):
def get_doc(self, _):
return _('add new user')
api.register(user__add)
api.register(user_add)
class user__del(crud.Del):
class user_del(public.mthd):
def get_doc(self, _):
return _('delete existing user')
api.register(user__del)
api.register(user_del)
class user__mod(crud.Mod):
class user_mod(public.mthd):
def get_doc(self, _):
return _('edit existing user')
api.register(user__mod)
api.register(user_mod)
class user__find(crud.Find):
class user_find(public.mthd):
def get_doc(self, _):
return _('search for users')
api.register(user__find)
api.register(user_find)
# Register some properties for the 'user' object:
class user__firstname(base.Property):
class user_firstname(public.prop):
pass
api.register(user__firstname)
api.register(user_firstname)
class user__lastname(base.Property):
class user_lastname(public.prop):
pass
api.register(user__lastname)
api.register(user_lastname)
class user__login(base.Property):
class user_login(public.prop):
pass
api.register(user__login)
api.register(user_login)
# Register some methods for the 'group' object:
class group__add(crud.Add):
class group_add(public.mthd):
def get_doc(self, _):
return _('add new group')
api.register(group__add)
api.register(group_add)
class group__del(crud.Del):
class group_del(public.mthd):
def get_doc(self, _):
return _('delete existing group')
api.register(group__del)
api.register(group_del)
class group__mod(crud.Mod):
class group_mod(public.mthd):
def get_doc(self, _):
return _('exit existing group')
api.register(group__mod)
return _('edit existing group')
api.register(group_mod)
class group__find(crud.Find):
class group_find(public.mthd):
def get_doc(self, _):
return _('search for groups')
api.register(group__find)
api.register(group_find)
# Register some methods for the 'service' object
class service__add(crud.Add):
class service_add(public.mthd):
def get_doc(self, _):
return _('add new service')
api.register(service__add)
api.register(service_add)
class service__del(crud.Del):
class service_del(public.mthd):
def get_doc(self, _):
return _('delete existing service')
api.register(service__del)
api.register(service_del)
class service__mod(crud.Mod):
class service_mod(public.mthd):
def get_doc(self, _):
return _('edit existing service')
api.register(service__mod)
api.register(service_mod)
class service__find(crud.Find):
class service_find(public.mthd):
def get_doc(self, _):
return _('search for services')
api.register(service__find)
api.register(service_find)
# And to emphasis that the registration order doesn't matter,
# we'll register the objects last:
class group(base.Object):
class group(public.obj):
def get_doc(self, _):
return _('')
api.register(group)
class service(base.Object):
class service(public.obj):
def get_doc(self, _):
return _('')
api.register(service)
class user(base.Object):
class user(public.obj):
def get_doc(self, _):
return _('')
api.register(user)

View File

@@ -28,19 +28,23 @@ import plugable
class generic_proxy(plugable.Proxy):
__slots__ = (
'get_label',
'get_doc',
)
class cmd_proxy(plugable.Proxy):
__slots__ = (
'__call__',
'get_doc',
)
class cmd(plugable.Plugin):
proxy = cmd_proxy
def get_doc(self, _):
raise NotImplementedError('%s.get_doc()' % self.name)
def __call__(self, *args, **kw):
print repr(self)
@@ -87,7 +91,21 @@ class mthd(attr, cmd):
class prop(attr):
proxy = generic_proxy
def get_doc(self, _):
return _('prop doc')
class PublicAPI(plugable.API):
__max_cmd_len = None
def __init__(self):
super(PublicAPI, self).__init__(cmd, obj, prop)
def __get_max_cmd_len(self):
if self.__max_cmd_len is None:
if not hasattr(self, 'cmd'):
return None
max_cmd_len = max(len(str(cmd)) for cmd in self.cmd)
object.__setattr__(self, '_PublicAPI__max_cmd_len', max_cmd_len)
return self.__max_cmd_len
max_cmd_len = property(__get_max_cmd_len)

View File

@@ -69,7 +69,7 @@ def test_Plugin():
api = 'the api instance'
p = plugable.Plugin()
assert read_only(p, 'name') == 'Plugin'
assert repr(p) == '%s.Plugin()' % plugable.__name__
assert repr(p) == '%s.Plugin' % plugable.__name__
assert read_only(p, 'api') is None
raises(AssertionError, p.finalize, None)
p.finalize(api)
@@ -80,7 +80,7 @@ def test_Plugin():
pass
p = some_plugin()
assert read_only(p, 'name') == 'some_plugin'
assert repr(p) == '%s.some_plugin()' % __name__
assert repr(p) == '%s.some_plugin' % __name__
assert read_only(p, 'api') is None
raises(AssertionError, p.finalize, None)
p.finalize(api)