Reputation: 17
I'm trying to figure out how I can open a txt file that contains a script and edit all of the variables inside the if not testfolder.filter
loop in the txt file. I'm using tkinter as GUI and I have some entry fields that I want to use to replace those variables and save the txt file after pressing the button called Add Alert
The script.txt I want to update:
from exchangelib import Credentials, Account, UTC_NOW, EWSTimeZone, EWSDateTime, EWSDate, Q
from collections import defaultdict
from datetime import timedelta, datetime
from kayako import KayakoAPI
from kayako import Department, TicketCount, TicketStatus, TicketPriority, TicketType, TicketNote, TicketAttachment, Ticket
#Kayako RESTAPI forbindelse
API_URL = 'https://hidden.com/api/index.php'
API_KEY = 'hidden'
SECRET_KEY = 'hidden'
api = KayakoAPI(API_URL, API_KEY, SECRET_KEY)
credentials = Credentials('[email protected]', 'hidden')
a = Account('[email protected]', credentials=credentials, autodiscover=True)
testfolder = a.public_folders_root / 'Hidden' / '[email protected]'
since = UTC_NOW() - timedelta(hours=24)
high = api.first(TicketPriority, title="High")
openTicket = api.first(TicketStatus, title="Open")
task = api.first(TicketType, title="Task")
departmentid = 5
if not testfolder.filter(subject__icontains=Subj, datetime_received__gt=since, sender=EmailAdr).exists():
ticket = api.create(Ticket, tickettypeid=task.id, ticketstatusid=openTicket.id, ticketpriorityid=high.id, departmentid=departmentid, userid="unassigned")
ticket.subject = TicketSubj
ticket.fullname = TicketName
ticket.email = TicketEmail
ticket.contents = TicketBody
ticket.add()
print('Log mangler! Opretter kayako ticket....')
else:
print('Log er fundet.')
My tkinter .py script
from tkinter import *
root = Tk()
Timer = 0
timer1 = int(Timer)
EmailAdr = StringVar()
Subj = StringVar()
TicketSubj = StringVar()
TicketName = StringVar()
TicketEmail = StringVar()
TicketBody = StringVar()
root.geometry("400x250")
e1 = Entry(root, textvariable=Subj)
e1.grid()
e2 = Entry(root, textvariable=EmailAdr)
e2.grid()
e3 = Entry(root)
e3.grid()
e4 = Entry(root, textvariable=TicketSubj)
e4.grid()
e5 = Entry(root, textvariable=TicketName)
e5.grid()
e6 = Entry(root, textvariable=TicketEmail)
e6.grid()
e7 = Entry(root, textvariable=TicketBody)
e7.grid()
def func1():
global Subj
Subj = e1.get()
def func2():
global EmailAdr
EmailAdr = e2.get()
def func3():
global Timer
Timer = e3.get()
def func4():
global TicketSubj
TicketSubj = e4.get()
def func5():
global TicketName
TicketName = e5.get()
def func6():
global TicketEmail
TicketEmail = e6.get()
def func7():
global TicketBody
TicketBody = e7.get()
def demAlle():
func1()
func2()
func3()
func4()
func5()
func6()
func7()
aButton = Button(root, text="Add Alert", command=demAlle)
aButton.grid()
root.mainloop()
Upvotes: 1
Views: 108
Reputation: 7176
You don't need to associate the Entrys with a variable. If you are just going to read them once its not necessary. Also I added a column with labels to see what field I'm working with.
You can make the substitutions in many ways, depending on how the "script.txt" can vary. I have assumed it may change and first find the first line of the if statement.
Assuming the if statement and the following lines will always stay in the same order I'm substituting the fields according to the Entrys, but only if something is entered into the Entry.
When working with the file I first read it into a list of rows as this is easy to work with. After processing I write the file with the substitutions (but with the name "new_script.txt". I'm assuming you use Utf-8 encoding, otherwise change this to your preferred encoding.
from tkinter import *
root = Tk()
root.geometry("400x250")
Label(root, text='Subj:').grid(row=0, column=0, sticky='w')
Subj_entry = Entry(root)
Subj_entry.grid(row=0, column=1)
Label(root, text='EmailAdr:').grid(row=1, column=0, sticky='w')
EmailAdr_entry = Entry(root)
EmailAdr_entry.grid(row=1, column=1)
Label(root, text='TicketSubj:').grid(row=3, column=0, sticky='w')
TicketSubj_entry = Entry(root)
TicketSubj_entry.grid(row=3, column=1)
Label(root, text='TicketName:').grid(row=4, column=0, sticky='w')
TicketName_entry = Entry(root)
TicketName_entry.grid(row=4, column=1)
Label(root, text='TicketEmail:').grid(row=5, column=0, sticky='w')
TicketEmail_entry = Entry(root)
TicketEmail_entry.grid(row=5, column=1)
Label(root, text='TicketBody:').grid(row=6, column=0, sticky='w')
TicketBody_entry = Entry(root)
TicketBody_entry.grid(row=6, column=1)
def demAlle():
# Read from Entries
Subj = Subj_entry.get()
EmailAdr = EmailAdr_entry.get()
TicketSubj = TicketSubj_entry.get()
TicketName = TicketName_entry.get()
TicketEmail = TicketEmail_entry.get()
TicketBody = TicketBody_entry.get()
# Read file into a list of rows which is easy to work with
with open('script.txt', encoding='utf-8') as script:
rows = script.readlines()
# Find the place where you want to alter the code
for index, row in enumerate(rows):
if row.startswith('if not testfolder.'):
break
# Altering lines; this can be done in many ways...
if Subj != '': # Check if field is empty
rows[index] = rows[index].replace('Subj', Subj)
if EmailAdr != '':
rows[index] = rows[index].replace('EmailAdr', EmailAdr)
if TicketSubj != '':
rows[index+2] = rows[index+2][:21] + TicketSubj + '\n'
if TicketName != '':
rows[index+3] = rows[index+3][:22] + TicketName + '\n'
if TicketEmail != '':
rows[index+4] = rows[index+4][:19] + TicketEmail + '\n'
if TicketBody != '':
rows[index+5] = rows[index+5][:22] + TicketBody + '\n'
# Open file and write the new contents
with open('new_script.txt', 'w', encoding='utf-8') as script:
for row in rows:
script.write(row)
aButton = Button(root, text="Add Alert", command=demAlle)
aButton.grid()
root.mainloop()
Upvotes: 1