class CompareApp(): def __init__(self, master, title): self.master = master self.master.title(title) self.pathList = set([]) self.fileLists = [] self.groupFiles = {} self.InitUI() def InitUI(self): self.wholeContainer = tk.Frame(self.master) self.wholeContainer.pack() button_width = 3 ### (1) button_padx = "2m" ### (2) button_pady = "1m" ### (2) buttons_frame_padx = "3m" ### (3) buttons_frame_pady = "2m" ### (3) buttons_frame_ipadx = "3m" ### (3) buttons_frame_ipady = "1m" ### (3) self.buttons_frame = tk.Frame(self.wholeContainer, height=10, width=980) ### self.buttons_frame.pack( side=tk.TOP, ### fill=tk.BOTH, expand=tk.YES, anchor="w", ipadx=buttons_frame_ipadx, ipady=buttons_frame_ipady, padx=buttons_frame_padx, pady=buttons_frame_pady, ) self.button1 = tk.Button(self.buttons_frame, command=self.Add) self.button1.configure(text="Add") self.button1.focus_force() self.button1.configure( width=button_width, ### (1) padx=button_padx, ### (2) pady=button_pady ### (2) ) self.button1.pack(side=tk.LEFT) self.button2 = tk.Button(self.buttons_frame, command=self.Remove) self.button2.configure(text="Remove") self.button2.configure( width=6, ### (1) padx=button_padx, ### (2) pady=button_pady ### (2) ) self.button2.pack(side=tk.LEFT) self.btnCompare = tk.Button(self.buttons_frame, command=self.compareFiles) self.btnCompare.configure(text="Compare Files") self.btnCompare.pack(side=tk.LEFT) self.btnCompare.bind("<Return>", self.compareFiles) self.btnDel = tk.Button(self.buttons_frame, command=self.deleteSelectedFiles) self.btnDel.configure(text="Deleted Selected") self.btnDel.pack(side=tk.LEFT) self.btnCompare.bind("<Return>", self.deleteSelectedFiles) # top frame self.top_frame = tk.Frame(self.wholeContainer, relief=tk.RIDGE, height=100, width=980,padx=10, pady=10) self.top_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=tk.YES, ) ### # left frame self.left_frame = tk.Frame(self.top_frame, relief=tk.RIDGE, height=100, width=500, ) self.left_frame.grid(row=0, column=0, sticky=("nsew")) # right frame self.right_frame = tk.Frame(self.top_frame, relief=tk.RIDGE, height=100, width=100, ) self.right_frame.grid(row=0, column=1, sticky=("nsew")) self.top_frame.columnconfigure(0, weight=1) self.top_frame.rowconfigure(0, weight=1) self.top_frame.columnconfigure(1, weight=2) self.top_frame.rowconfigure(1, weight=2) self.boxScroll = tk.Scrollbar(self.left_frame, orient="vertical", command=self.boxScrollFun) self.boxScroll.pack(side=tk.RIGHT,fill=tk.BOTH,expand=tk.NO,) self.folderBox = tk.Listbox(self.left_frame, selectmode=tk.MULTIPLE, relief=tk.RIDGE, height=20, width=10, yscrollcommand=self.boxScroll.set, ) self.folderBox.pack(side=tk.LEFT,fill=tk.BOTH,expand=tk.YES,) #self.boxScroll.pack(side="right", fill="y") self.checkScroll = tk.Scrollbar(self.right_frame, orient="vertical", command=self.checkScrollFun) self.checkScroll.pack(side=tk.RIGHT,fill=tk.BOTH,expand=tk.NO,) self.checkwithfile = CheckboxTreeview(self.right_frame, height=20, yscrollcommand=self.checkScroll.set) self.checkwithfile.pack(side=tk.LEFT,fill=tk.BOTH,expand=tk.YES,) # bottom frame self.bottom_frame = tk.Frame(self.wholeContainer, relief=tk.RIDGE, height=20, width=970,padx=10, pady=10 ) ### self.bottom_frame.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=tk.YES, ) ### self.output = tkscrolled.ScrolledText(self.bottom_frame, width=970, height=20, wrap='word') self.output.config(state=tk.DISABLED) self.output.pack(side=tk.LEFT) def boxScrollFun(self, *args): return self.folderBox.yview def checkScrollFun(self, *args): return self.checkwithfile.yview def Add(self): # otherwise ask the user what new file to open pathname = filedialog.askdirectory() pathname = os.path.abspath(pathname) print(pathname) if pathname in self.pathList: print("folder already exist" + pathname) return else: self.pathList.add(pathname) for id, path in enumerate(self.pathList): if path == pathname: self.folderBox.insert(id, pathname) if os.path.exists(pathname): for i in self.checkwithfile.get_children(): self.checkwithfile.delete(i) #self.fileLists = {} self.groupFiles = {} else: print("folder not found:"+pathname) return allFiles = set(self.fileLists) allFiles.update(self.getAllFileListByPath(pathname)) #print("allFiles:") #print(allFiles) self.fileLists = list(allFiles) #print("converted fileLists:") #print(self.fileLists) self.renewFileList() def renewFileList(self): fileList = [0 for i in range(len(self.fileLists))] for i in range(len(self.fileLists)): if os.path.exists(self.fileLists[i]): fileList[i]=(self.fileLists[i], os.path.getsize(self.fileLists[i])) else: print("remove not exist file:"+self.fileLists[i]) fileList.pop(i) fileList.sort(key=lambda filename: filename[1], reverse=True) for i in range(len(fileList)): self.fileLists[i] = fileList[i][0] print("file list:") print(self.fileLists) print("file list end") self.groupFileListSameSize() print("groupFiles:") print(self.groupFiles) index=0 for fileSize, fileList in self.groupFiles.items(): rootIndex = "r_" + str(index) sizeCal = str(fileSize)+'b' if fileSize > 1024: if fileSize > 1024*1024: if fileSize >1024*1024*1024: if fileSize >1024*1024*1024*1024: sizeCal = "{:.3f}".format(float(fileSize) / (1024*1024*1024*1024)) + 'T' else: sizeCal = "{:.3f}".format(float(fileSize) / (1024*1024*1024)) + 'G' else: sizeCal = "{:.3f}".format(float(fileSize) / (1024*1024)) + 'M' else: sizeCal = "{:.3f}".format(float(fileSize) / 1024) + 'k' fileNum = len(fileList) self.checkwithfile.insert('', "end", rootIndex, text=sizeCal+'('+str(fileNum)+')') self.checkwithfile.tag_configure("evenrow", background='white', foreground='black') self.checkwithfile.tag_configure("oddrow", background='black', foreground='white') for i in range(len(fileList)): childIndex = "c_"+str(fileSize)+'_i_'+str(i) textC = fileList[i] print("textC:"+textC) self.checkwithfile.insert(rootIndex, "end", childIndex, text=fileList[i]) index = index+1 def Remove(self): print("selected:") for i in self.folderBox.curselection(): print(i) filePath = self.folderBox.get(i) print(filePath) self.folderBox.delete(i) self.pathList.remove(filePath) #selected_text_list = [self.folderBox.get(i) for i in self.folderBox.curselection()] #print(selected_text_list) for i in self.checkwithfile.get_children(): self.checkwithfile.delete(i) # self.fileLists = {} self.groupFiles = {} if len(self.pathList) > 0: allFiles = set([]) for pathname in self.pathList: allFiles.update(self.getAllFileListByPath(pathname)) self.fileLists = list(allFiles) self.renewFileList() def getAllFileListByPath(self, pathname): fileSet = set([]) try: if os.path.isdir(pathname): fileList = os.listdir(pathname) print('pathname') print(pathname) print('filelist:') print(fileList) for file in fileList: if not file.startswith('.'): fullPath = pathname + os.sep + file if os.path.exists(fullPath): if os.path.isdir(fullPath): fileSet.update(self.getAllFileListByPath(fullPath)) else: fileSet.add(fullPath) elif os.path.exists(pathname): fileSet.add(pathname) except IOError: wx.LogError("Cannot open Directoy '%s'." % pathname) return fileSet def compareFiles(self): print("compare") for i in self.checkwithfile.get_children(): self.checkwithfile.delete(i) rootIn = 0 for fileSize, fileList in self.groupFiles.items(): rootIndex = "r_" + str(rootIn) sizeCal = str(fileSize)+'b' if fileSize > 1024: if fileSize > 1024*1024: if fileSize >1024*1024*1024: if fileSize >1024*1024*1024*1024: sizeCal = "{:.3f}".format(float(fileSize) / (1024*1024*1024*1024)) + 'T' else: sizeCal = "{:.3f}".format(float(fileSize) / (1024*1024*1024)) + 'G' else: sizeCal = "{:.3f}".format(float(fileSize) / (1024*1024)) + 'M' else: sizeCal = "{:.3f}".format(float(fileSize) / 1024) + 'k' fileNum = len(fileList) self.checkwithfile.insert('', "end", rootIndex, text=sizeCal+'('+str(fileNum)+')') indexFileGroup = {} groupedSameList = self.compareSameSizeFiles(fileList) childIn = 0 for groupIndex in range(len(groupedSameList)): #indexFileGroup[child] = fileList[groupIndex] textC = fileList[groupIndex] print("textC:"+textC) self.checkwithfile.tag_configure("evenrow", background='white', foreground='black') self.checkwithfile.tag_configure("oddrow", background='black', foreground='white') for i in range(len(groupedSameList[groupIndex])): childIndex = "c_" + str(fileSize) + '_i_' + str(childIn) if groupIndex % 2 == 0: self.checkwithfile.insert(rootIndex, "end", childIndex, text=fileList[i], tags=('evenrow',)) else: self.checkwithfile.insert(rootIndex, "end", childIndex, text=fileList[i], tags=('oddrow',)) if i > 0: self.checkwithfile.change_state(childIndex, 'checked') childIn = childIn + 1 rootIn = rootIn+1 def compareSameSizeFiles(self, fileList): groupComparedFiles=[] groupMap = [] compareIndex = "" groupComparedFiles.append(groupMap) for index in range(len(fileList)): fileName = fileList[index] groupMap = [] if len(groupComparedFiles) == 1 and len(groupComparedFiles[0]) == 0: groupComparedFiles[0].append(fileName) continue for i in range(len(groupComparedFiles)): (groupIndex, fileIndex) = self.getIndexInGroup(groupComparedFiles, fileName) if groupIndex == -1: outputStr = "Comparing: " + groupComparedFiles[i][0] + " and " + fileName + "\n" self.output.config(state=tk.NORMAL) self.output.insert("end", outputStr) self.output.see("end") self.output.config(state=tk.DISABLED) if filecmp.cmp(groupComparedFiles[i][0], fileName): groupComparedFiles[i].append(fileName) break else: groupMap.append(fileName) groupComparedFiles.append(groupMap) print("groupComparedFiles:") print(groupComparedFiles) return groupComparedFiles def getIndexInGroup(self, groupList, fileName): for groupIndex in range(len(groupList)): for fileIndex in range(len(groupList[groupIndex])): if groupList[groupIndex][fileIndex] == fileName: return (groupIndex, fileIndex) return (-1, -1) def groupFileListSameSize(self): if (len(self.fileLists)>0): fileSize = os.path.getsize(self.fileLists[0]) groupList = [] groupList.append(self.fileLists[0]) for i in range(len(self.fileLists)-1): fileNewSize = os.path.getsize(self.fileLists[i+1]) if(fileSize != fileNewSize): self.groupFiles[fileSize] = groupList fileSize = fileNewSize groupList = [] groupList.append(self.fileLists[i+1]) if len(groupList) > 0: self.groupFiles[fileSize] = groupList def deleteSelectedFiles(self): print("enter deleteSelectedFiles") for i in self.checkwithfile.get_children(): orgLen = len(self.checkwithfile.get_children(i)) newLen = orgLen for item in self.checkwithfile.get_children(i): if self.checkwithfile.tag_has("checked", item): fileName = self.checkwithfile.item(item)['text'] print(fileName) os.chmod(fileName, stat.S_IWUSR) os.remove(fileName) self.checkwithfile.delete(item) newLen -= 1 if orgLen > newLen: text = self.checkwithfile.item(i)['text'].replace("("+str(orgLen)+")", "("+str(newLen)+")") self.checkwithfile.item(i, text=text)
class main_Tab: def __init__(self, master, main_path=''): self.main_path = main_path self.master = master self.modules_config = {} #{module_name : } self.modules = {} self.fio = '' self.org = '' #Профили аудита self.profiles = {} self.current_profile_options = {} #Левый фрейм включает данные об аудиторе и о существующих профилях leftFrame = ttk.Frame(master) self.auditor_Frame = ttk.LabelFrame(leftFrame, text='Данные аудитора') self.profiles_Frame = VerticalScrolledFrame(leftFrame, text='Профили аудита') #Правый фрейм включает редактор текущего профиля и кнопку запуска аудита rightFrame = ttk.Frame(master) self.current_Frame = ttk.LabelFrame(rightFrame, text='Текущий профиль') self.btn_Frame = ttk.Frame(rightFrame) #Область данных об аудиторе fio_Label = ttk.Label(self.auditor_Frame, text='ФИО') self.fio_Entry = ttk.Entry(self.auditor_Frame, font=16, width=30) self.fio_Label = ttk.Label(self.auditor_Frame, font=16, width=30) org_Label = ttk.Label(self.auditor_Frame, text='Организация') self.org_Entry = ttk.Entry(self.auditor_Frame, font=16, width=30) self.org_Label = ttk.Label(self.auditor_Frame, font=16, width=30) self.fio_OK_btn = ttk.Button(self.auditor_Frame, text='OK', width=5, command=self.accept_auditor) fio_Label.grid(row=1, column=0, sticky='new', padx=10) self.fio_Entry.grid(row=2, column=0, sticky='nsew', padx=10) org_Label.grid(row=3, column=0, sticky='new', padx=10) self.org_Entry.grid(row=4, column=0, sticky='nsew', padx=10) self.fio_OK_btn.grid(row=5, column=0, sticky='se', padx=10) self.profileView = CheckboxTreeview(self.current_Frame) self.save_audit_btn = ttk.Button(self.btn_Frame, text='Сохранить профиль', command=self.save_btn_click) self.run_audit_btn = ttk.Button(self.btn_Frame, text='Запустить аудит', command=self.run_audit) self.ysb = ttk.Scrollbar(self.current_Frame, orient='vertical', command=self.profileView.yview) self.profileView.configure(yscroll=self.ysb.set) ttk.Style().configure('Vertical.TScrollbar', troughcolor='#f6f4f2', relief=tk.GROOVE) ttk.Style().configure('Treeview', background="#ffffff") ttk.Style().configure('Frame', background="#f6f4f2") #Размещения на фреймах leftFrame.pack(side=tk.LEFT, anchor='nw', fill=tk.Y) rightFrame.pack(side=tk.LEFT, anchor='nw', expand=1, fill=tk.BOTH) self.auditor_Frame.pack(side=tk.TOP, anchor='nw', padx=5, pady=5, fill=tk.X) self.profiles_Frame.pack(side=tk.TOP, anchor='sw', padx=5, pady=10, fill=tk.BOTH, expand=1) self.current_Frame.pack(side=tk.TOP, anchor='nw', padx=5, pady=5, fill=tk.BOTH, expand=1) self.btn_Frame.pack(side=tk.TOP, anchor='nw', fill=tk.X) self.ysb.pack(side=tk.RIGHT, anchor='n', fill=tk.Y) self.profileView.pack(side=tk.TOP, anchor='nw', fill=tk.BOTH, expand=1) self.save_audit_btn.pack(side=tk.LEFT, anchor='sw', padx=5, pady=5) self.run_audit_btn.pack(side=tk.LEFT, anchor='se', padx=5, pady=5, fill=tk.X, expand=1) self.auditor_Frame.grid_rowconfigure(2, minsize=30) self.loadProfiles() self.profileView.bind("<Button-1>", self.check_uncheck_item, True) def run_audit(self): for tab_name, tab in self.modules.items(): tab.run_audit() def sync_vars(self): for tab_name, tab in self.modules.items(): if type(self.current_profile_options[tab_name][1]) != type({}): i = 0 for var in tab.vars: var.set(self.current_profile_options[tab_name][1][i]) i += 1 else: i = len(tab.vars) - 1 for second_tab_name, second_tab in self.current_profile_options[ tab_name][1].items(): for j in range(len(second_tab[1]) - 1, -1, -1): tab.vars[i].set(second_tab[1][j]) i -= 1 def accept_auditor(self): if self.fio_OK_btn.cget('text') == 'OK': self.fio = self.fio_Entry.get() self.org = self.org_Entry.get() self.fio_OK_btn.configure(text='Изменить', width=10) self.fio_Entry.grid_remove() self.org_Entry.grid_remove() self.fio_Label.configure(text=self.fio) self.org_Label.configure(text=self.org) self.fio_Label.grid(row=2, column=0, sticky='nsew', padx=10) self.org_Label.grid(row=4, column=0, sticky='nsew', padx=10) else: self.fio_OK_btn.configure(text='OK', width=5) self.fio_Entry.grid() self.org_Entry.grid() self.fio_Label.grid_remove() self.org_Label.grid_remove() def loadProfiles(self): for child in self.profiles_Frame.interior.winfo_children(): child.destroy() #self.profiles_Frame.pack(side=tk.TOP, anchor='sw', padx=5, pady = 10, fill=tk.BOTH, expand=1) try: with open(self.main_path + '/profiles.json') as file: self.profiles = json.load(file) except: with open(self.main_path + '/profiles.json', 'w') as file: json.dump(self.profiles, file) prof_count = len(self.profiles) self.var = tk.IntVar() for i in range(prof_count): tk.Radiobutton(self.profiles_Frame.interior, variable=self.var, value=i, indicator=0, height=3, text=list(self.profiles.keys())[i], selectcolor="#f19572", activebackground="#f19572", command=self.changeCurrentProfile).pack(side=tk.TOP, anchor='nw', fill=tk.X, padx=5, pady=2) def dumpProfiles(self): with open(self.main_path + '/profiles.json', 'w') as file: json.dump(self.profiles, file) def changeCurrentProfile(self): currentProfileName = list(self.profiles.keys())[self.var.get()] self.current_profile_options = copy.deepcopy( self.profiles[currentProfileName]) self.profileView.heading('#0', text=list( self.profiles.keys())[self.var.get()], anchor='w') self.initTree() self.sync_vars() #self.profiles_Label.configure(text=self.var.get()) def save_btn_click(self): self.saveDialog = tk.Toplevel() save_frame = ttk.Frame(self.saveDialog) self.saveDialog.focus_set() width = self.master.winfo_screenwidth() // 5 + 96 height = self.master.winfo_screenheight() // 5 dw = (self.master.winfo_screenwidth() - width) // 2 dh = (self.master.winfo_screenheight() - height) // 2 self.saveDialog.geometry('{}x{}+{}+{}'.format(width, height, dw, dh)) self.saveDialog.resizable(False, False) self.saveDialog.title('Сохранение профиля') # Настройка виджетов в окне ввода пароля save_Label = ttk.Label(save_frame, text='Названия профиля') self.save_Entry = ttk.Entry(save_frame, font=16) save_Btn = ttk.Button(save_frame, text='Сохранить', width=15) self.save_Entry.bind('<Return>', self.save_click) save_Btn.bind('<Button-1>', self.save_click) save_Label.grid(row=1, column=0) self.save_Entry.grid(row=2, column=0, padx=10, sticky='nsew') save_Btn.grid(row=3, column=0, padx=10, pady=20, sticky='e') save_frame.pack(fill=tk.BOTH) save_frame.grid_rowconfigure(1, minsize=height // 3) save_frame.grid_rowconfigure(2, minsize=30) save_frame.grid_columnconfigure(0, minsize=width) self.save_Entry.focus_set() def save_click(self, event): """Функция-обработчик нажатия кнопки """ profile_name = self.save_Entry.get() self.profiles[profile_name] = self.current_profile_options self.dumpProfiles() self.loadProfiles() self.saveDialog.destroy() self.initTree() def initTree(self): if self.current_profile_options == {}: self.current_profile_options = dict( self.profiles["Профиль по умолчанию"]) for (key, value) in self.current_profile_options.items(): if self.modules_config.__contains__(key): if not self.profileView.exists(key): self.profileView.insert("", "end", key, text=key) self.profileView.change_state(key, value[0]) value = value[1] if type(value) == type("1"): i = 0 for func_key in self.modules_config[key][0].keys(): func_key = func_key.replace('\n', ' ') if not self.profileView.exists(key + func_key + "_" + str(i)): self.profileView.insert(key, "end", key + func_key + "_" + str(i), text=func_key) if value[i] == '0': self.profileView.change_state( key + func_key + "_" + str(i), 'unchecked') if value[i] == '1': self.profileView.change_state( key + func_key + "_" + str(i), 'checked') i += 1 else: j = 0 for (second_key, second_value) in dict(value).items(): if not self.profileView.exists(second_key): self.profileView.insert(key, "end", second_key, text=second_key) self.profileView.change_state(second_key, second_value[0]) second_value = second_value[1] if type(value[second_key][1]) == type("1"): i = 0 for func_key in self.modules_config[key][0][ j].keys(): func_key = func_key.replace('\n', ' ') if not self.profileView.exists(second_key + func_key + "_" + str(i)): self.profileView.insert( second_key, "end", second_key + func_key + "_" + str(i), text=func_key) if value[second_key][1][i] == '0': self.profileView.change_state( second_key + func_key + "_" + str(i), 'unchecked') if value[second_key][1][i] == '1': self.profileView.change_state( second_key + func_key + "_" + str(i), 'checked') i += 1 j += 1 def check_uncheck_item(self, event): x, y, widget = event.x, event.y, event.widget elem = widget.identify("element", x, y) if "image" in elem: # a box was clicked item = self.profileView.identify_row(y) children = self.profileView.get_children(item) parents = [] parent = widget.parent(item) i = 0 while parent != '': parents.append(parent) parent = widget.parent(parent) if parents and children == (): tag = self.profileView.item(parents[-1], "tags") self.current_profile_options[parents[-1]][0] = tag[0] if len(parents) == 2: tag = self.profileView.item(parents[0], "tags") self.current_profile_options[parents[-1]][1][ parents[-2]][0] = tag[0] tag = self.profileView.item(item, "tags") i = int(item.split('_')[1]) varL = self.current_profile_options[parents[-1]][1][ parents[-2]][1][0:i] varR = self.current_profile_options[parents[-1]][1][ parents[-2]][1][i + 1:] if "checked" in tag: self.current_profile_options[parents[-1]][1][ parents[-2]][1] = varL + '1' + varR else: self.current_profile_options[parents[-1]][1][ parents[-2]][1] = varL + '0' + varR else: tag = self.profileView.item(item, "tags") i = int(item.split('_')[1]) varL = self.current_profile_options[parents[-1]][1][0:i] varR = self.current_profile_options[parents[-1]][1][i + 1:] if "checked" in tag: self.current_profile_options[ parents[-1]][1] = varL + '1' + varR else: self.current_profile_options[ parents[-1]][1] = varL + '0' + varR else: tag = self.profileView.item(item, "tags") self.current_profile_options[item][0] = tag[0] profile_1 = '' for child in self.profileView.get_children(item): children = self.profileView.get_children(child) if children != (): tag = self.profileView.item(child, "tags") self.current_profile_options[item][1][child][0] = tag[ 0] profile = '' for s_child in self.profileView.get_children(child): tag = self.profileView.item(s_child, "tags") if 'checked' in tag: profile += '1' else: profile += '0' self.current_profile_options[item][1][child][ 1] = profile else: tag = self.profileView.item(child, "tags") if 'checked' in tag: profile_1 += '1' else: profile_1 += '0' self.current_profile_options[item][1] = profile_1 self.sync_vars()