-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add support for extensions * change jinja support to extension
- Loading branch information
1 parent
8937ea7
commit a39fd7b
Showing
92 changed files
with
838 additions
and
566 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Jinja | ||
|
||
This extension offers support for Jinja templates. | ||
|
||
## Usage | ||
|
||
To use jinja templates, first install the `jinja` extra: | ||
|
||
```shell | ||
pip install selva[jinja] | ||
``` | ||
|
||
Then activate the extension: | ||
|
||
=== "configuration/settings.yaml" | ||
```yaml | ||
extensions: | ||
- selva.ext.templates.jinja | ||
``` | ||
|
||
## Configuration | ||
|
||
Jinja can be configured through the `settings.yaml`. For example, to activate extensions: | ||
|
||
=== "configuration/settings.yaml" | ||
|
||
```yaml | ||
templates: | ||
jinja: | ||
extensions: | ||
- jinja2.ext.i18n | ||
- jinja2.ext.debug | ||
``` | ||
|
||
Full list of settings: | ||
|
||
```yaml | ||
templates: | ||
jinja: | ||
block_start_string: "" | ||
block_end_string: "" | ||
variable_start_string: "" | ||
variable_end_string: "" | ||
comment_start_string: "" | ||
comment_end_string: "" | ||
line_statement_prefix: "" | ||
line_comment_prefix: "" | ||
trim_blocks: true | ||
lstrip_blocks: true | ||
newline_sequence: "\n" # or "\r\n" or "\r" | ||
keep_trailing_newline: true | ||
extensions: | ||
- extension1 | ||
- extensions2 | ||
optimized: true | ||
undefined: "" # dotted path to python class | ||
finalize: "" # dotted path to python function | ||
autoescape: "" # dotted path to python function | ||
loader: "" # dotted path to python object | ||
cache_size: 1 | ||
auto_reload: true | ||
bytecode_cache: "" # dotted path to python object | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,39 @@ | ||
# Overview | ||
|
||
Selva provides extensions to integrate other libraries and funcionality | ||
Extensions are python packages that provide additional functionality or integrate | ||
external libraries into the framework. | ||
|
||
Current provided extensions are: | ||
Current builtin extensions are: | ||
|
||
- [SQLAlchemy](../extensions/sqlalchemy.md) | ||
- [SQLAlchemy](./sqlalchemy.md) | ||
- [Jinja](./jinja.md) | ||
|
||
## Activating extensions | ||
|
||
Extensions need to be activated in `settings.yaml`, in the `extensions` property: | ||
|
||
```yaml | ||
extensions: | ||
- selva.ext.data.sqlalchemy | ||
- selva.ext.templates.jinja | ||
``` | ||
## Creating extensions | ||
An extension is a python package or module that contains a function named `selva_extension` | ||
with arguments `selva.di.Container` and `selva.configuration.Settings`. It is called | ||
during the startup phase of the application and may also be a coroutine. | ||
|
||
```python | ||
from selva.configuration import Settings | ||
from selva.di import Container | ||
# (1) | ||
def selva_extension(container: Container, settings: Settings): | ||
pass | ||
``` | ||
|
||
1. `selva_extension` can also be `async`. | ||
|
||
The function can then access values in the settings object, register new services, | ||
retrieve the router service to register new routes, etc. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
logging: | ||
root: INFO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
databases[aiosqlite]==0.6.0 | ||
databases[aiosqlite]==0.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
extensions: | ||
- selva.ext.templates.jinja |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Annotated | ||
|
||
from asgikit.responses import respond_json | ||
from selva.di import Inject | ||
from selva.web import controller, get | ||
|
||
from .service import DefautDBService, OtherDBService | ||
|
||
|
||
@controller | ||
class Controller: | ||
default_db_service: Annotated[DefautDBService, Inject] | ||
other_db_service: Annotated[OtherDBService, Inject] | ||
|
||
@get | ||
async def index(self, request): | ||
db_version = await self.default_db_service.db_version() | ||
model = await self.default_db_service.get_model() | ||
dto = { | ||
"id": model.id, | ||
"name": model.name, | ||
} | ||
await respond_json(request.response, {"db_version": db_version, "model": dto}) | ||
|
||
@get("other") | ||
async def other(self, request): | ||
db_version = await self.other_db_service.db_version() | ||
await respond_json(request.response, {"db_version": db_version}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from sqlalchemy import Column, Integer, String | ||
from sqlalchemy.orm import DeclarativeBase | ||
|
||
|
||
class Base(DeclarativeBase): | ||
pass | ||
|
||
|
||
class MyModel(Base): | ||
__tablename__ = 'my_model' | ||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
name = Column(String(length=100)) | ||
|
||
def __repr__(self): | ||
return f"<MyModel(id={self.id}, name={self.name})>" |
Oops, something went wrong.