enhance --select, add --no-header option
This commit is contained in:
parent
ce27e88049
commit
e4b789d340
6 changed files with 33 additions and 22 deletions
|
|
@ -65,18 +65,23 @@ class ConsoleApplication(BaseApplication):
|
|||
results_options.add_option('-s', '--select', help='select result item key(s) to display (comma-separated)')
|
||||
self._parser.add_option_group(results_options)
|
||||
|
||||
formatting_options = OptionGroup(self._parser, 'Formatting Options')
|
||||
formatting_options.add_option('--no-header', dest='no_header', action='store_true', help='display only objects fields')
|
||||
self._parser.add_option_group(formatting_options)
|
||||
|
||||
def add_application_options(self, group):
|
||||
pass
|
||||
|
||||
def _handle_app_options(self):
|
||||
self.formatter = formatters[self.options.formatter]
|
||||
|
||||
if self.options.no_header:
|
||||
self.formatter.display_header = False
|
||||
|
||||
if self.options.select:
|
||||
self.formatter.display_keys = False
|
||||
if self.options.select == '*':
|
||||
self.selected_fields = None
|
||||
else:
|
||||
self.selected_fields = self.options.select.split(',')
|
||||
self.selected_fields = self.options.select.split(',')
|
||||
if '*' not in self.selected_fields:
|
||||
self.formatter.display_keys = False
|
||||
else:
|
||||
self.selected_fields = None
|
||||
|
||||
|
|
@ -240,6 +245,7 @@ class ConsoleApplication(BaseApplication):
|
|||
super(ConsoleApplication, klass).run(args)
|
||||
except BackendNotFound, e:
|
||||
logging.error(e)
|
||||
|
||||
def do(self, function, *args, **kwargs):
|
||||
kwargs['required_fields'] = self.options.select
|
||||
return self.weboob.do(function, *args, **kwargs)
|
||||
|
|
|
|||
|
|
@ -16,15 +16,16 @@
|
|||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
|
||||
|
||||
import types
|
||||
from weboob.tools.misc import iter_fields
|
||||
|
||||
|
||||
__all__ = ['IFormatter']
|
||||
|
||||
|
||||
class IFormatter(object):
|
||||
def __init__(self, display_keys=True, return_only=False):
|
||||
def __init__(self, display_keys=True, display_header=True, return_only=False):
|
||||
self.display_keys = display_keys
|
||||
self.display_header = display_header
|
||||
self.return_only = return_only
|
||||
|
||||
def after_format(self, formatted):
|
||||
|
|
@ -68,14 +69,6 @@ class IFormatter(object):
|
|||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def iter_fields(self, obj):
|
||||
for attribute_name in dir(obj):
|
||||
if attribute_name.startswith('_'):
|
||||
continue
|
||||
attribute = getattr(obj, attribute_name)
|
||||
if not isinstance(attribute, types.MethodType):
|
||||
yield attribute_name, attribute
|
||||
|
||||
def set_header(self, string):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
|
@ -89,12 +82,12 @@ class IFormatter(object):
|
|||
else:
|
||||
id_fields = ('id',)
|
||||
for k, v in d:
|
||||
if selected_fields is not None and k not in selected_fields:
|
||||
if selected_fields is not None and '*' not in selected_fields and k not in selected_fields:
|
||||
continue
|
||||
if k in id_fields and backend_name is not None:
|
||||
v = u'%s@%s' % (unicode(v), backend_name)
|
||||
yield k, v
|
||||
fields_iterator = obj.iter_fields() if hasattr(obj, 'iter_fields') else self.iter_fields(obj)
|
||||
fields_iterator = obj.iter_fields() if hasattr(obj, 'iter_fields') else iter_fields(obj)
|
||||
d = dict((k, v) for k, v in iter_select_and_decorate(fields_iterator))
|
||||
if condition is not None and not condition.is_valid(d):
|
||||
d = None
|
||||
|
|
|
|||
|
|
@ -38,4 +38,5 @@ class MultilineFormatter(IFormatter):
|
|||
return u'\n'.join(u'%s%s' % ((u'%s%s' % (k, self.key_value_separator) if self.display_keys else ''), v) for k, v in item.iteritems()) + self.after_item
|
||||
|
||||
def set_header(self, string):
|
||||
print string.encode('utf-8')
|
||||
if self.display_header:
|
||||
print string.encode('utf-8')
|
||||
|
|
|
|||
|
|
@ -38,4 +38,5 @@ class SimpleFormatter(IFormatter):
|
|||
return self.field_separator.join(u'%s%s' % ((u'%s%s' % (k, self.key_value_separator) if self.display_keys else ''), v) for k, v in item.iteritems())
|
||||
|
||||
def set_header(self, string):
|
||||
print string.encode('utf-8')
|
||||
if self.display_header:
|
||||
print string.encode('utf-8')
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ class TableFormatter(IFormatter):
|
|||
if self.column_headers is None:
|
||||
return None
|
||||
s = ''
|
||||
if self.header:
|
||||
if self.display_header and self.header:
|
||||
if self.result_funcname == 'get_string':
|
||||
s += self.header
|
||||
elif self.result_funcname == 'get_html_string':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue