Skip to content

Commit

Permalink
Merge pull request #49 from a5chin/feature/settings
Browse files Browse the repository at this point in the history
Add settings
  • Loading branch information
a5chin authored Dec 24, 2024
2 parents c1cca17 + 023cc31 commit ef0073d
Show file tree
Hide file tree
Showing 20 changed files with 229 additions and 21 deletions.
1 change: 1 addition & 0 deletions .env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IS_LOCAL=True
59 changes: 41 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ uv run ruff check . --fix
hodolint Dockerfile
```

## pytest
To run the test, use the following command:
```sh
uv run pytest
```

## Appendix

### Install libraries
Expand All @@ -144,34 +150,51 @@ uv add {libraries}
### The structure of this repository
```
.
├── .devcontainer
├── .devcontainer/
│ ├── devcontainer.json
│ └── Dockerfile
├── Dockerfile
├── .github
│ ├── actions
├── .github/
│ ├── actions/
│ │ ├── setup-git-config
│ │ │ └── action.yml
│ │ └── setup-python-with-uv
│ │ └── action.yml
│ ├── dependabot.yml
│ └── workflows
│ ├── docker.yml
│ ├── pyright.yml
│ ├── ruff.yml
│ └── test.yml
├── .dockergitignore
├── docs/
│ ├── workflows/
│ │ ├── docker.yml
│ │ ├── pyright.yml
│ │ ├── ruff.yml
│ │ └── test.yml
│ └── dependabot.yml
├── .vscode
│ ├── extensions.json
│ └── settings.json
├── tests/
│ └── tools/
│ └── test__logger.py
├── tools/
│ ├── config/
│ │ ├── __init__.py
│ │ ├── fastapi.py
│ │ └── settings.py
│ ├── logger/
│ │ ├── __init__.py
│ │ ├── color.py
│ │ ├── googlecloud.py
│ │ ├── local.py
│ │ ├── logger.py
│ │ ├── style.py
│ │ └── type.py
│ └── __init__.py
├── .dockerignore
├── .env.local
├── .gitignore
├── LICENSE
├── .pre-commit-config.yaml
├── .python-version
├── Dockerfile
├── pyproject.toml
├── pyrightconfig.json
├── .python-version
├── pytest.ini
├── README.md
├── ruff.toml
├── uv.lock
└── .vscode
├── extensions.json
└── settings.json
└── uv.lock
```
1 change: 1 addition & 0 deletions docs/guides/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ How to use this repository.
- [How to use pre-commit](pre-commit.md)
- [How to use Test](test.md)
- [How to use Tools](tools/index.md)
- [How to use config in this repository](tools/config.md)
- [How to use logger in this repository](tools/logger.md)
3 changes: 3 additions & 0 deletions docs/guides/pre-commit.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
```sh
uv run pre-commit uninstall
```

## pre-commit Configurations
If you want to configure the pre-commit, visit the [Configuration for pre-commit](../configurations/pre-commit.md) page.
2 changes: 1 addition & 1 deletion docs/guides/pyright.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ uv run pyright
```

## Pyright Configurations
Please refer [here](../configurations/pyright.md).
If you want to configure the Pyright, visit the [Configuration for Pyright](../configurations/pyright.md) page.
3 changes: 3 additions & 0 deletions docs/guides/ruff.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ uv run ruff format
```sh
uv run ruff check
```

## Configuration for Ruff
If you want to configure the Ruff, visit the [Configuration for ruff](../configurations/ruff.md) page.
3 changes: 3 additions & 0 deletions docs/guides/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ Test with coverage on VS Code
/// caption
Code coverage on editor
///

## Configuration for Test
If you want to configure the Test hook, visit the [Configuration for Test](../configurations/test.md) page.
30 changes: 30 additions & 0 deletions docs/guides/tools/config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Environment Variables
- `.env`
- Use this file when you want to set environment variables for the project.
- `.env.local`
- Use this file when you want to set environment variables for the local environment.

Addendum environment variables to `tools/config/settings.py`:
```{.py title="tools/config/settings.py" hl_lines="9"}
class Settings(BaseSettings):
"""Environment variables settings."""

model_config = SettingsConfigDict(
env_file=(".env", ".env.local"),
env_file_encoding="utf-8",
)

DEBUG: bool = False
IS_LOCAL: bool = False
```

## FastAPI
```python
from fastapi import FastAPI

from tools import Settings


settings = Settings()
app = FastAPI(**settings.fastapi_kwargs)
```
1 change: 1 addition & 0 deletions docs/guides/tools/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

How to use tools in this repository.

- [How to use config](config.md)
- [How to use logger](logger.md)
3 changes: 3 additions & 0 deletions docs/guides/uv.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ If you want to pin the Python version, run the following command:
```sh
uv pin python 3.12
```

