Skip to content

Commit b2c401a

Browse files
authored
Merge pull request #42 from seapagan/improve-cli-metadata
Add improvements to the CLI metadata generation.
2 parents d235f9c + 3cfb621 commit b2c401a

File tree

8 files changed

+100
-29
lines changed

8 files changed

+100
-29
lines changed

commands/custom.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
"""CLI functionality to customize the template."""
2-
import os
3-
import sys
4-
from pathlib import Path
2+
3+
from datetime import date
54

65
import asyncclick as click
76
import tomli
87
import tomli_w
98
from jinja2 import Template
109
from rich import print
1110

12-
from config.helpers import LICENCES, template
13-
14-
15-
def get_config_path():
16-
"""Return the full path of the custom config file."""
17-
script_dir = Path(os.path.dirname(os.path.realpath(sys.argv[0])))
18-
return script_dir / "config" / "metadata.py"
19-
20-
21-
def get_toml_path():
22-
"""Return the full path of the pyproject.toml."""
23-
script_dir = Path(os.path.dirname(os.path.realpath(sys.argv[0])))
24-
return script_dir / "pyproject.toml"
11+
from config.helpers import (
12+
LICENCES,
13+
get_api_version,
14+
get_config_path,
15+
get_toml_path,
16+
template,
17+
)
2518

2619

2720
def init():
@@ -36,6 +29,8 @@ def init():
3629
},
3730
"author": "Grant Ramsay (seapagan)",
3831
"website": "https://www.gnramsay.com",
32+
"email": "seapagan@gmail.com",
33+
"this_year": date.today().year,
3934
}
4035

4136
out = Template(template).render(data)
@@ -80,7 +75,7 @@ def choose_license():
8075

