summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2008-08-06 03:27:00 +0000
committerJason Gerard DeRose <jderose@redhat.com>2008-08-06 03:27:00 +0000
commit277685439c91f496df9510e02418da01160df0ea (patch)
treedf4e1ac59611a3ea0fa22779cd01a5e81c4dca58
parentc6f69e1c66b86f8f375a3c561922a42fdc0b1afb (diff)
downloadfreeipa-277685439c91f496df9510e02418da01160df0ea.tar.gz
freeipa-277685439c91f496df9510e02418da01160df0ea.tar.xz
freeipa-277685439c91f496df9510e02418da01160df0ea.zip
55: Cleaned up print_api() function in ipa script
-rwxr-xr-xipa103
-rw-r--r--ipalib/plugable.py19
2 files changed, 91 insertions, 31 deletions
diff --git a/ipa b/ipa
index 77bdb6ce..5606b373 100755
--- a/ipa
+++ b/ipa
@@ -39,7 +39,7 @@ def _(msg):
class row(object):
def __init__(self, tab, c1, c2=None):
assert type(tab) is int
- assert type(c1) is str
+ assert type(c1) in (str, int)
assert type(c2) is str or c2 is None
self.tab = tab
self.c1 = c1
@@ -48,27 +48,27 @@ class row(object):
def __len__(self):
return len(str(self.c1))
- def pretty_print(self, ljust):
+ def pretty_print(self, just):
tab = ' ' * (self.tab * TAB_WIDTH)
if self.c2 is None:
print '%s%s' % (tab, self.c1)
else:
- print '%s%s %s' % (tab, self.c1.ljust(ljust), self.c2)
+ if type(self.c1) is int:
+ c1 = str(self.c1).rjust(just)
+ else:
+ c1 = self.c1.ljust(just)
+ print '%s%s %s' % (tab, c1, self.c2)
def pretty_print(rows):
- def at_tab(tab):
+ rows = tuple(rows)
+ def get_lengths():
+ yield 0
for r in rows:
- if r.tab == tab:
+ if r.c2 is not None:
yield len(r)
-
- _max_len = {}
- def max_len(tab):
- if tab not in _max_len:
- _max_len[tab] = max(at_tab(tab))
- return _max_len[tab]
-
+ max_len = max(get_lengths())
for r in rows:
- r.pretty_print(max_len(r.tab))
+ r.pretty_print(max_len)
def print_commands():
@@ -87,27 +87,72 @@ def print_help(cmd):
-def iter_ns(tab, name):
- ns = getattr(api, name)
- yield row(
- tab,
- '%d %s:' % (len(ns), name)
- )
- for i in ns:
- yield row(
- tab + 1,
- i.name,
- repr(i)
- )
+
+
+
def print_api():
- rows = []
- for name in ['cmd', 'obj', 'mthd', 'prop']:
- rows.extend(iter_ns(0, name))
+ def iter_api(tab):
+ for name in api:
+ ns = getattr(api, name)
+ yield row(
+ tab,
+ name,
+ repr(ns),
+ )
+ for i in ns:
+ yield row(
+ tab + 1,
+ i.name,
+ repr(i)
+ )
+
+ def iter_obj(tab):
+ for obj in api.obj:
+ yield row(
+ tab,
+ obj.name,
+ repr(obj),
+ )
+ for (n, f) in [('mthd', '.%s()'), ('prop', '.%s')]:
+ ns = getattr(obj, n)
+ yield row(
+ tab + 1,
+ n,
+ repr(ns),
+ )
+ for attr in ns:
+ yield row(
+ tab + 2,
+ f % attr.name,
+ repr(attr),
+ )
+
+ def iter_summary(tab):
+ for name in api:
+ ns = getattr(api, name)
+ yield row(
+ tab,
+ len(ns),
+ name
+ )
+
+ def print_heading(h):
+ print '\n%s:' % h
+ print '-' * (len(h) + 1)
+
+ tab = 1
+ print_heading('API Overview')
+ pretty_print(iter_api(tab))
+
+ print_heading('Object Details')
+ pretty_print(iter_obj(tab))
+
+ print_heading('Summary')
+ pretty_print(iter_summary(tab))
- pretty_print(rows)
diff --git a/ipalib/plugable.py b/ipalib/plugable.py
index 6e6c6973..e017a8a4 100644
--- a/ipalib/plugable.py
+++ b/ipalib/plugable.py
@@ -175,12 +175,13 @@ class NameSpace(ReadOnly):
__max_len = None
- def __init__(self, items):
+ def __init__(self, items, base=None):
"""
`items` should be an iterable providing the members of this
NameSpace.
"""
object.__setattr__(self, '_NameSpace__items', tuple(items))
+ object.__setattr__(self, '_NameSpace__base', base)
# dict mapping Python name to item:
object.__setattr__(self, '_NameSpace__pname', {})
@@ -234,6 +235,14 @@ class NameSpace(ReadOnly):
object.__setattr__(self, '_NameSpace__max_len', ml)
return self.__max_len
+ def __repr__(self):
+ if self.__base is None:
+ base = repr(self.__base)
+ else:
+ base = '%s.%s' % (self.__base.__module__, self.__base.__name__)
+ return '%s(*proxies, base=%s)' % (self.__class__.__name__, base)
+
+
@@ -329,6 +338,8 @@ class Registrar(object):
class API(ReadOnly):
def __init__(self, *allowed):
+ keys = tuple(b.__name__ for b in allowed)
+ object.__setattr__(self, '_API__keys', keys)
object.__setattr__(self, 'register', Registrar(*allowed))
object.__setattr__(self, '_API__plugins', [])
@@ -337,7 +348,7 @@ class API(ReadOnly):
Finalize the registration, instantiate the plugins.
"""
for (base, plugins) in self.register:
- ns = NameSpace(self.__plugin_iter(base, plugins))
+ ns = NameSpace(self.__plugin_iter(base, plugins), base=base)
assert not hasattr(self, base.__name__)
object.__setattr__(self, base.__name__, ns)
for plugin in self.__plugins:
@@ -350,3 +361,7 @@ class API(ReadOnly):
plugin = cls()
self.__plugins.append(plugin)
yield base.proxy(plugin)
+
+ def __iter__(self):
+ for key in self.__keys:
+ yield key