CoolFace
Apppublic

masoudkhan/mymodel

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
loader_themes.py81 linesDownload Raw Back to tools
1import ast2import json3import os4import importlib5import logging6logger = logging.getLogger(__name__)7 8folder = os.path.dirname(os.path.abspath(__file__))9folder = os.path.dirname(folder)10folder = os.path.dirname(folder)11folder = os.path.join(folder, "assets", "themes")12 13import sys14sys.path.append(folder)15 16def get_class(file_name, class_name):17    with open(file_name, 'r') as file:18        content = file.read()19        syntax_tree = ast.parse(content)20        21        for node in ast.walk(syntax_tree):22            if isinstance(node, ast.ClassDef) and node.name == class_name:23                return node24 25    return None26 27def get_list():28    themes_list = [29        os.path.splitext(name)[0]30        for root, _, files in os.walk(folder, topdown=False)31        for name in files32        if name.endswith(".py") and root == folder33    ]34    return themes_list35 36def select_theme(name):37    selected_file = name + ".py"38    class_name = name39    full_path = os.path.join(folder, selected_file)40    class_found = get_class(full_path, class_name)41    if class_found:42        with open(os.path.join(folder, 'theme.json'), 'w') as json_file:43            json.dump({"file": selected_file, "class": class_name}, json_file)44        logger.info(f"Theme {class_name} successfully selected, restart applio.")45    else:46        logger.warn(f"Theme {class_name} was not found.")47 48def read_json():49    json_file_name = os.path.join(folder, 'theme.json')50    try:51        with open(json_file_name, 'r') as json_file:52            data = json.load(json_file)53            selected_file = data.get("file")54            class_name = data.get("class")55            if selected_file and class_name:56                return class_name57            else:58                return ""59    except:60        return "applio"61 62def load_json():63    json_file_name = os.path.join(folder, 'theme.json')64    try:65        with open(json_file_name, 'r') as json_file:66            data = json.load(json_file)67            selected_file = data.get("file")68            class_name = data.get("class")69            if selected_file and class_name:70                module = importlib.import_module(selected_file[:-3])71                obtained_class = getattr(module, class_name)72                instance = obtained_class()73                logger.info(f"Theme Loaded: {class_name}")74                return instance75            else:76                logger.warn("The theme is incorrect.")77                return None78    except Exception as e:79        logger.warning(f"Error Loading: {str(e)}")80        return None81