#!/usr/bin/python

import sys
import os.path
import time
import re

INPUT_SIZE = 40
OUTPUT_SIZE = 2

LINE_SIZE = 4 
HEADER = '<TICKER>,<PER>,<DATE>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>'
REGEX = '[^,]*,[^,]*,[^,]*,[^,]*,([^,]*),([^,]*),([^,]*),([^,]*),[^,]*'

STEP = 5

PROPAGATION_RANGE_BOTTOM=1.2
PROPAGATION_RANGE_TOP=1.4

PREPARE_DATA_SIZE = INPUT_SIZE * PROPAGATION_RANGE_TOP;

# -- The function must return input and output data for test.
#    Data is a list with all the data needed for the test.
#    Function returns input and output data as a string in format
#    "IN: x.xxx ... \nOUT: x.xxx ..."
#
def prepare(data):
	if (len(data) >= PREPARE_DATA_SIZE) :
		buff = 'IN: ';
		# Take the high value in the last line as a normalization value
		one = float(data[INPUT_SIZE - LINE_SIZE + 1]);
		# fill the input data
		for string in data[0:INPUT_SIZE] :
			value = float(string);
			buff += repr( value / one ) + ' ';
		buff += '\nOUT: ';
		# now determine min and max values in propagation range for the output
		left = int(PROPAGATION_RANGE_BOTTOM * INPUT_SIZE);
		right = int(PROPAGATION_RANGE_TOP * INPUT_SIZE);
		min_value = float(data[left]);
		max_value = float(data[left]);
		for string in data[left:right] :
			value = float(string);
			if value > max_value :
				max_value = value;
			if value < min_value :
				min_value = value;
		buff += repr( norm_output(min_value / one) ) + ' ' \
			+ repr( norm_output(max_value / one) );
		return buff;
	else :
		print "Error: wrong data size in prepare()";

def norm_output(value) :
	if value < 2 :
		return value * 0.5;
	else :
		return 2.0;

# -- Function that builds tests
#
def write_tests(in_file, out_file, header):
	test = 0;
	input_lines = INPUT_SIZE / LINE_SIZE;
	buff = [];

	for line in in_file :
		if len(buff) < PREPARE_DATA_SIZE :
			m = re.match(REGEX, line.replace('\r\n',''));
			if m != None :
				for grp in m.groups() :
					buff.append(grp);
		else :
			out_file.write('\n# Test case No. ' + repr(test) + '\n');
			test += 1;
			out_file.write( prepare(buff) );
			buff = buff[STEP:];
	return

# -- Test cases formatting function
#
def write_header(in_file, out_file, header):
	out_file.write('# Generated by script : ' + sys.argv[0] + ' on ' + time.asctime() + '\n');
	out_file.write('FILENAME: ' + sys.argv[1] + '\n');
	out_file.write('INPUT_SIZE: ' + repr(INPUT_SIZE) + '\n');
	out_file.write('OUTPUT_SIZE: ' + repr(OUTPUT_SIZE) + '\n');
	return

# -- Main function
#
def main(argv) :

	if len(argv) < 2 :
		print 'Usage ' + argv[0] + ' <filename>'
		return

	if not os.path.isfile(argv[1]) :
		print 'File not found: ' + argv[1];		

	in_file = open(argv[1],'r');
	header = in_file.readline().replace('\r\n', '');
	print ' file ' + argv[1] + ' with header [' + header + ']';
	out_filename = argv[1] + '.tsc';

	if header != HEADER :
		print 'Unsupported header!'
		return

	print 'Test cases will be written to "' + out_filename + '"';
	try :
		out_file = open(argv[1] + '.tsc', 'w')
		write_header(in_file, out_file, header)
		write_tests(in_file, out_file, header)
	except IOError :
		print 'Exception occured while trying to open file for writing';
	finally :
		out_file.close()
	in_file.close()

	return;

# --------------
#
	
if __name__ == "__main__" :
	main(sys.argv)

