Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the --help menu and add the --version and -v flags to the command-line interface #105

Open
wants to merge 50 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
44fcc81
feat: Add draft of new help message with version info in main.
gkapfham Sep 8, 2022
50b5e61
feat: Add version module with get_gatorgrade_version and
gkapfham Sep 8, 2022
d779154
fix: Refine the phrase in the epilog_message in main.
gkapfham Sep 8, 2022
babc827
refactor: Reorganize gatorgrade function in main to handle version
gkapfham Sep 8, 2022
04a29e9
fix: Delete the extra help_message not needed in main module.
gkapfham Sep 8, 2022
e95731a
feat: Add get_project_version and refactor version functions.
gkapfham Sep 9, 2022
8372f9f
refactor: Use improved markdown version strings in the main module.
gkapfham Sep 9, 2022
b2a7f61
refactor: Remove a not-needed and commented-out global message in main.
gkapfham Sep 9, 2022
6063901
feat: Add constants and all projects in PROJECTS list in version.
gkapfham Sep 9, 2022
573e44a
refactor: Further improve help and epilog messages in main.
gkapfham Sep 9, 2022
ce82d5b
fix: Remove spaces in a multi-line string in main.
gkapfham Sep 9, 2022
d02d49b
fix: Remove spaces in a multi-line string in main.
gkapfham Sep 9, 2022
da47102
refactor: Add version module in the util package.
gkapfham Sep 9, 2022
e67be95
refactor: Rewrite import for version to be in gatorgrade.util in main.
gkapfham Sep 9, 2022
a36cb57
refactor: Delete the version module in top-level package.
gkapfham Sep 9, 2022
bf92c80
coms: Add two and update one comment in main module.
gkapfham Sep 9, 2022
b890e10
fix: Delete conditional logic on help that was not needed in main.
gkapfham Sep 9, 2022
9d01e2b
feat: Add draft of the --version output in main.
gkapfham Sep 10, 2022
df32e7c
feat: Add versions module in util to not conflict with --version in
gkapfham Sep 10, 2022
9940fa0
refactor: Remove version module that conflicts with --version in main.
gkapfham Sep 10, 2022
93f0237
feat: Improve the help_message_markdown in main.
gkapfham Sep 10, 2022
07495e2
coms: Improve a comment in gatorgrade function in main.
gkapfham Sep 10, 2022
8ab46d8
refactor: Make the variable GATORGRADE_EMOJI in main module constants.
gkapfham Sep 10, 2022
a1ff3ec
feat: Improve main so that github_message is generated.
gkapfham Sep 10, 2022
fc0a4f3
style: Reformat the source code in versions.
gkapfham Sep 10, 2022
b72c6ee
feat: Add the github module in util with get_github_projects function.
gkapfham Sep 10, 2022
9b15c73
refactor: Use the GATORGRADE_EMOJI_RICH constant in main.
gkapfham Sep 10, 2022
3dda163
fix: Delete not-needed variables for GitHub projects in main.
gkapfham Sep 10, 2022
a9d9a99
refactor: Use the RICH_MARKUP_MODE_DEFAULT constant in main.
gkapfham Sep 10, 2022
18e977e
chore: Increase minimal version of Python to 3.8 in pyproject.toml.
gkapfham Sep 10, 2022
4528f52
chore: Use Python 3.8 and 3.10 on ubuntu-latest and 3.9 on macos-latest
gkapfham Sep 10, 2022
0070a2b
chore: Bump the poetry-version to 1.2 in the main.yml file.
gkapfham Sep 10, 2022
12808aa
chore: Update poetry.lock file to reflect move to Python 3.8 minimum.
gkapfham Sep 10, 2022
02c1231
fix: Revert/increment poetry-version in main.yml to 1.1.14 as 1.2 fails.
gkapfham Sep 10, 2022
726a864
Merge branch 'main' into feat/add-version
gkapfham Sep 10, 2022
bf24b98
chore: Specify Python 3.8 as the minimum version in publish.yml.
gkapfham Sep 10, 2022
7bce6fb
Merge branch 'main' into feat/add-version
gkapfham Sep 19, 2022
7bfa554
coms: Add a comment to versions to explain PROJECTS variable.
gkapfham Sep 19, 2022
5e80c32
coms: Fix an incorrect docstring in the versions module.
gkapfham Sep 19, 2022
dd4de85
coms: Add comments to explain steps in the get_project_versions in ve…
gkapfham Sep 19, 2022
ad8b4d9
refactor: Delete two not-needed functions in the versions module.
gkapfham Sep 19, 2022
ba76bbf
fix: Stop using an epilog in main and add more to version output.
gkapfham Sep 19, 2022
4499728
feat: Improve the output of --version in the main module.
gkapfham Sep 19, 2022
6d7b904
coms: Improve two comments in the main module.
gkapfham Sep 19, 2022
2bb8c01
fix: Delete the GATORGRADE_EMOJI constant in main.
gkapfham Sep 19, 2022
6fc26d7
refactor: Delete a not-needed variable in main.
gkapfham Sep 19, 2022
87d4587
coms: Add comments to explain steps in get_github_projects in github.
gkapfham Sep 19, 2022
e52e20a
coms: Delete a not-needed comment for typer app in main.
gkapfham Sep 19, 2022
5af4b70
coms: Clarify the docstring comment in main.
gkapfham Sep 19, 2022
a5850e2
chore: Bump the semver in the pyproject.toml file.
gkapfham Sep 19, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
python-version: ["3.7", "3.10"]
python-version: ["3.8", "3.10"]
include:
- os: macos-latest
python-version: "3.9"
- os: windows-latest
python-version: "3.8"
python-version: "3.9"
env:
OS: ${{ matrix.os }}
PYTHON: ${{ matrix.python-version }}
Expand All @@ -87,7 +87,7 @@ jobs:
- name: Install Poetry
uses: Gr1N/setup-poetry@v7
with:
poetry-version: 1.1.13
poetry-version: 1.1.14
- name: Setup Poetry
run: |
poetry --version
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Python 3.7
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: '3.7'
python-version: '3.8'
- name: Install Poetry
uses: Gr1N/setup-poetry@v7
- name: Publish
Expand Down
101 changes: 70 additions & 31 deletions gatorgrade/main.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,104 @@
"""Use Typer to run gatorgrade to run the checks and generate the yml file."""
"""Use Typer to run gatorgrade to run the GatorGrader checks."""

