from cPickle import load

def parse_solution_file(file_name, section=None,basis_compatible_encode_dict={}):
    """Convert the solution file into a dict of dicts.

    file_name:

    section: String.  'status', 'objective_value', 'variables', 'reduced_cost', 'pivot',
    or 'slack'.  If not None then just return the specific section.

    if a 'basis_compatible_encode_dict' of non-zero length is supplied, this function will assume you would like that type of encoded variable names.
    
    """
    file_format = 'soplex'
    basis_compatible_encode_dict_reverse_mapping = dict([(v, k) for k, v in basis_compatible_encode_dict.items()])
    basis_compatible_encode_dict = basis_compatible_encode_dict_reverse_mapping
    
    solution_dict = {}

    if file_format == 'soplex':
    	soplex17 = False
    	refinement_successful = False
        if section == 'status':
            fin = open(file_name)
            for line in fin:
            	if 'refinement finished: tolerances reached' in line:
            		refinement_successful = True
            	if 'SteepEx' in line:
            		soplex17 = True
                if 'Solution value is' in line:
                    line = line.split(':')[-1]
                    the_objective = line.strip();
                    status = 'OPTIMAL'
                    if soplex17==True:
                    	if refinement_successful==True:
                    		return status
                    else:
                    	return status
                if 'LP is infeasible' in line:
                	if soplex17 == False:
                		status = 'INFEASIBLE'
                    	return status
            return 'ERROR'        
       
       	soplex17 = False
    	refinement_successful = False
        in_file = open(file_name)
        record = False
        for line in in_file:
	    if 'refinement finished: tolerances reached' in line:
	        refinement_successful = True
	    if 'SteepEx' in line:
	        soplex17 = True
	    if section=='objective_value':
	        if 'Solution value is: ' in line:
                    if soplex17==False or (soplex17==True and refinement_successful==True):
	                return float(line[19:])
	            else:
	                return None
            if 'All other variables' in line and record == True:
                break
            if record == True:
                line = line.strip().split('\t')
                if len(basis_compatible_encode_dict) > 0:
                    variable_name = basis_compatible_encode_dict[line[0]]
                value = line[2]
                solution_dict[variable_name] = float(value)
            if 'Primal solution (name, id, value):' in line:
                record = True
                
        if soplex17==False or (soplex17==True and refinement_successful==True):
            return solution_dict
        else:
	        return {} # refinement tolerances not reached, so return nothing

if __name__ == '__main__':
    
    f = open('basis_compatible_encode_dict.pickle')
    basis_compatible_encode_dict = load(f)
    f.close()
    
    soplex_solution_file = 'e_coli_me_model_solution.stdout'
    
    human_readable_solution_file = 'human_readable_'+soplex_solution_file
    
    sol_dict = parse_solution_file(soplex_solution_file, section=None, basis_compatible_encode_dict=basis_compatible_encode_dict)

    fout = open(human_readable_solution_file,'w')
    for k,v in sol_dict.items():
        fout.write(k+'\t'+str(v)+'\n')
    fout.close()
    