2012-11-30 13:56:13 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-11-30 13:56:13 +05:30
|
|
|
|
|
|
|
import os,sys,string,damask
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2013-09-09 19:42:00 +05:30
|
|
|
scriptID = '$Id$'
|
|
|
|
scriptName = scriptID.split()[1]
|
2012-11-30 13:56:13 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(usage='%prog [options] [file[s]]', description = """
|
|
|
|
Show components of given ASCIItable(s).
|
2013-09-09 19:42:00 +05:30
|
|
|
""" + string.replace(scriptID,'\n','\\n')
|
2012-11-30 13:56:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
parser.add_option('-a','--head', dest='head', action='store_true', help='output all heading (info + labels)')
|
|
|
|
parser.add_option('-i','--info', dest='info', action='store_true', help='output info lines')
|
|
|
|
parser.add_option('-l','--labels', dest='labels', action='store_true', help='output labels')
|
|
|
|
parser.add_option('-d','--data', dest='data', action='store_true', help='output data')
|
2012-11-30 13:59:34 +05:30
|
|
|
parser.add_option('-c','--column', dest='col', action='store_true', help='switch to label column format')
|
2013-07-08 17:26:16 +05:30
|
|
|
parser.add_option('--nolabels', dest='nolabels', action='store_true', help='table has no labels')
|
2012-11-30 13:56:13 +05:30
|
|
|
|
2012-11-30 13:59:34 +05:30
|
|
|
parser.set_defaults(col = False)
|
2013-07-08 17:26:16 +05:30
|
|
|
parser.set_defaults(nolabels = False)
|
2012-11-30 13:56:13 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------ setup file handles ---------------------------------------
|
|
|
|
|
|
|
|
files = []
|
|
|
|
if filenames == []:
|
2013-09-09 19:42:00 +05:30
|
|
|
files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr})
|
2012-11-30 13:56:13 +05:30
|
|
|
else:
|
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
2013-09-09 19:42:00 +05:30
|
|
|
files.append({'name':name, 'input':open(name), 'output':sys.stdout, 'croak':sys.stderr})
|
2012-11-30 13:56:13 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ extract labels ---------------------------------------
|
|
|
|
|
|
|
|
for file in files:
|
2013-09-09 19:42:00 +05:30
|
|
|
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
|
|
|
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
|
|
|
|
2013-07-08 17:26:16 +05:30
|
|
|
table = damask.ASCIItable(file['input'],file['output'],buffered=False,labels=not options.nolabels) # make unbuffered ASCII_table
|
2012-11-30 13:56:13 +05:30
|
|
|
table.head_read() # read ASCII header info
|
|
|
|
if options.head or options.info: file['output'].write('\n'.join(table.info)+'\n')
|
2012-11-30 13:59:34 +05:30
|
|
|
if options.head or options.labels: file['output'].write({True:'\n',False:'\t'}[options.col].join(table.labels)+'\n')
|
2014-01-30 02:43:36 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output data ---------------------------------------
|
|
|
|
|
|
|
|
outputAlive = options.data
|
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
|
|
|
outputAlive = table.data_write() # output line
|
2012-11-30 13:56:13 +05:30
|
|
|
|
2014-01-30 02:46:44 +05:30
|
|
|
outputAlive and table.output_flush()
|
2012-11-30 13:56:13 +05:30
|
|
|
|
2012-11-30 16:33:53 +05:30
|
|
|
if file['name'] != 'STDIN':
|
2012-11-30 13:56:13 +05:30
|
|
|
file['input'].close()
|