Refactor installation code to improve performance and readability

This commit is contained in:
Vladyslav Hroshev
2024-03-23 22:37:33 +02:00
parent 70694411b8
commit abced98cf2

View File

@@ -16,7 +16,6 @@
import json # working with json files import json # working with json files
import os # system commands, working with files
import argparse # command-line options import argparse # command-line options
import shutil import shutil
import textwrap # example text in argparse import textwrap # example text in argparse
@@ -31,7 +30,12 @@ from scripts.theme import Theme
from scripts.gdm import GlobalTheme from scripts.gdm import GlobalTheme
def main(): def parse_args():
"""
Parse command-line arguments
:return: parsed arguments
"""
# script description # script description
parser = argparse.ArgumentParser(prog="python install.py", parser = argparse.ArgumentParser(prog="python install.py",
formatter_class=argparse.RawDescriptionHelpFormatter, formatter_class=argparse.RawDescriptionHelpFormatter,
@@ -84,19 +88,96 @@ def main():
overview_args = parser.add_argument_group('Overview tweaks') overview_args = parser.add_argument_group('Overview tweaks')
overview_args.add_argument('--launchpad', action='store_true', help='change Show Apps icon to MacOS Launchpad icon') overview_args.add_argument('--launchpad', action='store_true', help='change Show Apps icon to MacOS Launchpad icon')
args = parser.parse_args() return parser.parse_args()
colors = json.load(open(config.colors_json))
if args.gdm: def apply_tweaks(args, theme):
gdm_status = 1 """
if os.geteuid() != 0: Apply theme tweaks
print("You must run this script as root to install GDM theme.") :param args: parsed arguments
return 1 :param theme: Theme object
"""
if args.all: if args.panel_default_size:
print("Error: You can't install all colors for GDM theme. Use specific color.") with open(f"{config.tweaks_folder}/panel/def-size.css", "r") as f:
return 1 theme += f.read()
if args.panel_no_pill:
with open(f"{config.tweaks_folder}/panel/no-pill.css", "r") as f:
theme += f.read()
if args.panel_text_color:
theme += ".panel-button,\
.clock,\
.clock-display StIcon {\
color: rgba(" + ', '.join(map(str, hex_to_rgba(args.panel_text_color))) + ");\
}"
if args.launchpad:
with open(f"{config.tweaks_folder}/launchpad/launchpad.css", "r") as f:
theme += f.read()
theme *= f"{config.tweaks_folder}/launchpad/launchpad.png"
def install_theme(theme, hue, theme_name, sat, gdm=False):
"""
Check if GDM and install theme
:param theme: object to install
:param hue: color hue
:param theme_name: future theme name
:param sat: color saturation
:param gdm: if GDM theme
"""
if gdm:
theme.install(hue, sat)
else:
theme.install(hue, theme_name, sat)
def apply_colors(args, theme, colors, gdm=False):
"""
Apply accent colors to the theme
:param args: parsed arguments
:param theme: Theme object
:param colors: colors from colors.json
:param gdm: if GDM theme
"""
is_colors = False # check if any color arguments specified
# if custom color
if args.hue:
hue = args.hue
theme_name = args.name if args.name else f'hue{hue}'
install_theme(theme, hue, theme_name, args.sat, gdm)
return
else:
for color in colors["colors"]:
if args.all or getattr(args, color):
is_colors = True
hue = colors["colors"][color]["h"]
# if saturation already defined in color (gray)
sat = colors["colors"][color]["s"] if colors["colors"][color]["s"] is not None else args.sat
install_theme(theme, hue, color, sat, gdm)
if gdm:
return
if not is_colors:
print('No color arguments specified. Use -h or --help to see the available options.')
def global_theme(args, colors):
"""
Apply GDM theme
:param args: parsed arguments
:param colors: colors from colors.json
"""
gdm_theme = GlobalTheme(colors, f"{config.raw_theme_folder}/{config.gnome_folder}", gdm_theme = GlobalTheme(colors, f"{config.raw_theme_folder}/{config.gnome_folder}",
config.global_gnome_shell_theme, config.gnome_shell_gresource, config.global_gnome_shell_theme, config.gnome_shell_gresource,
@@ -108,30 +189,24 @@ def main():
print("GDM theme removed successfully.") print("GDM theme removed successfully.")
return 0 return 0
if args.red or args.pink or args.purple or args.blue or args.green or args.yellow or args.gray: try:
for color in colors["colors"]: apply_colors(args, gdm_theme, colors, gdm=True)
if getattr(args, color): except Exception as e:
hue = colors["colors"][color]["h"] print(f"Error: {e}")
sat = colors["colors"][color]["s"] if colors["colors"][color]["s"] is not None else args.sat return 1
gdm_status = gdm_theme.install(hue, sat)
elif args.hue:
hue = args.hue
gdm_status = gdm_theme.install(hue, args.sat)
else: else:
print('No color arguments specified. Use -h or --help to see the available options.')
if gdm_status == 0:
print("\nGDM theme installed successfully.") print("\nGDM theme installed successfully.")
print("You need to restart gdm.service to apply changes.") print("You need to restart gdm.service to apply changes.")
print("Run \"systemctl restart gdm.service\" to restart GDM.") print("Run \"systemctl restart gdm.service\" to restart GDM.")
# if not GDM theme
else: def local_theme(args, colors):
# remove marble theme """
Apply local theme
:param args: parsed arguments
:param colors: colors from colors.json
"""
if args.remove: if args.remove:
remove_files() remove_files()
@@ -139,58 +214,21 @@ def main():
config.themes_folder, config.temp_folder, config.themes_folder, config.temp_folder,
mode=args.mode, is_filled=args.filled) mode=args.mode, is_filled=args.filled)
# panel tweaks apply_tweaks(args, gnome_shell_theme)
if args.panel_default_size: apply_colors(args, gnome_shell_theme, colors)
with open(f"{config.tweaks_folder}/panel/def-size.css", "r") as f:
gnome_shell_theme += f.read()
if args.panel_no_pill:
with open(f"{config.tweaks_folder}/panel/no-pill.css", "r") as f:
gnome_shell_theme += f.read()
if args.panel_text_color: def main():
gnome_shell_theme += ".panel-button,\ args = parse_args()
.clock,\
.clock-display StIcon {\
color: rgba(" + ', '.join(map(str, hex_to_rgba(args.panel_text_color))) + ");\
}"
# dock tweaks colors = json.load(open(config.colors_json))
if args.launchpad:
with open(f"{config.tweaks_folder}/launchpad/launchpad.css", "r") as f:
gnome_shell_theme += f.read()
gnome_shell_theme *= f"{config.tweaks_folder}/launchpad/launchpad.png" if args.gdm:
global_theme(args, colors)
# what argument colors defined
if args.all:
# install hue colors listed in colors.json
for color in colors["colors"]:
hue = colors["colors"][color]["h"]
# if saturation already defined in color (gray)
sat = colors["colors"][color]["s"] if colors["colors"][color]["s"] is not None else args.sat
gnome_shell_theme.install(hue, color, sat)
elif args.red or args.pink or args.purple or args.blue or args.green or args.yellow or args.gray:
# install selected colors
for color in colors["colors"]:
if getattr(args, color): # if argument name is in defined colors
hue = colors["colors"][color]["h"]
# if saturation already defined in color (gray)
sat = colors["colors"][color]["s"] if colors["colors"][color]["s"] is not None else args.sat
gnome_shell_theme.install(hue, color, sat)
# custom color
elif args.hue:
hue = args.hue
theme_name = args.name if args.name else f'hue{hue}' # if defined name
gnome_shell_theme.install(hue, theme_name, args.sat)
# if not GDM theme
else: else:
print('No arguments or no color arguments specified. Use -h or --help to see the available options.') local_theme(args, colors)
if __name__ == "__main__": if __name__ == "__main__":