Here is a fixed version that works on operating systems that are not Windows and ignores empty raw files which fixes the bug that was reported earlier.
from tkinter import *
import os
import fnmatch
from tkinter import tix
from tkinter import filedialog
dir1 = 'MasterworkDwarfFortress/raw/objects/'
dir2 = 'Dwarf Fortress/raw/objects/'
match = ['creature*','item*','inorganic*','reaction*','plant*','entity*']
files = []
for m in match:
for file in os.listdir(dir1):
if fnmatch.fnmatch(file, m):
files.append(file)
totdat = [[]]*len(files)
for i in range(len(files)):
f = open(dir1+files[i], mode="rt", buffering=1, encoding="ISO-8859-1")
dat = []
for row in f:
dat.append(row)
totdat[i] = dat
f.close()
ddtot = [[]]*len(totdat)
for j in range(len(totdat)):
d = []
for i in range(len(totdat[j])):
par = totdat[j][i].partition(':')[0]
if (par == '[CREATURE') or (par == '!!!CREATURE'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[REACTION') or (par == '!!!REACTION'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[INORGANIC') or (par == '!!!INORGANIC'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[PLANT') or (par == '!!!PLANT'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_AMMO') or (par == '!!!ITEM_AMMO'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_ARMOR') or (par == '!!!ITEM_ARMOR'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_FOOD') or (par == '!!!ITEM_FOOD'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_GLOVES') or (par == '!!!ITEM_GLOVES'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_HELM') or (par == '!!!ITEM_HELM'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_INSTRUMENT') or (par == '!!!ITEM_INSTRUMENT'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_PANTS') or (par == '!!!ITEM_PANTS'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_SHIELD') or (par == '!!!ITEM_SHIELD'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_SHOES') or (par == '!!!ITEM_SHOES'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_SIEGEAMMO') or (par == '!!!ITEM_SIEGEAMMO'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_TOOL') or (par == '!!!ITEM_TOOL'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_TOY') or (par == '!!!ITEM_TOY'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_TRAPCOMP') or (par == '!!!ITEM_TRAPCOMP'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
elif (par == '[ITEM_WEAPON') or (par == '!!!ITEM_WEAPON'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!1') != 0:
d[-1][2] = 'off'
elif (par == '[ENTITY') or (par == '!!!ENTITY'):
d.append([totdat[j][i].partition(':')[2].partition(']')[0], i, 'on'])
if par.count('!!!') != 0:
d[-1][2] = 'off'
if len(d) != 0:
dd = []
for i in range(len(d)-1):
dd.append([d[i][0], d[i][1], d[i+1][1], d[i][2]])
dd.append([d[-1][0], d[-1][1], len(totdat[j]), d[-1][2]])
ddtot[j] = dd
if os.path.exists('configuration.txt'):
pass
else:
for i in range(len(files)):
A = files[i].partition('_')[0]
B = files[i].partition('_')[2].partition('.')[0]
for j in range(len(ddtot[i])):
C = ddtot[i][j][0]
ddtot[i][j][0] = A+'.'+B+'.'+C
def save():
fn = filedialog.asksaveasfilename()
wf = open(fn, 'w')
for i in range(len(ddtot)):
for j in range(len(ddtot[i])):
wf.write(ddtot[i][j][0]+'#'+ddtot[i][j][3]+'\n')
wf.close()
def load():
fn = filedialog.askopenfilename()
wf = open(fn)
dat = []
for row in wf:
dat.append(row)
wf.close()
for i in range(len(dat)):
for j in range(len(ddtot)):
for k in range(len(ddtot[j])):
if dat[i].partition('#')[0] == ddtot[j][k][0]:
ddtot[j][k][3] = dat[i].partition('#')[2].partition('\n')[0]
def write():
for i in range(len(files)):
wf = open(dir2 + files[i], 'w')
for k in range(ddtot[i][0][1]):
wf.write(totdat[i][k])
for j in range(len(ddtot[i])):
for z in range(ddtot[i][j][1],ddtot[i][j][2]):
if ddtot[i][j][3] == 'off':
wf.write(totdat[i][z].replace('[','!!!'))
elif ddtot[i][j][3] == 'on':
wf.write(totdat[i][z].replace('!!!','['))
wf.close()
if __name__ == '__main__':
root = tix.Tk()
canvas = tix.Canvas(root, width = 500, height=500)
canvas.grid(row=0, column=0, sticky=N+S+E+W)
root.grid_rowconfigure(0,weight=1)
frame = tix.Frame(canvas)
frame.rowconfigure(1,weight=1)
frame.columnconfigure(1,weight=1)
class states:
def __init__(self,ty):
self.t =Toplevel(root)
self.ty = ty
self.top = []
self.ord = []
self.sub = []
self.tot = []
self.base = []
self.makelist()
cls = tix.Button(self.t, text='Close', command=self.close)
cls.grid()
def close(self):
self.t.destroy()
def update(self,item):
if len(item.split('.')) == 2:
for i in range(len(ddtot)):
for j in range(len(ddtot[i])):
val = ddtot[i][j][0].split('.')[0]+'.'+ddtot[i][j][0].split('.')[1]
if val == item:
if self.cl.getstatus(item) == 'off':
self.cl.setstatus(ddtot[i][j][0], 'off')
ddtot[i][j][3] = 'off'
elif self.cl.getstatus(item) == 'on':
self.cl.setstatus(ddtot[i][j][0], 'on')
ddtot[i][j][3] = 'on'
elif len(item.split('.')) == 3:
for i in range(len(ddtot)):
for j in range(len(ddtot[i])):
if item == ddtot[i][j][0]:
ddtot[i][j][3] = self.cl.getstatus(item)
def update2(self,item):
if len(item.split('.')) == 2:
meti = item.partition('.')[2]
for i in range(len(self.ord)):
if self.ord[i].count(meti) != 0:
for j in range(len(self.sub)):
if (self.ord[i][1] == self.sub[j][0]) and (self.ord[i][0] == item.partition('.')[0]):
temp = self.ord[i][0]+'.'+self.ord[i][1]+'.'+self.sub[j][1]
if self.cl.getstatus(item) == 'off':
self.cl.setstatus(temp, 'off')
elif self.cl.getstatus(item) == 'on':
self.cl.setstatus(temp, 'on')
for k in range(len(ddtot)):
for l in range(len(ddtot[k])):
if ddtot[k][l][0] == temp:
ddtot[k][l][3] = self.cl.getstatus(temp)
elif len(item.split('.')) == 3:
for i in range(len(ddtot)):
for j in range(len(ddtot[i])):
if item == ddtot[i][j][0]:
ddtot[i][j][3] = self.cl.getstatus(item)
def makelist(self):
if os.path.exists('configuration.txt'):
self.cl = tix.CheckList(self.t, width=500, height=400, browsecmd=self.update2)
self.cl.grid()
f = open('configuration.txt')
conf = []
for row in f:
conf.append(row)
f.close()
for i in range(len(conf)):
if conf[i].partition(' ')[0] == 'BASE':
self.base.append([conf[i].partition(' ')[2], i, 0])
for i in range(len(self.base)-1):
self.base[i][2] = self.base[i+1][1]
self.base[-1][2] = len(conf)
for vl in self.base:
if vl[0].partition('\n')[0] == self.ty.upper():
for val in conf[vl[1]:vl[2]]:
if val.partition(' ')[0] == 'TOP':
self.top.append(val.partition(' ')[2].partition('\n')[0])
self.cl.hlist.add(self.top[-1], text=self.top[-1])
if val.partition(' ')[0] == 'ORD':
self.ord.append([self.top[-1], val.partition(' ')[2].partition('\n')[0]])
self.cl.hlist.add(self.top[-1]+'.'+self.ord[-1][1], text=self.ord[-1][1])
self.cl.setstatus(self.top[-1]+'.'+self.ord[-1][1], 'on')
if val.partition(' ')[0] == 'SUB':
self.sub.append([self.ord[-1][1],val.partition(' ')[2].partition('\n')[0]])
self.cl.hlist.add(self.ord[-1][0]+'.'+self.ord[-1][1]+'.'+self.sub[-1][1], text=self.sub[-1][1])
self.tot.append(self.ord[-1][0]+'.'+self.ord[-1][1]+'.'+self.sub[-1][1])
self.cl.autosetmode()
for i in range(len(ddtot)):
for j in range(len(ddtot[i])):
for k in range(len(self.sub)):
if self.sub[k][1] == ddtot[i][j][0]:
ddtot[i][j][0] = self.tot[k]
self.cl.setstatus(ddtot[i][j][0], ddtot[i][j][3])
elif ddtot[i][j][0] == self.tot[k]:
self.cl.setstatus(ddtot[i][j][0], ddtot[i][j][3])
else:
self.cl = tix.CheckList(self.t, width=500, height=400, browsecmd=self.update)
self.cl.grid()
self.cl.hlist.add(self.ty, text=self.ty + 's')
for i in range(len(files)):
if files[i].partition('_')[0] == self.ty:
self.top.append([files[i].partition('_')[0],files[i].partition('_')[2].partition('.')[0]])
self.cl.hlist.add(self.top[-1][0] +'.'+ self.top[-1][1], text=self.top[-1][1])
self.cl.setstatus(self.top[-1][0] +'.'+ self.top[-1][1], "on")
for j in range(len(ddtot[i])):
if ddtot[i][j][0].partition('.')[0] == self.top[-1][0]:
self.cl.hlist.add(ddtot[i][j][0], text=ddtot[i][j][0].partition('.')[2].partition('.')[2])
else:
self.sub.append(ddtot[i][j][0])
self.cl.hlist.add(self.top[-1][0] +'.'+ self.top[-1][1] + '.' + self.sub[-1], text=ddtot[i][j][0])
ddtot[i][j][0] = self.top[-1][0] +'.'+ self.top[-1][1] + '.' + self.sub[-1]
self.cl.setstatus(ddtot[i][j][0], ddtot[i][j][3])
self.cl.autosetmode()
canvas.create_window(0,0,anchor=NW,window=frame)
frame.update_idletasks()
canvas.config(scrollregion=canvas.bbox('all'))
def creatures():
states('creature')
def items():
states('item')
def reactions():
states('reaction')
def entities():
states('entity')
def inorganics():
states('inorganic')
def plants():
states('plant')
########################################################################
if os.path.exists('metatags.txt'):
def func(q):
if textvar[q].get() == txt[q] + ' Off':
for i in range(len(ddtot)):
for j in range(len(ddtot[i])):
for k in range(ddtot[i][j][1],ddtot[i][j][2]):
totdat[i][k] = totdat[i][k].replace('NO'+tag[q]+'![','NO'+tag[q]+'!!')
totdat[i][k] = totdat[i][k].replace('YES'+tag[q]+'!!','YES'+tag[q]+'![')
textvar[q].set(txt[q] + ' On')
elif textvar[q].get() == txt[q] + ' On':
for i in range(len(ddtot)):
for j in range(len(ddtot[i])):
for k in range(ddtot[i][j][1],ddtot[i][j][2]):
totdat[i][k] = totdat[i][k].replace('NO'+tag[q]+'!!','NO'+tag[q]+'![')
totdat[i][k] = totdat[i][k].replace('YES'+tag[q]+'![','YES'+tag[q]+'!!')
textvar[q].set(txt[q] + ' Off')
f = open('metatags.txt')
tags = []
txt = []
tag = []
textvar = []
b = []
z = 0
for row in f:
tags.append(row)
f.close()
for val in tags:
txt.append(val.partition('/')[2].partition('\n')[0])
tag.append(val.partition('/')[0])
textvar.append(StringVar())
textvar[z].set(txt[z] + ' On')
b.append(tix.Button(frame, textvariable=textvar[z], command=lambda z=z:func(z)))
b[z].grid()
z = z + 1
########################################################################
b1 = tix.Button(frame, text='Creatures', command=creatures)
b2 = tix.Button(frame, text='Reactions', command=reactions)
b3 = tix.Button(frame, text='Entities', command=entities)
b4 = tix.Button(frame, text='Items', command=items)
b5 = tix.Button(frame, text='Plants', command=plants)
b6 = tix.Button(frame, text='Inorganics', command=inorganics)
b7 = tix.Button(frame, text='Load', command=load)
b8 = tix.Button(frame, text='Save', command=save)
b9 = tix.Button(frame, text='Write', command=write)
b1.grid()
b2.grid()
b3.grid()
b4.grid()
b5.grid()
b6.grid()
b7.grid()
b8.grid()
b9.grid()
root.update()
root.mainloop()