From 921ee3a201dbf34864d543ea64e6975110845e63 Mon Sep 17 00:00:00 2001 From: Maxim Romanko Date: Thu, 22 Feb 2024 13:44:51 +0300 Subject: [PATCH] [ADD] New classes for GUI, added parsing linux drives --- app/__init__.py | 91 ++++++++++++++++++++++++++++++++++ {utils => app}/globals.py | 0 {utils => app}/utils_drive.py | 2 +- main.py | 27 ++-------- req.txt | Bin 0 -> 140 bytes requirements.txt | Bin 84 -> 0 bytes 6 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 app/__init__.py rename {utils => app}/globals.py (100%) rename {utils => app}/utils_drive.py (93%) create mode 100644 req.txt delete mode 100644 requirements.txt diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..1ccabc6 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,91 @@ +from typing import List +from app.utils_drive import Drive +from app.globals import * +from tkinter import * +from tkinter import ttk + +class App: + + drives: List[Drive] = [] + + selected = None + + drive_tree = None + + def __init__(self): + self.root = Tk() + self.root.title("Disk Eraser") + self.root.geometry("400x300") + self.root.minsize(400, 300) + self.__load_menu() + self.__init_table() + self.root.mainloop() + + def __load_menu(self): + main_menu = Menu() + + help_menu = Menu(tearoff=0) + help_menu.add_command(label="Содержание") + help_menu.add_separator() + help_menu.add_command(label="О программе") + + main_menu.add_cascade(label="Справка", menu=help_menu) + bottom_frame = ttk.Frame(borderwidth=1, relief=SOLID, padding=[3, 5]) + erase_button = ttk.Button(bottom_frame, text="Стереть диск") + erase_button.pack(side=RIGHT) + bottom_frame.pack(side=BOTTOM, fill=X) + self.root.config(menu=main_menu) + + def __load_drives(self): + + if OS_TYPE == "Windows": + import wmi + c = wmi.WMI() + self.drives = [] + if disks := c.Win32_DiskDrive(): + for disk in disks: + self.drives.append(Drive(disk.Model, disk.Name, disk.InterfaceType, disk.DefaultBlockSize, int(disk.Size))) + elif OS_TYPE == "Linux": + from diskinfo import Disk, DiskInfo + di = DiskInfo() + disks = di.get_disk_list(sorting=True) + self.drives = [] + for d in disks: + self.drives.append(Drive(d.get_model(), d.get_path(), d.get_type_str(), d.get_logical_block_size(), int(d.get_size()*512))) + + def __init_table(self): + + self.__load_drives() + + # определяем столбцы + columns = ("name", "type", "capacity") + self.drive_tree = ttk.Treeview(columns=columns, show="headings", selectmode="browse") + self.drive_tree.pack(fill=BOTH, expand=1, side=TOP) + + # определяем заголовки + self.drive_tree.heading("name", text="Имя", anchor=W) + self.drive_tree.heading("type", text="Тип", anchor=W) + self.drive_tree.heading("capacity", text="Объем", anchor=W) + + self.drive_tree.column("#1", stretch=YES, width=150, minwidth=120) + self.drive_tree.column("#2", stretch=NO, width=60, minwidth=60) + self.drive_tree.column("#3", stretch=NO, width=100, minwidth=100) + + # добавляем данные + for drive in self.drives: + self.drive_tree.insert("", END, values=(drive.name, drive.disk_type, self.__human_size(drive.capacity))) + + self.drive_tree.bind("<>", self.__drive_selected) + + def __human_size(self, size): + units = ['Б', 'КБ', 'МБ', 'ГБ', 'ТБ', 'ПБ'] + for unit in units: + if size < 1024: + return f"{size:.1f} {unit}" + size /= 1024 + + def __drive_selected(self, event): + for selected_item in self.drive_tree.selection(): + item = self.drive_tree.item(selected_item) + self.selected = item["values"] + print(self.selected) diff --git a/utils/globals.py b/app/globals.py similarity index 100% rename from utils/globals.py rename to app/globals.py diff --git a/utils/utils_drive.py b/app/utils_drive.py similarity index 93% rename from utils/utils_drive.py rename to app/utils_drive.py index 7f31813..79e4d2c 100644 --- a/utils/utils_drive.py +++ b/app/utils_drive.py @@ -1,4 +1,4 @@ -from utils.globals import OS_TYPE +from app.globals import OS_TYPE class Drive(): diff --git a/main.py b/main.py index cde6917..b1c9d1d 100644 --- a/main.py +++ b/main.py @@ -1,25 +1,8 @@ -from utils.utils_drive import Drive -from utils.globals import * +from app import App -if OS_TYPE == "Windows": - import wmi - c = wmi.WMI() - drives = [] - if disks := c.Win32_DiskDrive(): - for disk in disks: - drives.append(Drive(disk.Model, disk.Name, disk.InterfaceType, disk.DefaultBlockSize, int(disk.Size))) -elif OS_TYPE == "Linux": - import subprocess +def main(): + app = App() - # Execute the lsblk command to get information about block devices - result = subprocess.run(['lsblk', '-o', 'NAME,SIZE,MODEL,VENDOR,TRAN'], capture_output=True, text=True) - - # Get the output - output = result.stdout - - # Вывод информации о дисках - print(output) - -# for drive in drives: -# print(f"{drive.name} | {drive.path} | {drive.disk_type} | {drive.block_size} | {drive.capacity}") +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/req.txt b/req.txt new file mode 100644 index 0000000000000000000000000000000000000000..234ef277bee7267979415b3ffe284e00ecc688d3 GIT binary patch literal 140 zcmXYq(F#CN5Jb-V~2W!