#!/usr/bin/env python3

import argparse
import os
from math import ceil


def split_line(line):
	line = line.strip().split(';')
	return ';'.join(line[:-1]), int(line[-1])


def splitters(path, nparts, outdir):
	with open(path) as f:
		x = sorted([split_line(l)[0] for l in f])
		res = []
		if not os.path.isdir(outdir):
			os.makedirs(outdir)
		for i in range(nparts):
			dr = os.path.join(outdir, str(i))
			os.makedirs(dr)
			res.append((x[i*ceil(len(x)/nparts)], dr))
		res[0] = '', res[0][1]
		return res


def open_splitters(splitters, file):
	return [(guard, open(os.path.join(dr, file), 'w')) for guard, dr in splitters]


def main():
	parser = argparse.ArgumentParser(description='split_files.py -- A script that splits files with dictionaries or tuples into a few directories of smaller files. Then each directory can be merged independently and all results can be easilly put together by `cat`.')
	parser.add_argument('--input', nargs='+', required=True, help='Input files.')
	parser.add_argument('--outdir', action='store',required=True, help='Output directories.')
	parser.add_argument('--parts', action='store',required=True, type=int, help='Into how many parts the files should be split.')

	args = parser.parse_args()
	spl = splitters(args.input[0], args.parts,args.outdir)
	
	cnt = 0
	for path in args.input:
		cnt += 1
		spl2 = open_splitters(spl, str(cnt) + '.dict')
		with open(path) as f:
			for line in f:
				key, value = split_line(line)
				for guard, output in reversed(spl2):
					if key >= guard:
						output.write(key + ';' + str(value) + '\n')
						break
				else:
					raise "!@#$%^&*()"
			

if __name__ == '__main__':
	main()



