summaryrefslogtreecommitdiffstats
path: root/ipalib/cli.py
diff options
context:
space:
mode:
authorPavel Zuna <pzuna@redhat.com>2010-02-17 15:46:43 +0100
committerRob Crittenden <rcritten@redhat.com>2010-02-17 10:56:06 -0500
commit03967f62e9787b27d7f91684ec76c688529ff5bd (patch)
tree3dd10871fc79233d87e4c9f2a22bd809324b2176 /ipalib/cli.py
parenteab1e7cd252055eecfbc3d7a400fab027801d292 (diff)
downloadfreeipa-03967f62e9787b27d7f91684ec76c688529ff5bd.tar.gz
freeipa-03967f62e9787b27d7f91684ec76c688529ff5bd.tar.xz
freeipa-03967f62e9787b27d7f91684ec76c688529ff5bd.zip
Auto-generate --all and --raw for commands, that return entries.
Diffstat (limited to 'ipalib/cli.py')
-rw-r--r--ipalib/cli.py60
1 files changed, 34 insertions, 26 deletions
diff --git a/ipalib/cli.py b/ipalib/cli.py
index 0cd51a88e..b03af3b93 100644
--- a/ipalib/cli.py
+++ b/ipalib/cli.py
@@ -249,7 +249,7 @@ class textui(backend.Backend):
for (key, value) in rows:
self.print_indented('%s = %r' % (key, self.encode_binary(value)), indent)
- def print_attribute(self, attr, value, indent=1, one_value_per_line=True):
+ def print_attribute(self, attr, value, format='%s: %s', indent=1, one_value_per_line=True):
"""
Print an ldap attribute.
@@ -269,12 +269,12 @@ class textui(backend.Backend):
assert isinstance(attr, basestring)
if not isinstance(value, (list, tuple)):
# single-value attribute
- self.print_indented('%s: %s' % (attr, self.encode_binary(value)), indent)
+ self.print_indented(format % (attr, self.encode_binary(value)), indent)
else:
# multi-value attribute
if one_value_per_line:
for v in value:
- self.print_indented('%s: %s' % (attr, self.encode_binary(v)), indent)
+ self.print_indented(format % (attr, self.encode_binary(v)), indent)
else:
value = map(lambda v: self.encode_binary(v), value)
text = ', '.join(value)
@@ -289,7 +289,7 @@ class textui(backend.Backend):
)
else:
text = [text]
- self.print_indented('%s: %s' % (attr, text[0]), indent)
+ self.print_indented(format % (attr, text[0]), indent)
for line in text[1:]:
self.print_plain('%s%s' % (s_indent, line))
@@ -328,41 +328,49 @@ class textui(backend.Backend):
for attr in sorted(entry):
print_attr(attr)
- def print_entries(self, entries, params=None, format='%s: %s', indent=1):
+ def print_entries(self, entries, order=None, labels=None, print_all=False, format='%s: %s', indent=1):
assert isinstance(entries, (list, tuple))
first = True
for entry in entries:
if not first:
print ''
first = False
- self.print_entry(entry, params, format, indent)
+ self.print_entry(entry, order, labels, print_all, format, indent)
- def print_entry(self, entry, params=None, format='%s: %s', indent=1):
+ def print_entry(self, entry, order=None, labels=None, print_all=False, format='%s: %s', indent=1):
"""
"""
if isinstance(entry, (list, tuple)):
entry = dict(entry)
assert isinstance(entry, dict)
- if params:
- order = list(params)
- labels = dict((p.name, p.label) for p in params())
+ if labels is None:
+ labels = dict()
+ one_value_per_line = True
else:
- order = sorted(entry)
- labels = dict((k, k) for k in order)
- for key in order:
- if key not in entry:
- continue
- label = labels[key]
- value = entry[key]
- if isinstance(value, (list, tuple)):
- value = map(lambda v: self.encode_binary(v), value)
- value = ', '.join(value)
- if isinstance(value, dict):
- self.print_indented(format % (label, ''), indent)
- self.print_entry(value, params, indent=indent+1)
- else:
- self.print_indented(format % (label, value), indent)
-
+ one_value_per_line = False
+ if order is not None:
+ for key in order:
+ if key not in entry:
+ continue
+ label = labels.get(key, key)
+ value = entry[key]
+ if isinstance(value, dict):
+ self.print_indented(format % (label, ''), indent)
+ self.print_entry(
+ value, order, labels, print_all, format,
+ indent=indent+1
+ )
+ else:
+ self.print_attribute(
+ label, value, format, indent, one_value_per_line
+ )
+ del entry[key]
+ if print_all:
+ for key in sorted(entry):
+ label = labels.get(key, key)
+ self.print_attribute(
+ key, entry[key], format, indent, one_value_per_line
+ )
def print_dashed(self, string, above=True, below=True, indent=0, dash='-'):
"""