GNOME dependable installation, structure and style improvements

Split utils in utils.py to a directory
Moved ./gnome-shell_css/ to ./gnome-shell/.css/
This commit is contained in:
Vladyslav Hroshev
2024-09-30 00:14:09 +03:00
parent 48975c9b07
commit 8cce85a437
40 changed files with 413 additions and 306 deletions

10
scripts/utils/__init__.py Normal file
View File

@@ -0,0 +1,10 @@
from .concatenate_files import concatenate_files
from .copy_files import copy_files
from .destinaiton_return import destination_return
from .generate_file import generate_file
from .hex_to_rgba import hex_to_rgba
from .label_files import label_files
from .remove_files import remove_files
from .remove_keywords import remove_keywords
from .remove_properties import remove_properties
from .replace_keywords import replace_keywords

View File

@@ -0,0 +1,12 @@
def concatenate_files(edit_file, file):
"""
Merge two files
:param edit_file: where it will be appended
:param file: file you want to append
"""
with open(file, 'r') as read_file:
file_content = read_file.read()
with open(edit_file, 'a') as write_file:
write_file.write('\n' + file_content)

View File

@@ -0,0 +1,12 @@
import os
def copy_files(source, destination):
"""
Copy files from the source to another directory
:param source: where files will be copied
:param destination: where files will be pasted
"""
destination = os.path.expanduser(destination) # expand ~ to /home/user
os.makedirs(destination, exist_ok=True)
os.system(f"cp -aT {source} {destination}")

View File

@@ -0,0 +1,11 @@
def destination_return(themes_folder, path_name, theme_mode, theme_type):
"""
Copied/modified theme location
:param themes_folder: themes folder location
:param path_name: color name
:param theme_mode: theme name (light or dark)
:param theme_type: theme type (gnome-shell, gtk-4.0, ...)
:return: copied files' folder location
"""
return f"{themes_folder}/Marble-{path_name}-{theme_mode}/{theme_type}/"

View File

@@ -0,0 +1,37 @@
import os
import shutil
from .gnome import gnome_version
from .get_version_folder import get_version_folders
def generate_file(folder, final_file):
"""
Combines all files in a folder into a single file
:param folder: source folder
:param final_file: location where file will be created
"""
opened_file = open(final_file, "w")
css_folder = f"{folder}/.css/"
for file in os.listdir(css_folder):
with open(os.path.join(css_folder, file)) as f:
opened_file.write(f.read() + '\n')
version = gnome_version()
if version:
base_path = f"{folder}/.versions/"
version_folders = get_version_folders(version, base_path)
for version_folder in version_folders:
version_path = os.path.join(base_path, version_folder)
css_path = os.path.join(version_path, '.css')
for css_file in os.listdir(css_path):
with open(os.path.join(css_path, css_file)) as f:
opened_file.write(f.read() + '\n')
for file in os.listdir(version_path):
if file.endswith('.svg'):
shutil.move(os.path.join(version_path, file), folder)
opened_file.close()

View File

@@ -0,0 +1,29 @@
import os
def get_version_folders(version, base_path):
"""
Get version folders
:param version: gnome-shell version
:param base_path: base path to version folders
:return: list of matching version folders
"""
version_folders = os.listdir(base_path)
version = int(version.split('.')[0]) # Use only the major version for comparison
matching_folders = []
for folder in version_folders:
if '..' in folder:
from_version, to_version = folder.split('..')
if from_version and to_version:
if int(from_version) <= version <= int(to_version):
matching_folders.append(folder)
elif from_version:
if version >= int(from_version):
matching_folders.append(folder)
elif to_version:
if version <= int(to_version):
matching_folders.append(folder)
elif int(folder) == version:
matching_folders.append(folder)
return matching_folders

32
scripts/utils/gnome.py Normal file
View File

@@ -0,0 +1,32 @@
import subprocess
def gnome_version():
"""
Get gnome-shell version
"""
try:
output = subprocess.check_output(['gnome-shell', '--version'], text=True).strip()
return output.split(' ')[2]
except subprocess.CalledProcessError:
return None
def apply_gnome_theme(theme=None):
"""
Apply gnome-shell theme
:param theme: theme name
"""
try:
if theme is None:
current_theme = subprocess.check_output(['dconf', 'read', '/org/gnome/shell/extensions/user-theme/name'], text=True).strip().strip("'")
if current_theme.startswith("Marble"):
theme = current_theme
else:
return False
subprocess.run(['dconf', 'reset', '/org/gnome/shell/extensions/user-theme/name'], check=True)
subprocess.run(['dconf', 'write', '/org/gnome/shell/extensions/user-theme/name', f"'{theme}'"], check=True)
except subprocess.CalledProcessError:
return False
return True

