Auto-generate --all and --raw for commands, that return entries.

This commit is contained in:
Pavel Zuna
2010-02-17 15:46:43 +01:00
committed by Rob Crittenden
parent eab1e7cd25
commit 03967f62e9
4 changed files with 66 additions and 130 deletions

View File

@@ -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)
one_value_per_line = False
if order is not None:
for key in order:
if key not in entry:
continue
label = labels[key]
label = labels.get(key, 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)
self.print_entry(
value, order, labels, print_all, format,
indent=indent+1
)
else:
self.print_indented(format % (label, value), indent)
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='-'):
"""

View File

@@ -27,7 +27,7 @@ from base import lock, check_name, NameSpace
from plugable import Plugin
from parameters import create_param, parse_param_spec, Param, Str, Flag, Password
from util import make_repr
from output import Output
from output import Output, Entry, ListOfEntries
from text import _, ngettext
from errors import ZeroArgumentError, MaxArgumentError, OverlapError, RequiresRoot
@@ -778,6 +778,19 @@ class Command(HasParam):
"""
for option in self._get_param_iterable('options'):
yield option
for o in self.has_output:
if isinstance(o, (Entry, ListOfEntries)):
yield Flag('all',
cli_name='all',
doc='retrieve all attributes',
exclude='webui',
)
yield Flag('raw',
cli_name='raw',
doc='print entries as stored on the server',
exclude='webui',
)
return
def validate_output(self, output):
"""
@@ -817,12 +830,21 @@ class Command(HasParam):
continue
yield param
def output_for_cli(self, textui, output, *args, **options):
if not isinstance(output, dict):
return
result = output.get('result')
summary = output.get('summary')
order = [p.name for p in self.output_params()]
if options.get('all', False):
order.insert(0, 'dn')
print_all = True
else:
print_all = False
if options.get('raw', False):
labels = None
else:
labels = dict((p.name, p.label) for p in self.output_params())
for o in self.output:
if 'no_display' in self.output[o].flags:
@@ -830,9 +852,9 @@ class Command(HasParam):
result = output[o]
if isinstance(result, (tuple, list)):
textui.print_entries(result, self.output_params)
textui.print_entries(result, order, labels, print_all)
elif isinstance(result, dict):
textui.print_entry(result, self.output_params)
textui.print_entry(result, order, labels, print_all)
elif isinstance(result, unicode):
if o == 'summary':
textui.print_summary(result)
@@ -846,7 +868,6 @@ class Command(HasParam):
textui.print_count(result, '%s %%d' % self.output[o].doc)
class LocalOrRemote(Command):
"""
A command that is explicitly executed locally or remotely.

View File

