2012-02-16 14:06:35 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
import os,re,sys,string,fnmatch
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2013-09-09 19:42:00 +05:30
|
|
|
scriptID = '$Id$'
|
2014-08-07 00:36:33 +05:30
|
|
|
scriptName = scriptID.split()[1][:-3]
|
2012-02-16 14:06:35 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2012-02-16 14:06:35 +05:30
|
|
|
Filter rows according to condition and columns by either white or black listing.
|
|
|
|
|
2012-12-03 19:59:21 +05:30
|
|
|
Examples:
|
|
|
|
Every odd row if x coordinate is positive -- " #ip.x# >= 0.0 and #_row_#%2 == 1 ).
|
2013-12-12 08:05:01 +05:30
|
|
|
All rows where label 'foo' equals 'bar' -- " #foo# == \"bar\" "
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
""", version = scriptID)
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
parser.add_option('-w','--white', dest='whitelist', action='extend', type='string', metavar='<string LIST>',
|
|
|
|
help='white list of column labels (a,b,c,...)')
|
|
|
|
parser.add_option('-b','--black', dest='blacklist', action='extend', type='string', metavar='<string LIST>',
|
|
|
|
help='black list of column labels (a,b,c,...)')
|
|
|
|
parser.add_option('-c','--condition', dest='condition', type='string', metavar='string',
|
|
|
|
help='condition to filter rows')
|
2012-02-16 14:06:35 +05:30
|
|
|
|
|
|
|
parser.set_defaults(whitelist = [])
|
|
|
|
parser.set_defaults(blacklist = [])
|
2012-02-17 00:17:07 +05:30
|
|
|
parser.set_defaults(condition = '')
|
2012-02-16 14:06:35 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
# ------------------------------------------ setup file handles ------------------------------------
|
2012-02-16 14:06:35 +05:30
|
|
|
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-02-16 14:06:35 +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':open(name+'_tmp','w'), 'croak':sys.stderr})
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
#--- loop over input files -------------------------------------------------------------------------
|
2012-02-16 14:06:35 +05:30
|
|
|
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')
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
table = damask.ASCIItable(file['input'],file['output'],False) # make unbuffered ASCII_table
|
|
|
|
table.head_read() # read ASCII header info
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
|
|
|
|
2012-02-16 14:06:35 +05:30
|
|
|
specials = { \
|
|
|
|
'_row_': 0,
|
|
|
|
}
|
|
|
|
labels = []
|
|
|
|
positions = []
|
|
|
|
for position,label in enumerate(table.labels):
|
2012-11-29 16:12:13 +05:30
|
|
|
if (options.whitelist == [] or any([fnmatch.fnmatch(label,needle) for needle in options.whitelist])) \
|
2014-08-07 00:36:33 +05:30
|
|
|
and (options.blacklist == [] or not any([fnmatch.fnmatch(label,needle) for needle in options.blacklist])): # a label to keep?
|
|
|
|
labels.append(label) # remember name...
|
|
|
|
positions.append(position) # ...and position
|
2012-02-16 14:06:35 +05:30
|
|
|
|
|
|
|
interpolator = []
|
2014-08-07 00:36:33 +05:30
|
|
|
for position,operand in enumerate(set(re.findall(r'#(([s]#)?(.+?))#',options.condition))): # find three groups
|
2012-12-03 19:59:21 +05:30
|
|
|
options.condition = options.condition.replace('#'+operand[0]+'#',
|
|
|
|
{ '': '{%i}'%position,
|
|
|
|
's#':'"{%i}"'%position}[operand[1]])
|
2014-08-07 00:36:33 +05:30
|
|
|
if operand[2] in specials: # special label ?
|
2012-12-03 19:59:21 +05:30
|
|
|
interpolator += ['specials["%s"]'%operand[2]]
|
2012-02-16 14:06:35 +05:30
|
|
|
else:
|
|
|
|
try:
|
2012-12-03 19:59:21 +05:30
|
|
|
interpolator += ['%s(table.data[%i])'%({ '':'float',
|
|
|
|
's#':'str'}[operand[1]],
|
|
|
|
table.labels.index(operand[2]))]
|
2012-02-16 14:06:35 +05:30
|
|
|
except:
|
2013-04-17 01:40:17 +05:30
|
|
|
parser.error('column %s not found...\n'%operand[2])
|
2012-02-16 14:06:35 +05:30
|
|
|
|
|
|
|
evaluator = "'" + options.condition + "'.format(" + ','.join(interpolator) + ")"
|
2012-12-03 19:59:21 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
table.labels = labels # update with new label set
|
2012-02-16 14:06:35 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2012-02-17 00:17:07 +05:30
|
|
|
outputAlive = True
|
2014-08-07 00:36:33 +05:30
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
|
|
|
specials['_row_'] += 1 # count row
|
|
|
|
if options.condition == '' or eval(eval(evaluator)): # valid row ?
|
|
|
|
table.data = [table.data[position] for position in positions] # retain filtered columns
|
|
|
|
outputAlive = table.data_write() # output processed line
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
|
|
|
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
2012-02-16 14:06:35 +05:30
|
|
|
|
2014-08-22 22:28:53 +05:30
|
|
|
table.input_close() # close input ASCII table (works for stdin)
|
|
|
|
table.output_close() # close output ASCII table (works for stdout)
|
2012-02-16 14:06:35 +05:30
|
|
|
if file['name'] != 'STDIN':
|
2014-08-07 00:36:33 +05:30
|
|
|
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|