From 300b64eea8dae4731228c67a34cd90b5cf811c69 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sat, 6 Dec 2025 18:24:43 -0500 Subject: [PATCH] fixed changes --- src/githelp/cli.py | 4 +- src/githelp/data/overlays/tips.yml | 2 +- src/githelp/overlays.py | 67 +++++++++++++----------------- 3 files changed, 31 insertions(+), 42 deletions(-) diff --git a/src/githelp/cli.py b/src/githelp/cli.py index 9162cb8..62d8146 100644 --- a/src/githelp/cli.py +++ b/src/githelp/cli.py @@ -17,8 +17,8 @@ def githelp_cli(context, show_help): if context.invoked_subcommand is None: context.exit(0) -@githelp_cli.command(name = "list", help = "List of available githelp tip pages.") -def list_cmd(): +@githelp_cli.command(help = "List of available githelp tip pages.") +def list(): '''List all available githelp tip.''' click.echo(render_menu()) diff --git a/src/githelp/data/overlays/tips.yml b/src/githelp/data/overlays/tips.yml index cbc5266..8f9b527 100644 --- a/src/githelp/data/overlays/tips.yml +++ b/src/githelp/data/overlays/tips.yml @@ -43,7 +43,7 @@ clone: - "you want to use this to create a local copy of an existing Git repository" examples: - cmd: "git clone " - - say: "Downloads the remote repo into a new local folder." + say: "Downloads the remote repo into a new local folder." checkout: command: checkout diff --git a/src/githelp/overlays.py b/src/githelp/overlays.py index ff8be2d..9fc295a 100644 --- a/src/githelp/overlays.py +++ b/src/githelp/overlays.py @@ -4,8 +4,8 @@ #build absolute path to githelp/data/overlays THIS_DIR = os.path.dirname(os.path.abspath(__file__)) DATA_DIR = os.path.join(THIS_DIR, "data", "overlays") -#master dictionary file: to /src/githelp/data/overlays/tips.yml -MASTER_FILE = os.path.join(DATA_DIR, "tips.yml") +#TIPS dictionary file: to /src/githelp/data/overlays/tips.yml +TIPS_FILE = os.path.join(DATA_DIR, "tips.yml") def read_yaml(path): @@ -24,7 +24,7 @@ def read_yaml(path): or if any error occurs while reading/parsing. ''' try: - #r opens the file for reading in text mode, and encoding="utf-8" tells Python to decode the file's bytes + # opens the file for reading in text mode, and encoding="utf-8" tells Python to decode the file's bytes #as UTF-8 text into normal Python strings with open(path, "r", encoding="utf-8") as f: #convert YAML content to Python object or empty dict if file is empty @@ -34,18 +34,13 @@ def read_yaml(path): except Exception: #if error occurs return empty dict return {} - -def helper(): - - data = read_yaml(MASTER_FILE) - return data def load_overlay(command): ''' Get the tips for one git subcommand. - This first looks in the master file tips.yml under the given - command name (ex pull). If found, it returns the corresponding + This first looks in the TIPS file tips.yml under the given + command name (ex `pull`). If found, it returns the corresponding dictionary of tips. Parameters @@ -56,21 +51,21 @@ def load_overlay(command): ------- dict or None The overlay dictionary for the given command, with a ``"command"`` key - ensured, or ``None`` if no overlay is defined. + ensured, or ``None`` if no tip is defined. ''' - #set data to the master dictionary file - data = helper() - #check to see if the master is a dict and not empty - if isinstance(data, dict): - #get the section for this specific command and store it in section - section = data.get(command) - #check if section is a dict - if isinstance(section, dict): - # make sure the section has a command key - # does nothing if "command" already exists - section.setdefault("command", command) - #return the section - return section + data = read_yaml(TIPS_FILE) + + # if the YAML didn't load as a dict, bail out early + if not isinstance(data, dict): + return None + # section for this specific command + section = data.get(command) + # if there's no entry (or it's not a dict), bail out + if not isinstance(section, dict): + return None + # make sure the section has a "command" key + section.setdefault("command", command) + return section def list_tips_names(): @@ -80,20 +75,14 @@ def list_tips_names(): Returns ------- list of str - Sorted list of subcommand names found in the master tips mapping. + Sorted list of subcommand names found in the TIPS tips mapping. ''' - names = [] - data = helper() - #check if data is a dict and not empty + data = read_yaml(TIPS_FILE) if not isinstance(data, dict): - return names - - for key, value in data.items(): - if isinstance(value, dict): - names.append(key) + return [] - names.sort() - return names + # just return all the keys, sorted + return sorted(data.keys()) def render_overlay(dict): ''' Render a single overlay dictionary into a human readable text block. @@ -107,7 +96,7 @@ def render_overlay(dict): Returns ------- str - A formatted multi-line string + Using fstring to format the text block for display. ''' #line variable that stores a list of outputs lines = [] @@ -152,15 +141,15 @@ def render_menu(): Returns ------- str - A formatted multi-line string describing available commands. + Using fstring to format the text block for display. ''' #declare an empty list called lines lines = [] #display commands lines.append("Githelp Commands:") lines.append(" - explain Show an explaination for a git subcommand") - lines.append(" - list List available tip pages\n") - lines.append(" - tips Show tips.yml page for a git subcommand\n") + lines.append(" - list List available tip pages") + lines.append(" - tips Show tips.yml page for a git subcommand") #name variable that stores the list of tips names names = list_tips_names()