From cf2768db2d902edda7229977e71bb604d6bade88 Mon Sep 17 00:00:00 2001 From: Dev <90421310+EstatoDeviato@users.noreply.github.com> Date: Mon, 13 May 2024 16:24:03 +0200 Subject: [PATCH] Adding read_only argument in /save_hub and /musiclist_save commands, update /musiclists and /list_hubs commands (#140) * musiclist is a list and not a dict * Add read_only argument in /save_hub and update /list_hubs command --- server/commands/hubs.py | 54 +++++++++++++++++++++++++------------ server/commands/music.py | 58 +++++++++++++++++++++++++++++----------- 2 files changed, 79 insertions(+), 33 deletions(-) diff --git a/server/commands/hubs.py b/server/commands/hubs.py index 23bdc4e3..00085c93 100644 --- a/server/commands/hubs.py +++ b/server/commands/hubs.py @@ -1,4 +1,5 @@ import os +import shlex import oyaml as yaml # ordered yaml @@ -100,16 +101,17 @@ def ooc_cmd_save_hub(client, arg): """ Save the current Hub in the server's storage/hubs/.yaml file. If blank and you're a mod, it will save to server's config/areas_new.yaml for the server owner to approve. - Usage: /save_hub + Usage: /save_hub """ + args = shlex.split(arg) if not client.is_mod: - if arg == "": + if args[0] == "": raise ArgumentError( "You must be authorized to save the default hub!") - if len(arg) < 3: + if len(args[0]) < 3: raise ArgumentError("Filename must be at least 3 symbols long!") try: - if arg != "": + if args[0] != "": path = "storage/hubs" num_files = len( [f for f in os.listdir(path) if os.path.isfile( @@ -120,26 +122,28 @@ def ooc_cmd_save_hub(client, arg): "Server storage full! Please contact the server host to resolve this issue." ) try: - arg = f"{path}/{derelative(arg)}.yaml" - if os.path.isfile(arg): - with open(arg, "r", encoding="utf-8") as stream: + args[0] = f"{path}/{derelative(args[0])}.yaml" + if os.path.isfile(args[0]): + with open(args[0], "r", encoding="utf-8") as stream: hub = yaml.safe_load(stream) if "read_only" in hub and hub["read_only"] is True: raise ArgumentError( - f"Hub {arg} already exists and it is read-only!" + f"Hub {args[0]} already exists and it is read-only!" ) - with open(arg, "w", encoding="utf-8") as stream: + hub = client.area.area_manager.save(ignore=["can_gm", "max_areas"]) + if len(args) == 2 and args[1] == "read_only": + hub["read_only"] = True + with open(args[0], "w", encoding="utf-8") as stream: yaml.dump( - client.area.area_manager.save( - ignore=["can_gm", "max_areas"]), + hub, stream, default_flow_style=False, ) except ArgumentError: raise except Exception: - raise AreaError(f"File path {arg} is invalid!") - client.send_ooc(f"Saving as {arg}...") + raise AreaError(f"File path {args[0]} is invalid!") + client.send_ooc(f"Saving as {args[0]}...") else: client.server.hub_manager.save("config/areas_new.yaml") client.send_ooc( @@ -227,18 +231,34 @@ def ooc_cmd_overlay_hub(client, arg): client.send_ooc("Success, sending ARUP and refreshing music...") -@mod_only() def ooc_cmd_list_hubs(client, arg): """ Show all the available hubs for loading in the storage/hubs/ folder. Usage: /list_hubs """ - text = "Available hubs:" + hubs_editable = [] + hubs_read_only = [] for F in os.listdir("storage/hubs/"): if F.lower().endswith(".yaml"): - text += "\n- {}".format(F[:-5]) + with open(f"storage/hubs/{F}", "r", encoding="utf-8") as stream: + hub = yaml.safe_load(stream) + if "read_only" in hub and hub["read_only"] is True: + hubs_read_only.append(F[:-5]) + else: + hubs_editable.append(F[:-5]) + + hubs_read_only.sort() + msg = "\nā›©ļø Available Read Only Hubs: ā›©ļø\n" + for hub in hubs_read_only: + msg += f"\nšŸŒŽ [šŸ‘€]{hub}" + + if client.is_mod: + hubs_editable.sort() + msg += "\n\nā›©ļø Available Editable Hubs: ā›©ļø\n" + for hub in hubs_editable: + msg += f"\nšŸŒŽ {hub}" - client.send_ooc(text) + client.send_ooc(msg) @mod_only(hub_owners=True) diff --git a/server/commands/music.py b/server/commands/music.py index e86861b3..48a48a88 100644 --- a/server/commands/music.py +++ b/server/commands/music.py @@ -252,14 +252,34 @@ def ooc_cmd_musiclists(client, arg): Displays all the available music lists. Usage: /musiclists """ - text = "Available musiclists:" - from os import listdir - for F in listdir("storage/musiclists/"): + musiclist_editable = [] + musiclist_read_only = [] + for F in os.listdir("storage/musiclists/"): if F.lower().endswith(".yaml"): - text += "\n- {}".format(F[:-5]) + with open(f"storage/musiclists/{F}", "r", encoding="utf-8") as stream: + musiclist = yaml.safe_load(stream) + read_only = False + for item in musiclist: + if "read_only" in item and item["read_only"] is True: + musiclist_read_only.append(F[:-5]) + read_only = True + break + if not read_only: + musiclist_editable.append(F[:-5]) + + musiclist_read_only.sort() + msg = "\nšŸŽ¶ Available Read Only Musiclists: šŸŽ¶\n" + for ml in musiclist_read_only: + msg += f"\nšŸŽœ [šŸ‘€]{ml}" - client.send_ooc(text) + if client.is_mod: + musiclist_editable.sort() + msg += "\n\nšŸŽ¶ Available Editable Musiclists: šŸŽ¶\n" + for ml in musiclist_editable: + msg += f"\nšŸŽœ {ml}" + + client.send_ooc(msg) def ooc_cmd_musiclist(client, arg): @@ -375,17 +395,18 @@ def ooc_cmd_musiclist_save(client, arg): """ Allow you to save a musiclist on server list! If the musiclist you're editing is already in the server list, you don't have to add [MusiclistName] - Usage: /musiclist_save [MusiclistName] + Argument "read_only" is optional + Usage: /musiclist_save [MusiclistName] """ if arg == "": - client.send_ooc("Usage: /musiclist_save ") + client.send_ooc("Usage: /musiclist_save ") return args = shlex.split(arg) if args[0] not in ["local", "area", "hub"]: - client.send_ooc("Usage: /musiclist_save ") + client.send_ooc("Usage: /musiclist_save ") return - + if args[0] == "local": musiclist = client.music_list name = client.music_ref @@ -397,20 +418,25 @@ def ooc_cmd_musiclist_save(client, arg): name = client.area.area_manager.music_ref if name == "unsaved": - if len(args) == 2: + if len(args) >= 2: name = args[1] else: client.send_ooc("This is a new musiclist, you should give it a name") return + if len(args) > 2 and args[2].lower() == "read_only": + musiclist.append({"read_only": True}) + filepath = f"storage/musiclists/{name}.yaml" - with open(filepath, "r", encoding="utf-8") as stream: - test = yaml.safe_load(stream) - if "read_only" in test and test["read_only"] is True: - raise ArgumentError( - f"Musiclist '{name}' already exists and it is read-only!" - ) + if os.path.isfile(filepath): + with open(filepath, "r", encoding="utf-8") as stream: + test = yaml.safe_load(stream) + for item in test: + if "read_only" in item and item["read_only"] is True: + raise ArgumentError( + f"Musiclist '{name}' already exists and it is read-only!" + ) with open(filepath, "w", encoding="utf-8") as yaml_save: yaml.dump(musiclist, yaml_save) client.send_ooc(f"Musiclist '{name}' saved on server list!")