#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, codecs, argparse

def load_file(filename, data):
	with codecs.open(filename, 'r', 'utf8') as infile:
		for line in infile:
			line = line.strip()
			try:
				l,r,p,c = line.split(';')
				count = int(c)
				t = '%s;%s;%s' % (l,r,p)
				if t in data:
					data[t] += count
				else:
					data[t] = count
			except:
				print "Wrong line:'%s', skipping" % (line,)

def main():
	parser = argparse.ArgumentParser(description='merge_files.py -- A script for merging and accumulating multiple files with tuples')
	parser.add_argument('--indir', action='store', required=True, help='Input directory with tuples')
	parser.add_argument('--outfile', action='store',required=True, help='Output file')

	args = parser.parse_args()

	data = {} # tuple <l,r,p> -> count
	for (dirpath, dirnames, filenames) in os.walk(args.indir):
		for filename in sorted(filenames):
			print "Parsing", os.path.join(dirpath, filename)
			load_file(os.path.join(dirpath,filename), data)

	print "Writing", args.outfile
	with codecs.open(args.outfile, 'w', 'utf8') as outfile:
		for t in data:
			outfile.write('%s;%s\n' % (t,data[t]) )
	print "All done"

if __name__ == '__main__':
	main()



