Module select_fiat_entries
|
|
1
2
3 """This program reads a FIAT format data file
4 and displays some of the information.
5 You can select some of the lines with the
6 -select flag, and the display the selected lines
7 in several different ways.
8
9 Flags:
10 - -select expr Where expr is a python expression
11 that returns True or False. All the normal
12 builtins are available. The data in the
13 line is presented as a set of local variables,
14 so that if the value in the column labelled
15 "filename" is "f005.wav", then you could test
16 for 'filename=="f005.wav"'. Note that everything
17 is a string, so you may need to convert to int
18 or float before some tests: 'int(nspeakers)<5'.
19 - -showav Displays the entire line in a=v; format.
20 - -showfiat Displays the resulting file in FIAT format,
21 preserving the header information.
22 - -show format
23 This displays the file as a
24 text file, using evaluating format (which is a
25 snippet of python code that should return a string):
26 -show "'%s # %s'%(fname,loudness)"
27 will display the fname and loudness columns of the
28 selected lines, separated by a hash mark.
29 - -rav Reads the input file in a=v; format rather than fiat.
30
31 - -senv Exec some python code to set a global variable
32 that is visible in the -select expression.
33 (e.g. "x=3").
34 - -penv Exec some python code to set a global variable
35 that is visible in the -show expression.
36 (e.g. "x=3").
37 - -strap exception value
38 """
39
40 from gmisclib import die
41 from gmisclib import fiatio
42 from gmisclib import avio
43 from gmisclib import g2_select
44 from gmisclib import gpk_writer
45
46 SHOWAV = 1
47 SHOWFIAT = 2
48
49
50 -def process(f, read_fcn, o, select):
51 if f == '-':
52 fd = sys.stdin
53 else:
54 fd = open(f, 'r')
55
56 h1, d1 = read_fcn(fd)
57 o.headers(h1)
58
59 ne1 = 0
60 ne2 = 0
61 npr = 0
62 for (i, t) in enumerate(d1):
63 try:
64 ok = select.eval(t)
65 except NameError, x:
66 if ne1 == 0:
67 die.warn('NameError in selector: %s on item %d: %s' % (str(x), i+1, avio.concoct(t)))
68 ne1 += 1
69 ok = False
70 if not ok:
71 continue
72
73 try:
74 o.datum(t)
75 npr += 1
76 except NameError, x:
77 if ne2 == 0:
78 die.warn('NameError in show: %s on item %d: %s' % (str(x), i+1, avio.concoct(t)))
79 ne2 += 1
80 if ne1==len(d1):
81 die.warn('NameError in select on each of the %d data.' % len(d1))
82 if ne2>0 and npr == 0:
83 die.warn('NameError in show on each of the %d prints.' % ne2)
84
85
89
90
92 d, c = avio.read(fn)
93 return ({}, d)
94
95
97 """Look through a list of dictionaries, as one might get from fiatio.read
98 or avio.read, and pick out the dictionary that contains a specified
99 attribute=value pair.
100 """
101 for d in lod:
102 try:
103 if d[attribute] == value:
104 return d
105 except KeyError:
106 pass
107 return None
108
109
110 -class writer(gpk_writer.writer):
114
117
121
122
123 - def datum(self, data_item):
124 self.fd.write(str(self.s.eval(data_item)) + '\n')
125
128
129
130 if __name__ == '__main__':
131 import sys
132 arglist = sys.argv[1:]
133 select = g2_select.selector_c('True')
134 select.g['fiatio'] = fiatio
135 select.g['avio'] = avio
136 select.g['find'] = _find_lod
137 o = avio.writer(sys.stdout)
138 read_fcn = fiat_read
139 deferred_penv = []
140 n_senv = 1
141 while arglist and arglist[0].startswith('-'):
142 arg = arglist.pop(0)
143 if arg == '--':
144 break
145 elif arg == '-select':
146 select.set_code(arglist.pop(0))
147 elif arg == '-senv':
148 tmp = arglist.pop(0)
149 try:
150 select.globals(tmp)
151 except:
152 die.warn('Exception while executing -senv flag number %d: "%s"' %
153 (n_senv, g2_select._compact(tmp)))
154 raise
155 n_senv += 1
156 elif arg == '-strap':
157 select.set_trap(eval(arglist[0], select.g), eval(arglist[1], select.g))
158 arglist = arglist[2:]
159 elif arg == '-penv':
160 deferred_penv.append( arglist.pop(0) )
161 elif arg == '-show':
162 o = writer(sys.stdout, arglist.pop(0))
163 elif arg == '-showav':
164 o = avio.writer(sys.stdout)
165 elif arg == '-showfiat':
166 o = fiatio.writer(sys.stdout)
167 elif arg == '-rav':
168 read_fcn = avio_read
169 elif arg == '-rfiat':
170 read_fcn = fiatio_read
171 else:
172 die.die('Bad arg: %s' % arg)
173 if isinstance(o, writer):
174 n_penv = 1
175 for tmp in deferred_penv:
176 try:
177 o.globals( tmp )
178 except:
179 die.warn('Exception while executing -penv flag number %d: "%s"' %
180 (n_penv, g2_select._compact(tmp)))
181 raise
182 n_penv += 1
183 elif deferred_penv:
184 die.die("Flag -penv only makes sense with -show flag")
185
186 if len(arglist) < 1:
187 print __doc__
188 die.die('Not enough arguments')
189
190 for f in arglist:
191 process(f, read_fcn, o, select)
192