Files
deb-python-requestbuilder/requestbuilder/mixins.py
2013-02-20 18:02:43 -08:00

43 lines
1.7 KiB
Python

# Copyright (c) 2012-2013, Eucalyptus Systems, Inc.
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from . import Arg
from .command import BaseCommand
class TabifyingCommand(BaseCommand):
'''
A request mixin that provides the tabify() function along with its
associated --show-empty-fields command line arg.
'''
ARGS = [Arg('--show-empty-fields', action='store_true', route_to=None,
help='show empty values as "(nil)"')]
def tabify(self, fields, include=None):
'''
Join a list of strings with tabs. Nonzero items that Python considers
false are printed as-is if they appear in the include list, replaced
with '(nil)' if the user specifies --show-empty-fields at the command
line, and omitted otherwise.
'''
def allowable(item):
return bool(item) or item is 0 or item in (include or [])
if self.args['show_empty_fields']:
fstr = '(nil)'
else:
fstr = ''
return '\t'.join([str(f) if allowable(f) else fstr for f in fields])