From 40e21dd18c58d55576bb17f4d1e1c7d593fef7a8 Mon Sep 17 00:00:00 2001 From: Anton Bakker Date: Tue, 1 Aug 2023 15:52:35 +0200 Subject: [PATCH] import assets through importlib - so assets are packages as well --- .vscode/launch.json | 20 +++++++++++++++++++ .../assets/ __init__.py | 0 .../assets/openapi.yaml | 0 coordinates_transformation_api/main.py | 10 +++++++--- pyproject.toml | 3 +++ 5 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 coordinates_transformation_api/assets/ __init__.py rename openapi.yaml => coordinates_transformation_api/assets/openapi.yaml (100%) diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..f8e4aef --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: FastAPI", + "type": "python", + "request": "launch", + "module": "uvicorn", + "args": [ + "coordinates_transformation_api.main:start", + "--reload" + ], + "jinja": true, + "justMyCode": true + } + ] +} diff --git a/coordinates_transformation_api/assets/ __init__.py b/coordinates_transformation_api/assets/ __init__.py new file mode 100644 index 0000000..e69de29 diff --git a/openapi.yaml b/coordinates_transformation_api/assets/openapi.yaml similarity index 100% rename from openapi.yaml rename to coordinates_transformation_api/assets/openapi.yaml diff --git a/coordinates_transformation_api/main.py b/coordinates_transformation_api/main.py index dc7d9e5..be43798 100644 --- a/coordinates_transformation_api/main.py +++ b/coordinates_transformation_api/main.py @@ -4,6 +4,10 @@ import yaml import uvicorn +from importlib import resources as impresources +from . import assets + + default_headers = {"API-Version": "2.0.1"} app = FastAPI(docs_url="/api") @@ -81,12 +85,12 @@ async def transform(): def custom_openapi(): - with open("openapi.yaml", "rb") as openapi: - return yaml.load(openapi, yaml.SafeLoader) + oas_file_resource = (impresources.files(assets) / "openapi.yaml") + with oas_file_resource.open("rb") as oas_file: + return yaml.load(oas_file, yaml.SafeLoader) app.openapi = custom_openapi - def start(): uvicorn.run("coordinates_transformation_api.main:app", workers=2, host="0.0.0.0", port=8000, reload=True) diff --git a/pyproject.toml b/pyproject.toml index 1b49a3e..1668147 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,3 +37,6 @@ enabled = true [tool.setuptools.packages.find] include = ["coordinates_transformation_api*"] # package names should match these glob patterns (["*"] by default) exclude = [] # exclude packages matching these glob patterns (empty by default) + +[tool.setuptools.package-data] +"coordinates_transformation_api.assets" = ["*"]