#old dos orcad netlist repair program.
#Input: old.net SDT netlist, rpt.rpt netcompare report file
#Output: new.net repaired netlist, dict.txt change list.

import re
def replace_words(text, word_dic):
    rc = re.compile('|'.join(map(re.escape, word_dic)))
    def translate(match):
        return word_dic[match.group(0)]
    return rc.sub(translate, text)

fh = open('rpt.rpt')
x=0
word_dic = {}
while True:
    line = fh.readline()
    if (line.find('NET-netname(') is not -1) and (line.find('BRD-netname(') is not -1):
        s1='N'+line[25:30]
        s2='N'+line[57:62]
        if not s1 in word_dic:
            word_dic[s1]=s2
            x=x+1
    if not line:
        break
fh.close()
print (x)

fout = open("dict.txt", "w")
for key in sorted(word_dic):
    str1=str(key)+','+str(word_dic[key])+'\n'
    fout.write(str1)
fout.close()

fin = open("old.net", "r")
str2 = fin.read()
fin.close() 
str3 = replace_words(str2, word_dic)
fout = open("new.net", "w")
fout.write(str3)
fout.close()
