Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from sys import argv
- from multiprocessing import Process
- import os
- script, directory = argv
- #def convert_to_tmlearn(file, target):
- # line = target.read()
- # print "Read Line: %s" % (line)
- # return
- dirs = os.listdir(directory)
- def find_line(line, linelength):
- line
- linelength = linelength + len(line)
- line = target.readline(linelength)
- return (line, linelength)
- def reorganize_tms(buffer2,buffer3,num_ones_double,past_tm10_flag):
- # for some reason, this code won't work if used in a while true loop
- multiplier = 2
- if num_ones_double == 0:
- buffer3 = "0 "
- return (buffer3,multiplier)
- if past_tm10_flag == True:
- multiplier = 3
- buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
- buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
- num_ones_double -= 1
- buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
- buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
- num_ones_double -= 1
- buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
- buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
- num_ones_double -= 1
- buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
- buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
- num_ones_double -= 1
- buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
- buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
- num_ones_double -= 1
- buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
- buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
- num_ones_double -= 1
- buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
- buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
- num_ones_double -= 1
- buffer3 += buffer2[num_ones_double*multiplier - multiplier:]
- buffer2 = buffer2[:num_ones_double*multiplier - multiplier]
- num_ones_double -= 1
- return (buffer3,multiplier)
- def convert_binary_to_tm_no(multiple_of_eight,line,past_tm10_flag,ifdef_flag):
- line_buffer = ""
- buffer2 = ""
- some_count = 0
- num_ones = 0
- tm09_flag = False
- past_tm10_flag = False
- for line in line:
- some_count += 1
- which_tm = (8 * (multiple_of_eight) + 9) - (some_count - 4)
- if line == "%":
- if ifdef_flag:
- line_buffer += " "
- some_count -= 1
- line_buffer += " tmlearn "
- elif line == "1":
- buffer2 += "%s," % (which_tm)
- if which_tm == 9:
- tm09_flag = True
- elif which_tm >= 10:
- past_tm10_flag = True
- num_ones += 1
- num_ones_double = num_ones
- buffer3 = ""
- (buffer3,multiplier) = reorganize_tms(buffer2,buffer3,num_ones_double,past_tm10_flag)
- tm09_thing = multiplier
- if tm09_flag == True:
- tm09_thing = 2
- line_buffer += buffer3[:tm09_thing+(num_ones-1)*multiplier-1] + "\n"
- ifdef_flag = False
- return (line_buffer,past_tm10_flag,ifdef_flag)
- for file in dirs:
- if file == "test2backup.py":
- continue
- target = open(file, 'rw+')
- line = target.readline()
- linelength = 0
- line0 = line
- while True:
- (line, linelength) = find_line(line, linelength)
- if line == "; learnset\n":
- break
- (line, linelength) = find_line(line, linelength)
- saved_linelength = linelength
- main_line = ""
- multiple_of_eight = 0
- past_tm10_flag = False
- ifdef_flag = False
- multiple_copy = 0
- completed_ifdef = True
- completed_flag = False
- if_flag = False
- else_flag = False
- print file
- while not completed_flag:
- if line == "ENDC\n":
- #print "we did it"
- multiple_copy = 0
- main_line += "ENDC\n"
- completed_ifdef = True
- else_flag = False
- (line, linelength) = find_line(line, linelength)
- if line != "db 0 ; padding\n":
- (line, linelength) = find_line(line, linelength)
- if line == "IF DEF(_YELLOW)\n":
- main_line += "IF DEF(_YELLOW)\n"
- (line, linelength) = find_line(line, linelength)
- ifdef_flag = True
- if_flag = True
- completed_ifdef = False
- multiple_copy = multiple_of_eight
- anotherFlag = True
- continue
- elif line == "ELSE\n":
- multiple_of_eight = multiple_copy
- if_flag = False
- else_flag = True
- #print "it worked!"
- main_line += "ELSE\n"
- (line, linelength) = find_line(line, linelength)
- ifdef_flag = True
- continue
- elif if_flag:
- ifdef_flag = True
- elif else_flag:
- ifdef_flag = True
- if multiple_of_eight >= 7 and completed_ifdef == True:
- completed_flag = True
- #(line, linelength) = find_line(line, linelength)
- continue
- (line_buffer,past_tm10_flag,ifdef_flag) = convert_binary_to_tm_no(multiple_of_eight,line,past_tm10_flag,ifdef_flag)
- main_line += line_buffer
- multiple_of_eight += 1
- (line, linelength) = find_line(line, linelength)
- if multiple_of_eight >= 12:
- sys.exit("rekt")
- target.seek(linelength)
- line2 = line
- main_line += line2
- #print main_line
- target.seek(saved_linelength)
- target.write(main_line)
- target.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement