from gui.v2.actions.config import default_gui_config, load_gui_config, save_gui_config def _clean_ids(profile_ids): ids = [] for profile_id in profile_ids or []: try: profile_id = int(profile_id) except (TypeError, ValueError): continue if profile_id not in ids: ids.append(profile_id) return ids def _load_order(gui_config_file): if not gui_config_file: return [] config = load_gui_config(gui_config_file) if not config: return [] return _clean_ids(config.get("profiles", {}).get("visual_order", [])) def _save_order(gui_config_file, order): if not gui_config_file: return config = load_gui_config(gui_config_file) if config is None: config = default_gui_config() if "profiles" not in config: config["profiles"] = {} config["profiles"]["visual_order"] = _clean_ids(order) save_gui_config(gui_config_file, config) def normalize_profile_order(gui_config_file, existing_ids): existing_order = _clean_ids(existing_ids) existing = set(existing_order) saved_order = [profile_id for profile_id in _load_order( gui_config_file) if profile_id in existing] ordered = saved_order + [ profile_id for profile_id in existing_order if profile_id not in saved_order] _save_order(gui_config_file, ordered) return ordered def append_profile_to_visual_order(gui_config_file, profile_id, existing_ids): try: profile_id = int(profile_id) except (TypeError, ValueError): return existing_order = [ existing_id for existing_id in _clean_ids(existing_ids) if existing_id != profile_id] existing = set(existing_order) saved_order = [ existing_id for existing_id in _load_order(gui_config_file) if existing_id in existing] ordered = saved_order + [ existing_id for existing_id in existing_order if existing_id not in saved_order] ordered.append(profile_id) _save_order(gui_config_file, ordered)