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)')
|
results_options.add_option('-s', '--select', help='select result item key(s) to display (comma-separated)')
|
||||||
self._parser.add_option_group(results_options)
|
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):
|
def add_application_options(self, group):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def _handle_app_options(self):
|
def _handle_app_options(self):
|
||||||
self.formatter = formatters[self.options.formatter]
|
self.formatter = formatters[self.options.formatter]
|
||||||
|
|
||||||
|
if self.options.no_header:
|
||||||
|
self.formatter.display_header = False
|
||||||
|
|
||||||
if self.options.select:
|
if self.options.select:
|
||||||
self.formatter.display_keys = False
|
self.selected_fields = self.options.select.split(',')
|
||||||
if self.options.select == '*':
|
if '*' not in self.selected_fields:
|
||||||
self.selected_fields = None
|
self.formatter.display_keys = False
|
||||||
else:
|
|
||||||
self.selected_fields = self.options.select.split(',')
|
|
||||||
else:
|
else:
|
||||||
self.selected_fields = None
|
self.selected_fields = None
|
||||||
|
|
||||||
|
|
@ -240,6 +245,7 @@ class ConsoleApplication(BaseApplication):
|
||||||
super(ConsoleApplication, klass).run(args)
|
super(ConsoleApplication, klass).run(args)
|
||||||
except BackendNotFound, e:
|
except BackendNotFound, e:
|
||||||
logging.error(e)
|
logging.error(e)
|
||||||
|
|
||||||
def do(self, function, *args, **kwargs):
|
def do(self, function, *args, **kwargs):
|
||||||
kwargs['required_fields'] = self.options.select
|
kwargs['required_fields'] = self.options.select
|
||||||
return self.weboob.do(function, *args, **kwargs)
|
return self.weboob.do(function, *args, **kwargs)
|
||||||
|
|
|
||||||
|
|
@ -16,15 +16,16 @@
|
||||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
|
|
||||||
import types
|
from weboob.tools.misc import iter_fields
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['IFormatter']
|
__all__ = ['IFormatter']
|
||||||
|
|
||||||
|
|
||||||
class IFormatter(object):
|
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_keys = display_keys
|
||||||
|
self.display_header = display_header
|
||||||
self.return_only = return_only
|
self.return_only = return_only
|
||||||
|
|
||||||
def after_format(self, formatted):
|
def after_format(self, formatted):
|
||||||
|
|
@ -68,14 +69,6 @@ class IFormatter(object):
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
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):
|
def set_header(self, string):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
@ -89,12 +82,12 @@ class IFormatter(object):
|
||||||
else:
|
else:
|
||||||
id_fields = ('id',)
|
id_fields = ('id',)
|
||||||
for k, v in d:
|
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
|
continue
|
||||||
if k in id_fields and backend_name is not None:
|
if k in id_fields and backend_name is not None:
|
||||||
v = u'%s@%s' % (unicode(v), backend_name)
|
v = u'%s@%s' % (unicode(v), backend_name)
|
||||||
yield k, v
|
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))
|
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):
|
if condition is not None and not condition.is_valid(d):
|
||||||
d = None
|
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
|
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):
|
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())
|
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):
|
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:
|
if self.column_headers is None:
|
||||||
return None
|
return None
|
||||||
s = ''
|
s = ''
|
||||||
if self.header:
|
if self.display_header and self.header:
|
||||||
if self.result_funcname == 'get_string':
|
if self.result_funcname == 'get_string':
|
||||||
s += self.header
|
s += self.header
|
||||||
elif self.result_funcname == 'get_html_string':
|
elif self.result_funcname == 'get_html_string':
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,13 @@
|
||||||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
|
|
||||||
|
from dateutil import tz
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
from dateutil import tz
|
import types
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['toUnicode', 'local2utc', 'html2text', 'get_backtrace']
|
__all__ = ['toUnicode', 'local2utc', 'html2text', 'get_backtrace', 'iter_fields']
|
||||||
|
|
||||||
|
|
||||||
def toUnicode(text):
|
def toUnicode(text):
|
||||||
|
|
@ -77,3 +78,12 @@ def get_backtrace(empty="Empty backtrace."):
|
||||||
# No i18n here (imagine if i18n function calls error...)
|
# No i18n here (imagine if i18n function calls error...)
|
||||||
return "Error while trying to get backtrace"
|
return "Error while trying to get backtrace"
|
||||||
return empty
|
return empty
|
||||||
|
|
||||||
|
def iter_fields(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
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue