26: Added AbstractCommand.get_doc() method to return the gettext translated summary of command; added get_doc() method to all example

This commit is contained in:
Jason Gerard DeRose 2008-07-21 01:44:59 +00:00
parent 48c7da47c7
commit 7273d48169
3 changed files with 95 additions and 32 deletions

37
ipa
View File

@ -21,37 +21,46 @@
""" """
Command Line Interface to IPA. Command Line Interface to IPA.
Just proof of concept stuff in here right now.
""" """
import sys import sys
from ipalib.startup import api from ipalib.startup import api
def _(msg):
"""
Dummy gettext function for testing.
"""
return msg
def print_commands(): def print_commands():
print 'Commands:' print 'Commands:'
m = api.max_cmd_len m = api.max_cmd_len
for c in api.commands: for cmd in api.commands():
c = c.replace('_', '-') print ' %s %s' % (cmd.cli_name.ljust(m), cmd.get_doc(_))
print ' %s The help on %s' % (c.ljust(m), c)
def print_help(cmd): def print_help(cmd):
print 'Help on %s' % cmd print 'Help on %s' % cmd
def print_api(): def print_api():
print '\nCommands:' print 'Commands:'
for n in api.commands: for cmd in api.commands():
print ' %s' % n print ' %s [%s]' % (cmd.name, cmd.loc)
print '\nObjects:' print 'Objects:'
for obj in api.objects(): for obj in api.objects():
print ' %s' % obj.name print ' %s [%s]' % (obj.name, obj.loc)
for n in obj.methods: for meth in obj.methods():
print ' .%s()' % n print ' .%s() [%s]' % (meth.attr_name, meth.loc)
for n in obj.properties: for prop in obj.properties():
print ' .%s' % n print ' .%s [%s]' % (prop.attr_name, prop.loc)
print '\nStats:' print 'Stats:'
print ' %d objects' % len(api.objects)
print ' %d commands' % len(api.commands) print ' %d commands' % len(api.commands)
print ' %d objects' % len(api.objects)
if len(sys.argv) < 2: if len(sys.argv) < 2:

View File

@ -95,7 +95,7 @@ class NameSpace(object):
""" """
Returns True if namespace has an item named `key`. Returns True if namespace has an item named `key`.
""" """
return key.replace('-', '_') in self.__kw return bool(key in self.__kw)
def __iter__(self): def __iter__(self):
""" """
@ -135,17 +135,44 @@ class Named(object):
def _get_name(self): def _get_name(self):
return self.__class__.__name__ return self.__class__.__name__
def __get_loc(self):
cls = self.__class__
return '%s.%s' % (cls.__module__, cls.__name__)
loc = property(__get_loc)
def __get_name(self): def __get_name(self):
if self.__name is None: if self.__name is None:
self.__name = self._get_name() self.__name = self._get_name()
return self.__name return self.__name
name = property(__get_name) name = property(__get_name)
def __get_cli_name(self):
return self.name.replace('_', '-')
cli_name = property(__get_cli_name)
class AbstractCommand(object): class AbstractCommand(object):
def __call__(self): def __call__(self):
print 'You called %s()' % self.name print 'You called %s()' % self.name
def get_doc(self, _):
"""
This should return a gettext translated summarary of the command.
For example, if you were documenting the 'add-user' command, you're
method would look something like this.
>>> def get_doc(self, _):
>>> return _('add new user')
"""
raise NotImplementedError('%s.%s.%s()' % (
self.__class__.__module__,
self.__class__.__name__,
'get_doc',
)
)
class Attribute(Named): class Attribute(Named):
__locked = False __locked = False
__obj = None __obj = None

View File

@ -26,21 +26,37 @@ import base
from run import api from run import api
# Hypothetical functional commands (not associated with any object):
class krbtest(base.Command):
def get_doc(self, _):
return _('test your Kerberos ticket')
api.register(krbtest)
class discover(base.Command):
def get_doc(self, _):
return _('discover IPA servers on network')
api.register(discover)
# Register some methods for the 'user' object: # Register some methods for the 'user' object:
class user__add(crud.Add): class user__add(crud.Add):
pass def get_doc(self, _):
return _('add new user')
api.register(user__add) api.register(user__add)
class user__del(crud.Del): class user__del(crud.Del):
pass def get_doc(self, _):
return _('delete existing user')
api.register(user__del) api.register(user__del)
class user__mod(crud.Mod): class user__mod(crud.Mod):
pass def get_doc(self, _):
return _('edit existing user')
api.register(user__mod) api.register(user__mod)
class user__find(crud.Find): class user__find(crud.Find):
pass def get_doc(self, _):
return _('search for users')
api.register(user__find) api.register(user__find)
@ -53,57 +69,68 @@ class user__lastname(base.Property):
pass pass
api.register(user__lastname) api.register(user__lastname)
class user__lastname(base.Property): class user__login(base.Property):
pass pass
api.register(user__lastname) api.register(user__login)
# Register some methods for the 'group' object: # Register some methods for the 'group' object:
class group__add(crud.Add): class group__add(crud.Add):
pass def get_doc(self, _):
return _('add new group')
api.register(group__add) api.register(group__add)
class group__del(crud.Del): class group__del(crud.Del):
pass def get_doc(self, _):
return _('delete existing group')
api.register(group__del) api.register(group__del)
class group__mod(crud.Mod): class group__mod(crud.Mod):
pass def get_doc(self, _):
return _('exit existing group')
api.register(group__mod) api.register(group__mod)
class group__find(crud.Find): class group__find(crud.Find):
pass def get_doc(self, _):
return _('search for groups')
api.register(group__find) api.register(group__find)
# Register some methods for the 'service' object # Register some methods for the 'service' object
class service__add(crud.Add): class service__add(crud.Add):
pass def get_doc(self, _):
return _('add new service')
api.register(service__add) api.register(service__add)
class service__del(crud.Del): class service__del(crud.Del):
pass def get_doc(self, _):
return _('delete existing service')
api.register(service__del) api.register(service__del)
class service__mod(crud.Mod): class service__mod(crud.Mod):
pass def get_doc(self, _):
return _('edit existing service')
api.register(service__mod) api.register(service__mod)
class service__find(crud.Find): class service__find(crud.Find):
pass 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, # And to emphasis that the registration order doesn't matter,
# we'll register the objects last: # we'll register the objects last:
class group(base.Object): class group(base.Object):
pass def get_doc(self, _):
return _('')
api.register(group) api.register(group)
class service(base.Object): class service(base.Object):
pass def get_doc(self, _):
return _('')
api.register(service) api.register(service)
class user(base.Object): class user(base.Object):
pass def get_doc(self, _):
return _('')
api.register(user) api.register(user)