## Configuration for uv
If you want to configure the uv, visit the [Configuration for uv](../configurations/uv.md) page.
9 changes: 7 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ This repository contains configurations to set up a Python development environme
│ └── Dockerfile
├── .github/
│ ├── actions/
│ │ ├── setup-git-config.yml
│ │ ├── setup-git-config
│ │ │ └── action.yml
│ │ └── setup-python-with-uv.yml
│ │ └── setup-python-with-uv
│ │ └── action.yml
│ ├── workflows/
│ │ ├── docker.yml
Expand All @@ -29,6 +29,10 @@ This repository contains configurations to set up a Python development environme
│ └── tools/
│ └── test__logger.py
├── tools/
│ ├── config/
│ │ ├── __init__.py
│ │ ├── fastapi.py
│ │ └── settings.py
│ ├── logger/
│ │ ├── __init__.py
│ │ ├── color.py
Expand All @@ -39,6 +43,7 @@ This repository contains configurations to set up a Python development environme
│ │ └── type.py
│ └── __init__.py
├── .dockerignore
├── .env.local
├── .gitignore
├── .pre-commit-config.yaml
├── .python-version
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ nav:
- Test: guides/test.md
- Using tools:
- guides/tools/index.md
- config: guides/tools/config.md
- logger: guides/tools/logger.md
- Configurations:
- configurations/index.md
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license = { file = "LICENSE" }

dependencies = [
"google-cloud-logging>=3.11.3",
"pydantic-settings>=2.7.0",
"pydantic>=2.10.4",
]

Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest

from tools import Settings


@pytest.fixture
def settings() -> Settings:
"""Fixture for settings."""
return Settings()
31 changes: 31 additions & 0 deletions tests/tools/test__config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest

from tools import Settings
from tools.config import FastAPIKwArgs


class TestSettings:
"""Test class for Settings."""

@pytest.mark.usefixtures("settings")
def test_local(self, settings: Settings) -> None:
"""Test local settings."""
assert settings.IS_LOCAL

@pytest.mark.usefixtures("settings")
def test_fastapi_kwargs(self, settings: Settings) -> None:
"""Test fastapi_kwargs."""
assert (
settings.fastapi_kwargs
== FastAPIKwArgs(
debug=False,
title="FastAPI",
summary=None,
description="",
version="0.1.0",
openapi_url="/openapi.json",
docs_url="/docs",
redoc_url="/redoc",
openapi_prefix="",
).model_dump()
)
2 changes: 2 additions & 0 deletions tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Tools."""

from tools.config import Settings
from tools.logger import Logger, LogType

__all__ = [
"LogType",
"Logger",
"Settings",
]
9 changes: 9 additions & 0 deletions tools/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Settings."""

from tools.config.fastapi import FastAPIKwArgs
from tools.config.settings import Settings

__all__ = [
"FastAPIKwArgs",
"Settings",
]
15 changes: 15 additions & 0 deletions tools/config/fastapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pydantic import BaseModel


class FastAPIKwArgs(BaseModel):
"""FastAPI kwargs."""

debug: bool
title: str
version: str
summary: str | None
description: str
openapi_url: str
docs_url: str
redoc_url: str
openapi_prefix: str
43 changes: 43 additions & 0 deletions tools/config/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import Any

from pydantic_settings import BaseSettings, SettingsConfigDict

from tools.config.fastapi import FastAPIKwArgs


class Settings(BaseSettings):
"""Environment variables settings."""

model_config = SettingsConfigDict(
env_file=(".env", ".env.local"),
env_file_encoding="utf-8",
)

IS_LOCAL: bool = False

debug: bool = False
title: str = "FastAPI"
summary: str | None = None
description: str = ""
version: str = "0.1.0"
openapi_url: str = "/openapi.json"
docs_url: str = "/docs"
redoc_url: str = "/redoc"
openapi_prefix: str = ""
api_prefix_v1: str = "/api/v1"
allowed_hosts: list[str] = ["*"]

@property
def fastapi_kwargs(self) -> dict[str, Any]:
"""FastAPI kwargs."""
return FastAPIKwArgs(
debug=self.debug,
title=self.title,
summary=self.summary,
description=self.description,
version=self.version,
openapi_url=self.openapi_url,
docs_url=self.docs_url,
redoc_url=self.redoc_url,
openapi_prefix=self.openapi_prefix,
).model_dump()
24 changes: 24 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit ef0073d

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCover
tests
   conftest.py50100%
tests/tools
   test__config.py100100%
   test__logger.py240100%
tools
   __init__.py30100%
tools/config
   __init__.py30100%
   fastapi.py110100%
   settings.py200100%
tools/logger
   __init__.py50100%
   color.py120100%
   googlecloud.py100100%
   local.py120100%
   logger.py230100%
   style.py70100%
   type.py50100%
TOTAL1500100%

Tests Skipped Failures Errors Time
6 0 💤 0 ❌ 0 🔥 1.264s ⏱️

Please sign in to comment.