#!/usr/bin/env python
# -*- coding: utf-8 -*-

import ConfigParser, argparse, subprocess, os.path, os
from ConfGenerator import ConfigurationGenerator

def init_config(path):
	'''
	Reading all options from config file and return as dictionary
	'''
	config = ConfigParser.RawConfigParser()
	config.read(path)
	result={}
	for section in config.sections():
		for option in config.options(section):
			line =  config.get(section, option)
			line = line.split(';')
			if len(line)>1:
				collection=[]
				for entry in line:
					collection.append(entry)
				result[option]=collection
			else:
				result[option]=line[0]
	return result 

def run_parallel(config, max_proc):
	'''
	Runing builder program for all configurations
	'''
	print "Running Parallel Builder..."
	command = "cat %s"
	lines = ["echo" , config]
	parallel=["parallel", "-k", "-j", max_proc, "--nice", "19", "--eta"]
	p1 = subprocess.Popen(lines, stdout=subprocess.PIPE)
	p2 = subprocess.Popen(parallel, stdin=p1.stdout, stdout=subprocess.PIPE)
	print(p2.stdout.read())
	p1.stdout.close()
	out = p2.communicate()
	print out
	print "Done"

def run_basedict(cfg):
	'''
	Runing basedict program for all corpuses
	'''
	print "Building Base Dictionary ..."
	
	corp_list = cfg['corpus']
	if type(corp_list).__name__!='list':
		corp_list=[corp_list]
	
	args=[]
	args = [cfg['basedict_bin'], '--tagset', cfg['tagset'], '--reader', 
		cfg['reader'],'--output', os.path.join(cfg['out_dir'], "dict.txt")]

	for corpus in corp_list:	
		args.append('--corpus')
		args.append(corpus)

	#Handling optional paramteres
	if cfg['posfilters']:
		posfilters = cfg['posfilters']
		if type(posfilters).__name__ !='list':
			posfilters = [posfilters]
		for pos in posfilters:
			args.append('--posfilters')
			args.append(pos)
	add_parameter(args, cfg['pos'], '--pos')
	add_parameter(args, cfg['disamb_only'], '--disamb-only')
	add_parameter(args, cfg['mintf'], '--mintf')

	#Running program
	print "Creating dictionary for corpus " + os.path.basename(corpus)
	p1 = subprocess.Popen(args)
	p1.wait()

	print "Done.."

def run_tuplesproc(cfg):
	'''
	Running tuplesproc program for directory where builder saved files with tuples
	'''
	print "Building SuperMatrix..."
	args=[]
	args = [cfg['tuplesproc_bin'], '--matrix-name', cfg['matrix_name'], '--input', os.path.join(cfg['out_dir'],"tuples"), 
		'--out', cfg['out_dir']]

	args.append('--dict')
	if cfg['dict'] and os.path.exists(cfg['dict']):
		args.append(cfg['dict'])
	else:
		args.append(os.path.join(cfg['out_dir'], "dict.txt"))
	
	#optional parameters
	if cfg['relations']:
		relations = cfg['relations']
		if type(relations).__name__ !='list':
			relations = [relations]
		for relation in relations:
			args.append('--relations')
			args.append(relation)
	add_parameter(args, cfg['matrix_description'], '--matrix-description')		
	add_parameter(args, cfg['tokens'], '--tokens')
	add_parameter(args, cfg['row_file'], '--row-file')
	add_parameter(args, cfg['column_file'], '--column-file')
	add_parameter(args, cfg['relation_file'], '--relation-file')
	add_parameter(args, cfg['row_min_tf'], '--row-min-tf')	
	add_parameter(args, cfg['col_min_tf'], '--col-min-tf')
	p1 = subprocess.Popen(args)
	p1.wait()

	print "Done..."

def add_parameter(args, parameter, name):
	if parameter:
		args.append(name)
		args.append(parameter)


def main(conf_path):
	cfg = init_config(conf_path)
	generator = ConfigurationGenerator()
	
	#Creating dictionaries for all corpuses	
	if not cfg['dict'] or not os.path.exists(cfg['dict']):
		run_basedict(cfg)

	#running builder in parallel
	builder_conf = generator.generate_configuration(cfg['builder_bin'], cfg['corpus'],
			cfg['operator'], cfg['reader'], cfg['out_dir'], 
			cfg['tagset'], 100000, True)
	run_parallel(builder_conf, cfg['max_jobs'])
	run_tuplesproc(cfg)

if __name__ == "__main__":
	parser = argparse.ArgumentParser(description='Creator')
	parser.add_argument('config_path', action="store",
		help="Path to the configuration file")
	args = parser.parse_args()
	main(args.config_path)