import sys
from pathlib import Path

import typer
from rich.console import Console
from rich.markdown import Markdown

from gatorgrade.input.parse_config import parse_config
from gatorgrade.output.output import run_checks
from gatorgrade.util import github
from gatorgrade.util import versions

# create an app for the Typer-based CLI
# define constants used in this module
DEFAULT_VERSION = False
GATORGRADE_EMOJI_RICH = ":crocodile:"
FILE = "gatorgrade.yml"
FAILURE = 1
RICH_MARKUP_MODE_DEFAULT = "markdown"

# define the emoji that will be prepended to the help message
gatorgrade_emoji = "🐊"
# define the overall help message
help_message_markdown = f"""
{GATORGRADE_EMOJI_RICH} GatorGrade runs the GatorGrader checks in a specified configuration file.
"""

# create a Typer app that
# create a Typer app that:
# --> does not support completion
# --> has a specified help message with an emoji
# --> has a specified help message with an emoji for tagline
# --> uses "markdown" mode so that markdown and emojis work
app = typer.Typer(
add_completion=False,
help=f"{gatorgrade_emoji} Run the GatorGrader checks in the specified gatorgrade.yml file.",
help=help_message_markdown,
rich_markup_mode=RICH_MARKUP_MODE_DEFAULT,
)

# create a default console for printing with rich
console = Console()

# define constants used in this module
FILE = "gatorgrade.yml"
FAILURE = 1


