Skip to content

Commit

Permalink
remove os import and add function to get file size
Browse files Browse the repository at this point in the history
  • Loading branch information
Ati1707 committed Nov 14, 2024
1 parent ced888e commit 02c7954
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
42 changes: 30 additions & 12 deletions helper/file_operations.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
import os
from pathlib import Path, PurePath
import shutil


def get_file_from_path(file_path):
return os.path.basename(file_path)
return PurePath(file_path).name

def split_file_and_extension(file):
return file.rpartition(".")[0], file.rpartition(".")[-1]
def get_file_name_without_extension(file):
return file.rpartition(".")[0]

def create_folders():
if not os.path.exists("database/"):
os.makedirs("database/")
if not os.path.exists("downloads/"):
os.makedirs("downloads/")
if not Path.exists(Path("database/")):
Path.mkdir(Path("database/"))

def create_temp_folder():
if not os.path.exists("temp/"):
os.makedirs("temp/")
if not Path.exists(Path("temp/")):
Path.mkdir(Path("temp/"))

def delete_temp_folder():
if os.path.exists("temp/"):
shutil.rmtree("temp/")
if Path.exists(Path("temp/")):
shutil.rmtree("temp/")

def get_file_size(file_path):
return _convert_size(Path(file_path).stat().st_size)

def _convert_size(size_bytes):
# Edge case for size 0 bytes
if size_bytes == 0:
return "0 B"

# List of size units
size_units = ["B", "KB", "MB", "GB"]
i = 0

# Convert to larger units until size is below 1024
while size_bytes >= 1024 and i < len(size_units) - 1:
size_bytes /= 1024
i += 1

# Return formatted size with 2 decimal places
return f"{size_bytes:.2f} {size_units[i]}"
16 changes: 12 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from tkinter.constants import DISABLED

import customtkinter as ctk
from customtkinter import CTk, filedialog
from customtkinter import CTk, filedialog, CTkLabel
import pywinstyles
from content_database import get_archives, delete_archive
from installer import start_installer_gui
Expand Down Expand Up @@ -38,6 +38,7 @@ def __init__(self, parent, tab_name: str, asset_name: str = "", file_path: str =
super().__init__(parent)
self.asset_name = asset_name
self.file_path = file_path
self.file_size = file_operations.get_file_size(self.file_path)

# Checkbox for the asset
self.checkbox = ctk.CTkCheckBox(self, text=truncate_string(asset_name))
Expand All @@ -48,15 +49,22 @@ def __init__(self, parent, tab_name: str, asset_name: str = "", file_path: str =

# Install button
if tab_name == "Install":
self.label = CTkLabel(self, text=self.file_size)
self.label.grid(row=0, column=1, padx=20, pady=10, sticky="w")

# Install button
self.button = ctk.CTkButton(self, text=tab_name, command=lambda: start_install_thread(self.install_asset))
self.button.grid(row=0, column=1, padx=20, pady=10, sticky="e")
self.button.grid(row=0, column=2, padx=20, pady=10, sticky="e")
else:

# Uninstall button
self.button = ctk.CTkButton(self, text=tab_name, command=self.remove_asset)
self.button.grid(row=0, column=1, padx=20, pady=10, sticky="e")

# Column configuration for layout
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=0)
self.columnconfigure(2, weight=0)


def install_asset(self):
Expand Down Expand Up @@ -163,7 +171,7 @@ def select_file(self):
file_path = filedialog.askopenfilename()
if file_path:
file_name = file_operations.get_file_from_path(file_path)
asset_name, _ = file_operations.split_file_and_extension(file_name)
asset_name = file_operations.get_file_name_without_extension(file_name)
self.add_asset_widget(asset_name, file_path)

def install_assets(self):
Expand All @@ -190,7 +198,7 @@ def drop_files(self, files):
"""Handles file drop to create asset widgets."""
for file_path in files:
file_name = file_operations.get_file_from_path(file_path)
asset_name, _ = file_operations.split_file_and_extension(file_name)
asset_name = file_operations.get_file_name_without_extension(file_name)
self.after(50, self.add_asset_widget, asset_name, file_path)


Expand Down

0 comments on commit 02c7954

Please sign in to comment.