From fd0c5159cb34e33e40decf76d401432ca793bd46 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Wed, 25 Jan 2023 14:11:55 +0000 Subject: [PATCH 1/9] put the current year in when regenerating metadata Signed-off-by: Grant Ramsay --- commands/custom.py | 5 +++++ config/helpers.py | 4 +++- config/metadata.py | 1 + config/settings.py | 1 + pyproject.toml | 23 ++++++++++++++++++----- resources/home.py | 1 + templates/index.html | 2 +- 7 files changed, 30 insertions(+), 7 deletions(-) diff --git a/commands/custom.py b/commands/custom.py index 6b6a2391..6be0e94e 100644 --- a/commands/custom.py +++ b/commands/custom.py @@ -1,6 +1,7 @@ """CLI functionality to customize the template.""" import os import sys +from datetime import date from pathlib import Path import asyncclick as click @@ -136,6 +137,9 @@ def metadata(): default=custom_metadata.contact["url"], ), } + + data["this_year"] = date.today().year + print("\nYou have entered the following data:") print(f"[green]Title : [/green]{data['title']}") print(f"[green]Description : [/green]{data['desc']}") @@ -144,6 +148,7 @@ def metadata(): print(f"[green]Author : [/green]{data['author']}") print(f"[green]Email : [/green]{data['email']}") print(f"[green]Website : [/green]{data['website']}") + print(f"[green](C) Year : [/green]{data['this_year']}") if click.confirm("\nIs this Correct?", abort=True, default=True): # write the metadata diff --git a/config/helpers.py b/config/helpers.py index 1abcbef6..1e1d9df6 100644 --- a/config/helpers.py +++ b/config/helpers.py @@ -4,7 +4,7 @@ @dataclass class MetadataBase: - """This is the base Metadata class used for customizsation.""" + """This is the base Metadata class used for customization.""" title: str description: str @@ -12,6 +12,7 @@ class MetadataBase: contact: dict[str, str] license_info: dict[str, str] email: str + year: str # List of acceptable Opensource Licenses with a link to their text. @@ -47,6 +48,7 @@ class MetadataBase: "url": "{{ website }}", }, email="{{ email }}", + year="{{ this_year }}" ) """ diff --git a/config/metadata.py b/config/metadata.py index 3e632272..c39a7ec9 100644 --- a/config/metadata.py +++ b/config/metadata.py @@ -18,4 +18,5 @@ "url": "https://www.gnramsay.com", }, email="seapagan@gmail.com", + year="2023" ) diff --git a/config/settings.py b/config/settings.py index c78d5f57..80db23ca 100644 --- a/config/settings.py +++ b/config/settings.py @@ -42,6 +42,7 @@ class Settings(BaseSettings): repository = custom_metadata.repository contact = custom_metadata.contact license_info = custom_metadata.license_info + year = custom_metadata.year # email settings mail_username = "test_username" diff --git a/pyproject.toml b/pyproject.toml index 3e01afaf..103c04f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,9 @@ name = "API Template" version = "1.3.0" description = "Run 'api-admin custom metadata' to change this information." -authors = ["Grant Ramsay "] +authors = [ + "Grant Ramsay ", +] license = "MIT" readme = "README.md" @@ -23,14 +25,23 @@ anyio = "^3.6.2" email-validator = "^1.3.1" tomli = "^2.0.1" tomli-w = "^1.0.0" -fastapi-mail = { extras = ["httpx"], version = "^1.2.0" } + +[tool.poetry.dependencies.fastapi-mail] +extras = [ + "httpx", +] +version = "^1.2.0" [tool.poetry.dependencies.uvicorn] -extras = ["standard"] +extras = [ + "standard", +] version = "^0.20.0" [tool.poetry.dependencies.passlib] -extras = ["bcrypt"] +extras = [ + "bcrypt", +] version = "^1.7.4" [tool.poetry.group.dev.dependencies] @@ -38,5 +49,7 @@ openapi-readme = "^0.2.1" aiosmtpd = "^1.4.4" [build-system] -requires = ["poetry-core"] +requires = [ + "poetry-core", +] build-backend = "poetry.core.masonry.api" diff --git a/resources/home.py b/resources/home.py index d30805ab..f3b6171e 100644 --- a/resources/home.py +++ b/resources/home.py @@ -23,6 +23,7 @@ def root_path( "repository": get_settings().repository, "author": get_settings().contact["name"], "website": get_settings().contact["url"], + "year": get_settings().year, } return templates.TemplateResponse("index.html", context) diff --git a/templates/index.html b/templates/index.html index 37dd4594..7de6aa79 100644 --- a/templates/index.html +++ b/templates/index.html @@ -42,7 +42,7 @@

{{ description }}

