Run installation concurrently (Unexpected behavior), class for managing console lines

This commit is contained in:
Vladyslav Hroshev
2025-04-01 19:11:06 +03:00
parent e5f8662269
commit 038c2fcda1
4 changed files with 190 additions and 85 deletions

View File

@@ -8,6 +8,7 @@ from .utils import (
copy_files, # copy files from source to destination
destination_return, # copied/modified theme location
generate_file) # combine files from folder to one file
from .utils.console import Console, Color, Format
class Theme:
@@ -28,7 +29,7 @@ class Theme:
self.temp_folder = f"{temp_folder}/{theme_type}"
self.theme_folder = theme_folder
self.theme_type = theme_type
self.mode = [mode] if mode else ['light', 'dark']
self.modes = [mode] if mode else ['light', 'dark']
self.destination_folder = destination_folder
self.main_styles = f"{self.temp_folder}/{theme_type}.css"
self.is_filled = is_filled
@@ -58,9 +59,81 @@ class Theme:
return self
def __del__(self):
# delete temp folder
shutil.rmtree(self.temp_folder, ignore_errors=True)
def prepare(self):
# move files to temp folder
copy_files(self.theme_folder, self.temp_folder)
generate_file(f"{self.theme_folder}", self.temp_folder, self.main_styles)
# after generating main styles, remove .css and .versions folders
shutil.rmtree(f"{self.temp_folder}/.css/", ignore_errors=True)
shutil.rmtree(f"{self.temp_folder}/.versions/", ignore_errors=True)
# if theme is filled
if self.is_filled:
for apply_file in os.listdir(f"{self.temp_folder}/"):
replace_keywords(f"{self.temp_folder}/{apply_file}",
("BUTTON-COLOR", "ACCENT-FILLED-COLOR"),
("BUTTON_HOVER", "ACCENT-FILLED_HOVER"),
("BUTTON_ACTIVE", "ACCENT-FILLED_ACTIVE"),
("BUTTON_INSENSITIVE", "ACCENT-FILLED_INSENSITIVE"),
("BUTTON-TEXT-COLOR", "TEXT-BLACK-COLOR"),
("BUTTON-TEXT_SECONDARY", "TEXT-BLACK_SECONDARY"))
def add_to_start(self, content):
"""
Add content to the start of main styles
:param content: content to add
"""
with open(self.main_styles, 'r') as main_styles:
main_content = main_styles.read()
with open(self.main_styles, 'w') as main_styles:
main_styles.write(content + '\n' + main_content)
def install(self, hue, name: str, sat=None, destination=None):
"""
Copy files and generate theme with specified accent color
:param hue
:param name: theme name
:param sat
:param destination: folder where theme will be installed
"""
joint_modes = f"({', '.join(self.modes)})"
line = Console.Line(name)
formatted_name = Console.format(name.capitalize(), color=Color.get(name), format_type=Format.BOLD)
formatted_mode = Console.format(joint_modes, color=Color.GRAY)
line.update(f"Creating {formatted_name} {formatted_mode} theme...")
try:
self._install_and_apply_theme(hue, name, sat=sat, destination=destination)
line.success(f"{formatted_name} {formatted_mode} theme created successfully.")
except Exception as err:
line.error(f"Error installing {formatted_name} theme: {str(err)}")
def _install_and_apply_theme(self, hue, name, sat=None, destination=None):
is_dest = bool(destination)
for mode in self.modes:
if not is_dest:
destination = destination_return(self.destination_folder, name, mode, self.theme_type)
copy_files(self.temp_folder + '/', destination)
self.__apply_theme(hue, self.temp_folder, destination, mode, sat=sat)
def __apply_theme(self, hue, source, destination, theme_mode, sat=None):
"""
Apply theme to all files in directory
:param hue
:param source
:param destination: file directory
:param theme_mode: theme name (light or dark)
:param sat: color saturation (optional)
"""
for apply_file in os.listdir(f"{source}/"):
self.__apply_colors(hue, destination, theme_mode, apply_file, sat=sat)
def __apply_colors(self, hue, destination, theme_mode, apply_file, sat=None):
"""
@@ -96,75 +169,4 @@ class Theme:
replaced_colors.append((element, f"rgba({red}, {green}, {blue}, {alpha})"))
# replace colors
replace_keywords(os.path.expanduser(f"{destination}/{apply_file}"), *replaced_colors)
def __apply_theme(self, hue, source, destination, theme_mode, sat=None):
"""
Apply theme to all files in directory
:param hue
:param source
:param destination: file directory
:param theme_mode: theme name (light or dark)
:param sat: color saturation (optional)
"""
for apply_file in os.listdir(f"{source}/"):
self.__apply_colors(hue, destination, theme_mode, apply_file, sat=sat)
def prepare(self):
# move files to temp folder
copy_files(self.theme_folder, self.temp_folder)
generate_file(f"{self.theme_folder}", self.temp_folder, self.main_styles)
# after generating main styles, remove .css and .versions folders
shutil.rmtree(f"{self.temp_folder}/.css/", ignore_errors=True)
shutil.rmtree(f"{self.temp_folder}/.versions/", ignore_errors=True)
# if theme is filled
if self.is_filled:
for apply_file in os.listdir(f"{self.temp_folder}/"):
replace_keywords(f"{self.temp_folder}/{apply_file}",
("BUTTON-COLOR", "ACCENT-FILLED-COLOR"),
("BUTTON_HOVER", "ACCENT-FILLED_HOVER"),
("BUTTON_ACTIVE", "ACCENT-FILLED_ACTIVE"),
("BUTTON_INSENSITIVE", "ACCENT-FILLED_INSENSITIVE"),
("BUTTON-TEXT-COLOR", "TEXT-BLACK-COLOR"),
("BUTTON-TEXT_SECONDARY", "TEXT-BLACK_SECONDARY"))
def install(self, hue, name, sat=None, destination=None):
"""
Copy files and generate theme with different accent color
:param hue
:param name: theme name
:param sat: color saturation (optional)
:param destination: folder where theme will be installed
"""
is_dest = bool(destination)
print(f"Creating {name} {', '.join(self.mode)} theme...", end=" ")
try:
for mode in self.mode:
if not is_dest:
destination = destination_return(self.destination_folder, name, mode, self.theme_type)
copy_files(self.temp_folder + '/', destination)
self.__apply_theme(hue, self.temp_folder, destination, mode, sat=sat)
except Exception as err:
print("\nError: " + str(err))
else:
print("Done.")
def add_to_start(self, content):
"""
Add content to the start of main styles
:param content: content to add
"""
with open(self.main_styles, 'r') as main_styles:
main_content = main_styles.read()
with open(self.main_styles, 'w') as main_styles:
main_styles.write(content + '\n' + main_content)
replace_keywords(os.path.expanduser(f"{destination}/{apply_file}"), *replaced_colors)