diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-08-05 22:21:57 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-08-05 22:21:57 +0000 |
commit | 159207514fadfacb6e1df9713abd2c61c24d7b77 (patch) | |
tree | d49c3f592c50c2564defd48ea2e086bd1809d88d | |
parent | 1fce1487f9ba85aeee36178c4efadbc983b612cc (diff) | |
download | freeipa-159207514fadfacb6e1df9713abd2c61c24d7b77.tar.gz freeipa-159207514fadfacb6e1df9713abd2c61c24d7b77.tar.xz freeipa-159207514fadfacb6e1df9713abd2c61c24d7b77.zip |
52: Got cli working against new framework
-rwxr-xr-x | ipa | 39 | ||||
-rw-r--r-- | ipalib/plugable.py | 5 | ||||
-rw-r--r-- | ipalib/plugins.py | 75 | ||||
-rw-r--r-- | ipalib/public.py | 20 | ||||
-rw-r--r-- | ipalib/tests/test_plugable.py | 4 |
5 files changed, 77 insertions, 66 deletions
@@ -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]() diff --git a/ipalib/plugable.py b/ipalib/plugable.py index 8f2cbc27a..2c5fd18c6 100644 --- a/ipalib/plugable.py +++ b/ipalib/plugable.py @@ -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): diff --git a/ipalib/plugins.py b/ipalib/plugins.py index 85f3a9f46..7495acc24 100644 --- a/ipalib/plugins.py +++ b/ipalib/plugins.py @@ -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) diff --git a/ipalib/public.py b/ipalib/public.py index b9f858fb9..3bcf697fd 100644 --- a/ipalib/public.py +++ b/ipalib/public.py @@ -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) diff --git a/ipalib/tests/test_plugable.py b/ipalib/tests/test_plugable.py index 1fa34bd56..a9a6492d1 100644 --- a/ipalib/tests/test_plugable.py +++ b/ipalib/tests/test_plugable.py @@ -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) |