'''
step 2 - format queries
'''
fIn = 'log.dilum_9_step_1.txt'
fOut = 'log.dilum_9_step_2.txt'

fdIn = open(fIn, 'r')
fdOut = open(fOut, 'w')

queries = 0
for line in fdIn:
    if '<' not in line: continue
    line = line.lower()
    if 'log:' in line: line = line.replace('log:', '')
    elif 'log.1308246253:' in line: line = line.replace('log.1308246253:', '')
    elif 'log.1303748492:' in line: line = line.replace('log.1303748492:', '')
    elif 'log.1306508935:' in line: line = line.replace('log.1306508935:', '')
    elif 'log.1295702352:' in line: line = line.replace('log.1295702352:', '')
    elif 'log.1295703000:' in line: line = line.replace('log.1295703000:', '')
    elif 'log.1295985621:' in line: line = line.replace('log.1295985621:', '')
    elif 'log.1296594179:' in line: line = line.replace('log.1296594179:', '')

    if '</request%3' in line: line = line.replace('</request%3', '</request>')
    elif '</request%' in line: line = line.replace('</request%', '</request>')
    elif line[-10:] == '</request\n': line += '>\n'
    elif line[-9:] == '</reques\n': line += 't>\n'
    elif line[-8:] == '</reque\n': line += 'st>\n'
    elif line[-6:] == '</req\n': line += 'uest>\n'
    elif line[-4:] == '</r\n': line += 'equest>\n'

    if '><' in line: line = line.replace('><', '>\n\t\t<')

    if '<request>' in line: 
        line = '<query>\n' + line
        queries += 1
    if '</request>' in line:
        line = '\t' + line + '</query>\n'

    fdOut.write(line)
fdIn.close()
fdOut.close()