@@ -135,16 +135,6 @@ class LDAPCreate(crud.Create):
"""
takes_options = (
Flag('raw',
cli_name='raw',
doc='print entries as they are stored in LDAP',
exclude='webui',
),
Flag('all',
cli_name='all',
doc='retrieve all attributes',
exclude='webui',
),
Str('addattr*', validate_add_attribute,
cli_name='addattr',
doc='Add an attribute/value pair. Format is attr=value',
@@ -242,18 +232,6 @@ class LDAPRetrieve(LDAPQuery):
"""
Retrieve an LDAP entry.
"""
takes_options = (
Flag('raw',
cli_name='raw',
doc='print entries as they are stored in LDAP',
),
Flag('all',
cli_name='all',
doc='retrieve all attributes',
),
)
has_output = output.standard_entry
def execute(self, *keys, **options):
@@ -295,16 +273,6 @@ class LDAPUpdate(LDAPQuery, crud.Update):
"""
takes_options = (
Flag('raw',
cli_name='raw',
doc='print entries as they are stored in LDAP',
exclude='webui',
),
Flag('all',
cli_name='all',
doc='retrieve all attributes',
exclude='webui',
),
Str('addattr*', validate_add_attribute,
cli_name='addattr',
doc='Add an attribute/value pair. Format is attr=value',
@@ -457,14 +425,6 @@ class LDAPModMember(LDAPQuery):
member_param_doc = 'comma-separated list of %s'
member_count_out = ('%i member processed.', '%i members processed.')
takes_options = (
Flag('raw',
cli_name='raw',
doc='print entries as they are stored in LDAP',
exclude='webui',
),
)
def get_options(self):
for option in super(LDAPModMember, self).get_options():
yield option
@@ -645,17 +605,6 @@ class LDAPSearch(crud.Search):
"""
Retrieve all LDAP entries matching the given criteria.
"""
takes_options = (
Flag('raw',
cli_name='raw',
doc='print entries as they are stored in LDAP',
),
Flag('all',
cli_name='all',
doc='retrieve all attributes',
),
)
def get_args(self):
for key in self.obj.get_ancestor_primary_keys():
yield key
@@ -664,13 +613,6 @@ class LDAPSearch(crud.Search):
def get_options(self):
for option in super(LDAPSearch, self).get_options():
yield option
if self.obj.uuid_attribute:
yield Str('%s?' % self.obj.uuid_attribute,
cli_name='uuid',
doc='unique identifier',
attribute=True,
query=True,
)
def execute(self, *args, **options):
ldap = self.obj.backend
@@ -716,13 +658,11 @@ class LDAPSearch(crud.Search):
self.post_callback(self, ldap, entries, truncated, *args, **options)
if not options.get('raw', False):
for i in xrange(len(entries)):
dn = self.obj.get_primary_key_from_dn(entries[i][0])
self.obj.convert_attribute_members(
entries[i][1], *args, **options
)
entries[i] = (dn, entries[i][1])
for e in entries:
self.obj.convert_attribute_members(e[1], *args, **options)
for e in entries:
e[1]['dn'] = e[0]
entries = tuple(e for (dn, e) in entries)
return dict(

View File

@@ -110,7 +110,6 @@ def _get_record_dn(ldap, zone, idnsname):
class dns(Object):
"""DNS zone/SOA record object."""
label = _('DNS')
takes_params = (
@@ -176,7 +175,6 @@ class dns_add(crud.Create):
"""
Create new DNS zone/SOA record.
"""
def execute(self, *args, **options):
ldap = self.Backend.ldap2
idnsname = args[0]
@@ -228,7 +226,6 @@ class dns_del(crud.Delete):
"""
Delete existing DNS zone/SOA record.
"""
def execute(self, *args, **options):
ldap = self.api.Backend.ldap2
idnsname = args[0]
@@ -264,7 +261,6 @@ class dns_mod(crud.Update):
"""
Modify DNS zone/SOA record.
"""
def execute(self, *args, **options):
ldap = self.api.Backend.ldap2
idnsname = args[0]
@@ -306,13 +302,6 @@ class dns_find(crud.Search):
"""
Search for DNS zones/SOA records.
"""
takes_options = (
Flag('all',
doc='retrieve all attributes',
),
)
def execute(self, term, **options):
ldap = self.api.Backend.ldap2
@@ -365,13 +354,6 @@ class dns_show(crud.Retrieve):
"""
Display DNS zone/SOA record.
"""
takes_options = (
Flag('all',
doc='retrieve all attributes',
),
)
def execute(self, idnsname, **options):
ldap = self.api.Backend.ldap2
@@ -404,7 +386,6 @@ class dns_enable(Command):
"""
Activate DNS zone.
"""
takes_args = (
Str('zone',
cli_name='zone',
@@ -440,7 +421,6 @@ class dns_disable(Command):
"""
Deactivate DNS zone.
"""
takes_args = (
Str('zone',
cli_name='zone',
@@ -476,7 +456,6 @@ class dns_add_rr(Command):
"""
Add new DNS resource record.
"""
takes_args = (
Str('zone',
cli_name='zone',
@@ -593,7 +572,6 @@ class dns_del_rr(Command):
"""
Delete DNS resource record.
"""
takes_args = (
Str('zone',
cli_name='zone',
@@ -681,7 +659,6 @@ class dns_find_rr(Command):
"""
Search for DNS resource records.
"""
takes_args = (
Str('zone',
cli_name='zone',
@@ -709,9 +686,6 @@ class dns_find_rr(Command):
cli_name='data',
doc='type-specific data',
),
Flag('all',
doc='retrieve all attributes',
),
)
has_output = output.standard_list_of_entries
@@ -811,7 +785,6 @@ class dns_show_rr(Command):
"""
Show existing DNS resource records.
"""
takes_args = (
Str('zone',
cli_name='zone',
@@ -825,12 +798,6 @@ class dns_show_rr(Command):
),
)
takes_options = (
Flag('all',
doc='retrieve all attributes',
),
)
has_output = output.standard_entry
def execute(self, zone, idnsname, **options):