Question: import pyzipper import os import subprocess import json import time import tempfile import sys from datetime import datetime, timezone import tkinter as tk from tkinter

import pyzipper
import os
import subprocess
import json
import time
import tempfile
import sys
from datetime import datetime, timezone
import tkinter as tk
from tkinter import messagebox, scrolledtext
def display_message(message):
output_text.insert(tk.END, message +"
")
def get_job_type():
while True:
jobtype = jobtype_entry.get().lower()
if jobtype in ['start', 'stop', 'exit']:
if jobtype == 'exit':
display_message("You have chosen to exit the automation script")
time.sleep(10)
exit()
else:
break
else:
display_message('Please type either "start" or "stop" or "exit".')
while True:
username = os.getlogin().lower()
todays_date = datetime.today().date()
formatted_todays_date = todays_date.strftime("%m/%d/%Y").strip('0')
z_path = r"C:\Program Files\7-Zip\7z.exe"
zipped_file = r'file.zip'
passwd='pw'
with pyzipper.AESZipFile(zipped_file, mode='r') as arch_folder:
arch_folder.setpassword(passwd.encode('utf-8'))
userlist_node_path = 'users.js'
with arch_folder.open(userlist_node_path) as userlist:
data = userlist.read().decode('utf-8')
data_results = data[data.find('{') : data.find('}')+2]
data_formatted = data_results.replace('\'','"').replace('
','\"').replace(':','\":')
data_loads = json.loads(data_formatted)
if username not in data_loads:
print('Sorry, you are NOT AUTHORIZED to '+ jobtype +' any automations')
break
elif username in data_loads:
for key, value in data_loads.items():
if key == username:
values = value
version ='0.9'
start_automations = values +['Cancel']
start_input_selection =''
if jobtype == 'start':
select_to_start_automation(jobtype, start_automations, start_input_selection, z_path, passwd, zipped_file, username, version)
break
def select_to_start_automation(jobtype, start_automations, start_input_selection, z_path, passwd, zipped_file, username, version):
start_input_message = 'Please select the number of the automation you wish to start, or choose '+ str(len(start_automations))+' to CANCEL.
'
for index, item in enumerate(start_automations):
start_input_message += f'{index+1}){item}
'
start_input_message +="
Your Choice: "
display_message(f"{start_input_message}")
try:
start_user_input = automation_entry.get()
start_user_input = int(start_user_input)
if int(start_user_input)== len(start_automations):
start_input_selection = 'valid'
display_message(f"Option selected: Cancel.
")
time.sleep(10)
elif (int(start_user_input)>0 and int(start_user_input)<= len(start_automations)):
start_input_selection = 'valid'
job_name = start_automations[int(start_user_input)-1]
temp_dir = tempfile.TemporaryDirectory()
output_dir = str(temp_dir.name)+'\\'
subprocess.call([z_path, 'x','-p'+passwd, zipped_file, '-o'+output_dir], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
stop_job_authorize_node_path= output_dir+'perms.js'
args =[username, job_name, version, jobtype]
display_message(f"Requesting API...
")
subprocess.run(['node', stop_job_authorize_node_path, *args], shell=True, capture_output= True, text=True, check=True)
else:
display_message('1 That was not a valid number.')
except ValueError:
display_message('2 That was not a valid number.')
root= tk.Tk()
root.title(" API")
output_text = scrolledtext.ScrolledText(root, width =100, height =10)
output_text.grid(row =0, column=0, columnspan=2, padx=10, pady=5)
jobtype_entry_label = tk.Label(root, text="Please type: then click submit")
jobtype_entry_label.grid(row =1, column=0, padx=10, pady=5)
jobtype_entry = tk.Entry(root, width=80)
jobtype_entry.grid(row =1, column=1, padx=10, pady=5)
automation_entry_label = tk.Label(root, text="Please type then click submit")
automation_entry_label.grid(row =2, column=0, padx=10, pady=5)
automation_entry = tk.Entry(root, width=80)
automation_entry.grid(row =2, column=1, padx=10, pady=5)
output_text.insert(tk.END, f"Do you want to start stop an automation, or exit the script?
")
submit_button = tk.Button(root, text= "Submit", command = get_job_type)
submit_button.grid(row =3, column=0, columnspan=2, pady=10)
root.mainloop()
Please fix the code. I unable to get the except block to stop displaying, It always displays

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!