was ihr hier seht ist bereits das endprodukt.. funzt!
nochmals danke an alle!
[/edit]
[edit2]
ich hab mal etwas weiter gebastelt also aktualisiere ich mal
probleme gibt es momentan keine
[/edit2]
Hallo,
nach längerer abstinenz melde ich mich auch mal wieder..
hab endlcih mal wieder zeit mit python weiter zu basteln..
leider scheitert es irgendwie bereits bei den einfachsten sachen..
ich hab mal versucht klassen zu benutzen
nunja.. das kam bei raus:
Code: Alles auswählen
#---------------------------------------------------------
# imports
import os
import re
import tkFileDialog
import tkMessageBox
import ScrolledText
import shutil
from Tkinter import *
class convertWindow:
#---------------------------------------------------------
# build gui
def __init__(self):
self.root=Tk()
frame=Frame(self.root)
frame.pack(expand=YES)
self.root.title('XML converter vol 1.0')
selectrahmen = Frame(self.root, relief='groove', borderwidth='2')
selectrahmen.pack(fill='x')
select1 = Frame(selectrahmen)
select1.pack(fill='x')
select2 = Frame(selectrahmen)
select2.pack(fill='x')
selectLab = Label(select1, text='Would you like to convert a single file or multiple files?')
selectLab.pack(side = 'top', anchor='w')
self.v = IntVar()
Radiobutton(select2, text='single', variable=self.v, value=1, command=self.sing).pack(side='left')
Radiobutton(select2, text='multiple', variable=self.v, value=2, command=self.mult).pack(side='left')
self.v.set('1')
eingaberahmen = Frame(self.root, relief='groove', borderwidth='2')
eingaberahmen.pack(fill='x')
suchDatei = Label(eingaberahmen, text = 'File: ')
suchDatei.grid(row=1,column=1)
self.datei = Entry(eingaberahmen, width=45)
self.datei.grid(row=1,column=2)
self.dat = Button(eingaberahmen, text = 'search', command=self.get_file)
self.dat.grid(row=1,column=3)
suchPfad = Label(eingaberahmen, text = 'Directory: ')
suchPfad.grid(row=2,column=1)
self.path = Entry(eingaberahmen, width=45, state='disabled')
self.path.grid(row=2,column=2)
self.direct = Button(eingaberahmen, text = 'search',state='disabled', command=self.get_dir)
self.direct.grid(row=2,column=3)
eingaberahmen2 = Frame(self.root)
eingaberahmen2.pack()
self.cancel = Button(eingaberahmen2, text = 'Cancel', command=self.can)
self.cancel.pack(side='left')
self.ok = Button(eingaberahmen2, text = 'OK', command=self.init)
self.ok.pack(side='left')
ausgaberahmen = Frame(self.root, relief='groove', borderwidth='2')
ausgaberahmen.pack(fill='x')
ausgabeLab = Label(ausgaberahmen, text = 'Messagescreen').pack(side = 'top', anchor='w')
self.ausgabe = ScrolledText.ScrolledText(ausgaberahmen, width=61, height=10, state='disabled', bg='white')
self.ausgabe.pack()
self.set_out('Please select a file for conversion.', 'y')
#---------------------------------------------------------
# get directory
def get_dir(self):
self.path.insert(0,tkFileDialog.askdirectory())
#---------------------------------------------------------
# get filepath
def get_file(self):
self.datei.insert(0,tkFileDialog.askopenfilename())
#---------------------------------------------------------
# cancel
def can(self):
old1 = self.path['state']
old2 = self.datei['state']
self.path['state'] = 'normal'
self.datei['state'] = 'normal'
self.path.delete(0,END)
self.datei.delete(0,END)
self.path['state'] = old1
self.datei['state'] = old2
self.v.set('1')
self.path['state'] = 'disabled'
self.direct['state'] = 'disabled'
self.datei['state'] = 'normal'
self.dat['state'] = 'normal'
self.set_out('Please select a file for conversion.', 'y')
#---------------------------------------------------------
# convert a single file window behaviour
def sing(self):
self.path.delete(0,END)
#self.datei.delete(0,END)
self.path['state'] = 'disabled'
self.direct['state'] = 'disabled'
self.datei['state'] = 'normal'
self.dat['state'] = 'normal'
self.set_out('Please select a file for conversion.', 'y')
#---------------------------------------------------------
# convert multiple files window behaviour
def mult(self):
#self.path.delete(0,END)
self.datei.delete(0,END)
self.path['state'] = 'normal'
self.direct['state'] = 'normal'
self.datei['state'] = 'disabled'
self.dat['state'] = 'disabled'
self.set_out('Please select a directory to make the rogram search for files.\nNote: ALL xml files will be converted!', 'y')
#---------------------------------------------------------
# print messages to messagescreen
def set_out(self, text, opt):
text = text + '\n--------------------------------------------------------------------------------------------------------------------------\n'
self.ausgabe['state'] = 'normal'
if (opt == 'y'):
self.ausgabe.delete(1.0,END)
self.ausgabe.insert(END, str(text))
self.ausgabe['state'] = 'disabled'
self.ausgabe.see(END)
self.ausgabe.update_idletasks()
#---------------------------------------------------------
# start conversion
def init(self):
conversion = conv()
#---------------------------------------------------------
# conversion
class conv:
def __init__(self):
x = convert.v.get()
if (x == 1):
pfad = convert.datei.get()
if (pfad == ''):
tkMessageBox.showerror('Error','You must specify a file to convert!')
return
if not pfad.endswith('.xml'):
tkMessageBox.showerror('Error','Invalid file format!\nYou must specify a XML-file!')
convert.can()
return
convert.set_out(pfad, 'n')
self.convertfile(pfad)
pfad, filename = os.path.split(pfad)
self.bak_killer_call(pfad)
self.restart()
elif (x == 2):
pfad = convert.path.get()
if (pfad == ''):
tkMessageBox.showerror('Error','You must specify a directory!')
return
convert.set_out(pfad, 'n')
self.multipleFiles(pfad, '.xml')
self.bak_killer_call(pfad)
self.restart()
#---------------------------------------------------------
# restart program or quit
def restart(self):
eingabe = tkMessageBox.askyesno('More...','Would you like to convert more files?')
if eingabe:
convert.can()
return
else:
convert.set_out('phasing down application','n')
convert.root.destroy()
#---------------------------------------------------------
# xml_check
def xml_check(self, text):
#check for TemiX control structures
if re.search('\[CDATA\[', text):
return 1
#check for ProjektKit Tag
if re.search('<pkit-', text):
return 2
else:
return 0
#---------------------------------------------------------
# filter_select
def filter_select(self, text):
#TemiX Tags included:
if (self.xml_check(text) == 1):
tkMessageBox.showinfo('Info','''File was already opened in TemiX. \n '[' and ']' could not be converted!''')
text = self.f0(text)
text = self.f1(text)
text = self.f3(text)
text = self.f4(text)
return text
#ProjektKit export:
if (self.xml_check(text) == 2):
text = self.f0(text)
text = self.f1(text)
text = self.f2(text)
text = self.f3(text)
text = self.f4(text)
return text
else:
return text
#---------------------------------------------------------
# filter definitions
def f0(self, text):
return text.replace('[Q-]','').replace('[PM-]','').replace('[role]','')
def f1(self, text):
return text.replace('','>')
def f2(self, text):
return text.replace('[','').replace(']','')
def f3(self, text):
while 1:
ort = re.search ('<para class="heading.">(.+?)</para>', text)
if ort == None:
break
text = re.sub('<para class="heading.">(.+?)</para>', r'<p><b>\1</b></p>', text)
return text
def f4(self, text):
return text.replace('<para>','<p>').replace('</para>','<p>')
#---------------------------------------------------------
# multiple file search and convert
def multipleFiles(self, path, extension, depth=0):
for item in os.listdir(path):
itemWithPath = os.path.join(path, item)
if item.endswith(extension):
datei = itemWithPath
convert.set_out('Starting conversion', 'n')
in_file = open(datei,'r')
inhalt = in_file.read()
in_file.close()
bak = datei + '_bak'
try:
shutil.copyfile(datei, bak)
except IOError:
convert.set_out('Backup file could not be generated','n')
new_string = str(inhalt)
new_string = self.filter_select(new_string)
try:
out_file = open(datei, 'w')
except IOError:
convert.set_out('File could not be opened!','n')
out_file.write(new_string)
out_file.close()
convert.set_out('File converted and written','n')
if os.path.isdir(itemWithPath):
self.multipleFiles(itemWithPath, extension, depth + 1)
#---------------------------------------------------------
# single file convert
def convertfile(self, datei):
convert.set_out('Starting conversion','n')
in_file = open(datei,'r')
inhalt = in_file.read()
in_file.close()
bak = datei + '_bak'
try:
shutil.copyfile(datei, bak)
except IOError:
convert.set_out('Backup file could not be generated','n')
new_string = str(inhalt)
new_string = self.filter_select(new_string)
try:
out_file = open(datei, 'w')
except IOError:
convert.set_out('File could not be opened!','n')
out_file.write(new_string)
out_file.close()
convert.set_out('File converted and written','n')
#---------------------------------------------------------
# bak_killer call
def bak_killer_call(self, pfad):
convert.set_out('Starting remove engine','n')
eingabe = tkMessageBox.askyesno('Remove backup?','Would you like to remove the backup [*.xml_bak] files?')
if not eingabe:
convert.set_out('To remove backup files, simply run bak_remover.','n')
return
else:
eingabe = tkMessageBox.askyesno('Remove backup?','Really remove ALL backup files?')
if not eingabe:
return
else:
self.bak_killer(pfad, '.xml_bak')
#---------------------------------------------------------
# backup killer
def bak_killer(self, path, extension, depth=0):
for item in os.listdir(path):
itemWithPath = os.path.join(path, item)
if item.endswith(extension):
os.remove (itemWithPath)
if os.path.isdir(itemWithPath):
self.bak_killer(itemWithPath, extension, depth + 1)
#---------------------------------------------------------
# start gui
convert = convertWindow()
convert.root.mainloop()
jemand evtl ne idee?
wär super
slide