8176
while choice.strip().lower() not in [lic.lower() for lic in license_list]:
8277
choice = click.prompt(
83-
f"\nChoose a license from the following options.\n"
78+
f"\nChoose a license from the following options:\n"
8479
f"{license_strings}\nYour Choice of License?",
8580
type=str,
8681
default=custom_metadata.license_info["name"],
@@ -89,6 +84,18 @@ def choose_license():
8984
return get_case_insensitive_dict(choice)
9085

9186

87+
def choose_version(current_version):
88+
"""Change the version or reset it."""
89+
choice = click.prompt(
90+
"Version Number (use * to reset to '0.0.1')",
91+
type=str,
92+
default=current_version,
93+
)
94+
if choice == "*":
95+
return "0.0.1"
96+
return choice
97+
98+
9299
@click.group(name="custom")
93100
def customize_group():
94101
"""Customize the Template Strings and Metadata.
@@ -105,6 +112,7 @@ def metadata():
105112
Documentation, Author details, Repository URL and more.
106113
"""
107114
print("\n[green]API-Template : Customize application Metadata\n")
115+
108116
data = {
109117
"title": click.prompt(
110118
"Enter your API title", type=str, default=custom_metadata.title
@@ -114,6 +122,7 @@ def metadata():
114122
type=str,
115123
default=custom_metadata.description,
116124
),
125+
"version": choose_version(get_api_version()),
117126
"repo": click.prompt(
118127
"URL to your Repository",
119128
type=str,
@@ -136,14 +145,19 @@ def metadata():
136145
default=custom_metadata.contact["url"],
137146
),
138147
}
148+
149+
data["this_year"] = date.today().year
150+
139151
print("\nYou have entered the following data:")
140152
print(f"[green]Title : [/green]{data['title']}")
141153
print(f"[green]Description : [/green]{data['desc']}")
154+
print(f"[green]Version : [/green]{data['version']}")
142155
print(f"[green]Repository : [/green]{data['repo']}")
143156
print(f"[green]License : [/green]{data['license']['name']}")
144157
print(f"[green]Author : [/green]{data['author']}")
145158
print(f"[green]Email : [/green]{data['email']}")
146159
print(f"[green]Website : [/green]{data['website']}")
160+
print(f"[green](C) Year : [/green]{data['this_year']}")
147161

148162
if click.confirm("\nIs this Correct?", abort=True, default=True):
149163
# write the metadata
@@ -161,6 +175,7 @@ def metadata():
161175
with open(get_toml_path(), "rb") as f:
162176
config = tomli.load(f)
163177
config["tool"]["poetry"]["name"] = data["title"]
178+
config["tool"]["poetry"]["version"] = data["version"]
164179
config["tool"]["poetry"]["description"] = data["desc"]
165180
config["tool"]["poetry"]["authors"] = [
166181
f"{data['author']} <{data['email']}>"

config/helpers.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,53 @@
1-
"""Define the structure of the MetadataBase class."""
1+
"""Helper classes and functions for config use."""
2+
import os
3+
import sys
24
from dataclasses import dataclass
5+
from pathlib import Path
6+
7+
import tomli
8+
9+
10+
def get_toml_path():
11+
"""Return the full path of the pyproject.toml."""
12+
script_dir = Path(os.path.dirname(os.path.realpath(__name__)))
13+
14+
return script_dir / "pyproject.toml"
15+
16+
17+
def get_config_path():
18+
"""Return the full path of the custom config file."""
19+
script_dir = Path(os.path.dirname(os.path.realpath(sys.argv[0])))
20+
return script_dir / "config" / "metadata.py"
21+
22+
23+
def get_api_version() -> str:
24+
"""Return the API version from the pyproject.toml file."""
25+
try:
26+
with open(get_toml_path(), "rb") as f:
27+
config = tomli.load(f)
28+
version = config["tool"]["poetry"]["version"]
29+
30+
return version
31+
32+
except Exception as e:
33+
print(f"Cannot read the pyproject.toml file : {e}")
34+
quit(3)
35+
36+
37+
"""Define the structure of the MetadataBase class."""
338

439

540
@dataclass
641
class MetadataBase:
7-
"""This is the base Metadata class used for customizsation."""
42+
"""This is the base Metadata class used for customization."""
843

944
title: str
1045
description: str
1146
repository: str
1247
contact: dict[str, str]
1348
license_info: dict[str, str]
1449
email: str
50+
year: str
1551

1652

1753
# List of acceptable Opensource Licenses with a link to their text.
@@ -47,6 +83,7 @@ class MetadataBase:
4783
"url": "{{ website }}",
4884
},
4985
email="{{ email }}",
86+
year="{{ this_year }}"
5087
)
5188
5289
"""

config/metadata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
"url": "https://opensource.org/licenses/MIT",
1515
},
1616
contact={
17-
"name": "Grant Ramsay",
17+
"name": "Grant Ramsay (seapagan)",
1818
"url": "https://www.gnramsay.com",
1919
},
2020
email="seapagan@gmail.com",
21+
year="2023"
2122
)

config/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Settings(BaseSettings):
4242
repository = custom_metadata.repository
4343
contact = custom_metadata.contact
4444
license_info = custom_metadata.license_info
45+
year = custom_metadata.year
4546

4647
# email settings
4748
mail_username = "test_username"

main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from fastapi.staticfiles import StaticFiles
66
from rich import print
77

8+
from config.helpers import get_api_version
89
from config.settings import get_settings
910
from database.db import database
1011
from resources import config_error
@@ -17,7 +18,7 @@
1718
docs_url=None, # we customize this ourselves
1819
license_info=get_settings().license_info,
1920
contact=get_settings().contact,
20-
version="1.3.0",
21+
version=get_api_version(),
2122
)
2223

2324
app.include_router(api_router)

pyproject.toml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
[tool.poetry]
22
name = "API Template"
3-
version = "1.3.0"
3+
version = "1.3.1"
44
description = "Run 'api-admin custom metadata' to change this information."
5-
authors = ["Grant Ramsay <seapagan@gmail.com>"]
5+
authors = [
6+
"Grant Ramsay (seapagan) <seapagan@gmail.com>",
7+
]
68
license = "MIT"
79
readme = "README.md"
810

@@ -23,20 +25,31 @@ anyio = "^3.6.2"
2325
email-validator = "^1.3.1"
2426
tomli = "^2.0.1"
2527
tomli-w = "^1.0.0"
26-
fastapi-mail = { extras = ["httpx"], version = "^1.2.0" }
28+
29+
[tool.poetry.dependencies.fastapi-mail]
30+
extras = [
31+
"httpx",
32+
]
33+
version = "^1.2.0"
2734

2835
[tool.poetry.dependencies.uvicorn]
29-
extras = ["standard"]
36+
extras = [
37+
"standard",
38+
]
3039
version = "^0.20.0"
3140

3241
[tool.poetry.dependencies.passlib]
33-
extras = ["bcrypt"]
42+
extras = [
43+
"bcrypt",
44+
]
3445
version = "^1.7.4"
3546

3647
[tool.poetry.group.dev.dependencies]
3748
openapi-readme = "^0.2.1"
3849
aiosmtpd = "^1.4.4"
3950

4051
[build-system]
41-
requires = ["poetry-core"]
52+
requires = [
53+
"poetry-core",
54+
]
4255
build-backend = "poetry.core.masonry.api"

resources/home.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from fastapi import APIRouter, Header, Request
55
from fastapi.templating import Jinja2Templates
66

7+
from config.helpers import get_api_version
78
from config.settings import get_settings
89

910
router = APIRouter()
@@ -23,6 +24,8 @@ def root_path(
2324
"repository": get_settings().repository,
2425
"author": get_settings().contact["name"],
2526
"website": get_settings().contact["url"],
27+
"year": get_settings().year,
28+
"version": get_api_version(),
2629
}
2730
return templates.TemplateResponse("index.html", context)
2831

templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<body>
1919
<header>
20-
<h1>{{ title }}</h1>
20+
<h1>{{ title }} v{{ version }}</h1>
2121
<h2>{{ description }}</h2>
2222
</header>
2323
<main>
@@ -42,7 +42,7 @@ <h2>{{ description }}</h2>
4242
<a href="{{ website }}" rel="noopener noreferrer"
4343
>{{ author }}</a
4444
>
45-
2022
45+
{{ year }}
4646
</footer>
4747
</body>
4848
</html>

0 commit comments

Comments
 (0)