import os
import subprocess
import sys
import shutil
import xml.etree.ElementTree as ET
import string

#Generate command list from mkiv file
mkiv_path = sys.argv[1] if len(sys.argv) >= 2 else 'C:/context/tex/texmf-context/tex/context/modules/mkiv/'

cwd = os.getcwd()
os.chdir(mkiv_path)
if(os.path.exists('./x-setups-generate.mkiv')):
	subprocess.run('context x-setups-generate.mkiv')
else:
	print('Cannot find x-setups-generate.mkiv')
	print('Please specify the mkiv directory')
	print('Default direcotry is C:/context/tex/texmf-context/tex/context/modules/mkiv/')
	sys.exit()
shutil.copy2('./context-en.xml', cwd)
os.chdir(cwd)


'''
def finalize_keyword(command, nm, cd):
	seq = command.find(cd + 'sequence')
	instances = command.find(cd + 'instances')

	if seq == None:	# No part of keywords is user-defined
		return [nm]
	else:	# part of or the whole keyword is user-defined (or has some pre-defined keyword parts)
		# First find the part of keywords that needs to be replaced
		toReplace = seq.findall(cd + 'variable') + seq.findall(cd + 'instance')
		if len(toReplace) != 0:
			replacing_str = toReplace[0].get('value') # garuanteed only one value for replacing if seq is not None
		else:
			replacing_str = ''

		# Next find the pre-defined parts of the keywords that can be used for replacing
		const = instances.findall(cd + 'constant') if instances != None else None

		if const == None or len(const) == 0: # no pre-defined keyword parts
			return [nm.replace(replacing_str, replacing_str.upper())]
		else:
			names = []	# return a list of keywords
			for replace_ea in const:
				names.append(nm.replace(replacing_str, replace_ea.get('value')))
			return names
'''

# Parse xml file
tree = ET.parse('context-en.xml')
root = tree.getroot()
cd = '{http://www.pragma-ade.com/commands}'

cl_bg = set()	#section beginning keyword
cl_en = set()	#section ending keyword 
cl_n = set()	#other keywords

for command in root.iter(cd + 'command'):

	# process attributes, if type == 'environment', seperate to paired keywords
	nm = command.get('name')
	begin = command.get('begin')
	end = command.get('end')
	tp = command.get('type')
	seq = command.find(cd + 'sequence')

	if seq != None: # ignore abstract keywords
		continue
	else:
		if tp == 'environment':
			if begin != None:
				names_start = begin + nm #finalize_keyword(command, begin + nm, cd)
			else:
				names_start = 'start' + nm #finalize_keyword(command, 'start' + nm, cd)
			
			if end != None and all(c in string.printable for c in end): # incase end = ""
				names_stop = end + nm #finalize_keyword(command, end + nm, cd)
			elif end == None:
				names_stop = 'stop' + nm #finalize_keyword(command, 'stop' + nm, cd)
			else:
				names_stop =  nm #finalize_keyword(command, nm, cd)

			# for x in names_start:
			# 	cl_bg.add(x)
			# for x in names_stop:
			# 	cl_en.add(x)
			cl_bg.add(names_start)
			cl_en.add(names_stop)
		else:
			# names =  finalize_keyword(command, nm, cd)
			# for x in names:
			# 	cl_n.add(x)
			cl_n.add(nm)

# write final result
f = open('KEYWORDS.txt', 'w')
f.write('\n\n-----------------------------------------------Start-----------------------------------------\n\n')
f.write('\n\n-----------------------------------Keywords starting a session-------------------------------\n\n')
for x in sorted(cl_bg):
	f.write('\\' + x + '\t')
f.write('\n\n-----------------------------------Keywords ending a session---------------------------------\n\n')
for x in sorted(cl_en):
	f.write('\\' + x + '\t')
f.write('\n\n--------------------------------Keywords other than the 2 classes----------------------------\n\n')
for x in sorted(cl_n):
	f.write('\\' + x + '\t')
f.write('\n\n------------------------------------------------END--------------------------------------------\n')