try:
	import sys, itertools
except Exception as err:
	print("Error while importing libs! Reason:\n %s" %(err))

PATH_LIST_1 = "test_list1.txt"
PATH_LIST_2 = "test_list2.txt"
PATH_LIST_3 = "test_list3.txt"
PATH_DEST = "combined.lst"

def printf(mtext, mtype = 'warn'):
	############################################
	#	Print text w/ color
	#
	###########################################

	print(craft_msg(mtext, mtype))

def craft_msg(mtext, mtype = 'warn'):
	####################################################
	#	create text message with color
	#	bad: red
	#	warn: yellow
	#	good: light green
	#	This functions is using for Linux terminal only
	####################################################

	mtext = {
		'bad': '\033[91m{}\033[00m'.format(mtext),
		'warn': '\033[93m{}\033[00m'.format(mtext),
		'good': '\033[92m{}\033[00m'.format(mtext)
	}
	return (mtext[mtype])
	
def die(msg, error):
	printf(msg, "bad")
	printf(error, "bad")
	sys.exit(1)
	


def getfile(path_1, path_2, path_3):
	try:
		printf("Reading wordlist....")
		get_1, get_2, get_3 = open(path_1), open(path_2), open(path_3)
		return get_1, get_2, get_3
	except Exception as err:
		die("Error while opening wordlist", err)

			
def gen_word(path_1, path_2, path_3, path_final):
	# create new empty file
	try:
		printf("Creating new wordlist")
		open(path_final, 'w').write("")
	except Exception as err:
		die("Can not create new empty destination file", err)
		
	try:
		word_1, word_2, word_3 = getfile(path_1, path_2, path_3)
		printf("Opening complete", "good")
		
		word_1, word_2, word_3 = word_1.read().split("\n"),  word_2.read().split("\n"), word_3.read().split("\n")
		printf("Combining new wordlist")
		word_final = open(path_final, 'a')
		for char_1 in word_1:
			for char_2 in word_2:
				for char_3 in word_3:

					for char_final in itertools.product((char_1, char_2, char_3), repeat = 3):
						#print "".join(char_final)
						word_final.write("%s\n" %("".join(char_final)))
						
		printf("Create wordlist is done", "good")
	except Exception as err:
		die("Error while running", err)
	finally:
		printf("Cleaning resources")
		try:
			word_final.close()
		except:
			pass
		try:
			word_1.close()
		except:
			pass
		try:
			word_2.close()
		except:
			pass
		try:
			word_3.close()
		except:
			pass
			
if __name__ == "__main__":
	gen_word(PATH_LIST_1, PATH_LIST_2, PATH_LIST_3, PATH_DEST)
	printf("Script ended", "good")