@app.callback(invoke_without_command=True)
def gatorgrade(
ctx: typer.Context,
filename: Path = typer.Option(FILE, "--config", "-c", help="Name of the YML file."),
version: bool = typer.Option(
DEFAULT_VERSION, "--version", "-v", help="Display version information."
),
):
"""Run the GatorGrader checks in the specified gatorgrade.yml file."""
"""Run the GatorGrader checks in the specified configuration file."""
# if ctx.subcommand is None then this means
# that, by default, gatorgrade should run in checking mode
if ctx.invoked_subcommand is None:
# parse the provided configuration file
checks = parse_config(filename)
# there are valid checks and thus the
# tool should run them with run_checks
if len(checks) > 0:
checks_status = run_checks(checks)
# no checks were created and this means
# that, most likely, the file was not
# valid and thus the tool cannot run checks
else:
checks_status = False
# requesting version information overrides all other commands;
# if the version details are requested, print them and exit
if version:
# define the version label with suitable emoji
version_label = ":wrench: Version information:"
# define the message about the project versions
version_message = versions.get_project_versions()
# define a contribution message with suitable emoji
contribution_message = (
":tada: Want to contribute to this project? Check these GitHub sites!"
)
# define the message about GitHub repositories
github_message = github.get_github_projects()
# output all of the details about gatorgrade
# 1) standard help message that was defined previously
console.print(help_message_markdown)
# 2) version message
console.print(version_label)
console.print(Markdown(version_message))
console.print()
console.print(f"The file {filename} either does not exist or is not valid.")
console.print("Exiting now!")
# 3) contribution message
console.print(contribution_message)
console.print(Markdown(github_message))
console.print()
# at least one of the checks did not pass or
# the provided file was not valid and thus
# the tool should return a non-zero exit
# code to designate some type of failure
if checks_status is not True:
sys.exit(FAILURE)
# run the checking function since --version was not provided
else:
# parse the provided configuration file
checks = parse_config(filename)
# there are valid checks and thus the
# tool should run them with run_checks
if len(checks) > 0:
checks_status = run_checks(checks)
# no checks were created and this means
# that, most likely, the file was not
# valid and thus the tool cannot run checks
else:
checks_status = False
console.print()
console.print(
f"The file {filename} either does not exist or is not valid."
)
console.print("Exiting now!")
console.print()
# at least one of the checks did not pass or
# the provided file was not valid and thus
# the tool should return a non-zero exit
# code to designate some type of failure
if checks_status is not True:
sys.exit(FAILURE)


# @app.command()
Expand Down
36 changes: 36 additions & 0 deletions gatorgrade/util/github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Collect GitHub URLs for display in the command-line interface."""

from typing import Dict

COLON = ":"
DASH = "-"
SPACE = " "
NEWLINE = "\n"
NEWLINE_NEWLINE = "\n\n"

PROJECTS = {
"gatorgrade": "https://github.com/GatorEducator/gatorgrade",
"gatorgrader": "https://github.com/GatorEducator/gatorgrader",
}


def get_github_projects(projects: Dict[str, str] = PROJECTS) -> str:
"""Create a GitHub repository string for all specified projects."""
# create an empty project_version_str that will be appended
# to with information about each project and its GitHub URL
project_version_str = ""
# iterate through the provided projects and display:
# 1) the name of the project
# 2) the GitHub URL for the project
for project_name in projects.keys():
current_project_version_str = (
DASH
+ SPACE
+ project_name
+ COLON
+ SPACE
+ projects[project_name]
+ NEWLINE_NEWLINE
)
project_version_str = project_version_str + current_project_version_str
return project_version_str
38 changes: 38 additions & 0 deletions gatorgrade/util/versions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Extract version information for display in various aspects of the command-line interface."""

from importlib.metadata import version
from typing import List

COLON = ":"
DASH = "-"
SPACE = " "
NEWLINE = "\n"
NEWLINE_NEWLINE = "\n\n"


# Projects are hard-coded and may need to be updated if there are
# new dependencies specified inside of the pyproject.toml file.
# With that said, this approach avoids the need to parse the
# dependencies listed in the pyproject.toml file.
PROJECTS = ["gatorgrade", "gatorgrader", "pyyaml", "rich", "typer"]


def get_project_version(project: str) -> str:
"""Extract the version information for a project and return formatted version string."""
project_version_str = version(project)
return project + COLON + SPACE + project_version_str


def get_project_versions(project_list: List[str] = PROJECTS) -> str:
"""Create a version string for all specified projects."""
# create an empty version string that this function
# will append to for each of the projects in the project_list
project_version_str = ""
# iterate through every project in the project_list and then
# include its name and its current version in project_version_str
for project in project_list:
current_project_version_str = (
DASH + SPACE + get_project_version(project) + NEWLINE_NEWLINE
)
project_version_str = project_version_str + current_project_version_str
return project_version_str
Loading