From 277685439c91f496df9510e02418da01160df0ea Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Wed, 6 Aug 2008 03:27:00 +0000 Subject: 55: Cleaned up print_api() function in ipa script --- ipa | 103 ++++++++++++++++++++++++++++++++++++++--------------- ipalib/plugable.py | 19 ++++++++-- 2 files changed, 91 insertions(+), 31 deletions(-) diff --git a/ipa b/ipa index 77bdb6ce7..5606b3730 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 6e6c6973b..e017a8a42 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 -- cgit