cleaner solution to output to a file
This commit is contained in:
parent
1f73bf5a11
commit
1382b50a7d
2 changed files with 23 additions and 28 deletions
|
|
@ -55,30 +55,36 @@ class IFormatter(object):
|
|||
|
||||
MANDATORY_FIELDS = None
|
||||
|
||||
def __init__(self, display_keys=True, display_header=True, return_only=False):
|
||||
def __init__(self, display_keys=True, display_header=True, return_only=False, outfile=sys.stdout):
|
||||
self.display_keys = display_keys
|
||||
self.display_header = display_header
|
||||
self.return_only = return_only
|
||||
self.interactive = False
|
||||
self.print_lines = 0
|
||||
self.termrows = 0
|
||||
self.outfile = outfile
|
||||
# XXX if stdin is not a tty, it seems that the command fails.
|
||||
if os.isatty(sys.stdout.fileno()) and os.isatty(sys.stdin.fileno()):
|
||||
self.termrows = int(os.popen('stty size', 'r').read().split()[0])
|
||||
|
||||
def after_format(self, formatted):
|
||||
for line in formatted.split('\n'):
|
||||
if self.termrows and (self.print_lines + 1) >= self.termrows:
|
||||
sys.stdout.write(PROMPT)
|
||||
sys.stdout.flush()
|
||||
readch()
|
||||
sys.stdout.write('\b \b' * len(PROMPT))
|
||||
self.print_lines = 0
|
||||
if self.outfile != sys.stdout:
|
||||
with open(self.outfile, "a+") as outfile:
|
||||
outfile.write(formatted.encode('utf-8'))
|
||||
|
||||
if isinstance(line, unicode):
|
||||
line = line.encode('utf-8')
|
||||
print line
|
||||
self.print_lines += 1
|
||||
else:
|
||||
for line in formatted.split('\n'):
|
||||
if self.termrows and (self.print_lines + 1) >= self.termrows:
|
||||
self.outfile.write(PROMPT)
|
||||
self.outfile.flush()
|
||||
readch()
|
||||
self.outfile.write('\b \b' * len(PROMPT))
|
||||
self.print_lines = 0
|
||||
|
||||
if isinstance(line, unicode):
|
||||
line = line.encode('utf-8')
|
||||
print line
|
||||
self.print_lines += 1
|
||||
|
||||
def build_id(self, v, backend_name):
|
||||
return u'%s@%s' % (unicode(v), backend_name)
|
||||
|
|
|
|||
|
|
@ -144,6 +144,7 @@ class ReplApplication(Cmd, BaseApplication):
|
|||
help='select output formatter (%s)' % u', '.join(available_formatters))
|
||||
formatting_options.add_option('--no-header', dest='no_header', action='store_true', help='do not display header')
|
||||
formatting_options.add_option('--no-keys', dest='no_keys', action='store_true', help='do not display item keys')
|
||||
formatting_options.add_option('--outfile', dest='outfile', help='file to export result')
|
||||
self._parser.add_option_group(formatting_options)
|
||||
|
||||
self._interactive = False
|
||||
|
|
@ -1067,6 +1068,8 @@ class ReplApplication(Cmd, BaseApplication):
|
|||
self.formatter.display_header = False
|
||||
if self.options.no_keys:
|
||||
self.formatter.display_keys = False
|
||||
if self.options.outfile:
|
||||
self.formatter.outfile = self.options.outfile
|
||||
if self.interactive:
|
||||
self.formatter.interactive = True
|
||||
return name
|
||||
|
|
@ -1075,30 +1078,16 @@ class ReplApplication(Cmd, BaseApplication):
|
|||
self.formatter.set_header(string)
|
||||
|
||||
def format(self, result, output=sys.stdout):
|
||||
saveout = sys.stdout
|
||||
try :
|
||||
fsock = open(output, 'w')
|
||||
except TypeError:
|
||||
if output == None:
|
||||
raise OutputIsNone("output is None")
|
||||
elif output == sys.stdout:
|
||||
fsock = sys.stdout
|
||||
else:
|
||||
raise
|
||||
sys.stdout = fsock
|
||||
fields = self.selected_fields
|
||||
if fields in ('$direct', '$full'):
|
||||
fields = None
|
||||
self.formatter.output = output
|
||||
try:
|
||||
self.formatter.format(obj=result, selected_fields=fields)
|
||||
except FieldNotFound, e:
|
||||
print e
|
||||
except MandatoryFieldsNotFound, e:
|
||||
print >> sys.stderr, '%s Hint: select missing fields or use another formatter (ex: multiline).' % e
|
||||
sys.stdout = saveout
|
||||
if fsock != sys.stdout:
|
||||
fsock.close()
|
||||
|
||||
def flush(self):
|
||||
self.formatter.flush()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue