mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-01-23 14:53:12 -06:00
55: Cleaned up print_api() function in ipa script
This commit is contained in:
parent
c6f69e1c66
commit
277685439c
85
ipa
85
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,11 +87,20 @@ def print_help(cmd):
|
||||
|
||||
|
||||
|
||||
def iter_ns(tab, name):
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def print_api():
|
||||
def iter_api(tab):
|
||||
for name in api:
|
||||
ns = getattr(api, name)
|
||||
yield row(
|
||||
tab,
|
||||
'%d %s:' % (len(ns), name)
|
||||
name,
|
||||
repr(ns),
|
||||
)
|
||||
for i in ns:
|
||||
yield row(
|
||||
@ -100,14 +109,50 @@ def iter_ns(tab, 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_api():
|
||||
rows = []
|
||||
for name in ['cmd', 'obj', 'mthd', 'prop']:
|
||||
rows.extend(iter_ns(0, 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)
|
||||
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user