#! python

import re
import sys
from gmisclib import avio
from gmisclib import dictops

def read(f, **kv):
	d, c = avio.read(open(f, 'r'))
	for datum in d:
		for k in list(datum.keys()):
			datum.update(kv)
			if k.startswith('C:'):
				c, dist, p = k.split(':')
				if dist == '*':
					continue
				kprime = ':'.join(('s', dist, p))
				sigma = float(datum.pop(kprime))
				datum['I:' + k[2:]] = float(datum[k])*sigma
	return d



def langmap(f):
	LMAP = {re.compile('French'): 'French', re.compile('English'): 'English',
		re.compile('Russian'): 'Russian', re.compile('Greek'): 'Greek',
		re.compile('Chinese'): 'Mandarin'
		}
	for (k, v) in LMAP.items():
		if k.search(f):
			return v
	raise ValueError, "Cannot match filename=%s to language" % f


def read_many(flist):
	out = []
	for f in flist:
		d = read(f, language=langmap(f), _FILENAME=f)
		out.extend(d)
	return out


if __name__ == '__main__':
	read(sys.argv[1])