{{ author }} - 2022 + {{ year }} From 58347a43d79b2230a147ba4c7ad253a9aefa4a27 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Wed, 25 Jan 2023 17:33:44 +0000 Subject: [PATCH 2/9] fix default metadata generation Signed-off-by: Grant Ramsay --- commands/custom.py | 2 ++ config/metadata.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/commands/custom.py b/commands/custom.py index 6be0e94e..d2734259 100644 --- a/commands/custom.py +++ b/commands/custom.py @@ -37,6 +37,8 @@ def init(): }, "author": "Grant Ramsay (seapagan)", "website": "https://www.gnramsay.com", + "email": "seapagan@gmail.com", + "this_year": date.today().year, } out = Template(template).render(data) diff --git a/config/metadata.py b/config/metadata.py index c39a7ec9..9678f7c6 100644 --- a/config/metadata.py +++ b/config/metadata.py @@ -14,7 +14,7 @@ "url": "https://opensource.org/licenses/MIT", }, contact={ - "name": "Grant Ramsay", + "name": "Grant Ramsay (seapagan)", "url": "https://www.gnramsay.com", }, email="seapagan@gmail.com", From 8f107e3ce1ece2241f94947a37b51e8fe879de05 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Wed, 25 Jan 2023 17:34:51 +0000 Subject: [PATCH 3/9] refactor config.helpers.py bring more functions out of main code as helpers. Signed-off-by: Grant Ramsay --- config/helpers.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/config/helpers.py b/config/helpers.py index 1e1d9df6..0c380b2f 100644 --- a/config/helpers.py +++ b/config/helpers.py @@ -1,5 +1,39 @@ -"""Define the structure of the MetadataBase class.""" +"""Helper classes and functions for config use.""" +import os +import sys from dataclasses import dataclass +from pathlib import Path + +import tomli + + +def get_toml_path(): + """Return the full path of the pyproject.toml.""" + script_dir = Path(os.path.dirname(os.path.realpath(sys.argv[0]))) + return script_dir / "pyproject.toml" + + +def get_config_path(): + """Return the full path of the custom config file.""" + script_dir = Path(os.path.dirname(os.path.realpath(sys.argv[0]))) + return script_dir / "config" / "metadata.py" + + +def get_api_version() -> str: + """Return the API version from the pyproject.toml file.""" + try: + with open(get_toml_path(), "rb") as f: + config = tomli.load(f) + version = config["tool"]["poetry"]["version"] + + return version + + except Exception as e: + print(f"Cannot read the pyproject.toml file : {e}") + quit(3) + + +"""Define the structure of the MetadataBase class.""" @dataclass From c3e3e63099b03e398f9abdb2d5d28e7bc689fe85 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Wed, 25 Jan 2023 17:35:33 +0000 Subject: [PATCH 4/9] allow changing the version in CLI config Signed-off-by: Grant Ramsay --- commands/custom.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/commands/custom.py b/commands/custom.py index d2734259..0cd0636e 100644 --- a/commands/custom.py +++ b/commands/custom.py @@ -1,8 +1,6 @@ """CLI functionality to customize the template.""" -import os -import sys + from datetime import date -from pathlib import Path import asyncclick as click import tomli @@ -10,19 +8,13 @@ from jinja2 import Template from rich import print -from config.helpers import LICENCES, template - - -def get_config_path(): - """Return the full path of the custom config file.""" - script_dir = Path(os.path.dirname(os.path.realpath(sys.argv[0]))) - return script_dir / "config" / "metadata.py" - - -def get_toml_path(): - """Return the full path of the pyproject.toml.""" - script_dir = Path(os.path.dirname(os.path.realpath(sys.argv[0]))) - return script_dir / "pyproject.toml" +from config.helpers import ( + LICENCES, + get_api_version, + get_config_path, + get_toml_path, + template, +) def init(): @@ -108,6 +100,9 @@ def metadata(): Documentation, Author details, Repository URL and more. """ print("\n[green]API-Template : Customize application Metadata\n") + + version = get_api_version() + data = { "title": click.prompt( "Enter your API title", type=str, default=custom_metadata.title @@ -117,6 +112,7 @@ def metadata(): type=str, default=custom_metadata.description, ), + "version": click.prompt("Version Number", type=str, default=version), "repo": click.prompt( "URL to your Repository", type=str, @@ -168,6 +164,7 @@ def metadata(): with open(get_toml_path(), "rb") as f: config = tomli.load(f) config["tool"]["poetry"]["name"] = data["title"] + config["tool"]["poetry"]["version"] = data["version"] config["tool"]["poetry"]["description"] = data["desc"] config["tool"]["poetry"]["authors"] = [ f"{data['author']} <{data['email']}>" From 31236cc594f066fd8f5ddc9e9fe33b0f61b6648d Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Wed, 25 Jan 2023 18:32:37 +0000 Subject: [PATCH 5/9] read API version from toml file at runtime Signed-off-by: Grant Ramsay --- config/helpers.py | 3 ++- main.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/config/helpers.py b/config/helpers.py index 0c380b2f..3225e4a7 100644 --- a/config/helpers.py +++ b/config/helpers.py @@ -9,7 +9,8 @@ def get_toml_path(): """Return the full path of the pyproject.toml.""" - script_dir = Path(os.path.dirname(os.path.realpath(sys.argv[0]))) + script_dir = Path(os.path.dirname(os.path.realpath(__name__))) + return script_dir / "pyproject.toml" diff --git a/main.py b/main.py index 7a65210d..0283342d 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ from fastapi.staticfiles import StaticFiles from rich import print +from config.helpers import get_api_version from config.settings import get_settings from database.db import database from resources import config_error @@ -17,7 +18,7 @@ docs_url=None, # we customize this ourselves license_info=get_settings().license_info, contact=get_settings().contact, - version="1.3.0", + version=get_api_version(), ) app.include_router(api_router) From 6c323241a44da6996af24be8c534606c8de9f050 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Wed, 25 Jan 2023 18:35:30 +0000 Subject: [PATCH 6/9] display version number on root template. version number taken from the toml file. Signed-off-by: Grant Ramsay --- resources/home.py | 2 ++ templates/index.html | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/resources/home.py b/resources/home.py index f3b6171e..123cd6f4 100644 --- a/resources/home.py +++ b/resources/home.py @@ -4,6 +4,7 @@ from fastapi import APIRouter, Header, Request from fastapi.templating import Jinja2Templates +from config.helpers import get_api_version from config.settings import get_settings router = APIRouter() @@ -24,6 +25,7 @@ def root_path( "author": get_settings().contact["name"], "website": get_settings().contact["url"], "year": get_settings().year, + "version": get_api_version(), } return templates.TemplateResponse("index.html", context) diff --git a/templates/index.html b/templates/index.html index 7de6aa79..3c376743 100644 --- a/templates/index.html +++ b/templates/index.html @@ -17,7 +17,7 @@
-

{{ title }}

+

{{ title }} v{{ version }}

{{ description }}

From 456dfaf5eec262d8d58ff380d6a21969b7ebeba5 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Wed, 25 Jan 2023 18:43:02 +0000 Subject: [PATCH 7/9] show confirmation of version number in CLI output Signed-off-by: Grant Ramsay --- commands/custom.py | 1 + 1 file changed, 1 insertion(+) diff --git a/commands/custom.py b/commands/custom.py index 0cd0636e..d4bc2428 100644 --- a/commands/custom.py +++ b/commands/custom.py @@ -141,6 +141,7 @@ def metadata(): print("\nYou have entered the following data:") print(f"[green]Title : [/green]{data['title']}") print(f"[green]Description : [/green]{data['desc']}") + print(f"[green]Version : [/green]{data['version']}") print(f"[green]Repository : [/green]{data['repo']}") print(f"[green]License : [/green]{data['license']['name']}") print(f"[green]Author : [/green]{data['author']}") From 2f294a1104eebfa3186862b8a1873ca298963cbe Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Wed, 25 Jan 2023 19:08:07 +0000 Subject: [PATCH 8/9] allow to reset the version from config Signed-off-by: Grant Ramsay --- commands/custom.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/commands/custom.py b/commands/custom.py index d4bc2428..8e305f02 100644 --- a/commands/custom.py +++ b/commands/custom.py @@ -75,7 +75,7 @@ def choose_license(): while choice.strip().lower() not in [lic.lower() for lic in license_list]: choice = click.prompt( - f"\nChoose a license from the following options.\n" + f"\nChoose a license from the following options:\n" f"{license_strings}\nYour Choice of License?", type=str, default=custom_metadata.license_info["name"], @@ -84,6 +84,18 @@ def choose_license(): return get_case_insensitive_dict(choice) +def choose_version(current_version): + """Change the version or reset it.""" + choice = click.prompt( + "Version Number (use * to reset to '0.0.1')", + type=str, + default=current_version, + ) + if choice == "*": + return "0.0.1" + return choice + + @click.group(name="custom") def customize_group(): """Customize the Template Strings and Metadata. @@ -101,8 +113,6 @@ def metadata(): """ print("\n[green]API-Template : Customize application Metadata\n") - version = get_api_version() - data = { "title": click.prompt( "Enter your API title", type=str, default=custom_metadata.title @@ -112,7 +122,7 @@ def metadata(): type=str, default=custom_metadata.description, ), - "version": click.prompt("Version Number", type=str, default=version), + "version": choose_version(get_api_version()), "repo": click.prompt( "URL to your Repository", type=str, From 3cfb6214697159121a94a9d1695567bec0f835f1 Mon Sep 17 00:00:00 2001 From: Grant Ramsay Date: Wed, 25 Jan 2023 19:08:18 +0000 Subject: [PATCH 9/9] bump to 1.3.1 Signed-off-by: Grant Ramsay --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 103c04f8..7e4dd52d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,9 +1,9 @@ [tool.poetry] name = "API Template" -version = "1.3.0" +version = "1.3.1" description = "Run 'api-admin custom metadata' to change this information." authors = [ - "Grant Ramsay ", + "Grant Ramsay (seapagan) ", ] license = "MIT" readme = "README.md"