View File

@@ -0,0 +1,22 @@
def hex_to_rgba(hex_color):
"""
Convert hex(a) to rgba
:param hex_color: input value
"""
try:
if len(hex_color) in range(6, 10):
hex_color = hex_color.lstrip('#') + "ff"
# if is convertable
int(hex_color[:], 16)
else:
raise ValueError
except ValueError:
raise ValueError(f'Error: Invalid HEX color code: {hex_color}')
else:
return int(hex_color[0:2], 16), \
int(hex_color[2:4], 16), \
int(hex_color[4:6], 16), \
int(hex_color[6:8], 16) / 255

View File

@@ -0,0 +1,44 @@
import os
def label_files(directory, label, *args):
"""
Add a label to all files in a directory
:param directory: folder where files are located
:param label: label to add
:param args: files to change links to labeled files
:return:
"""
# Open all files
files = [open(file, 'r') for file in args]
read_files = []
filenames = []
for filename in os.listdir(directory):
# Skip if the file is already labeled
if label in filename:
continue
# Split the filename into name and extension
name, extension = os.path.splitext(filename)
# Form the new filename and rename the file
new_filename = f"{name}-{label}{extension}"
os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
filenames.append((filename, new_filename))
# Replace the filename in all files
for i, file in enumerate(files):
read_file = file.read()
read_file.replace(filenames[i][0], filenames[i][1])
read_files.append(read_file)
file.close()
write_files = [open(file, 'w') for file in args]
# Write the changes to the files and close them
for i, file in enumerate(write_files):
file.write(read_files[i])
file.close()

View File

@@ -0,0 +1,48 @@
# TODO: Less verbose output for the user and simplify the code
from .. import config
import os
def remove_files():
"""
Delete already installed Marble theme
"""
paths = (config.themes_folder, "~/.local/share/themes")
print("💡 You do not need to delete files if you want to update theme.\n")
confirmation = input(f"Do you want to delete all \"Marble\" folders in {' and in '.join(paths)}? (y/N) ").lower()
if confirmation == "y":
for path in paths:
# Check if the path exists
if os.path.exists(os.path.expanduser(path)):
# Get the list of folders in the path
folders = os.listdir(os.path.expanduser(path))
# toggle if folder has no marble theme
found_folder = False
for folder in folders:
if folder.startswith("Marble"):
folder_path = os.path.join(os.path.expanduser(path), folder)
print(f"Deleting folder {folder_path}...", end='')
try:
os.system(f"rm -r {folder_path}")
except Exception as e:
print(f"Error deleting folder {folder_path}: {e}")
else:
found_folder = True
print("Done.")
if not found_folder:
print(f"No folders starting with \"Marble\" found in {path}.")
else:
print(f"The path {path} does not exist.")

View File

@@ -0,0 +1,15 @@
def remove_keywords(file, *args):
"""
Remove keywords from a file
:param file: file name
:param args: keywords to remove
"""
with open(file, "r") as read_file:
content = read_file.read()
for arg in args:
content = content.replace(arg, "")
with open(file, "w") as write_file:
write_file.write(content)

View File

@@ -0,0 +1,20 @@
def remove_properties(file, *args):
"""
Remove properties from a file
:param file: file name
:param args: properties to remove
"""
new_content = ""
with open(file, "r") as read_file:
content = read_file.read()
for line in content.splitlines():
if not any(prop in line for prop in args):
new_content += line + "\n"
elif "}" in line:
new_content += "}\n"
with open(file, "w") as write_file:
write_file.write(new_content)

View File

@@ -0,0 +1,19 @@
def replace_keywords(file, *args):
"""
Replace file with several keywords
:param file: file name where keywords must be replaced
:param args: (keyword, replacement), (...), ...
"""
# skip binary files in project
if not file.lower().endswith(('.css', '.scss', '.svg')):
return
with open(file, "r") as read_file:
content = read_file.read()
for keyword, replacement in args:
content = content.replace(keyword, replacement)
with open(file, "w") as write_file:
write_file.write(content)