我阅读了问题“导入 python 文件的整个文件夹”,但它不包括无需导入每个文件的方法。
除了要求避免多次导入之外,还要求类的数量与编写的代码量无关。我也想避免“from source import *”出现在代码中,但我并不反对它。
我已经尝试过这个。
从 system.models 导入 *
但这会导致
“模块”对象不可调用
我也试过这个
导入系统 sys.path.insert(0, r'../system/models')
但这会导致全局变量不包含类。
最终目标是让全局变量包含其他包中的类,这样我就可以做到这一点
model = globals()[class_name]()
> # import os
> # import win32com.client as win32
> # from bs4 import BeautifulSoup
> # import re
> # from datetime import datetime
> # import shutil
> # from tkinter import *
> # from tkinter import WORD, messagebox
> # import tkinter as tk
> # import subprocess
> # import sys
>
> # # preidm logic
> # def get_preidm_logs():
> # global preidm_logs
> # preidm_logs = []
> # # Specify the directory path for the PREIDM folder
> # preidm_directories = [
> # ('Dev', r'C:\User Items\Projects\SP 2\PREIDM'),
> # ('Prod', r'C:\User Items\Projects\SP 2\PREIDM 1'),
> # ('Prod', r'C:\User Items\Projects\SP 2\PREIDM 2'),
> # ]
>
> # for label, preidm_directory in preidm_directories:
> # drive, folder = os.path.splitdrive(preidm_directory)
>
> # with os.scandir(preidm_directory) as entries:
> # text_files = [entry.name for entry in entries if entry.is_file() and entry.name.endswith('.txt')]
>
> # if not text_files:
> # write_to_output(f"{drive} - {label} No text files found in the PREIDM folder.")
> # return "No text files found in the PREIDM folder."
>
> # latest_file = max(text_files, key=lambda x: os.path.getmtime(os.path.join(preidm_directory, x)))
>
> # with open(os.path.join(preidm_directory, latest_file), 'r') as file:
> # content = file.read()
>
> # threshold_matches = re.findall(r'Delete threshold: (\d+)', content)
> # preidm_finished = "preidm finished" in content.lower()
> # no_dept_found = "id found with no dept" in content.lower()
>
> # if not threshold_matches or not preidm_finished:
> # preidm_logs.append(
> # f"{drive} - {label} PreIDM Failed.")
> # write_to_output(f"{drive} - {label} PreIDM Failed.")
> # else:
> # delete_threshold = int(threshold_matches[-1])
> # preidm_logs.append(
> # f"{drive} - {label} Delete threshold: {delete_threshold}")
> # write_to_output(f"{drive} - {label} Delete threshold: {delete_threshold}")
> # if no_dept_found:
> # preidm_logs.append(
> # f"{drive} - ID found with no dept")
> # write_to_output(f"{drive} - ID found with no dept")
>
> # return preidm_logs
>
> # # mirpt logic
> # def get_mirpt_logs():
> # global mirpt_logs
> # mirpt_logs = []
> # mirpt_directories = [
> # r'C:\User Items\Projects\SP 2\MIRPT Logs'
> # ]
>
> # for mirpt_directory in mirpt_directories:
> # with os.scandir(mirpt_directory) as entries:
> # text_files = [entry.name for entry in entries if entry.is_file() and entry.name.endswith('.txt')]
>
> # if not text_files:
> # mirpt_logs.append(
> # f"{os.path.basename(mirpt_directory)} - No text files found.")
> # write_to_output(f"{os.path.basename(mirpt_directory)} - No text files found.")
> # continue
>
> # latest_file = max(text_files, key=lambda x: os.path.getmtime(os.path.join(mirpt_directory, x)))
>
> # with open(os.path.join(mirpt_directory, latest_file), 'r') as file:
> # last_line = file.readlines()[-1].strip()
>
> # if "no adhoc requests pending" in last_line.lower():
> # mirpt_logs.append(
> # f"{os.path.basename(mirpt_directory)} - SUCCESS")
> # write_to_output(f"{os.path.basename(mirpt_directory)} - SUCCESS")
> # else:
> # mirpt_logs.append(
> # f"{os.path.basename(mirpt_directory)} - FAILED")
> # write_to_output(f"{os.path.basename(mirpt_directory)} - FAILED")
>
> # return mirpt_logs
>
> # def report_bug():
> # today = datetime.now().strftime("%Y-%m-%d")
> # outlook = win32.Dispatch('outlook.application')
> # mail = outlook.CreateItem(0) # 0 represents an email item
> # mail.To = '[email protected]'
> # mail.Subject = 'CheckOut App Bug Report - ' + str(today)
> # mail.HTMLBody = "Please describe your issue here."
> # mail.Display()
>
> # def write_to_output(text, bold=False):
> # output_text.configure(state="normal", fg="black")
> # if bold:
> # output_text.insert("end", f"{text}\n", "bold")
> # else:
> # output_text.insert("end", f"{text}\n")
> # output_text.configure(state="disabled")
>
> # def table_creator():
> # # Regular expression pattern to match tasks starting with 'Task Faizan', 'const', or 'cust'
> # pattern_faizan = re.compile(r'^Task Faizan.*')
> # pattern_const_cust = re.compile(r'^(const|cust).*')
>
> # # hardcoded tasks
> # hardcoded_tasks = ['Hardcoded 1', 'Hardcoded 2', 'Hardcoded 3']
>
> # # Define source directories
> # source_directories = {
> # 'IA': 'C:\\Users\\faiza\\Desktop\\IA',
> # 'MO': 'C:\\Users\\faiza\\Desktop\\MO',
> # 'Prod': 'C:\\Users\\faiza\\Desktop\\Prod'
> # }
>
> # # Destination directories in the current location
> # destination_directories = {
> # 'IA': './IA/',
> # 'MO': './MO/',
> # 'Prod': './Prod/'
> # }
>
> # def copy_latest_files(source_dir, destination_dir):
> # try:
> # os.makedirs(destination_dir, exist_ok=True)
> # files = [f for f in os.listdir(source_dir) if f.endswith('.html')]
> # files.sort(key=lambda x: os.path.getmtime(os.path.join(source_dir, x)), reverse=True)
> # latest_files = files[:4]
>
> # for file_name in latest_files:
> # source_path = os.path.join(source_dir, file_name)
> # destination_path = os.path.join(destination_dir, file_name)
> # shutil.copyfile(source_path, destination_path)
> # except Exception as e:
> # print(f"Error copying files from {source_dir} to {destination_dir}: {str(e)}")
>
>
> # # Copy HTML files from source directories to destination directories
> # for env, source_dir in source_directories.items():
> # destination_dir = destination_directories[env]
> # copy_latest_files(source_dir, destination_dir)
>
> # # Create the confirmation message with the list of files
> # message = "The following files will be used to generate the Task Status Report:\n\n"
>
> # for env, destination_dir in destination_directories.items():
> # files = [f for f in os.listdir(destination_dir) if f.endswith('.html')]
> # files.sort(key=lambda x: os.path.getmtime(os.path.join(destination_dir, x)), reverse=True)
> # latest_files = files[:4]
> # message += f"{env}:\n"
> # for file_name in latest_files:
> # message += f"{file_name}\n"
> # message += "\n"
>
> # # Ask for confirmation using a message box
> # confirmed = messagebox.askyesno("Confirmation", message)
>
> # # If the user confirms, proceed with generating the email
> # if confirmed:
> # pass
> # else:
> # write_to_output("Email generation cancelled.")
> # return
>
> # def get_latest_files(directory):
> # try:
> # files = [os.path.join(directory, f) for f in os.listdir(directory) if f.endswith('.html')]
> # files.sort(key=os.path.getmtime, reverse=True)
> # return files[:4]
> # except Exception as e:
> # print(f"Error fetching files from {directory}: {str(e)}")
> # return []
>
> # # Function to get tasks and their statuses from an HTML file
> # def get_tasks_and_status_from_html(file_path):
> # tasks = []
> # try:
> # with open(file_path, 'r') as html_file:
> # html_content = html_file.read()
> # soup = BeautifulSoup(html_content, 'html.parser')
> # # Find all td elements containing task names
> # td_elements = soup.find_all('td')
> # for td in td_elements:
> # task_name = td.get_text().strip()
> # # Check if the task matches the pattern and add it to the list
> # if pattern_faizan.match(task_name) or pattern_const_cust.match(task_name) or task_name in hardcoded_tasks:
> # status = file_path.split('\\')[-1].split(' - ')[1].split('.')[0]
> # # Replace 'Error' status with 'Failed'
> # status = 'Failed' if status == 'Errors' else status
> # tasks.append((task_name, status))
> # except Exception as e:
> # print(f"Error processing file {file_path}: {str(e)}")
> # return tasks
>
> # # Fetch and process files from source directories
> # tasks_by_env = {}
> # for env, source_dir in source_directories.items():
> # destination_dir = destination_directories[env]
> # latest_files = get_latest_files(source_dir)
> # tasks_with_status = []
> # for file in latest_files:
> # tasks_with_status.extend(get_tasks_and_status_from_html(file))
> # tasks_by_env[env] = list(set(tasks_with_status))
>
> # # Create HTML for the email body with a table and summary section
> # html_body = f"""
> # <html>
> # <head></head>
> # <body>
> # <p style='font-size: 25px'>Tasks and Environment Status:</p>
> # <table style="border-collapse: collapse; width: 70%; text-align: center; font-size: 20px;">
> # <tr>
> # <th style="background-color: #ADD8E6; padding: 12px;">Task Name</th>
> # <th style="background-color: #ADD8E6; padding: 12px;">IA</th>
> # <th style="background-color: #ADD8E6; padding: 12px;">MO</th>
> # <th style="background-color: #ADD8E6; padding: 12px;">Prod</th>
> # </tr>
> # """
>
> # # Merge tasks and statuses for each category
> # merged_tasks = {}
> # for env, tasks_status_list in tasks_by_env.items():
> # for task, status in tasks_status_list:
> # categorized_task = task
> # if pattern_faizan.match(task):
> # categorized_task = 'All Faizan'
> # elif pattern_const_cust.match(task):
> # categorized_task = 'Practical Tasks'
>
> # if categorized_task not in merged_tasks:
> # merged_tasks[categorized_task] = {k: [] for k in source_directories.keys()}
>
> # merged_tasks[categorized_task][env].append(status)
>
> # # function to determine the status of a category
>
> # def determine_category_status(statuses):
> # if len(statuses) == 0:
> # return '-'
> # elif 'Failed' in statuses:
> # return 'Failed'
> # else:
> # return 'Successful'
>
> # # function to set color of the status
>
> # def set_color(status):
> # if status == 'Successful':
> # return 'green'
> # elif status == 'Failed':
> # return 'red'
> # else:
> # return 'black'
>
>
> # # Fill the table with tasks and their statuses for each environment
> # for task, statuses in merged_tasks.items():
> # ia_status = determine_category_status(statuses['IA'])
> # mo_status = determine_category_status(statuses['MO'])
> # prod_status = determine_category_status(statuses['Prod'])
>
> # html_body += f"""
> # <tr>
> # <td style="border: 1px solid black; padding: 12px;">{task}</td>
> # <td style="border: 1px solid black; padding: 12px; color: {set_color(ia_status)}">{ia_status}</td>
> # <td style="border: 1px solid black; padding: 12px; color: {set_color(mo_status)}">{mo_status}</td>
> # <td style="border: 1px solid black; padding: 12px; color: {set_color(prod_status)}">{prod_status}</td>
> # </tr>
> # """
>
> # # Complete the HTML table
> # html_body += """
> # </table>
> # """
>
> # # Generate the summary table section
> # summary_table = "<p style='font-size: 25px'>Summary Table:</p>"
> # summary_table += "<table style='border-collapse: collapse; width: 50%; text-align: left; font-size:20px'><tr><th
> style='background-color: black; color: white; padding:
> 12px;'>Entity</th><th style='background-color: black; color: white;
> padding: 12px;'>Details</th></tr>"
>
> # for env, tasks in tasks_by_env.items():
> # failed_tasks = [task for task, status in tasks if status == 'Failed']
> # if len(failed_tasks) == 0 and len(tasks) != 0:
> # summary_table += f"<tr><td style='border: 1px solid black; padding: 12px;'>{env}</td><td style='border: 1px solid black;
> padding: 12px;'>All tasks ran successfully</td></tr>"
> # else:
> # summary_table += f"<tr><td style='border: 1px solid black; padding: 12px;'>{env}</td><td style='border: 1px solid black;
> padding: 12px;'>{', '.join(failed_tasks)}</td></tr>"
>
>
> # # add viper/autosys jobs/pa tester rows to the summary table for (success/failed) as details
> # summary_table += f"<tr><td style='border: 1px solid black; padding: 12px;'>Viper</td><td style='border: 1px solid black; padding:
> 12px;'>{viper_status}</td></tr>"
> # summary_table += f"<tr><td style='border: 1px solid black; padding: 12px;'>Autosys Job</td><td style='border: 1px solid black;
> padding: 12px;'>{autosys_job_status}</td></tr>"
> # summary_table += f"<tr><td style='border: 1px solid black; padding: 12px;'>PA Tester</td><td style='border: 1px solid black;
> padding: 12px;'>{pa_tester_status}</td></tr>"
> # summary_table += f"<tr><td style='border: 1px solid black; padding: 12px;'>PreIDM</td><td style='border: 1px solid black;
> padding: 12px;'>{', '.join(preidm_logs)}</td></tr>"
> # summary_table += f"<tr><td style='border: 1px solid black; padding: 12px;'>MIRPT</td><td style='border: 1px solid black; padding:
> 12px;'>{', '.join(mirpt_logs)}</td></tr>"
> # summary_table += "</table>"
>
> # # Combine the HTML body with the summary table section
> # html_body += summary_table
>
> # # Complete the HTML body
> # html_body += """
> # </body>
> # </html>
> # """
>
> # # Get current date for subject
> # current_date = datetime.now().strftime("%Y-%m-%d")
>
> # # Create an Outlook instance
> # outlook = win32.Dispatch('outlook.application')
> # mail = outlook.CreateItem(0)
>
> # # Set email properties
> # mail.Subject = f'IAM Checkout {current_date}'
> # mail.HTMLBody = html_body
>
> # # Display the generated email content in Outlook
> # mail.Display()
> # # display bold text in output
> # write_to_output("Email generated successfully.")
>
>
> # def create_gui():
> # # create global variables for viper/autosys job/pa tester status
> # global viper_status
> # global autosys_job_status
> # global pa_tester_status
> # global preidm_logs
> # global mirpt_logs
> # viper_status = "-"
> # autosys_job_status = "-"
> # pa_tester_status = "-"
> # preidm_logs = []
> # mirpt_logs = []
>
> # def toggle_viper():
> # global viper_status
> # if viper_status == "Success":
> # viper_status = "Failed"
> # button_viper.config(bg="orange", relief="sunken")
> # else:
> # viper_status = "Success"
> # button_viper.config(bg="green", relief="sunken")
>
> # def toggle_autosys_job():
> # global autosys_job_status
> # if autosys_job_status == "Success":
> # autosys_job_status = "Failed"
> # button_autosys_job.config(bg="orange", relief="sunken")
> # else:
> # autosys_job_status = "Success"
> # button_autosys_job.config(bg="green", relief="sunken")
>
> # def toggle_pa_tester():
> # global pa_tester_status
> # if pa_tester_status == "Success":
> # pa_tester_status = "Failed"
> # button_pa_tester.config(bg="orange", relief="sunken")
> # else:
> # pa_tester_status = "Success"
> # button_pa_tester.config(bg="green", relief="sunken")
>
> # # list of urls to open
> # urls = [
> # ("Autosys job", ["https://www.google.com", "https://drive.google.com", "https://myaccount.google.com"]),
> # ("PA tester", ["https://www.yahoo.com"]),
> # ("Viper", ["https://mail.google.com"])
> # ]
>
> # # function to open all autosys job urls in browser
> # def open_urls():
> # for url in urls[0][1]:
> # os.startfile(url)
>
> # # create root window
> # root = tk.Tk()
> # root.title("CheckMate")
> # root.geometry("900x600")
>
> # def restart_app():
> # python = sys.executable
> # script = 'test.py'
> # root.destroy()
> # subprocess.call([python, script])
>
> # # About information popup
> # def show_about_info():
> # message_info = """CheckPoint App Version 1.0
>
> # Developed by:
> # - Faizan Shaikh (fshaik10)
> # - Sam Pulikkottil (spulik10)
>
> # Features:
> # - Comprehensive email reports to document Sailpoint tasks.
> # - MIRPT Logs Analysis: Check synchronization status of ADHoc requests.
> # - PRE-IDM Logs Verification: Check delete threshold & verify successful PRE-IDM execution.
> # - Viper Verification/PA Tester Verification/Autosys Job Verification: Check if the monitors, servers and jobs ran
> successfully.
>
> # Feedback and Bug Reporting:
> # Got thoughts on how to make CheckPoint even better? Shoot us your feedback and bug reports at fshaik10 & spullik. We're all ears!
>
> # Cheers n CheckMate!
> # """
> # messagebox.showinfo("About", message_info)
>
> # # Custom font
> # custom_font = ("Arial", 12)
>
> # # Menu bar
> # menubar = tk.Menu(root)
> # root.config(menu=menubar)
>
> # # Direct menu to show message_info
> # help_menu = tk.Menu(menubar, tearoff=0)
> # menubar.add_cascade(label="Menu", menu=help_menu)
> # help_menu.add_command(label="About", command=show_about_info)
> # help_menu.add_command(label="Report Bug", command=report_bug)
> # help_menu.add_command(label="Restart", command=restart_app)
> # help_menu.add_command(label="Exit", command=root.quit)
>
> # # Branding logo
> # logo_image = tk.PhotoImage(file='logo2.png')
> # branding_label = tk.Label(root, image=logo_image)
> # branding_label.pack(pady=10)
>
> # # Button style definition for consistency
> # button_style = {
> # # travelers insurance company color red background
> # "bg": "#e01719",
> # "fg": "white",
> # "width": 15,
> # "height": 2,
> # "relief": tk.RAISED,
> # "font": custom_font,
> # }
>
> # # Create a frame for buttons
> # button_frame = tk.Frame(root)
> # button_frame.pack()
>
> # # Create buttons and pack them inside the button frame
> # button_viper = tk.Button(button_frame, text="Viper", command=toggle_viper, **button_style)
> # button_viper.pack(side=tk.LEFT, padx=30, pady=10)
>
> # button_autosys_job = tk.Button(button_frame, text="Autosys Job", command=toggle_autosys_job, **button_style)
> # button_autosys_job.pack(side=tk.LEFT, padx=30, pady=10)
>
> # button_pa_tester = tk.Button(button_frame, text="PA Tester", command=toggle_pa_tester, **button_style)
> # button_pa_tester.pack(side=tk.LEFT, padx=30, pady=10)
>
> # # Create a frame for the second set of buttons
> # second_button_frame = tk.Frame(root)
> # second_button_frame.pack()
>
> # button_preidm = tk.Button(second_button_frame, text="PREIDM", command=get_preidm_logs, **button_style)
> # button_preidm.pack(side=tk.LEFT, padx=30, pady=10)
>
> # button_mirpt = tk.Button(second_button_frame, text="MIRPT", command=get_mirpt_logs, **button_style)
> # button_mirpt.pack(side=tk.LEFT, padx=30, pady=10)
>
> # button_table = tk.Button(second_button_frame, text="Execute>>>>", command=table_creator, **button_style)
> # button_table.pack(side=tk.LEFT, padx=30, pady=10)
>
> # # Create a frame for hyperlinks
> # hyperlink_frame = tk.Frame(root)
> # hyperlink_frame.pack()
>
> # # Create hyperlink labels and pack them inside the hyperlink frame
> # hyperlink_viper = tk.Label(hyperlink_frame, text="Viper", fg="blue", cursor="hand2")
> # hyperlink_viper.pack(side=tk.LEFT, padx=10, pady=10)
> # hyperlink_viper.bind("<Button-1>", lambda e: os.startfile("https://mail.google.com"))
>
> # hyperlink_autosys_job = tk.Label(hyperlink_frame, text="Autosys job", fg="blue", cursor="hand2")
> # hyperlink_autosys_job.pack(side=tk.LEFT, padx=10, pady=10)
> # hyperlink_autosys_job.bind("<Button-1>", lambda e: open_urls())
>
> # hyperlink_pa_tester = tk.Label(hyperlink_frame, text="PA Tester", fg="blue", cursor="hand2")
> # hyperlink_pa_tester.pack(side=tk.LEFT, padx=10, pady=10)
> # hyperlink_pa_tester.bind("<Button-1>", lambda e: os.startfile("https://www.yahoo.com"))
>
> # # Create an output section
> # output_frame = tk.Frame(root)
> # output_frame.pack(padx=5, pady=5, fill=tk.BOTH, expand=True)
>
> # scrollbar = tk.Scrollbar(output_frame)
> # scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
>
> # global output_text
> # output_text = tk.Text(output_frame, wrap=tk.WORD, yscrollcommand=scrollbar.set)
> # output_text.pack(fill=tk.BOTH, expand=True)
> # scrollbar.config(command=output_text.yview)
>
> # root.mainloop()
>
> # create_gui()
>
> > Blockquote