Tkinter实现

本文介绍了一个使用Python编写的日志处理脚本,它能自动读取原始log文件,筛选并重写特定SQL操作,如CREATE TABLE、INSERT和DELETE,同时跳过部分无用信息。通过选择文件和输出路径,工具可以生成处理后的log文件。
摘要由CSDN通过智能技术生成
# !/usr/bin/python
# -*-coding:utf-8 -*-

import tkinter as tk
import tkinter.messagebox
from pathlib import Path
from tkinter import filedialog
import threading
from tkinter.filedialog import askdirectory

file_path = ''
res_path = ''
def comm():
    global file_path
    file_path = filedialog.askopenfilename()
    print(file_path)

def res_comm():
    global res_path
    res_path = askdirectory()
    print(res_path)


def ping():
    global res_path,file_path
    if file_path=='':
        tkinter.messagebox.showwarning(title='提示',message='没有选择原始log文件路径')
    log_path=Path(file_path)
    obj = '/'+(file_path.split('.')[0]).split('/')[-1]+'.txt'

    if res_path=='':
        tkinter.messagebox.showwarning(title='提示',message='没有选择处理后log文件路径')
    res_path +=obj
    respath = Path(res_path)


    with open(log_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
        start_index = 0
        end_index = 0
        var_start = []
        del_list = []
        remain_list =[]
        for a in range(len(lines)):
            if lines[a].lower().startswith('create temporary'):
                start_index = a - 2
                break
        for b in range(len(lines)):
            if lines[b].startswith('COMMIT'):
                end_index = b - 5
                break
        if end_index == 0:
            for i in range(len(lines) - 1, 0, -1):
                if lines[i].startswith('total time: '):
                    end_index = i - 1
                    break
        for j in range(start_index, len(lines)):
            if lines[j].lower().startswith('---'):
                var_start.append(j - 2)
                remain_list.append(j+2)

        if var_start:
            for k in var_start:
                del_list.extend([x for x in range(k,k+4)])
                del_list.extend([x for x in range(k+5,k + 8)])
        # print(del_list)
        # print(remain_list)
        #
        # for i in remain_list:
        #     print(lines[i])

        with open(respath, 'a+', encoding='utf-8') as res:
            for i in range(start_index, end_index, 1):
                if del_list == [] or (del_list and i not in del_list):
                    if i in remain_list:
                        res.writelines(str('--') + lines[i])
                    else:
                        if lines[i].startswith('CREATE TABLE'):
                            pass
                        elif lines[i].startswith('Time'):
                            pass
                        elif lines[i].startswith('INSERT 0'):
                            res.writelines(str('--') + lines[i])
                        elif lines[i].startswith('DELETE') and len(lines[i].split(' ')) == 2:
                            res.writelines(str('--') + lines[i])

                        elif lines[i].startswith('UPDATE') and len(lines[i].split(' ')) == 2:
                            res.writelines(str('--') + lines[i])
                        elif lines[i].startswith('BEGIN'):
                            pass

                        else:
                            if lines[i].split():
                                res.writelines(lines[i])

    tkinter.messagebox.showwarning(title='提示', message='已生成')


#定义信息显示的方法
def showinfo(result):
    #realtime = time.strftime("%Y-%m-%d %H:%M:%S ")
    textvar =result #系统时间和传入结果
    text.insert("end",textvar) #显示在text框里面
    text.insert("insert","\n") #换行

#定义清除方法
def clear():
    text.delete(0.0,tk.END) #清楚text中的内容,0.0为删除全部
    #显示text
    text.place(x=90,y=70) #place为布局,放在坐标为(100,150)的地方

def thread_it(func, *args):
    '''将函数放入线程中执行'''
    # 创建线程
    t = threading.Thread(target=func, args=args)
    # 守护线程
    t.setDaemon(True)
    # 启动线程
    t.start()


if __name__ == '__main__':
    # log_path = r'D:\工作\项目\17 四笔钱\测试文档\仓内数据\GSQL\example.log'
    # res_path = 'example.txt'


    window = tk.Tk()  # 窗口函数
    window.title("日志处理工具")  # 窗口的名字
    window.geometry("500x100")  # 窗口的大小,x是字母
    # 显示text定义
    text = tk.Text(window, width=90, height=100)  # 设置高度和宽度


    tk.Label(window, text='选择要处理的日志:', font=("Arial", 12)).grid(row=27, column=0 )
    comm_button = tk.Button(window, text="Select1", font=("Arial", 10), command=lambda: thread_it(comm))
    comm_button.place(x=220, y=0)  #

    tk.Label(window, text='选择处理后日志路径:', font=("Arial", 12)).grid(row=80, column=0)
    comm_button = tk.Button(window, text="Select2", font=("Arial", 10), command=lambda: thread_it(res_comm))
    comm_button.place(x=220, y=30)  #

    ping_button = tk.Button(window, text="Run", font=("Arial", 12), command=lambda: thread_it(ping))
    ping_button.place(x=350, y=15)  # 显示清除button


    window.mainloop()  # 界面循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值