Compare commits
1 Commits
dev
...
b4e6cecdfe
| Author | SHA1 | Date | |
|---|---|---|---|
|
b4e6cecdfe
|
@@ -16,22 +16,19 @@ jobs:
|
|||||||
- uses: actions/setup-python@v5
|
- uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
cache: 'pip'
|
cache: 'pip'
|
||||||
- name: Bugis Core
|
- name: Create virtualenv
|
||||||
run: |
|
run: |
|
||||||
cd core
|
|
||||||
python -m venv .venv
|
python -m venv .venv
|
||||||
.venv/bin/pip install -r requirements-dev.txt
|
.venv/bin/pip install -r requirements-dev.txt
|
||||||
|
- name: Execute build
|
||||||
|
run: |
|
||||||
.venv/bin/python -m build
|
.venv/bin/python -m build
|
||||||
.venv/bin/pip install .
|
|
||||||
.venv/bin/python -m mypy -p src
|
|
||||||
.venv/bin/python -m unittest discover -s tests
|
|
||||||
- name: Publish artifacts
|
- name: Publish artifacts
|
||||||
env:
|
env:
|
||||||
TWINE_REPOSITORY_URL: ${{ vars.PYPI_REGISTRY_URL }}
|
TWINE_REPOSITORY_URL: ${{ vars.PYPI_REGISTRY_URL }}
|
||||||
TWINE_USERNAME: ${{ vars.PUBLISHER_USERNAME }}
|
TWINE_USERNAME: ${{ vars.PUBLISHER_USERNAME }}
|
||||||
TWINE_PASSWORD: ${{ secrets.PUBLISHER_TOKEN }}
|
TWINE_PASSWORD: ${{ secrets.PUBLISHER_TOKEN }}
|
||||||
run: |
|
run: |
|
||||||
cd core
|
|
||||||
.venv/bin/python -m twine upload --repository gitea dist/*{.whl,tar.gz}
|
.venv/bin/python -m twine upload --repository gitea dist/*{.whl,tar.gz}
|
||||||
build_docker_image:
|
build_docker_image:
|
||||||
name: "Build Docker image"
|
name: "Build Docker image"
|
||||||
|
|||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,7 +1,7 @@
|
|||||||
.venv
|
.venv
|
||||||
__pycache__
|
__pycache__
|
||||||
*.pyc
|
*.pyc
|
||||||
_version.py
|
src/bugis/_version.py
|
||||||
*.egg-info
|
*.egg-info
|
||||||
/build
|
/build
|
||||||
/dist
|
/dist
|
||||||
|
|||||||
14
Dockerfile
14
Dockerfile
@@ -9,10 +9,10 @@ RUN adduser -D luser
|
|||||||
USER luser
|
USER luser
|
||||||
WORKDIR /home/luser
|
WORKDIR /home/luser
|
||||||
COPY --chown=luser:users ./requirements-dev.txt ./requirements-dev.txt
|
COPY --chown=luser:users ./requirements-dev.txt ./requirements-dev.txt
|
||||||
COPY --chown=luser:users ./requirements-run.txt ./requirements-run.txt
|
COPY --chown=luser:users ./requirements-dev.txt ./requirements-run.txt
|
||||||
WORKDIR /home/luser/
|
WORKDIR /home/luser/
|
||||||
RUN python -m venv .venv
|
RUN python -m venv .venv
|
||||||
RUN --mount=type=cache,target=/home/luser/.cache/pip,uid=1000,gid=1000 .venv/bin/pip wheel -w /home/luser/wheel pygraphviz
|
RUN --mount=type=cache,target=/home/luser/.cache/pip,uid=1000,gid=1000 .venv/bin/pip wheel -w /home/luser/wheel -r requirements-dev.txt pygraphviz
|
||||||
RUN --mount=type=cache,target=/home/luser/.cache/pip,uid=1000,gid=1000 .venv/bin/pip install -r requirements-dev.txt /home/luser/wheel/*.whl
|
RUN --mount=type=cache,target=/home/luser/.cache/pip,uid=1000,gid=1000 .venv/bin/pip install -r requirements-dev.txt /home/luser/wheel/*.whl
|
||||||
COPY --chown=luser:users . /home/luser/bugis
|
COPY --chown=luser:users . /home/luser/bugis
|
||||||
WORKDIR /home/luser/bugis
|
WORKDIR /home/luser/bugis
|
||||||
@@ -21,12 +21,12 @@ RUN --mount=type=cache,target=/home/luser/.cache/pip,uid=1000,gid=1000 /home/lus
|
|||||||
|
|
||||||
FROM base AS release
|
FROM base AS release
|
||||||
RUN mkdir /srv/http
|
RUN mkdir /srv/http
|
||||||
RUN adduser -D -h /var/lib/bugis -u 1000 bugis
|
RUN adduser -D -h /var/bugis -u 1000 bugis
|
||||||
USER bugis
|
USER bugis
|
||||||
WORKDIR /var/lib/bugis
|
WORKDIR /var/bugis
|
||||||
COPY --chown=bugis:users conf/pip.conf ./.pip/pip.conf
|
COPY --chown=bugis:users conf/pip.conf ./.pip/pip.conf
|
||||||
RUN python -m venv .venv
|
RUN python -m venv .venv
|
||||||
RUN --mount=type=cache,target=/var/bugis/.cache/pip,uid=1000,gid=1000 --mount=type=bind,ro,from=build,source=/home/luser/requirements-run.txt,target=/requirements-run.txt --mount=type=bind,ro,from=build,source=/home/luser/wheel,target=/wheel .venv/bin/pip install -r /requirements-run.txt /wheel/*.whl
|
RUN --mount=type=cache,target=/var/bugis/.cache/pip,uid=1000,gid=1000 --mount=type=bind,ro,from=build,source=/home/luser/bugis/requirements-run.txt,target=/requirements-run.txt --mount=type=bind,ro,from=build,source=/home/luser/wheel,target=/wheel .venv/bin/pip install -r /requirements-run.txt /wheel/*.whl
|
||||||
RUN --mount=type=cache,target=/var/bugis/.cache/pip,uid=1000,gid=1000 --mount=type=bind,ro,from=build,source=/home/luser/bugis/dist,target=/dist .venv/bin/pip install /dist/*.whl
|
RUN --mount=type=cache,target=/var/bugis/.cache/pip,uid=1000,gid=1000 --mount=type=bind,ro,from=build,source=/home/luser/bugis/dist,target=/dist .venv/bin/pip install /dist/*.whl
|
||||||
VOLUME /srv/http
|
VOLUME /srv/http
|
||||||
WORKDIR /srv/http
|
WORKDIR /srv/http
|
||||||
@@ -36,7 +36,7 @@ ENV GRANIAN_PORT=8000
|
|||||||
ENV GRANIAN_INTERFACE=asgi
|
ENV GRANIAN_INTERFACE=asgi
|
||||||
ENV GRANIAN_LOOP=asyncio
|
ENV GRANIAN_LOOP=asyncio
|
||||||
ENV GRANIAN_LOG_ENABLED=false
|
ENV GRANIAN_LOG_ENABLED=false
|
||||||
ENV GRANIAN_LOG_ACCESS_ENABLED=true
|
|
||||||
ENTRYPOINT ["/var/lib/bugis/.venv/bin/python", "-m", "granian", "bugis.asgi:application"]
|
ENTRYPOINT ["/var/bugis/.venv/bin/python", "-m", "granian", "bugis.asgi:application"]
|
||||||
EXPOSE 8000/tcp
|
EXPOSE 8000/tcp
|
||||||
|
|
||||||
|
|||||||
19
README.md
19
README.md
@@ -18,23 +18,6 @@ docker run --rm -v /your/document/directory:/srv/http --user $(id -u):$(id -g)
|
|||||||
### Run in docker with `nginx` and `plantUML` server
|
### Run in docker with `nginx` and `plantUML` server
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
STATIC_ROOT=/your/document/directory UID=$(id -u) GID=$(id -g) docker compose up --build
|
docker compose up --build
|
||||||
```
|
```
|
||||||
|
|
||||||
### Install with pipx
|
|
||||||
|
|
||||||
```bash
|
|
||||||
pipx install -r requirements-run.txt .
|
|
||||||
```
|
|
||||||
|
|
||||||
or
|
|
||||||
|
|
||||||
```bash
|
|
||||||
pipx install --extra-index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple/ bugis[run]
|
|
||||||
```
|
|
||||||
|
|
||||||
### Run from cli
|
|
||||||
|
|
||||||
```bash
|
|
||||||
bugis -a 127.0.0.1 -p 8000
|
|
||||||
```
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
[build-system]
|
|
||||||
requires = ["setuptools>=61.0", "setuptools-scm>=8"]
|
|
||||||
build-backend = "setuptools.build_meta"
|
|
||||||
|
|
||||||
[project]
|
|
||||||
name = "bugis_cli"
|
|
||||||
dynamic = ["version"]
|
|
||||||
authors = [
|
|
||||||
{ name="Walter Oggioni", email="oggioni.walter@gmail.com" },
|
|
||||||
]
|
|
||||||
description = "Markdown to HTML renderer"
|
|
||||||
readme = "README.md"
|
|
||||||
requires-python = ">=3.10"
|
|
||||||
classifiers = [
|
|
||||||
'Development Status :: 3 - Alpha',
|
|
||||||
'Topic :: Utilities',
|
|
||||||
'License :: OSI Approved :: MIT License',
|
|
||||||
'Intended Audience :: System Administrators',
|
|
||||||
'Intended Audience :: Developers',
|
|
||||||
'Environment :: Console',
|
|
||||||
'License :: OSI Approved :: MIT License',
|
|
||||||
'Programming Language :: Python :: 3',
|
|
||||||
]
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
"granian",
|
|
||||||
"bugis"
|
|
||||||
]
|
|
||||||
|
|
||||||
[project.optional-dependencies]
|
|
||||||
dev = [
|
|
||||||
"build", "mypy", "ipdb", "twine"
|
|
||||||
]
|
|
||||||
|
|
||||||
[project.urls]
|
|
||||||
"Homepage" = "https://github.com/woggioni/bugis"
|
|
||||||
"Bug Tracker" = "https://github.com/woggioni/bugis/issues"
|
|
||||||
|
|
||||||
[tool.mypy]
|
|
||||||
python_version = "3.12"
|
|
||||||
disallow_untyped_defs = true
|
|
||||||
show_error_codes = true
|
|
||||||
no_implicit_optional = true
|
|
||||||
warn_return_any = true
|
|
||||||
warn_unused_ignores = true
|
|
||||||
exclude = ["scripts", "docs", "test"]
|
|
||||||
strict = true
|
|
||||||
|
|
||||||
[tool.setuptools_scm]
|
|
||||||
root='..'
|
|
||||||
version_file = "src/bugis/cli/_version.py"
|
|
||||||
|
|
||||||
[project.scripts]
|
|
||||||
bugis = "bugis.cli:main"
|
|
||||||
@@ -1,171 +0,0 @@
|
|||||||
#
|
|
||||||
# This file is autogenerated by pip-compile with Python 3.12
|
|
||||||
# by the following command:
|
|
||||||
#
|
|
||||||
# pip-compile --extra=dev --output-file=requirements-dev.txt
|
|
||||||
#
|
|
||||||
--index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple
|
|
||||||
--extra-index-url https://pypi.org/simple
|
|
||||||
|
|
||||||
aiofiles==24.1.0
|
|
||||||
# via bugis
|
|
||||||
anyio==4.6.2.post1
|
|
||||||
# via httpx
|
|
||||||
asttokens==2.4.1
|
|
||||||
# via stack-data
|
|
||||||
bugis==0.2.2
|
|
||||||
# via bugis_cli (pyproject.toml)
|
|
||||||
build==1.2.2.post1
|
|
||||||
# via bugis_cli (pyproject.toml)
|
|
||||||
certifi==2024.8.30
|
|
||||||
# via
|
|
||||||
# httpcore
|
|
||||||
# httpx
|
|
||||||
# requests
|
|
||||||
cffi==1.17.1
|
|
||||||
# via cryptography
|
|
||||||
charset-normalizer==3.4.0
|
|
||||||
# via requests
|
|
||||||
click==8.1.7
|
|
||||||
# via granian
|
|
||||||
cryptography==43.0.3
|
|
||||||
# via secretstorage
|
|
||||||
decorator==5.1.1
|
|
||||||
# via
|
|
||||||
# ipdb
|
|
||||||
# ipython
|
|
||||||
docutils==0.21.2
|
|
||||||
# via readme-renderer
|
|
||||||
executing==2.1.0
|
|
||||||
# via stack-data
|
|
||||||
granian==1.6.3
|
|
||||||
# via bugis_cli (pyproject.toml)
|
|
||||||
h11==0.14.0
|
|
||||||
# via httpcore
|
|
||||||
h2==4.1.0
|
|
||||||
# via httpx
|
|
||||||
hpack==4.0.0
|
|
||||||
# via h2
|
|
||||||
httpcore==1.0.6
|
|
||||||
# via httpx
|
|
||||||
httpx[http2]==0.27.2
|
|
||||||
# via bugis
|
|
||||||
hyperframe==6.0.1
|
|
||||||
# via h2
|
|
||||||
idna==3.10
|
|
||||||
# via
|
|
||||||
# anyio
|
|
||||||
# httpx
|
|
||||||
# requests
|
|
||||||
importlib-metadata==8.5.0
|
|
||||||
# via twine
|
|
||||||
ipdb==0.13.13
|
|
||||||
# via bugis_cli (pyproject.toml)
|
|
||||||
ipython==8.29.0
|
|
||||||
# via ipdb
|
|
||||||
jaraco-classes==3.4.0
|
|
||||||
# via keyring
|
|
||||||
jaraco-context==6.0.1
|
|
||||||
# via keyring
|
|
||||||
jaraco-functools==4.1.0
|
|
||||||
# via keyring
|
|
||||||
jedi==0.19.1
|
|
||||||
# via ipython
|
|
||||||
jeepney==0.8.0
|
|
||||||
# via
|
|
||||||
# keyring
|
|
||||||
# secretstorage
|
|
||||||
keyring==25.5.0
|
|
||||||
# via twine
|
|
||||||
markdown==3.7
|
|
||||||
# via bugis
|
|
||||||
markdown-it-py==3.0.0
|
|
||||||
# via rich
|
|
||||||
matplotlib-inline==0.1.7
|
|
||||||
# via ipython
|
|
||||||
mdurl==0.1.2
|
|
||||||
# via markdown-it-py
|
|
||||||
more-itertools==10.5.0
|
|
||||||
# via
|
|
||||||
# jaraco-classes
|
|
||||||
# jaraco-functools
|
|
||||||
mypy==1.13.0
|
|
||||||
# via bugis_cli (pyproject.toml)
|
|
||||||
mypy-extensions==1.0.0
|
|
||||||
# via mypy
|
|
||||||
nh3==0.2.18
|
|
||||||
# via readme-renderer
|
|
||||||
packaging==24.1
|
|
||||||
# via build
|
|
||||||
parso==0.8.4
|
|
||||||
# via jedi
|
|
||||||
pexpect==4.9.0
|
|
||||||
# via ipython
|
|
||||||
pkginfo==1.10.0
|
|
||||||
# via twine
|
|
||||||
prompt-toolkit==3.0.48
|
|
||||||
# via ipython
|
|
||||||
ptyprocess==0.7.0
|
|
||||||
# via pexpect
|
|
||||||
pure-eval==0.2.3
|
|
||||||
# via stack-data
|
|
||||||
pwo==0.0.4
|
|
||||||
# via bugis
|
|
||||||
pycparser==2.22
|
|
||||||
# via cffi
|
|
||||||
pygments==2.18.0
|
|
||||||
# via
|
|
||||||
# bugis
|
|
||||||
# ipython
|
|
||||||
# readme-renderer
|
|
||||||
# rich
|
|
||||||
pygraphviz==1.14
|
|
||||||
# via bugis
|
|
||||||
pyproject-hooks==1.2.0
|
|
||||||
# via build
|
|
||||||
pyyaml==6.0.2
|
|
||||||
# via bugis
|
|
||||||
readme-renderer==44.0
|
|
||||||
# via twine
|
|
||||||
requests==2.32.3
|
|
||||||
# via
|
|
||||||
# requests-toolbelt
|
|
||||||
# twine
|
|
||||||
requests-toolbelt==1.0.0
|
|
||||||
# via twine
|
|
||||||
rfc3986==2.0.0
|
|
||||||
# via twine
|
|
||||||
rich==13.9.3
|
|
||||||
# via twine
|
|
||||||
secretstorage==3.3.3
|
|
||||||
# via keyring
|
|
||||||
six==1.16.0
|
|
||||||
# via asttokens
|
|
||||||
sniffio==1.3.1
|
|
||||||
# via
|
|
||||||
# anyio
|
|
||||||
# httpx
|
|
||||||
stack-data==0.6.3
|
|
||||||
# via ipython
|
|
||||||
traitlets==5.14.3
|
|
||||||
# via
|
|
||||||
# ipython
|
|
||||||
# matplotlib-inline
|
|
||||||
twine==5.1.1
|
|
||||||
# via bugis_cli (pyproject.toml)
|
|
||||||
typing-extensions==4.12.2
|
|
||||||
# via
|
|
||||||
# mypy
|
|
||||||
# pwo
|
|
||||||
urllib3==2.2.3
|
|
||||||
# via
|
|
||||||
# requests
|
|
||||||
# twine
|
|
||||||
uvloop==0.21.0
|
|
||||||
# via granian
|
|
||||||
watchdog==5.0.3
|
|
||||||
# via bugis
|
|
||||||
wcwidth==0.2.13
|
|
||||||
# via prompt-toolkit
|
|
||||||
zipp==3.20.2
|
|
||||||
# via importlib-metadata
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
#
|
|
||||||
# This file is autogenerated by pip-compile with Python 3.12
|
|
||||||
# by the following command:
|
|
||||||
#
|
|
||||||
# pip-compile --output-file=requirements.txt
|
|
||||||
#
|
|
||||||
--index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple
|
|
||||||
--extra-index-url https://pypi.org/simple
|
|
||||||
|
|
||||||
aiofiles==24.1.0
|
|
||||||
# via bugis
|
|
||||||
anyio==4.6.2.post1
|
|
||||||
# via httpx
|
|
||||||
bugis==0.2.2
|
|
||||||
# via bugis_cli (pyproject.toml)
|
|
||||||
certifi==2024.8.30
|
|
||||||
# via
|
|
||||||
# httpcore
|
|
||||||
# httpx
|
|
||||||
click==8.1.7
|
|
||||||
# via granian
|
|
||||||
granian==1.6.3
|
|
||||||
# via bugis_cli (pyproject.toml)
|
|
||||||
h11==0.14.0
|
|
||||||
# via httpcore
|
|
||||||
h2==4.1.0
|
|
||||||
# via httpx
|
|
||||||
hpack==4.0.0
|
|
||||||
# via h2
|
|
||||||
httpcore==1.0.6
|
|
||||||
# via httpx
|
|
||||||
httpx[http2]==0.27.2
|
|
||||||
# via bugis
|
|
||||||
hyperframe==6.0.1
|
|
||||||
# via h2
|
|
||||||
idna==3.10
|
|
||||||
# via
|
|
||||||
# anyio
|
|
||||||
# httpx
|
|
||||||
markdown==3.7
|
|
||||||
# via bugis
|
|
||||||
pwo==0.0.4
|
|
||||||
# via bugis
|
|
||||||
pygments==2.18.0
|
|
||||||
# via bugis
|
|
||||||
pygraphviz==1.14
|
|
||||||
# via bugis
|
|
||||||
pyyaml==6.0.2
|
|
||||||
# via bugis
|
|
||||||
sniffio==1.3.1
|
|
||||||
# via
|
|
||||||
# anyio
|
|
||||||
# httpx
|
|
||||||
typing-extensions==4.12.2
|
|
||||||
# via pwo
|
|
||||||
uvloop==0.21.0
|
|
||||||
# via granian
|
|
||||||
watchdog==5.0.3
|
|
||||||
# via bugis
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
from os import environ
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Optional, Sequence
|
|
||||||
import argparse
|
|
||||||
import yaml
|
|
||||||
from granian import Granian
|
|
||||||
from pwo import Maybe
|
|
||||||
|
|
||||||
from bugis.configuration import instance, Configuration
|
|
||||||
from granian.constants import HTTPModes, ThreadModes, Loops
|
|
||||||
from dataclasses import asdict
|
|
||||||
from typing import Any, Mapping
|
|
||||||
|
|
||||||
def main(args: Optional[Sequence[str]] = None) -> None:
|
|
||||||
parser = argparse.ArgumentParser(description="A simple CLI program to render Markdown files")
|
|
||||||
default_configuration_file = (Maybe.of_nullable(environ.get('XDG_CONFIG_HOME'))
|
|
||||||
.map(lambda it: Path(it))
|
|
||||||
.map(lambda it: it / 'bugis' / 'bugis.yaml')
|
|
||||||
.or_else_get(
|
|
||||||
lambda: Maybe.of_nullable(environ.get('HOME'))
|
|
||||||
.map(lambda it: Path(it) / '.config' / 'bugis' / 'bugis.yaml').or_none())
|
|
||||||
.filter(Path.exists)
|
|
||||||
.or_none()
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'-c',
|
|
||||||
'--configuration',
|
|
||||||
help='Path to the configuration file',
|
|
||||||
default=default_configuration_file,
|
|
||||||
type=Path,
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'-a',
|
|
||||||
'--address',
|
|
||||||
help='Server bind address',
|
|
||||||
default='127.0.0.1',
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'-p',
|
|
||||||
'--port',
|
|
||||||
help='Server port',
|
|
||||||
default='8000',
|
|
||||||
type=int
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--access-log',
|
|
||||||
help='Enable access log',
|
|
||||||
action='store_true',
|
|
||||||
dest='log_access'
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--logging-configuration',
|
|
||||||
help='Logging configuration file',
|
|
||||||
dest='log_config_file'
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'-w', '--workers',
|
|
||||||
help='Number of worker processes',
|
|
||||||
default='1',
|
|
||||||
dest='workers',
|
|
||||||
type=int
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'-t', '--threads',
|
|
||||||
help='Number of threads per worker',
|
|
||||||
default='1',
|
|
||||||
dest='threads',
|
|
||||||
type=int
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--http',
|
|
||||||
help='HTTP protocol version',
|
|
||||||
dest='http',
|
|
||||||
type=lambda it: HTTPModes(it),
|
|
||||||
choices=[str(mode) for mode in HTTPModes],
|
|
||||||
default='auto',
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--threading-mode',
|
|
||||||
help='Threading mode',
|
|
||||||
dest='threading_mode',
|
|
||||||
type=lambda it: ThreadModes(it),
|
|
||||||
choices=[str(mode) for mode in ThreadModes],
|
|
||||||
default=ThreadModes.workers
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--loop',
|
|
||||||
help='Loop',
|
|
||||||
dest='loop',
|
|
||||||
type=lambda it: Loops(it),
|
|
||||||
choices=[str(mode) for mode in Loops]
|
|
||||||
)
|
|
||||||
arguments = parser.parse_args(args)
|
|
||||||
|
|
||||||
def parse(configuration: Path) -> Any:
|
|
||||||
with open(configuration, 'r') as f:
|
|
||||||
return yaml.safe_load(f)
|
|
||||||
|
|
||||||
def assign(it: Configuration) -> None:
|
|
||||||
global instance
|
|
||||||
instance = it
|
|
||||||
|
|
||||||
Maybe.of_nullable(arguments.configuration).map(parse).if_present(assign)
|
|
||||||
conf = instance
|
|
||||||
|
|
||||||
granian_conf = asdict(conf).setdefault('granian', dict())
|
|
||||||
for k, v in vars(arguments).items():
|
|
||||||
if v is not None:
|
|
||||||
granian_conf[k] = v
|
|
||||||
if arguments.log_config_file:
|
|
||||||
with open(arguments.log_config_file, 'r') as f:
|
|
||||||
granian_conf['log_dictconfig'] = yaml.safe_load(f)
|
|
||||||
granian_conf = Configuration.GranianConfiguration.from_dict(granian_conf)
|
|
||||||
|
|
||||||
Granian(
|
|
||||||
"bugis.asgi:application",
|
|
||||||
**asdict(granian_conf)
|
|
||||||
).serve()
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
from . import main
|
|
||||||
import sys
|
|
||||||
|
|
||||||
main(sys.argv[1:])
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
version: 1
|
|
||||||
disable_existing_loggers: False
|
|
||||||
handlers:
|
|
||||||
console:
|
|
||||||
class : logging.StreamHandler
|
|
||||||
formatter: default
|
|
||||||
level : INFO
|
|
||||||
stream : ext://sys.stderr
|
|
||||||
access:
|
|
||||||
class : logging.StreamHandler
|
|
||||||
formatter: access
|
|
||||||
level : INFO
|
|
||||||
stream : ext://sys.stdout
|
|
||||||
formatters:
|
|
||||||
default:
|
|
||||||
format: '{asctime}.{msecs:0<3.0f} [{levelname}] ({processName:s}/{threadName:s}) - {name} - {message}'
|
|
||||||
style: '{'
|
|
||||||
datefmt: '%Y-%m-%d %H:%M:%S'
|
|
||||||
access:
|
|
||||||
format: '%(message)s'
|
|
||||||
loggers:
|
|
||||||
root:
|
|
||||||
handlers: [console]
|
|
||||||
_granian:
|
|
||||||
level: INFO
|
|
||||||
propagate: False
|
|
||||||
granian.access:
|
|
||||||
handlers: [ access ]
|
|
||||||
level: INFO
|
|
||||||
propagate: False
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
server {
|
|
||||||
listen 8080;
|
|
||||||
http2 on;
|
|
||||||
|
|
||||||
server_name localhost;
|
|
||||||
|
|
||||||
location / {
|
|
||||||
proxy_pass http://granian:8000;
|
|
||||||
proxy_http_version 1.1;
|
|
||||||
proxy_set_header Host $host;
|
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
||||||
proxy_read_timeout 60s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
{
|
|
||||||
"version": 1,
|
|
||||||
"disable_existing_loggers": false,
|
|
||||||
"handlers": {
|
|
||||||
"console": {
|
|
||||||
"class": "logging.StreamHandler",
|
|
||||||
"formatter": "default",
|
|
||||||
"level": "DEBUG",
|
|
||||||
"stream": "ext://sys.stderr"
|
|
||||||
},
|
|
||||||
"access": {
|
|
||||||
"class": "logging.StreamHandler",
|
|
||||||
"formatter": "access",
|
|
||||||
"level": "DEBUG",
|
|
||||||
"stream": "ext://sys.stdout"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"formatters": {
|
|
||||||
"default": {
|
|
||||||
"format": "{asctime}.{msecs:0<3.0f} [{levelname}] ({processName:s}/{threadName:s}) - {name} - {message}",
|
|
||||||
"style": "{",
|
|
||||||
"datefmt": "%Y-%m-%d %H:%M:%S"
|
|
||||||
},
|
|
||||||
"access": {
|
|
||||||
"format": "%(message)s"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"loggers": {
|
|
||||||
"root": {
|
|
||||||
"handlers": [
|
|
||||||
"console"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"_granian": {
|
|
||||||
"level": "DEBUG",
|
|
||||||
"propagate": false
|
|
||||||
},
|
|
||||||
"granian.access": {
|
|
||||||
"handlers": [
|
|
||||||
"access"
|
|
||||||
],
|
|
||||||
"level": "DEBUG",
|
|
||||||
"propagate": false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
from bugis.core import BugisApp, HttpContext
|
|
||||||
|
|
||||||
|
|
||||||
class Hello(BugisApp):
|
|
||||||
|
|
||||||
async def handle_request(self, ctx: HttpContext) -> None:
|
|
||||||
async for chunk in ctx.request_body:
|
|
||||||
print(chunk)
|
|
||||||
await ctx.send_str(200, 'Hello World')
|
|
||||||
|
|
||||||
|
|
||||||
app = BugisApp()
|
|
||||||
|
|
||||||
|
|
||||||
@app.GET('/hello')
|
|
||||||
@app.GET('/hello2')
|
|
||||||
async def handle_request(ctx: HttpContext) -> None:
|
|
||||||
async for chunk in ctx.request_body:
|
|
||||||
print(chunk)
|
|
||||||
await ctx.send_str(200, 'Hello World')
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
[build-system]
|
|
||||||
requires = ["setuptools>=61.0", "setuptools-scm>=8"]
|
|
||||||
build-backend = "setuptools.build_meta"
|
|
||||||
|
|
||||||
[project]
|
|
||||||
name = "bugis_core"
|
|
||||||
dynamic = ["version"]
|
|
||||||
authors = [
|
|
||||||
{ name="Walter Oggioni", email="oggioni.walter@gmail.com" },
|
|
||||||
]
|
|
||||||
description = "Markdown to HTML renderer"
|
|
||||||
readme = "README.md"
|
|
||||||
requires-python = ">=3.10"
|
|
||||||
classifiers = [
|
|
||||||
'Development Status :: 3 - Alpha',
|
|
||||||
'Topic :: Utilities',
|
|
||||||
'License :: OSI Approved :: MIT License',
|
|
||||||
'Intended Audience :: System Administrators',
|
|
||||||
'Intended Audience :: Developers',
|
|
||||||
'Environment :: Console',
|
|
||||||
'License :: OSI Approved :: MIT License',
|
|
||||||
'Programming Language :: Python :: 3',
|
|
||||||
]
|
|
||||||
|
|
||||||
dependencies = [
|
|
||||||
"pwo",
|
|
||||||
]
|
|
||||||
|
|
||||||
[project.optional-dependencies]
|
|
||||||
dev = [
|
|
||||||
"build", "mypy", "ipdb", "twine", "granian", "httpx"
|
|
||||||
]
|
|
||||||
|
|
||||||
rsgi = [
|
|
||||||
"granian"
|
|
||||||
]
|
|
||||||
|
|
||||||
[project.urls]
|
|
||||||
"Homepage" = "https://github.com/woggioni/bugis"
|
|
||||||
"Bug Tracker" = "https://github.com/woggioni/bugis/issues"
|
|
||||||
|
|
||||||
[tool.mypy]
|
|
||||||
python_version = "3.12"
|
|
||||||
disallow_untyped_defs = true
|
|
||||||
show_error_codes = true
|
|
||||||
no_implicit_optional = true
|
|
||||||
warn_return_any = true
|
|
||||||
warn_unused_ignores = true
|
|
||||||
exclude = ["scripts", "docs", "test"]
|
|
||||||
strict = true
|
|
||||||
|
|
||||||
[tool.setuptools_scm]
|
|
||||||
root='..'
|
|
||||||
version_file = "src/bugis/core/_version.py"
|
|
||||||
|
|
||||||
@@ -1,138 +0,0 @@
|
|||||||
#
|
|
||||||
# This file is autogenerated by pip-compile with Python 3.12
|
|
||||||
# by the following command:
|
|
||||||
#
|
|
||||||
# pip-compile --extra=dev --output-file=requirements-dev.txt
|
|
||||||
#
|
|
||||||
--index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple
|
|
||||||
--extra-index-url https://pypi.org/simple
|
|
||||||
|
|
||||||
aiofiles==24.1.0
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
asttokens==2.4.1
|
|
||||||
# via stack-data
|
|
||||||
build==1.2.2.post1
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
certifi==2024.8.30
|
|
||||||
# via requests
|
|
||||||
cffi==1.17.1
|
|
||||||
# via cryptography
|
|
||||||
charset-normalizer==3.4.0
|
|
||||||
# via requests
|
|
||||||
cryptography==43.0.3
|
|
||||||
# via secretstorage
|
|
||||||
decorator==5.1.1
|
|
||||||
# via
|
|
||||||
# ipdb
|
|
||||||
# ipython
|
|
||||||
docutils==0.21.2
|
|
||||||
# via readme-renderer
|
|
||||||
executing==2.1.0
|
|
||||||
# via stack-data
|
|
||||||
idna==3.10
|
|
||||||
# via requests
|
|
||||||
importlib-metadata==8.5.0
|
|
||||||
# via twine
|
|
||||||
ipdb==0.13.13
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
ipython==8.29.0
|
|
||||||
# via ipdb
|
|
||||||
jaraco-classes==3.4.0
|
|
||||||
# via keyring
|
|
||||||
jaraco-context==6.0.1
|
|
||||||
# via keyring
|
|
||||||
jaraco-functools==4.1.0
|
|
||||||
# via keyring
|
|
||||||
jedi==0.19.1
|
|
||||||
# via ipython
|
|
||||||
jeepney==0.8.0
|
|
||||||
# via
|
|
||||||
# keyring
|
|
||||||
# secretstorage
|
|
||||||
keyring==25.5.0
|
|
||||||
# via twine
|
|
||||||
markdown-it-py==3.0.0
|
|
||||||
# via rich
|
|
||||||
matplotlib-inline==0.1.7
|
|
||||||
# via ipython
|
|
||||||
mdurl==0.1.2
|
|
||||||
# via markdown-it-py
|
|
||||||
more-itertools==10.5.0
|
|
||||||
# via
|
|
||||||
# jaraco-classes
|
|
||||||
# jaraco-functools
|
|
||||||
mypy==1.13.0
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
mypy-extensions==1.0.0
|
|
||||||
# via mypy
|
|
||||||
nh3==0.2.18
|
|
||||||
# via readme-renderer
|
|
||||||
packaging==24.1
|
|
||||||
# via build
|
|
||||||
parso==0.8.4
|
|
||||||
# via jedi
|
|
||||||
pexpect==4.9.0
|
|
||||||
# via ipython
|
|
||||||
pkginfo==1.10.0
|
|
||||||
# via twine
|
|
||||||
prompt-toolkit==3.0.48
|
|
||||||
# via ipython
|
|
||||||
ptyprocess==0.7.0
|
|
||||||
# via pexpect
|
|
||||||
pure-eval==0.2.3
|
|
||||||
# via stack-data
|
|
||||||
pwo==0.0.5
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
pycparser==2.22
|
|
||||||
# via cffi
|
|
||||||
pygments==2.18.0
|
|
||||||
# via
|
|
||||||
# ipython
|
|
||||||
# readme-renderer
|
|
||||||
# rich
|
|
||||||
pyproject-hooks==1.2.0
|
|
||||||
# via build
|
|
||||||
pyyaml==6.0.2
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
readme-renderer==44.0
|
|
||||||
# via twine
|
|
||||||
requests==2.32.3
|
|
||||||
# via
|
|
||||||
# requests-toolbelt
|
|
||||||
# twine
|
|
||||||
requests-toolbelt==1.0.0
|
|
||||||
# via twine
|
|
||||||
rfc3986==2.0.0
|
|
||||||
# via twine
|
|
||||||
rich==13.9.3
|
|
||||||
# via twine
|
|
||||||
secretstorage==3.3.3
|
|
||||||
# via keyring
|
|
||||||
six==1.16.0
|
|
||||||
# via asttokens
|
|
||||||
sortedcontainers==2.4.0
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
stack-data==0.6.3
|
|
||||||
# via ipython
|
|
||||||
traitlets==5.14.3
|
|
||||||
# via
|
|
||||||
# ipython
|
|
||||||
# matplotlib-inline
|
|
||||||
twine==5.1.1
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
types-pyyaml==6.0.12.20240917
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
typing-extensions==4.12.2
|
|
||||||
# via
|
|
||||||
# mypy
|
|
||||||
# pwo
|
|
||||||
urllib3==2.2.3
|
|
||||||
# via
|
|
||||||
# requests
|
|
||||||
# twine
|
|
||||||
watchdog==5.0.3
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
wcwidth==0.2.13
|
|
||||||
# via prompt-toolkit
|
|
||||||
zipp==3.20.2
|
|
||||||
# via importlib-metadata
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
#
|
|
||||||
# This file is autogenerated by pip-compile with Python 3.12
|
|
||||||
# by the following command:
|
|
||||||
#
|
|
||||||
# pip-compile --output-file=requirements.txt
|
|
||||||
#
|
|
||||||
--index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple
|
|
||||||
--extra-index-url https://pypi.org/simple
|
|
||||||
|
|
||||||
aiofiles==24.1.0
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
pwo==0.0.5
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
pyyaml==6.0.2
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
sortedcontainers==2.4.0
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
typing-extensions==4.12.2
|
|
||||||
# via pwo
|
|
||||||
watchdog==5.0.3
|
|
||||||
# via bugis_core (pyproject.toml)
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
from ._app import BugisApp
|
|
||||||
from ._http_method import HttpMethod
|
|
||||||
from ._http_context import HttpContext
|
|
||||||
from ._tree import Tree, PathIterator
|
|
||||||
from ._path_handler import PathHandler
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'HttpMethod',
|
|
||||||
'BugisApp',
|
|
||||||
'HttpContext',
|
|
||||||
'Tree',
|
|
||||||
'PathHandler',
|
|
||||||
'PathIterator'
|
|
||||||
]
|
|
||||||
@@ -1,149 +0,0 @@
|
|||||||
from abc import ABC, abstractmethod
|
|
||||||
from asyncio import Queue, AbstractEventLoop
|
|
||||||
from asyncio import get_running_loop
|
|
||||||
from logging import getLogger
|
|
||||||
from typing import Callable, Awaitable, Any, Mapping, Sequence, Optional, Unpack, Tuple, TYPE_CHECKING
|
|
||||||
from pathlib import Path, PurePath
|
|
||||||
from pwo import Maybe, AsyncQueueIterator
|
|
||||||
from hashlib import md5
|
|
||||||
from ._http_context import HttpContext
|
|
||||||
from ._http_method import HttpMethod
|
|
||||||
from ._types import StrOrStrings
|
|
||||||
from base64 import b64encode, b64decode
|
|
||||||
from mimetypes import guess_type
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from _typeshed import StrOrBytesPath
|
|
||||||
try:
|
|
||||||
from ._rsgi import RsgiContext
|
|
||||||
from granian._granian import RSGIHTTPProtocol, RSGIHTTPScope # type: ignore
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
from ._asgi import AsgiContext
|
|
||||||
from ._tree import Tree
|
|
||||||
from ._types.asgi import LifespanScope, HTTPScope as ASGIHTTPScope, WebSocketScope
|
|
||||||
|
|
||||||
log = getLogger(__name__)
|
|
||||||
|
|
||||||
type HttpHandler = Callable[[HttpContext, Unpack[Any]], Awaitable[None]]
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractBugisApp(ABC):
|
|
||||||
async def __call__(self,
|
|
||||||
scope: ASGIHTTPScope | WebSocketScope | LifespanScope,
|
|
||||||
receive: Callable[[], Awaitable[Any]],
|
|
||||||
send: Callable[[Mapping[str, Any]], Awaitable[None]]) -> None:
|
|
||||||
loop = get_running_loop()
|
|
||||||
if scope['type'] == 'lifespan':
|
|
||||||
while True:
|
|
||||||
message = await receive()
|
|
||||||
if message['type'] == 'lifespan.startup':
|
|
||||||
self.setup(loop)
|
|
||||||
await send({'type': 'lifespan.startup.complete'})
|
|
||||||
elif message['type'] == 'lifespan.shutdown':
|
|
||||||
self.shutdown(loop)
|
|
||||||
await send({'type': 'lifespan.shutdown.complete'})
|
|
||||||
elif scope['type'] == 'http':
|
|
||||||
queue: Queue[Optional[bytes]] = Queue()
|
|
||||||
ctx = AsgiContext(scope, receive, send, AsyncQueueIterator(queue))
|
|
||||||
request_handling = loop.create_task(self.handle_request(ctx))
|
|
||||||
while True:
|
|
||||||
message = await receive()
|
|
||||||
if message['type'] == 'http.request':
|
|
||||||
Maybe.of(message['body']).filter(lambda it: len(it) > 0).if_present(queue.put_nowait)
|
|
||||||
if not message.get('more_body', False):
|
|
||||||
queue.put_nowait(None)
|
|
||||||
await request_handling
|
|
||||||
break
|
|
||||||
elif message['type'] == 'http.disconnect':
|
|
||||||
request_handling.cancel()
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def setup(self, loop: AbstractEventLoop) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def shutdown(self, loop: AbstractEventLoop) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
async def handle_request(self, ctx: HttpContext) -> None:
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
def __rsgi_init__(self, loop: AbstractEventLoop) -> None:
|
|
||||||
self.setup(loop)
|
|
||||||
|
|
||||||
def __rsgi_del__(self, loop: AbstractEventLoop) -> None:
|
|
||||||
self.shutdown(loop)
|
|
||||||
|
|
||||||
async def __rsgi__(self, scope: RSGIHTTPScope, protocol: RSGIHTTPProtocol) -> None:
|
|
||||||
ctx = RsgiContext(scope, protocol)
|
|
||||||
await self.handle_request(ctx)
|
|
||||||
|
|
||||||
|
|
||||||
class BugisApp(AbstractBugisApp):
|
|
||||||
_tree: Tree
|
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self._tree = Tree()
|
|
||||||
|
|
||||||
async def handle_request(self, ctx: HttpContext) -> None:
|
|
||||||
result = self._tree.get_handler(ctx.path, ctx.method)
|
|
||||||
if result is not None:
|
|
||||||
handler, captured = result
|
|
||||||
await handler.handle_request(ctx, captured)
|
|
||||||
else:
|
|
||||||
await ctx.send_empty(404)
|
|
||||||
pass
|
|
||||||
|
|
||||||
def route(self,
|
|
||||||
paths: StrOrStrings,
|
|
||||||
methods: Optional[HttpMethod | Sequence[HttpMethod]] = None,
|
|
||||||
recursive: bool = False) -> Callable[[HttpHandler], HttpHandler]:
|
|
||||||
|
|
||||||
def wrapped(handler: HttpHandler) -> HttpHandler:
|
|
||||||
nonlocal methods
|
|
||||||
nonlocal paths
|
|
||||||
_methods: Tuple[Optional[HttpMethod], ...]
|
|
||||||
if methods is None:
|
|
||||||
_methods = (None,)
|
|
||||||
elif isinstance(methods, HttpMethod):
|
|
||||||
_methods = (methods,)
|
|
||||||
else:
|
|
||||||
_methods = tuple(methods)
|
|
||||||
_paths: Tuple[str, ...]
|
|
||||||
if isinstance(paths, str):
|
|
||||||
_paths = (paths,)
|
|
||||||
else:
|
|
||||||
_paths = tuple(paths)
|
|
||||||
for method in _methods:
|
|
||||||
for path in _paths:
|
|
||||||
self._tree.register(path, method, handler, recursive)
|
|
||||||
return handler
|
|
||||||
|
|
||||||
return wrapped
|
|
||||||
|
|
||||||
def GET(self, path: str, recursive: bool = False) -> Callable[[HttpHandler], HttpHandler]:
|
|
||||||
return self.route(path, (HttpMethod.GET,), recursive)
|
|
||||||
|
|
||||||
def POST(self, path: str, recursive: bool = False) -> Callable[[HttpHandler], HttpHandler]:
|
|
||||||
return self.route(path, (HttpMethod.POST,), recursive)
|
|
||||||
|
|
||||||
def PUT(self, path: str, recursive: bool = False) -> Callable[[HttpHandler], HttpHandler]:
|
|
||||||
return self.route(path, (HttpMethod.PUT,), recursive)
|
|
||||||
|
|
||||||
def DELETE(self, path: str, recursive: bool = False) -> Callable[[HttpHandler], HttpHandler]:
|
|
||||||
return self.route(path, (HttpMethod.DELETE,), recursive)
|
|
||||||
|
|
||||||
def OPTIONS(self, path: str, recursive: bool = False) -> Callable[[HttpHandler], HttpHandler]:
|
|
||||||
return self.route(path, (HttpMethod.OPTIONS,), recursive)
|
|
||||||
|
|
||||||
def HEAD(self, path: str, recursive: bool = False) -> Callable[[HttpHandler], HttpHandler]:
|
|
||||||
return self.route(path, (HttpMethod.HEAD,), recursive)
|
|
||||||
|
|
||||||
def PATCH(self, path: str, recursive: bool = False) -> Callable[[HttpHandler], HttpHandler]:
|
|
||||||
return self.route(path, (HttpMethod.PATCH,), recursive)
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,148 +0,0 @@
|
|||||||
from typing import (
|
|
||||||
Sequence,
|
|
||||||
Tuple,
|
|
||||||
Dict,
|
|
||||||
Mapping,
|
|
||||||
Callable,
|
|
||||||
Any,
|
|
||||||
AsyncIterator,
|
|
||||||
Awaitable,
|
|
||||||
AsyncGenerator,
|
|
||||||
Optional,
|
|
||||||
List,
|
|
||||||
Iterable
|
|
||||||
)
|
|
||||||
|
|
||||||
from pwo import Maybe
|
|
||||||
from pathlib import Path
|
|
||||||
from ._http_method import HttpMethod
|
|
||||||
from ._http_context import HttpContext
|
|
||||||
from ._types import StrOrStrings
|
|
||||||
from ._types.asgi import HTTPScope
|
|
||||||
|
|
||||||
|
|
||||||
def decode_headers(headers: Iterable[Tuple[bytes, bytes]]) -> Dict[str, Sequence[str]]:
|
|
||||||
result: Dict[str, List[str]] = dict()
|
|
||||||
for key, value in headers:
|
|
||||||
key_str: str
|
|
||||||
value_str: str
|
|
||||||
if isinstance(key, bytes):
|
|
||||||
key_str = key.decode()
|
|
||||||
elif isinstance(key, str):
|
|
||||||
key_str = key
|
|
||||||
else:
|
|
||||||
raise NotImplementedError('This should never happen')
|
|
||||||
if isinstance(value, bytes):
|
|
||||||
value_str = value.decode()
|
|
||||||
elif isinstance(value, str):
|
|
||||||
value_str = value
|
|
||||||
else:
|
|
||||||
raise NotImplementedError('This should never happen')
|
|
||||||
ls = result.setdefault(key_str.lower(), list())
|
|
||||||
ls.append(value_str)
|
|
||||||
return {
|
|
||||||
k: tuple(v) for k, v in result.items()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def encode_headers(headers: Mapping[str, StrOrStrings]) -> Tuple[Tuple[bytes, bytes], ...]:
|
|
||||||
result = []
|
|
||||||
for key, value in headers.items():
|
|
||||||
if isinstance(value, str):
|
|
||||||
result.append((key.encode(), value.encode()))
|
|
||||||
elif isinstance(value, Sequence):
|
|
||||||
for single_value in value:
|
|
||||||
result.append((key.encode(), single_value.encode()))
|
|
||||||
return tuple(result)
|
|
||||||
|
|
||||||
|
|
||||||
class AsgiContext(HttpContext):
|
|
||||||
pathsend: bool
|
|
||||||
receive: Callable[[], Awaitable[Any]]
|
|
||||||
send: Callable[[Mapping[str, Any]], Awaitable[None]]
|
|
||||||
scheme: str
|
|
||||||
method: HttpMethod
|
|
||||||
path: str
|
|
||||||
query_string: str
|
|
||||||
headers: Mapping[str, Sequence[str]]
|
|
||||||
client: Optional[Tuple[str, int]]
|
|
||||||
server: Optional[Tuple[str, Optional[int]]]
|
|
||||||
request_body: AsyncIterator[bytes]
|
|
||||||
|
|
||||||
def __init__(self,
|
|
||||||
scope: HTTPScope,
|
|
||||||
receive: Callable[[], Awaitable[Any]],
|
|
||||||
send: Callable[[Mapping[str, Any]], Awaitable[None]],
|
|
||||||
request_body_iterator: AsyncIterator[bytes]):
|
|
||||||
self.receive = receive
|
|
||||||
self.send = send
|
|
||||||
self.pathsend = (Maybe.of_nullable(scope.get('extensions'))
|
|
||||||
.map(lambda it: it.get("http.response.pathsend"))
|
|
||||||
.is_present)
|
|
||||||
self.path = scope['path']
|
|
||||||
self.query_string = scope['query_string'].decode()
|
|
||||||
self.method = HttpMethod(scope['method'])
|
|
||||||
self.scheme = scope['scheme']
|
|
||||||
self.client = scope['client']
|
|
||||||
self.server = scope['server']
|
|
||||||
self.headers = decode_headers(scope['headers'])
|
|
||||||
self.request_body = request_body_iterator
|
|
||||||
|
|
||||||
async def stream_body(self,
|
|
||||||
status: int,
|
|
||||||
body_generator: AsyncGenerator[bytes, None],
|
|
||||||
headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
await self._send_head(status, headers)
|
|
||||||
async for chunk in body_generator:
|
|
||||||
await self.send({
|
|
||||||
'type': 'http.response.body',
|
|
||||||
'body': chunk,
|
|
||||||
'more_body': True
|
|
||||||
})
|
|
||||||
await self.send({
|
|
||||||
'type': 'http.response.body',
|
|
||||||
'body': '',
|
|
||||||
'more_body': False
|
|
||||||
})
|
|
||||||
|
|
||||||
async def send_bytes(self, status: int, body: bytes, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
await self._send_head(status, headers)
|
|
||||||
await self.send({
|
|
||||||
'type': 'http.response.body',
|
|
||||||
'body': body,
|
|
||||||
})
|
|
||||||
|
|
||||||
async def send_str(self, status: int, body: str, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
await self._send_head(status, headers)
|
|
||||||
await self.send({
|
|
||||||
'type': 'http.response.body',
|
|
||||||
'body': body.encode(),
|
|
||||||
})
|
|
||||||
|
|
||||||
async def _send_head(self, status: int, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
await self.send({
|
|
||||||
'type': 'http.response.start',
|
|
||||||
'status': status,
|
|
||||||
'headers': Maybe.of_nullable(headers).map(encode_headers).or_else(tuple())
|
|
||||||
})
|
|
||||||
|
|
||||||
async def send_file(self,
|
|
||||||
status: int,
|
|
||||||
path: Path,
|
|
||||||
headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
if self.pathsend:
|
|
||||||
await self._send_head(status, headers)
|
|
||||||
await self.send({
|
|
||||||
'type': 'http.response.pathsend',
|
|
||||||
'path': path
|
|
||||||
})
|
|
||||||
else:
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
async def send_empty(self, status: int, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
await self._send_head(status, headers)
|
|
||||||
await self.send({
|
|
||||||
'type': 'http.response.body',
|
|
||||||
'body': '',
|
|
||||||
'more_body': False
|
|
||||||
})
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
from typing import (
|
|
||||||
Callable,
|
|
||||||
Awaitable,
|
|
||||||
Tuple,
|
|
||||||
AsyncIterator,
|
|
||||||
AsyncGenerator,
|
|
||||||
Mapping,
|
|
||||||
Sequence,
|
|
||||||
Any,
|
|
||||||
Optional
|
|
||||||
)
|
|
||||||
from abc import ABC, abstractmethod
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from ._http_method import HttpMethod
|
|
||||||
from ._types.base import StrOrStrings
|
|
||||||
|
|
||||||
|
|
||||||
class HttpContext(ABC):
|
|
||||||
pathsend: bool
|
|
||||||
receive: Callable[[None], Awaitable[Any]]
|
|
||||||
send: Callable[[Mapping[str, Any]], Awaitable[None]]
|
|
||||||
scheme: str
|
|
||||||
method: HttpMethod
|
|
||||||
path: str
|
|
||||||
query_string: str
|
|
||||||
headers: Mapping[str, Sequence[str]]
|
|
||||||
client: Optional[Tuple[str, int]]
|
|
||||||
server: Optional[Tuple[str, Optional[int]]]
|
|
||||||
request_body: AsyncIterator[bytes]
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
async def stream_body(self,
|
|
||||||
status: int,
|
|
||||||
body_generator: AsyncGenerator[bytes, None],
|
|
||||||
headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
async def send_bytes(self, status: int, body: bytes, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
async def send_str(self, status: int, body: str, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
await self.send_bytes(status, body.encode(), headers)
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
async def send_file(self, status: int, path: Path, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
async def send_empty(self, status: int, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
pass
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
from enum import StrEnum
|
|
||||||
|
|
||||||
|
|
||||||
class HttpMethod(StrEnum):
|
|
||||||
OPTIONS = 'OPTIONS'
|
|
||||||
HEAD = 'HEAD'
|
|
||||||
GET = 'GET'
|
|
||||||
POST = 'POST'
|
|
||||||
PUT = 'PUT'
|
|
||||||
DELETE = 'DELETE'
|
|
||||||
PATCH = 'PATCH'
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
from dataclasses import dataclass
|
|
||||||
from typing import (
|
|
||||||
Optional,
|
|
||||||
Dict,
|
|
||||||
List,
|
|
||||||
)
|
|
||||||
from ._types import NodeType
|
|
||||||
from ._path_handler import PathHandler
|
|
||||||
from ._path_matcher import PathMatcher
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
from abc import ABC, abstractmethod
|
|
||||||
from typing import (
|
|
||||||
Sequence,
|
|
||||||
Dict,
|
|
||||||
Optional
|
|
||||||
)
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from ._http_context import HttpContext
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Matches:
|
|
||||||
|
|
||||||
kwargs: Dict[str, str] = field(default_factory=dict)
|
|
||||||
|
|
||||||
path: Optional[Sequence[str]] = None
|
|
||||||
|
|
||||||
unmatched_paths: Sequence[str] = field(default_factory=list)
|
|
||||||
|
|
||||||
|
|
||||||
class PathHandler(ABC):
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
async def handle_request(self, ctx: HttpContext, captured: Matches) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
@property
|
|
||||||
@abstractmethod
|
|
||||||
def recursive(self) -> bool:
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
|
|
||||||
type PathHandlers = (PathHandler | Sequence[PathHandler])
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
from fnmatch import fnmatch
|
|
||||||
from abc import ABC, abstractmethod
|
|
||||||
from typing import Optional, Sequence, Dict, List, Union
|
|
||||||
from dataclasses import dataclass
|
|
||||||
from ._path_handler import PathHandler
|
|
||||||
from ._types import NodeType, PathMatcherResult
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Node:
|
|
||||||
key: NodeType
|
|
||||||
parent: Optional[Union['Node', 'PathMatcher']]
|
|
||||||
children: Dict[NodeType, 'Node']
|
|
||||||
handlers: List[PathHandler]
|
|
||||||
path_matchers: List['PathMatcher']
|
|
||||||
|
|
||||||
|
|
||||||
class PathMatcher(ABC):
|
|
||||||
parent: Optional[Union['Node', 'PathMatcher']]
|
|
||||||
children: Dict[NodeType, Node]
|
|
||||||
handlers: List[PathHandler]
|
|
||||||
path_matchers: List['PathMatcher']
|
|
||||||
|
|
||||||
def __init__(self,
|
|
||||||
parent: Optional[Union['Node', 'PathMatcher']],
|
|
||||||
children: Dict[NodeType, Node],
|
|
||||||
handlers: List[PathHandler],
|
|
||||||
path_matchers: List['PathMatcher']
|
|
||||||
):
|
|
||||||
self.parent = parent
|
|
||||||
self.children = children
|
|
||||||
self.handlers = handlers
|
|
||||||
self.path_matchers = path_matchers
|
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def match(self, path: Sequence[str]) -> Optional[PathMatcherResult]:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class StrMatcher(PathMatcher):
|
|
||||||
name: str
|
|
||||||
|
|
||||||
def __init__(self,
|
|
||||||
name: str,
|
|
||||||
parent: Optional[Node | PathMatcher],
|
|
||||||
children: Dict[NodeType, Node],
|
|
||||||
handlers: List[PathHandler],
|
|
||||||
path_matchers: List[PathMatcher],
|
|
||||||
):
|
|
||||||
super().__init__(parent, children, handlers, path_matchers)
|
|
||||||
self.name = name
|
|
||||||
|
|
||||||
def match(self, path: Sequence[str]) -> Optional[PathMatcherResult]:
|
|
||||||
if len(path):
|
|
||||||
return {self.name: path[0]}
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class IntMatcher(PathMatcher):
|
|
||||||
name: str
|
|
||||||
|
|
||||||
def __init__(self,
|
|
||||||
name: str,
|
|
||||||
parent: Optional[Node | PathMatcher],
|
|
||||||
children: Dict[NodeType, Node],
|
|
||||||
handlers: List[PathHandler],
|
|
||||||
path_matchers: List[PathMatcher],
|
|
||||||
):
|
|
||||||
super().__init__(parent, children, handlers, path_matchers)
|
|
||||||
self.name = name
|
|
||||||
|
|
||||||
def match(self, path: Sequence[str]) -> Optional[PathMatcherResult]:
|
|
||||||
if len(path) > 0:
|
|
||||||
try:
|
|
||||||
return {self.name: int(path[0])}
|
|
||||||
except ValueError:
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
class GlobMatcher(PathMatcher):
|
|
||||||
pattern: str
|
|
||||||
|
|
||||||
def __init__(self,
|
|
||||||
pattern: str,
|
|
||||||
parent: Optional[Node | PathMatcher],
|
|
||||||
children: Dict[NodeType, Node],
|
|
||||||
handlers: List[PathHandler],
|
|
||||||
path_matchers: List[PathMatcher],
|
|
||||||
):
|
|
||||||
super().__init__(parent, children, handlers, path_matchers)
|
|
||||||
self.pattern = pattern
|
|
||||||
|
|
||||||
def match(self, path: Sequence[str]) -> Optional[PathMatcherResult]:
|
|
||||||
return path if fnmatch('/'.join(path), self.pattern) else None
|
|
||||||
@@ -1,107 +0,0 @@
|
|||||||
from functools import reduce
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import (
|
|
||||||
Any,
|
|
||||||
Sequence,
|
|
||||||
Mapping,
|
|
||||||
AsyncIterator,
|
|
||||||
Tuple,
|
|
||||||
AsyncGenerator,
|
|
||||||
Optional,
|
|
||||||
List,
|
|
||||||
Dict,
|
|
||||||
Callable,
|
|
||||||
cast
|
|
||||||
)
|
|
||||||
|
|
||||||
from granian._granian import RSGIHTTPProtocol, RSGIHTTPScope
|
|
||||||
from pwo import Maybe
|
|
||||||
|
|
||||||
from ._types import StrOrStrings
|
|
||||||
from ._http_context import HttpContext
|
|
||||||
from ._http_method import HttpMethod
|
|
||||||
|
|
||||||
|
|
||||||
class RsgiContext(HttpContext):
|
|
||||||
protocol: RSGIHTTPProtocol
|
|
||||||
scheme: str
|
|
||||||
method: HttpMethod
|
|
||||||
path: str
|
|
||||||
query_string: str
|
|
||||||
headers: Mapping[str, Sequence[str]]
|
|
||||||
client: Optional[Tuple[str, int]]
|
|
||||||
server: Optional[Tuple[str, Optional[int]]]
|
|
||||||
request_body: AsyncIterator[bytes]
|
|
||||||
head = Optional[Tuple[int, Sequence[Tuple[str, str]]]]
|
|
||||||
|
|
||||||
def __init__(self, scope: RSGIHTTPScope, protocol: RSGIHTTPProtocol):
|
|
||||||
self.scheme = scope.scheme
|
|
||||||
self.path = scope.path
|
|
||||||
self.method = HttpMethod(scope.method)
|
|
||||||
self.query_string = scope.query_string
|
|
||||||
|
|
||||||
def acc(d: Dict[str, List[str]], t: Tuple[str, str]) -> Dict[str, List[str]]:
|
|
||||||
d.setdefault(t[0].lower(), list()).append(t[1])
|
|
||||||
return d
|
|
||||||
|
|
||||||
fun = cast(Callable[[Mapping[str, Sequence[str]], tuple[str, str]], Mapping[str, Sequence[str]]], acc)
|
|
||||||
self.headers = reduce(fun, scope.headers.items(), {})
|
|
||||||
self.client = (Maybe.of(scope.client.split(':'))
|
|
||||||
.map(lambda it: (it[0], int(it[1])))
|
|
||||||
.or_else_throw(RuntimeError))
|
|
||||||
self.server = (Maybe.of(scope.server.split(':'))
|
|
||||||
.map(lambda it: (it[0], int(it[1])))
|
|
||||||
.or_else_throw(RuntimeError))
|
|
||||||
self.request_body = aiter(protocol)
|
|
||||||
self.protocol = protocol
|
|
||||||
|
|
||||||
# @staticmethod
|
|
||||||
# def _rearrange_headers(headers: Mapping[str, Sequence[str]]) -> List[Tuple[str, str]]:
|
|
||||||
# return list(
|
|
||||||
# ((key, value) for key, values in headers.items() for value in values)
|
|
||||||
# )
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _rearrange_headers(headers: Mapping[str, StrOrStrings]) -> List[Tuple[str, str]]:
|
|
||||||
result = []
|
|
||||||
for key, value in headers.items():
|
|
||||||
if isinstance(value, str):
|
|
||||||
result.append((key, value))
|
|
||||||
elif isinstance(value, Sequence):
|
|
||||||
for single_value in value:
|
|
||||||
result.append((key, single_value))
|
|
||||||
return result
|
|
||||||
|
|
||||||
async def stream_body(self,
|
|
||||||
status: int,
|
|
||||||
body_generator: AsyncGenerator[bytes, None],
|
|
||||||
headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
transport = self.protocol.response_stream(status,
|
|
||||||
Maybe.of_nullable(headers)
|
|
||||||
.map(self._rearrange_headers)
|
|
||||||
.or_else([]))
|
|
||||||
async for chunk in body_generator:
|
|
||||||
await transport.send_bytes(chunk)
|
|
||||||
|
|
||||||
async def send_bytes(self, status: int, body: bytes, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
rearranged_headers = Maybe.of_nullable(headers).map(RsgiContext._rearrange_headers).or_else(list())
|
|
||||||
if len(body) > 0:
|
|
||||||
self.protocol.response_bytes(status, rearranged_headers, body)
|
|
||||||
else:
|
|
||||||
self.protocol.response_empty(status, rearranged_headers)
|
|
||||||
|
|
||||||
async def send_str(self, status: int, body: str, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
rearranged_headers = Maybe.of_nullable(headers).map(RsgiContext._rearrange_headers).or_else(list())
|
|
||||||
if len(body) > 0:
|
|
||||||
self.protocol.response_str(status, rearranged_headers, body)
|
|
||||||
else:
|
|
||||||
self.protocol.response_empty(status, rearranged_headers)
|
|
||||||
|
|
||||||
async def send_file(self, status: int, path: Path, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
rearranged_headers = (Maybe.of_nullable(headers).map(RsgiContext._rearrange_headers)
|
|
||||||
.or_else(list()))
|
|
||||||
self.protocol.response_file(status, rearranged_headers, str(path))
|
|
||||||
|
|
||||||
async def send_empty(self, status: int, headers: Optional[Mapping[str, StrOrStrings]] = None) -> None:
|
|
||||||
rearranged_headers = Maybe.of_nullable(headers).map(RsgiContext._rearrange_headers).or_else(list())
|
|
||||||
self.protocol.response_empty(status, rearranged_headers)
|
|
||||||
@@ -1,224 +0,0 @@
|
|||||||
from itertools import chain
|
|
||||||
from typing import (
|
|
||||||
Sequence,
|
|
||||||
Awaitable,
|
|
||||||
Callable,
|
|
||||||
Optional,
|
|
||||||
Generator,
|
|
||||||
Self,
|
|
||||||
List,
|
|
||||||
Tuple,
|
|
||||||
Mapping,
|
|
||||||
Any,
|
|
||||||
)
|
|
||||||
from typing_extensions import Unpack
|
|
||||||
from urllib.parse import urlparse
|
|
||||||
|
|
||||||
from pwo import Maybe, index_of_with_escape
|
|
||||||
|
|
||||||
from ._http_context import HttpContext
|
|
||||||
from ._http_method import HttpMethod
|
|
||||||
from ._path_handler import PathHandler
|
|
||||||
from ._path_matcher import PathMatcher, IntMatcher, GlobMatcher, StrMatcher, Node
|
|
||||||
from ._path_handler import Matches
|
|
||||||
from ._types import NodeType
|
|
||||||
|
|
||||||
|
|
||||||
class Tree:
|
|
||||||
|
|
||||||
def __init__(self) -> None:
|
|
||||||
self.root = Node('/', None, {}, [], [])
|
|
||||||
|
|
||||||
def search(self, path: Generator[str, None, None], method: HttpMethod) \
|
|
||||||
-> Optional[Tuple[Node | PathMatcher, Matches]]:
|
|
||||||
paths: List[str] = list(path)
|
|
||||||
result: Node | PathMatcher = self.root
|
|
||||||
|
|
||||||
matches = Matches()
|
|
||||||
it, i = iter((it for it in paths)), -1
|
|
||||||
while True:
|
|
||||||
node = result
|
|
||||||
leaf, i = next(it, None), i + 1
|
|
||||||
if leaf is None:
|
|
||||||
break
|
|
||||||
child = node.children.get(leaf)
|
|
||||||
if child is None and isinstance(leaf, str):
|
|
||||||
for matcher in node.path_matchers:
|
|
||||||
match = matcher.match(paths[i:])
|
|
||||||
if match is not None:
|
|
||||||
if isinstance(match, Mapping):
|
|
||||||
matches.kwargs.update(match)
|
|
||||||
elif isinstance(match, Sequence):
|
|
||||||
matches.path = match
|
|
||||||
result = matcher
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
result = child
|
|
||||||
child = result.children.get(method)
|
|
||||||
if child is not None:
|
|
||||||
result = child
|
|
||||||
matches.unmatched_paths = paths[i:]
|
|
||||||
return None if result == self.root else (result, matches)
|
|
||||||
|
|
||||||
def add(self, path: Generator[str, None, None], method: Optional[HttpMethod], *path_handlers: PathHandler) -> Node | PathMatcher:
|
|
||||||
lineage: Generator[NodeType, None, None] = (it for it in
|
|
||||||
chain(path,
|
|
||||||
Maybe.of_nullable(method)
|
|
||||||
.map(lambda it: [it])
|
|
||||||
.or_else([])))
|
|
||||||
result: Node | PathMatcher = self.root
|
|
||||||
it = iter(lineage)
|
|
||||||
|
|
||||||
while True:
|
|
||||||
node = result
|
|
||||||
leaf = next(it, None)
|
|
||||||
if leaf is None:
|
|
||||||
break
|
|
||||||
child = node.children.get(leaf)
|
|
||||||
if child is None:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
result = child
|
|
||||||
key = leaf
|
|
||||||
while key is not None:
|
|
||||||
new_node = self.parse(key, result)
|
|
||||||
if isinstance(new_node, Node):
|
|
||||||
result.children[key] = new_node
|
|
||||||
else:
|
|
||||||
result.path_matchers.append(new_node)
|
|
||||||
result = new_node
|
|
||||||
key = next(it, None)
|
|
||||||
|
|
||||||
result.handlers = list(chain(result.handlers, path_handlers))
|
|
||||||
return result
|
|
||||||
|
|
||||||
def register(self,
|
|
||||||
path: str,
|
|
||||||
method: Optional[HttpMethod],
|
|
||||||
callback: Callable[[HttpContext, Unpack[Any]], Awaitable[None]],
|
|
||||||
recursive: bool) -> None:
|
|
||||||
class Handler(PathHandler):
|
|
||||||
|
|
||||||
async def handle_request(self, ctx: HttpContext, captured: Matches) -> None:
|
|
||||||
args = Maybe.of_nullable(captured.path).map(lambda it: [it]).or_else([])
|
|
||||||
await callback(ctx, *args, **captured.kwargs)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def recursive(self) -> bool:
|
|
||||||
return recursive
|
|
||||||
|
|
||||||
handler = Handler()
|
|
||||||
self.add((p for p in PathIterator(path)), method, handler)
|
|
||||||
|
|
||||||
def find_node(self, path: Generator[str, None, None], method: HttpMethod = HttpMethod.GET) \
|
|
||||||
-> Optional[Tuple[Node | PathMatcher, Matches]]:
|
|
||||||
return (Maybe.of_nullable(self.search(path, method))
|
|
||||||
.filter(lambda it: len(it[0].handlers) > 0)
|
|
||||||
.or_none())
|
|
||||||
|
|
||||||
def get_handler(self, url: str, method: HttpMethod = HttpMethod.GET) \
|
|
||||||
-> Optional[Tuple[PathHandler, Matches]]:
|
|
||||||
path = urlparse(url).path
|
|
||||||
result: Optional[Tuple[Node | PathMatcher, Matches]] = self.find_node((p for p in PathIterator(path)), method)
|
|
||||||
if result is None:
|
|
||||||
return None
|
|
||||||
node, captured = result
|
|
||||||
# requested = (p for p in PathIterator(path))
|
|
||||||
# found = reversed([n for n in NodeAncestryIterator(node) if n != self.root])
|
|
||||||
# unmatched: List[str] = []
|
|
||||||
# for r, f in zip(requested, found):
|
|
||||||
# if f is None:
|
|
||||||
# unmatched.append(r)
|
|
||||||
for handler in node.handlers:
|
|
||||||
if len(captured.unmatched_paths) == 0:
|
|
||||||
return handler, captured
|
|
||||||
elif handler.recursive:
|
|
||||||
return handler, captured
|
|
||||||
# if handler.match(unmatched, method):
|
|
||||||
# return (handler, unmatched)
|
|
||||||
return None
|
|
||||||
|
|
||||||
def parse(self, leaf: str, parent: Optional[Node | PathMatcher]) -> Node | PathMatcher:
|
|
||||||
start = 0
|
|
||||||
result = index_of_with_escape(leaf, '${', '\\', 0)
|
|
||||||
if result >= 0:
|
|
||||||
start = result + 2
|
|
||||||
end = leaf.index('}', start + 2)
|
|
||||||
definition = leaf[start:end]
|
|
||||||
try:
|
|
||||||
colon = definition.index(':')
|
|
||||||
except ValueError:
|
|
||||||
colon = None
|
|
||||||
if colon is None:
|
|
||||||
key = definition
|
|
||||||
kind = 'str'
|
|
||||||
else:
|
|
||||||
key = definition[:colon]
|
|
||||||
kind = definition[colon+1:] if colon is not None else 'str'
|
|
||||||
if kind == 'str':
|
|
||||||
return StrMatcher(name=key, parent=parent, children={}, handlers=[], path_matchers=[])
|
|
||||||
elif kind == 'int':
|
|
||||||
return IntMatcher(name=key, parent=parent, children={}, handlers=[], path_matchers=[])
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Unknown kind: '{kind}'")
|
|
||||||
result = index_of_with_escape(leaf, '*', '\\', 0)
|
|
||||||
if result >= 0:
|
|
||||||
return GlobMatcher(pattern=leaf, parent=parent, children={}, handlers=[], path_matchers=[])
|
|
||||||
else:
|
|
||||||
return Node(key=leaf, parent=parent, children={}, handlers=[], path_matchers=[])
|
|
||||||
|
|
||||||
|
|
||||||
class PathIterator:
|
|
||||||
path: str
|
|
||||||
cursor: int
|
|
||||||
|
|
||||||
def __init__(self, path: str):
|
|
||||||
self.path = path
|
|
||||||
self.cursor = 0
|
|
||||||
|
|
||||||
def __iter__(self) -> Self:
|
|
||||||
return self
|
|
||||||
|
|
||||||
def advance_cursor(self, next_value: int) -> None:
|
|
||||||
if next_value < len(self.path):
|
|
||||||
self.cursor = next_value
|
|
||||||
else:
|
|
||||||
self.cursor = -1
|
|
||||||
|
|
||||||
def __next__(self) -> str:
|
|
||||||
if self.cursor < 0:
|
|
||||||
raise StopIteration()
|
|
||||||
else:
|
|
||||||
while self.cursor >= 0:
|
|
||||||
next_separator = self.path.find('/', self.cursor)
|
|
||||||
if next_separator < 0:
|
|
||||||
result = self.path[self.cursor:]
|
|
||||||
self.cursor = next_separator
|
|
||||||
return result
|
|
||||||
elif next_separator == self.cursor:
|
|
||||||
self.advance_cursor(next_separator + 1)
|
|
||||||
else:
|
|
||||||
result = self.path[self.cursor:next_separator]
|
|
||||||
self.advance_cursor(next_separator + 1)
|
|
||||||
return result
|
|
||||||
raise StopIteration()
|
|
||||||
|
|
||||||
|
|
||||||
class NodeAncestryIterator:
|
|
||||||
node: Node | PathMatcher
|
|
||||||
|
|
||||||
def __init__(self, node: Node):
|
|
||||||
self.node = node
|
|
||||||
|
|
||||||
def __iter__(self) -> Self:
|
|
||||||
return self
|
|
||||||
|
|
||||||
def __next__(self) -> Node | PathMatcher:
|
|
||||||
parent = self.node.parent
|
|
||||||
if parent is None:
|
|
||||||
raise StopIteration()
|
|
||||||
else:
|
|
||||||
self.node = parent
|
|
||||||
return parent
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
from typing import (
|
|
||||||
TypedDict,
|
|
||||||
Literal,
|
|
||||||
Iterable,
|
|
||||||
Tuple,
|
|
||||||
Optional,
|
|
||||||
NotRequired,
|
|
||||||
Dict,
|
|
||||||
Any,
|
|
||||||
Union,
|
|
||||||
Mapping,
|
|
||||||
Sequence
|
|
||||||
)
|
|
||||||
|
|
||||||
from .base import StrOrStrings, PathMatcherResult
|
|
||||||
|
|
||||||
from bugis.core._http_method import HttpMethod
|
|
||||||
|
|
||||||
type NodeType = (str | HttpMethod)
|
|
||||||
|
|
||||||
|
|
||||||
class ASGIVersions(TypedDict):
|
|
||||||
spec_version: str
|
|
||||||
version: Union[Literal["2.0"], Literal["3.0"]]
|
|
||||||
|
|
||||||
|
|
||||||
class HTTPScope(TypedDict):
|
|
||||||
type: Literal["http"]
|
|
||||||
asgi: ASGIVersions
|
|
||||||
http_version: str
|
|
||||||
method: str
|
|
||||||
scheme: str
|
|
||||||
path: str
|
|
||||||
raw_path: bytes
|
|
||||||
query_string: bytes
|
|
||||||
root_path: str
|
|
||||||
headers: Iterable[Tuple[bytes, bytes]]
|
|
||||||
client: Optional[Tuple[str, int]]
|
|
||||||
server: Optional[Tuple[str, Optional[int]]]
|
|
||||||
state: NotRequired[Dict[str, Any]]
|
|
||||||
extensions: Optional[Dict[str, Dict[object, object]]]
|
|
||||||
|
|
||||||
|
|
||||||
class WebSocketScope(TypedDict):
|
|
||||||
type: Literal["websocket"]
|
|
||||||
asgi: ASGIVersions
|
|
||||||
http_version: str
|
|
||||||
scheme: str
|
|
||||||
path: str
|
|
||||||
raw_path: bytes
|
|
||||||
query_string: bytes
|
|
||||||
root_path: str
|
|
||||||
headers: Iterable[Tuple[bytes, bytes]]
|
|
||||||
client: Optional[Tuple[str, int]]
|
|
||||||
server: Optional[Tuple[str, Optional[int]]]
|
|
||||||
subprotocols: Iterable[str]
|
|
||||||
state: NotRequired[Dict[str, Any]]
|
|
||||||
extensions: Optional[Dict[str, Dict[object, object]]]
|
|
||||||
|
|
||||||
|
|
||||||
class LifespanScope(TypedDict):
|
|
||||||
type: Literal["lifespan"]
|
|
||||||
asgi: ASGIVersions
|
|
||||||
state: NotRequired[Dict[str, Any]]
|
|
||||||
|
|
||||||
|
|
||||||
class RSGI:
|
|
||||||
class Scope(TypedDict):
|
|
||||||
proto: Literal['http'] # = 'http'
|
|
||||||
rsgi_version: str
|
|
||||||
http_version: str
|
|
||||||
server: str
|
|
||||||
client: str
|
|
||||||
scheme: str
|
|
||||||
method: str
|
|
||||||
path: str
|
|
||||||
query_string: str
|
|
||||||
headers: Mapping[str, str]
|
|
||||||
authority: Optional[str]
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'HttpMethod',
|
|
||||||
'HTTPScope',
|
|
||||||
'LifespanScope',
|
|
||||||
'RSGI',
|
|
||||||
'ASGIVersions',
|
|
||||||
'WebSocketScope',
|
|
||||||
'NodeType',
|
|
||||||
'StrOrStrings'
|
|
||||||
]
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
from typing import (
|
|
||||||
Sequence,
|
|
||||||
TypedDict,
|
|
||||||
Literal,
|
|
||||||
Iterable,
|
|
||||||
Tuple,
|
|
||||||
Optional,
|
|
||||||
NotRequired,
|
|
||||||
Dict,
|
|
||||||
Any,
|
|
||||||
Union
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class ASGIVersions(TypedDict):
|
|
||||||
spec_version: str
|
|
||||||
version: Union[Literal["2.0"], Literal["3.0"]]
|
|
||||||
|
|
||||||
|
|
||||||
class HTTPScope(TypedDict):
|
|
||||||
type: Literal["http"]
|
|
||||||
asgi: ASGIVersions
|
|
||||||
http_version: str
|
|
||||||
method: str
|
|
||||||
scheme: str
|
|
||||||
path: str
|
|
||||||
raw_path: bytes
|
|
||||||
query_string: bytes
|
|
||||||
root_path: str
|
|
||||||
headers: Iterable[Tuple[bytes, bytes]]
|
|
||||||
client: Optional[Tuple[str, int]]
|
|
||||||
server: Optional[Tuple[str, Optional[int]]]
|
|
||||||
state: NotRequired[Dict[str, Any]]
|
|
||||||
extensions: Optional[Dict[str, Dict[object, object]]]
|
|
||||||
|
|
||||||
|
|
||||||
class WebSocketScope(TypedDict):
|
|
||||||
type: Literal["websocket"]
|
|
||||||
asgi: ASGIVersions
|
|
||||||
http_version: str
|
|
||||||
scheme: str
|
|
||||||
path: str
|
|
||||||
raw_path: bytes
|
|
||||||
query_string: bytes
|
|
||||||
root_path: str
|
|
||||||
headers: Iterable[Tuple[bytes, bytes]]
|
|
||||||
client: Optional[Tuple[str, int]]
|
|
||||||
server: Optional[Tuple[str, Optional[int]]]
|
|
||||||
subprotocols: Iterable[str]
|
|
||||||
state: NotRequired[Dict[str, Any]]
|
|
||||||
extensions: Optional[Dict[str, Dict[object, object]]]
|
|
||||||
|
|
||||||
|
|
||||||
class LifespanScope(TypedDict):
|
|
||||||
type: Literal["lifespan"]
|
|
||||||
asgi: ASGIVersions
|
|
||||||
state: NotRequired[Dict[str, Any]]
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
from typing import Sequence, Mapping, Any
|
|
||||||
|
|
||||||
type StrOrStrings = (str | Sequence[str])
|
|
||||||
type PathMatcherResult = Mapping[str, Any] | Sequence[str]
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
from typing import (
|
|
||||||
Sequence,
|
|
||||||
TypedDict,
|
|
||||||
Literal,
|
|
||||||
Iterable,
|
|
||||||
Tuple,
|
|
||||||
Optional,
|
|
||||||
NotRequired,
|
|
||||||
Dict,
|
|
||||||
Any,
|
|
||||||
Union,
|
|
||||||
Mapping,
|
|
||||||
)
|
|
||||||
|
|
||||||
class HTTPScope(TypedDict):
|
|
||||||
proto: Literal['http']
|
|
||||||
rsgi_version: str
|
|
||||||
http_version: str
|
|
||||||
server: str
|
|
||||||
client: str
|
|
||||||
scheme: str
|
|
||||||
method: str
|
|
||||||
path: str
|
|
||||||
query_string: str
|
|
||||||
headers: Mapping[str, str]
|
|
||||||
authority: Optional[str]
|
|
||||||
@@ -1,127 +0,0 @@
|
|||||||
import unittest
|
|
||||||
import json
|
|
||||||
import httpx
|
|
||||||
from pwo import async_test
|
|
||||||
from bugis.core import BugisApp, HttpContext, HttpMethod
|
|
||||||
from typing import Sequence
|
|
||||||
|
|
||||||
|
|
||||||
class AsgiTest(unittest.TestCase):
|
|
||||||
app: BugisApp
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.app = BugisApp()
|
|
||||||
|
|
||||||
@self.app.GET('/hello')
|
|
||||||
@self.app.GET('/hello2')
|
|
||||||
@self.app.route('/hello3')
|
|
||||||
async def handle_request(ctx: HttpContext) -> None:
|
|
||||||
async for chunk in ctx.request_body:
|
|
||||||
print(chunk)
|
|
||||||
await ctx.send_str(200, 'Hello World!')
|
|
||||||
|
|
||||||
@self.app.route(('/foo/bar',), HttpMethod.PUT, recursive=True)
|
|
||||||
async def handle_request(ctx: HttpContext) -> None:
|
|
||||||
async for chunk in ctx.request_body:
|
|
||||||
print(chunk)
|
|
||||||
await ctx.send_str(200, ctx.path)
|
|
||||||
|
|
||||||
@self.app.route(('/foo/*',), HttpMethod.PUT, recursive=True)
|
|
||||||
async def handle_request(ctx: HttpContext, path: Sequence[str]) -> None:
|
|
||||||
async for chunk in ctx.request_body:
|
|
||||||
print(chunk)
|
|
||||||
await ctx.send_str(200, json.dumps(path))
|
|
||||||
|
|
||||||
@self.app.GET('/employee/${employee_id}')
|
|
||||||
async def handle_request(ctx: HttpContext, employee_id: str) -> None:
|
|
||||||
async for chunk in ctx.request_body:
|
|
||||||
print(chunk)
|
|
||||||
await ctx.send_str(200, employee_id)
|
|
||||||
|
|
||||||
@self.app.GET('/square/${x:int}')
|
|
||||||
async def handle_request(ctx: HttpContext, x: int) -> None:
|
|
||||||
async for chunk in ctx.request_body:
|
|
||||||
print(chunk)
|
|
||||||
await ctx.send_str(200, str(x * x))
|
|
||||||
|
|
||||||
@self.app.GET('/department/${department_id:int}/employee/${employee_id:int}')
|
|
||||||
async def handle_request(ctx: HttpContext, department_id: int, employee_id: int) -> None:
|
|
||||||
async for chunk in ctx.request_body:
|
|
||||||
print(chunk)
|
|
||||||
await ctx.send_str(200, json.dumps({
|
|
||||||
'department_id': department_id,
|
|
||||||
'employee_id': employee_id
|
|
||||||
}))
|
|
||||||
|
|
||||||
@async_test
|
|
||||||
async def test_hello(self):
|
|
||||||
transport = httpx.ASGITransport(app=self.app)
|
|
||||||
|
|
||||||
async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
|
|
||||||
r = await client.get("/hello")
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
self.assertEqual(r.text, "Hello World!")
|
|
||||||
|
|
||||||
r = await client.get("/hello2")
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
self.assertEqual(r.text, "Hello World!")
|
|
||||||
|
|
||||||
r = await client.post("/hello3")
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
self.assertEqual(r.text, "Hello World!")
|
|
||||||
|
|
||||||
r = await client.get("/hello4")
|
|
||||||
self.assertEqual(r.status_code, 404)
|
|
||||||
self.assertTrue(len(r.text) == 0)
|
|
||||||
|
|
||||||
@async_test
|
|
||||||
async def test_foo(self):
|
|
||||||
transport = httpx.ASGITransport(app=self.app)
|
|
||||||
|
|
||||||
async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
|
|
||||||
r = await client.put("/foo/fizz/baz")
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
response = json.loads(r.text)
|
|
||||||
self.assertEqual(['fizz', 'baz'], response)
|
|
||||||
|
|
||||||
@async_test
|
|
||||||
async def test_foo_bar(self):
|
|
||||||
transport = httpx.ASGITransport(app=self.app)
|
|
||||||
|
|
||||||
async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
|
|
||||||
r = await client.put("/foo/bar/baz")
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
self.assertEqual('/foo/bar/baz', r.text)
|
|
||||||
|
|
||||||
@async_test
|
|
||||||
async def test_employee(self):
|
|
||||||
transport = httpx.ASGITransport(app=self.app)
|
|
||||||
|
|
||||||
async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
|
|
||||||
r = await client.get("/employee/101325")
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
self.assertEqual(r.text, '101325')
|
|
||||||
|
|
||||||
@async_test
|
|
||||||
async def test_square(self):
|
|
||||||
transport = httpx.ASGITransport(app=self.app)
|
|
||||||
|
|
||||||
async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
|
|
||||||
x = 30
|
|
||||||
r = await client.get(f"/square/{x}")
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
self.assertEqual(r.text, str(x * x))
|
|
||||||
|
|
||||||
@async_test
|
|
||||||
async def test_department_employee(self):
|
|
||||||
transport = httpx.ASGITransport(app=self.app)
|
|
||||||
|
|
||||||
async with httpx.AsyncClient(transport=transport, base_url="http://127.0.0.1:80") as client:
|
|
||||||
r = await client.get("department/189350/employee/101325")
|
|
||||||
self.assertEqual(r.status_code, 200)
|
|
||||||
response = json.loads(r.text)
|
|
||||||
self.assertEqual({
|
|
||||||
'department_id': 189350,
|
|
||||||
'employee_id': 101325
|
|
||||||
}, response)
|
|
||||||
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
import unittest
|
|
||||||
|
|
||||||
|
|
||||||
# Define some test cases
|
|
||||||
class TestAddition(unittest.TestCase):
|
|
||||||
def test_add_positive(self):
|
|
||||||
self.assertEqual(1 + 2, 3)
|
|
||||||
|
|
||||||
def test_add_negative(self):
|
|
||||||
self.assertEqual(-1 + (-1), -2)
|
|
||||||
|
|
||||||
|
|
||||||
class TestSubtraction(unittest.TestCase):
|
|
||||||
def test_subtract_positive(self):
|
|
||||||
self.assertEqual(5 - 3, 2)
|
|
||||||
|
|
||||||
def test_subtract_negative(self):
|
|
||||||
self.assertEqual(-5 - (-2), -3)
|
|
||||||
|
|
||||||
|
|
||||||
# Now let's create a TestSuite
|
|
||||||
def suite():
|
|
||||||
suite = unittest.TestSuite()
|
|
||||||
|
|
||||||
# Add tests to the suite
|
|
||||||
suite.addTest(TestAddition('test_add_positive'))
|
|
||||||
suite.addTest(TestAddition('test_add_negative'))
|
|
||||||
suite.addTest(TestSubtraction('test_subtract_positive'))
|
|
||||||
# suite.addTest(TestSubtraction('test_subtract_negative'))
|
|
||||||
# suite.addTest(TestSubtraction('test_subtract_negative2'))
|
|
||||||
|
|
||||||
return suite
|
|
||||||
|
|
||||||
|
|
||||||
# Running the suite
|
|
||||||
if __name__ == "__main__":
|
|
||||||
runner = unittest.TextTestRunner()
|
|
||||||
runner.run(suite())
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
from typing import Sequence, Tuple, Optional, List
|
|
||||||
|
|
||||||
from bugis.core import Tree, PathHandler, HttpContext, HttpMethod, PathIterator
|
|
||||||
from bugis.core import HttpMethod
|
|
||||||
from pwo import Maybe
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
|
|
||||||
class PathIteratorTest(unittest.TestCase):
|
|
||||||
cases: Tuple[Tuple[str, Tuple[str, ...]], ...] = (
|
|
||||||
('/', tuple()),
|
|
||||||
('root/foo', ('root', 'foo')),
|
|
||||||
('/root', ('root',)),
|
|
||||||
('/root', ('root',)),
|
|
||||||
('/root/', ('root',)),
|
|
||||||
('/root/bar/', ('root', 'bar')),
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_path_iterator(self):
|
|
||||||
for (case, expected) in self.cases:
|
|
||||||
with self.subTest(case) as _:
|
|
||||||
components = tuple((c for c in PathIterator(case)))
|
|
||||||
self.assertEqual(expected, components)
|
|
||||||
|
|
||||||
|
|
||||||
class TreeTest(unittest.TestCase):
|
|
||||||
tree: Tree
|
|
||||||
handlers: List[PathHandler]
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
self.tree = Tree()
|
|
||||||
|
|
||||||
class TestHandler(PathHandler):
|
|
||||||
|
|
||||||
def handle_request(self, ctx: HttpContext):
|
|
||||||
pass
|
|
||||||
|
|
||||||
@property
|
|
||||||
def recursive(self) -> bool:
|
|
||||||
return True
|
|
||||||
|
|
||||||
self.handlers = [TestHandler() for _ in range(20)]
|
|
||||||
|
|
||||||
routes: Tuple[Tuple[Tuple[str, ...], Optional[HttpMethod], PathHandler], ...] = (
|
|
||||||
(('home', 'something'), HttpMethod.GET, self.handlers[0]),
|
|
||||||
(('home', 'something_else'), HttpMethod.GET, self.handlers[1]),
|
|
||||||
(('home', 'something_else'), HttpMethod.POST, self.handlers[2]),
|
|
||||||
(('home', 'something', 'object'), HttpMethod.GET, self.handlers[3]),
|
|
||||||
(('home', 'something_else', 'foo'), HttpMethod.GET, self.handlers[4]),
|
|
||||||
(('home',), HttpMethod.GET, self.handlers[5]),
|
|
||||||
(('home',), HttpMethod.POST, self.handlers[6]),
|
|
||||||
(('home',), None, self.handlers[7]),
|
|
||||||
(('home', '*.md'), None, self.handlers[8]),
|
|
||||||
(('home', 'something', '*', 'blah', '*.md'), None, self.handlers[9]),
|
|
||||||
(('home', 'bar', '*'), None, self.handlers[10]),
|
|
||||||
|
|
||||||
)
|
|
||||||
|
|
||||||
for path, method, handler in routes:
|
|
||||||
self.tree.add((p for p in path), method, handler)
|
|
||||||
|
|
||||||
def test_tree(self):
|
|
||||||
|
|
||||||
cases: Tuple[Tuple[str, HttpMethod, Optional[int]], ...] = (
|
|
||||||
('http://localhost:127.0.0.1:5432/home/something', HttpMethod.GET, 0),
|
|
||||||
('http://localhost:127.0.0.1:5432/home/something_else', HttpMethod.GET, 1),
|
|
||||||
('http://localhost:127.0.0.1:5432/home/something_else', HttpMethod.POST, 2),
|
|
||||||
('http://localhost:127.0.0.1:5432/home/something/object', HttpMethod.GET, 3),
|
|
||||||
('http://localhost:127.0.0.1:5432/home/something_else/foo', HttpMethod.GET, 4),
|
|
||||||
('http://localhost:127.0.0.1:5432/', HttpMethod.GET, None),
|
|
||||||
('http://localhost:127.0.0.1:5432/home', HttpMethod.GET, 5),
|
|
||||||
('http://localhost:127.0.0.1:5432/home', HttpMethod.POST, 6),
|
|
||||||
('http://localhost:127.0.0.1:5432/home', HttpMethod.PUT, 7),
|
|
||||||
('http://localhost:127.0.0.1:5432/home/README.md', HttpMethod.GET, 8),
|
|
||||||
('http://localhost:127.0.0.1:5432/home/something/ciao/blah/README.md', HttpMethod.GET, 9),
|
|
||||||
('http://localhost:127.0.0.1:5432/home/bar/ciao/blah/README.md', HttpMethod.GET, 10),
|
|
||||||
)
|
|
||||||
for url, method, handler_num in cases:
|
|
||||||
with self.subTest(f"{str(method)} {url}"):
|
|
||||||
res = self.tree.get_handler(url, method)
|
|
||||||
self.assertIs(Maybe.of(handler_num).map(self.handlers.__getitem__).or_none(),
|
|
||||||
Maybe.of_nullable(res).map(lambda it: it[0]).or_none())
|
|
||||||
|
|
||||||
@@ -29,7 +29,7 @@ dependencies = [
|
|||||||
"PyYAML",
|
"PyYAML",
|
||||||
"pygraphviz",
|
"pygraphviz",
|
||||||
"aiofiles",
|
"aiofiles",
|
||||||
"httpx[http2]"
|
"aiohttp[speedups]",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
|
|||||||
@@ -7,21 +7,30 @@
|
|||||||
--index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple
|
--index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple
|
||||||
--extra-index-url https://pypi.org/simple
|
--extra-index-url https://pypi.org/simple
|
||||||
|
|
||||||
|
aiodns==3.2.0
|
||||||
|
# via aiohttp
|
||||||
aiofiles==24.1.0
|
aiofiles==24.1.0
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
anyio==4.6.2.post1
|
aiohappyeyeballs==2.4.3
|
||||||
# via httpx
|
# via aiohttp
|
||||||
|
aiohttp[speedups]==3.10.10
|
||||||
|
# via bugis (pyproject.toml)
|
||||||
|
aiosignal==1.3.1
|
||||||
|
# via aiohttp
|
||||||
asttokens==2.4.1
|
asttokens==2.4.1
|
||||||
# via stack-data
|
# via stack-data
|
||||||
|
attrs==24.2.0
|
||||||
|
# via aiohttp
|
||||||
|
brotli==1.1.0
|
||||||
|
# via aiohttp
|
||||||
build==1.2.2.post1
|
build==1.2.2.post1
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
certifi==2024.8.30
|
certifi==2024.8.30
|
||||||
# via
|
# via requests
|
||||||
# httpcore
|
|
||||||
# httpx
|
|
||||||
# requests
|
|
||||||
cffi==1.17.1
|
cffi==1.17.1
|
||||||
# via cryptography
|
# via
|
||||||
|
# cryptography
|
||||||
|
# pycares
|
||||||
charset-normalizer==3.4.0
|
charset-normalizer==3.4.0
|
||||||
# via requests
|
# via requests
|
||||||
click==8.1.7
|
click==8.1.7
|
||||||
@@ -36,25 +45,16 @@ docutils==0.21.2
|
|||||||
# via readme-renderer
|
# via readme-renderer
|
||||||
executing==2.1.0
|
executing==2.1.0
|
||||||
# via stack-data
|
# via stack-data
|
||||||
|
frozenlist==1.5.0
|
||||||
|
# via
|
||||||
|
# aiohttp
|
||||||
|
# aiosignal
|
||||||
granian==1.6.1
|
granian==1.6.1
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
h11==0.14.0
|
|
||||||
# via httpcore
|
|
||||||
h2==4.1.0
|
|
||||||
# via httpx
|
|
||||||
hpack==4.0.0
|
|
||||||
# via h2
|
|
||||||
httpcore==1.0.6
|
|
||||||
# via httpx
|
|
||||||
httpx[http2]==0.27.2
|
|
||||||
# via bugis (pyproject.toml)
|
|
||||||
hyperframe==6.0.1
|
|
||||||
# via h2
|
|
||||||
idna==3.10
|
idna==3.10
|
||||||
# via
|
# via
|
||||||
# anyio
|
|
||||||
# httpx
|
|
||||||
# requests
|
# requests
|
||||||
|
# yarl
|
||||||
importlib-metadata==8.5.0
|
importlib-metadata==8.5.0
|
||||||
# via twine
|
# via twine
|
||||||
ipdb==0.13.13
|
ipdb==0.13.13
|
||||||
@@ -87,6 +87,10 @@ more-itertools==10.5.0
|
|||||||
# via
|
# via
|
||||||
# jaraco-classes
|
# jaraco-classes
|
||||||
# jaraco-functools
|
# jaraco-functools
|
||||||
|
multidict==6.1.0
|
||||||
|
# via
|
||||||
|
# aiohttp
|
||||||
|
# yarl
|
||||||
mypy==1.13.0
|
mypy==1.13.0
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
mypy-extensions==1.0.0
|
mypy-extensions==1.0.0
|
||||||
@@ -103,12 +107,16 @@ pkginfo==1.10.0
|
|||||||
# via twine
|
# via twine
|
||||||
prompt-toolkit==3.0.48
|
prompt-toolkit==3.0.48
|
||||||
# via ipython
|
# via ipython
|
||||||
|
propcache==0.2.0
|
||||||
|
# via yarl
|
||||||
ptyprocess==0.7.0
|
ptyprocess==0.7.0
|
||||||
# via pexpect
|
# via pexpect
|
||||||
pure-eval==0.2.3
|
pure-eval==0.2.3
|
||||||
# via stack-data
|
# via stack-data
|
||||||
pwo==0.0.4
|
pwo==0.0.4
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
|
pycares==4.4.0
|
||||||
|
# via aiodns
|
||||||
pycparser==2.22
|
pycparser==2.22
|
||||||
# via cffi
|
# via cffi
|
||||||
pygments==2.18.0
|
pygments==2.18.0
|
||||||
@@ -139,10 +147,6 @@ secretstorage==3.3.3
|
|||||||
# via keyring
|
# via keyring
|
||||||
six==1.16.0
|
six==1.16.0
|
||||||
# via asttokens
|
# via asttokens
|
||||||
sniffio==1.3.1
|
|
||||||
# via
|
|
||||||
# anyio
|
|
||||||
# httpx
|
|
||||||
stack-data==0.6.3
|
stack-data==0.6.3
|
||||||
# via ipython
|
# via ipython
|
||||||
traitlets==5.14.3
|
traitlets==5.14.3
|
||||||
@@ -165,5 +169,7 @@ watchdog==5.0.3
|
|||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
wcwidth==0.2.13
|
wcwidth==0.2.13
|
||||||
# via prompt-toolkit
|
# via prompt-toolkit
|
||||||
|
yarl==1.16.0
|
||||||
|
# via aiohttp
|
||||||
zipp==3.20.2
|
zipp==3.20.2
|
||||||
# via importlib-metadata
|
# via importlib-metadata
|
||||||
|
|||||||
@@ -7,51 +7,57 @@
|
|||||||
--index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple
|
--index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple
|
||||||
--extra-index-url https://pypi.org/simple
|
--extra-index-url https://pypi.org/simple
|
||||||
|
|
||||||
|
aiodns==3.2.0
|
||||||
|
# via aiohttp
|
||||||
aiofiles==24.1.0
|
aiofiles==24.1.0
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
anyio==4.6.2.post1
|
aiohappyeyeballs==2.4.3
|
||||||
# via httpx
|
# via aiohttp
|
||||||
certifi==2024.8.30
|
aiohttp[speedups]==3.10.10
|
||||||
# via
|
# via bugis (pyproject.toml)
|
||||||
# httpcore
|
aiosignal==1.3.1
|
||||||
# httpx
|
# via aiohttp
|
||||||
|
attrs==24.2.0
|
||||||
|
# via aiohttp
|
||||||
|
brotli==1.1.0
|
||||||
|
# via aiohttp
|
||||||
|
cffi==1.17.1
|
||||||
|
# via pycares
|
||||||
click==8.1.7
|
click==8.1.7
|
||||||
# via granian
|
# via granian
|
||||||
|
frozenlist==1.5.0
|
||||||
|
# via
|
||||||
|
# aiohttp
|
||||||
|
# aiosignal
|
||||||
granian==1.6.1
|
granian==1.6.1
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
h11==0.14.0
|
|
||||||
# via httpcore
|
|
||||||
h2==4.1.0
|
|
||||||
# via httpx
|
|
||||||
hpack==4.0.0
|
|
||||||
# via h2
|
|
||||||
httpcore==1.0.6
|
|
||||||
# via httpx
|
|
||||||
httpx[http2]==0.27.2
|
|
||||||
# via bugis (pyproject.toml)
|
|
||||||
hyperframe==6.0.1
|
|
||||||
# via h2
|
|
||||||
idna==3.10
|
idna==3.10
|
||||||
# via
|
# via yarl
|
||||||
# anyio
|
|
||||||
# httpx
|
|
||||||
markdown==3.7
|
markdown==3.7
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
|
multidict==6.1.0
|
||||||
|
# via
|
||||||
|
# aiohttp
|
||||||
|
# yarl
|
||||||
|
propcache==0.2.0
|
||||||
|
# via yarl
|
||||||
pwo==0.0.4
|
pwo==0.0.4
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
|
pycares==4.4.0
|
||||||
|
# via aiodns
|
||||||
|
pycparser==2.22
|
||||||
|
# via cffi
|
||||||
pygments==2.18.0
|
pygments==2.18.0
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
pygraphviz==1.14
|
pygraphviz==1.14
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
pyyaml==6.0.2
|
pyyaml==6.0.2
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
sniffio==1.3.1
|
|
||||||
# via
|
|
||||||
# anyio
|
|
||||||
# httpx
|
|
||||||
typing-extensions==4.12.2
|
typing-extensions==4.12.2
|
||||||
# via pwo
|
# via pwo
|
||||||
uvloop==0.21.0
|
uvloop==0.21.0
|
||||||
# via granian
|
# via granian
|
||||||
watchdog==5.0.3
|
watchdog==5.0.3
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
|
yarl==1.16.0
|
||||||
|
# via aiohttp
|
||||||
|
|||||||
@@ -2,50 +2,56 @@
|
|||||||
# This file is autogenerated by pip-compile with Python 3.12
|
# This file is autogenerated by pip-compile with Python 3.12
|
||||||
# by the following command:
|
# by the following command:
|
||||||
#
|
#
|
||||||
# pip-compile --output-file=requirements.txt pyproject.toml
|
# pip-compile pyproject.toml
|
||||||
#
|
#
|
||||||
--index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple
|
--index-url https://gitea.woggioni.net/api/packages/woggioni/pypi/simple
|
||||||
--extra-index-url https://pypi.org/simple
|
--extra-index-url https://pypi.org/simple
|
||||||
|
|
||||||
|
aiodns==3.2.0
|
||||||
|
# via aiohttp
|
||||||
aiofiles==24.1.0
|
aiofiles==24.1.0
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
anyio==4.6.2.post1
|
aiohappyeyeballs==2.4.3
|
||||||
# via httpx
|
# via aiohttp
|
||||||
certifi==2024.8.30
|
aiohttp[speedups]==3.10.10
|
||||||
# via
|
|
||||||
# httpcore
|
|
||||||
# httpx
|
|
||||||
h11==0.14.0
|
|
||||||
# via httpcore
|
|
||||||
h2==4.1.0
|
|
||||||
# via httpx
|
|
||||||
hpack==4.0.0
|
|
||||||
# via h2
|
|
||||||
httpcore==1.0.6
|
|
||||||
# via httpx
|
|
||||||
httpx[http2]==0.27.2
|
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
hyperframe==6.0.1
|
aiosignal==1.3.1
|
||||||
# via h2
|
# via aiohttp
|
||||||
idna==3.10
|
attrs==24.2.0
|
||||||
|
# via aiohttp
|
||||||
|
brotli==1.1.0
|
||||||
|
# via aiohttp
|
||||||
|
cffi==1.17.1
|
||||||
|
# via pycares
|
||||||
|
frozenlist==1.5.0
|
||||||
# via
|
# via
|
||||||
# anyio
|
# aiohttp
|
||||||
# httpx
|
# aiosignal
|
||||||
|
idna==3.10
|
||||||
|
# via yarl
|
||||||
markdown==3.7
|
markdown==3.7
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
|
multidict==6.1.0
|
||||||
|
# via
|
||||||
|
# aiohttp
|
||||||
|
# yarl
|
||||||
|
propcache==0.2.0
|
||||||
|
# via yarl
|
||||||
pwo==0.0.4
|
pwo==0.0.4
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
|
pycares==4.4.0
|
||||||
|
# via aiodns
|
||||||
|
pycparser==2.22
|
||||||
|
# via cffi
|
||||||
pygments==2.18.0
|
pygments==2.18.0
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
pygraphviz==1.14
|
pygraphviz==1.14
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
pyyaml==6.0.2
|
pyyaml==6.0.2
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
sniffio==1.3.1
|
|
||||||
# via
|
|
||||||
# anyio
|
|
||||||
# httpx
|
|
||||||
typing-extensions==4.12.2
|
typing-extensions==4.12.2
|
||||||
# via pwo
|
# via pwo
|
||||||
watchdog==5.0.3
|
watchdog==5.0.3
|
||||||
# via bugis (pyproject.toml)
|
# via bugis (pyproject.toml)
|
||||||
|
yarl==1.16.0
|
||||||
|
# via aiohttp
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
|
|
||||||
from bugis.core import BugisApp
|
|
||||||
from pathlib import PurePath
|
|
||||||
from bugis.server.server import static_resources
|
|
||||||
import os
|
|
||||||
|
|
||||||
root = os.getenv('STATIC_ROOT') or '.'
|
|
||||||
app = BugisApp()
|
|
||||||
|
|
||||||
static_resources(app, '/view', root)
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
[build-system]
|
|
||||||
requires = ["setuptools>=61.0", "setuptools-scm>=8"]
|
|
||||||
build-backend = "setuptools.build_meta"
|
|
||||||
|
|
||||||
[project]
|
|
||||||
name = "bugis_server"
|
|
||||||
dynamic = ["version"]
|
|
||||||
authors = [
|
|
||||||
{ name="Walter Oggioni", email="oggioni.walter@gmail.com" },
|
|
||||||
]
|
|
||||||
description = "Static file renderer"
|
|
||||||
readme = "README.md"
|
|
||||||
requires-python = ">=3.10"
|
|
||||||
classifiers = [
|
|
||||||
'Development Status :: 3 - Alpha',
|
|
||||||
'Topic :: Utilities',
|
|
||||||
'License :: OSI Approved :: MIT License',
|
|
||||||
'Intended Audience :: System Administrators',
|
|
||||||
'Intended Audience :: Developers',
|
|
||||||
'Environment :: Console',
|
|
||||||
'License :: OSI Approved :: MIT License',
|
|
||||||
'Programming Language :: Python :: 3',
|
|
||||||
]
|
|
||||||
dependencies = [
|
|
||||||
"bugis_core",
|
|
||||||
"Markdown",
|
|
||||||
"Pygments",
|
|
||||||
"watchdog",
|
|
||||||
"pwo",
|
|
||||||
"PyYAML",
|
|
||||||
"pygraphviz",
|
|
||||||
"aiofiles",
|
|
||||||
"httpx[http2]"
|
|
||||||
]
|
|
||||||
|
|
||||||
[project.optional-dependencies]
|
|
||||||
dev = [
|
|
||||||
"build", "granian", "mypy", "ipdb", "twine"
|
|
||||||
]
|
|
||||||
|
|
||||||
run = [
|
|
||||||
"granian"
|
|
||||||
]
|
|
||||||
|
|
||||||
[tool.setuptools.package-data]
|
|
||||||
bugis = ['static/*', 'default-conf/*']
|
|
||||||
|
|
||||||
[project.urls]
|
|
||||||
"Homepage" = "https://github.com/woggioni/bugis"
|
|
||||||
"Bug Tracker" = "https://github.com/woggioni/bugis/issues"
|
|
||||||
|
|
||||||
[tool.mypy]
|
|
||||||
python_version = "3.12"
|
|
||||||
disallow_untyped_defs = true
|
|
||||||
show_error_codes = true
|
|
||||||
no_implicit_optional = true
|
|
||||||
warn_return_any = true
|
|
||||||
warn_unused_ignores = true
|
|
||||||
exclude = ["scripts", "docs", "test"]
|
|
||||||
strict = true
|
|
||||||
|
|
||||||
[tool.setuptools_scm]
|
|
||||||
root='..'
|
|
||||||
version_file = "src/bugis/server/_version.py"
|
|
||||||
|
|
||||||
[project.scripts]
|
|
||||||
bugis = "bugis.server.cli:main"
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
from abc import ABC, abstractmethod
|
|
||||||
from typing import Optional, Tuple, Callable
|
|
||||||
from functools import lru_cache
|
|
||||||
from pathlib import PurePath
|
|
||||||
from .renderer import RenderingManager
|
|
||||||
|
|
||||||
|
|
||||||
class Cache(ABC):
|
|
||||||
_rendering_manager: RenderingManager
|
|
||||||
|
|
||||||
def __init__(self, rendering_manager: RenderingManager):
|
|
||||||
self._rendering_manager = rendering_manager
|
|
||||||
|
|
||||||
def get(self, key: bytes, file: PurePath) -> Optional[Tuple[str, bytes]]:
|
|
||||||
if self.filter(file):
|
|
||||||
return self.load(key, file)
|
|
||||||
else:
|
|
||||||
return self._rendering_manager.render(file)
|
|
||||||
|
|
||||||
def filter(self, file: PurePath) -> bool:
|
|
||||||
return file.suffix.lower() in {'md', 'puml', 'dot', 'texi', 'texinfo', 'txi'}
|
|
||||||
|
|
||||||
def load(self, key: bytes, file: PurePath) -> Optional[Tuple[str, bytes]]:
|
|
||||||
return self._rendering_manager.render(file)
|
|
||||||
|
|
||||||
|
|
||||||
class NoCache(Cache):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class InMemoryCache(Cache):
|
|
||||||
_cached_loader: Callable[[bytes, PurePath], Tuple[str, bytes]]
|
|
||||||
|
|
||||||
def __init__(self, rendering_manager: RenderingManager, max_size: int = 1024):
|
|
||||||
super().__init__(rendering_manager)
|
|
||||||
|
|
||||||
@lru_cache(maxsize=max_size)
|
|
||||||
def cached_loader(key: bytes, file: PurePath) -> Tuple[str, bytes]:
|
|
||||||
return super().load(key, file)
|
|
||||||
|
|
||||||
self._cached_loader = cached_loader
|
|
||||||
|
|
||||||
def load(self, key: bytes, file: PurePath) -> Optional[Tuple[str, bytes]]:
|
|
||||||
return self._cached_loader(key, file)
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
|
|
||||||
def main():
|
|
||||||
pass
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
from pathlib import PurePath
|
|
||||||
from typing import Callable, Mapping, Optional, Tuple
|
|
||||||
from pwo import Maybe
|
|
||||||
|
|
||||||
|
|
||||||
class RenderingManager:
|
|
||||||
_renderers: Mapping[str, Callable[[PurePath], Tuple[str, bytes]]]
|
|
||||||
|
|
||||||
def __init__(self, renderers: Mapping[str, Callable[[PurePath], Tuple[str, bytes]]]):
|
|
||||||
self._renderers = renderers
|
|
||||||
|
|
||||||
def render(self, file: PurePath) -> Optional[Tuple[str, bytes]]:
|
|
||||||
return Maybe.of_nullable(self._renderers.get(file.suffix.lower())).map(lambda it: it(file)).or_none()
|
|
||||||
|
|
||||||
# def register(self, suffix: str, renderer: Callable[[PurePath], Tuple[str, bytes]]) -> None:
|
|
||||||
# self._renderers[suffix.lower()] = renderer
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
from base64 import b64encode, b64decode
|
|
||||||
from hashlib import md5
|
|
||||||
from mimetypes import guess_type
|
|
||||||
from pathlib import Path, PurePath
|
|
||||||
from typing import TYPE_CHECKING, Optional, Callable, Awaitable, AsyncGenerator, Any, Unpack, Mapping, Tuple
|
|
||||||
|
|
||||||
from aiofiles.os import listdir
|
|
||||||
from aiofiles.ospath import isdir, isfile
|
|
||||||
from bugis.core import HttpContext, BugisApp
|
|
||||||
from pwo import Maybe
|
|
||||||
from .cache import Cache, InMemoryCache
|
|
||||||
from .renderer import RenderingManager
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
|
||||||
from _typeshed import StrOrBytesPath
|
|
||||||
|
|
||||||
|
|
||||||
def parse_etag(etag: str) -> Optional[str]:
|
|
||||||
def skip_weak_marker(s):
|
|
||||||
if s.startswith('W/'):
|
|
||||||
return s[2:]
|
|
||||||
else:
|
|
||||||
return s
|
|
||||||
|
|
||||||
return (
|
|
||||||
Maybe.of_nullable(etag)
|
|
||||||
.map(skip_weak_marker)
|
|
||||||
.or_else(None)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def static_resources(app: BugisApp,
|
|
||||||
path: str,
|
|
||||||
root: 'StrOrBytesPath',
|
|
||||||
favicon: PurePath = None,
|
|
||||||
file_filter: Callable[[PurePath], Awaitable[bool]] = None,
|
|
||||||
renderers: Mapping[str, Tuple[str, bytes]] = None,
|
|
||||||
cache_ctor: Callable[[RenderingManager], Cache] = lambda rm: InMemoryCache(rm)
|
|
||||||
):
|
|
||||||
renderer = RenderingManager(renderers or {})
|
|
||||||
cache = cache_ctor(renderer)
|
|
||||||
|
|
||||||
async def no_filter(_: PurePath):
|
|
||||||
return True
|
|
||||||
|
|
||||||
if file_filter is None:
|
|
||||||
file_filter = no_filter
|
|
||||||
|
|
||||||
def compute_etag(resource: Path) -> str:
|
|
||||||
md = md5()
|
|
||||||
print(resource)
|
|
||||||
with resource.open('rb') as file:
|
|
||||||
while True:
|
|
||||||
chunk = file.read(0x10000)
|
|
||||||
if len(chunk):
|
|
||||||
md.update(chunk)
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
return b64encode(md.digest()).decode()
|
|
||||||
|
|
||||||
if isinstance(root, str):
|
|
||||||
folder = Path(root)
|
|
||||||
else:
|
|
||||||
folder = root
|
|
||||||
|
|
||||||
prefix = PurePath(path)
|
|
||||||
|
|
||||||
async def handler(context: HttpContext, *_: Unpack[Any]) -> None:
|
|
||||||
requested = (PurePath(context.path)).relative_to(prefix)
|
|
||||||
resource = folder / requested
|
|
||||||
if not resource.is_relative_to(folder) or not resource.exists():
|
|
||||||
return await context.send_empty(404)
|
|
||||||
|
|
||||||
if await isfile(resource):
|
|
||||||
|
|
||||||
proposed_etag = ((Maybe.of_nullable(context.headers.get('if-none-match'))
|
|
||||||
.filter(lambda it: len(it) > 0)
|
|
||||||
.map(lambda it: it[-1])
|
|
||||||
.map(parse_etag))
|
|
||||||
.or_none())
|
|
||||||
current_etag = compute_etag(resource)
|
|
||||||
|
|
||||||
if proposed_etag == current_etag:
|
|
||||||
return await context.send_empty(304)
|
|
||||||
else:
|
|
||||||
cache_result = cache.get(b64decode(current_etag), resource)
|
|
||||||
if cache_result is None:
|
|
||||||
mime_type = (Maybe.of(guess_type(resource.name))
|
|
||||||
.map(lambda it: it[0])
|
|
||||||
.or_else('application/octet-stream'))
|
|
||||||
return await context.send_file(200, resource, {
|
|
||||||
'content-type': mime_type or 'application/octet-stream',
|
|
||||||
'etag': 'W/' + current_etag,
|
|
||||||
'cache-control': 'no-cache',
|
|
||||||
})
|
|
||||||
else:
|
|
||||||
content_type, body = cache_result
|
|
||||||
await context.send_bytes(200, body, {
|
|
||||||
'content-type': content_type,
|
|
||||||
'etag': 'W/' + current_etag,
|
|
||||||
'cache-control': 'no-cache',
|
|
||||||
})
|
|
||||||
elif isdir(resource):
|
|
||||||
headers = {
|
|
||||||
'content-type': 'text/html'
|
|
||||||
}
|
|
||||||
await context.send_str(200, await directory_listing(prefix, requested, resource, favicon, file_filter),
|
|
||||||
headers)
|
|
||||||
|
|
||||||
return app.GET(path, True)(handler)
|
|
||||||
|
|
||||||
|
|
||||||
async def directory_listing(prefix: PurePath,
|
|
||||||
path_info: PurePath,
|
|
||||||
path: PurePath,
|
|
||||||
favicon: PurePath,
|
|
||||||
file_filter: Callable[[PurePath], Awaitable[bool]]) -> str:
|
|
||||||
title = "Directory listing for %s" % path_info
|
|
||||||
result = "<!DOCTYPE html><html><head>"
|
|
||||||
if favicon:
|
|
||||||
result += f'<link rel="icon" type="image/x-icon" href="{favicon}">'
|
|
||||||
result += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
|
|
||||||
result += "<title>" + title + "</title></head>"
|
|
||||||
result += "<body><h1>" + title + "</h1><hr>"
|
|
||||||
result += "<ul>"
|
|
||||||
if path_info != '/':
|
|
||||||
result += "<li><a href=\"../\"/>../</li>"
|
|
||||||
|
|
||||||
async def ls(entry_filter: Callable[[PurePath], Awaitable[bool]]) -> AsyncGenerator[str, Any]:
|
|
||||||
async def result():
|
|
||||||
for entry in sorted(await listdir(path)):
|
|
||||||
if await entry_filter(path / entry):
|
|
||||||
yield entry
|
|
||||||
|
|
||||||
return result()
|
|
||||||
|
|
||||||
async for entry in await ls(isdir):
|
|
||||||
result += '<li><a href="' + entry + '/' + '"/>' + entry + '/' + '</li>'
|
|
||||||
|
|
||||||
async def composite_file_filter(entry: PurePath) -> bool:
|
|
||||||
return await isfile(entry) and await file_filter(entry)
|
|
||||||
|
|
||||||
async for entry in await ls(composite_file_filter):
|
|
||||||
result += '<li><a href="' + entry + '"/>' + entry + '</li>'
|
|
||||||
return result
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
from .cli import main
|
import argparse
|
||||||
|
from cli import main
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
main(sys.argv[1:])
|
main(sys.argv[1:])
|
||||||
|
|||||||
@@ -1,18 +1,27 @@
|
|||||||
import logging
|
import logging
|
||||||
from asyncio import get_running_loop
|
from logging.config import dictConfig as configure_logging
|
||||||
from typing import Optional, Awaitable, Callable, Any, Mapping
|
|
||||||
|
from yaml import safe_load
|
||||||
|
|
||||||
|
from .configuration import Configuration
|
||||||
|
|
||||||
|
with open(Configuration.instance.logging_configuration_file, 'r') as input_file:
|
||||||
|
conf = safe_load(input_file)
|
||||||
|
configure_logging(conf)
|
||||||
|
|
||||||
|
|
||||||
from pwo import Maybe
|
from pwo import Maybe
|
||||||
|
|
||||||
from .server import Server
|
from .server import Server
|
||||||
|
from asyncio import get_running_loop
|
||||||
|
from .asgi_utils import decode_headers
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
log = logging.getLogger('access')
|
log = logging.getLogger('access')
|
||||||
log.propagate = False
|
log.propagate = False
|
||||||
|
|
||||||
_server: Optional[Server] = None
|
_server: Optional[Server] = None
|
||||||
|
|
||||||
async def application(scope, receive, send : Callable[[Mapping[str, Any]], Awaitable[None]]):
|
async def application(scope, receive, send):
|
||||||
global _server
|
global _server
|
||||||
if scope['type'] == 'lifespan':
|
if scope['type'] == 'lifespan':
|
||||||
while True:
|
while True:
|
||||||
@@ -24,6 +33,22 @@ async def application(scope, receive, send : Callable[[Mapping[str, Any]], Await
|
|||||||
await _server.stop()
|
await _server.stop()
|
||||||
await send({'type': 'lifespan.shutdown.complete'})
|
await send({'type': 'lifespan.shutdown.complete'})
|
||||||
else:
|
else:
|
||||||
|
def maybe_log(evt):
|
||||||
|
d = {
|
||||||
|
'response_headers': (Maybe.of_nullable(evt.get('headers'))
|
||||||
|
.map(decode_headers)
|
||||||
|
.or_none()),
|
||||||
|
'status': evt['status']
|
||||||
|
}
|
||||||
|
log.info(None, extra=dict(**{k : v for k, v in d.items() if k is not None}, **scope))
|
||||||
|
def wrapped_send(*args, **kwargs):
|
||||||
|
result = send(*args, **kwargs)
|
||||||
|
(Maybe.of(args)
|
||||||
|
.filter(lambda it: len(it) > 0)
|
||||||
|
.map(lambda it: it[0])
|
||||||
|
.filter(lambda it: it.get('type') == 'http.response.start')
|
||||||
|
.if_present(maybe_log))
|
||||||
|
return result
|
||||||
pathsend = (Maybe.of_nullable(scope.get('extensions'))
|
pathsend = (Maybe.of_nullable(scope.get('extensions'))
|
||||||
.map(lambda it: it.get("http.response.pathsend"))
|
.map(lambda it: it.get("http.response.pathsend"))
|
||||||
.is_present)
|
.is_present)
|
||||||
@@ -36,7 +61,7 @@ async def application(scope, receive, send : Callable[[Mapping[str, Any]], Await
|
|||||||
.map(lambda it: it.decode())
|
.map(lambda it: it.decode())
|
||||||
.or_else(None),
|
.or_else(None),
|
||||||
Maybe.of_nullable(scope.get('query_string', None)).map(lambda it: it.decode()).or_else(None),
|
Maybe.of_nullable(scope.get('query_string', None)).map(lambda it: it.decode()).or_else(None),
|
||||||
send,
|
wrapped_send,
|
||||||
pathsend
|
pathsend
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ class FileWatcher(PatternMatchingEventHandler):
|
|||||||
_topic_manager: TopicManager
|
_topic_manager: TopicManager
|
||||||
_loop: AbstractEventLoop
|
_loop: AbstractEventLoop
|
||||||
_topic_manager_loop: Task
|
_topic_manager_loop: Task
|
||||||
_running_tasks: Future
|
_running_tasks : Future
|
||||||
|
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
super().__init__(patterns=['*.md'],
|
super().__init__(patterns=['*.md'],
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from granian import Granian
|
|||||||
from pwo import Maybe
|
from pwo import Maybe
|
||||||
|
|
||||||
from .configuration import Configuration
|
from .configuration import Configuration
|
||||||
from granian.constants import HTTPModes, Interfaces, ThreadModes, Loops
|
|
||||||
|
|
||||||
def main(args: Optional[Sequence[str]] = None):
|
def main(args: Optional[Sequence[str]] = None):
|
||||||
parser = argparse.ArgumentParser(description="A simple CLI program to render Markdown files")
|
parser = argparse.ArgumentParser(description="A simple CLI program to render Markdown files")
|
||||||
@@ -27,88 +27,15 @@ def main(args: Optional[Sequence[str]] = None):
|
|||||||
default=default_configuration_file,
|
default=default_configuration_file,
|
||||||
type=Path,
|
type=Path,
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
|
||||||
'-a',
|
|
||||||
'--address',
|
|
||||||
help='Server bind address',
|
|
||||||
default='127.0.0.1',
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'-p',
|
|
||||||
'--port',
|
|
||||||
help='Server port',
|
|
||||||
default='8000',
|
|
||||||
type=int
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--access-log',
|
|
||||||
help='Enable access log',
|
|
||||||
action='store_true',
|
|
||||||
dest='log_access'
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--logging-configuration',
|
|
||||||
help='Logging configuration file',
|
|
||||||
dest='log_config_file'
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'-w', '--workers',
|
|
||||||
help='Number of worker processes',
|
|
||||||
default='1',
|
|
||||||
dest='workers',
|
|
||||||
type = int
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'-t', '--threads',
|
|
||||||
help='Number of threads per worker',
|
|
||||||
default='1',
|
|
||||||
dest='threads',
|
|
||||||
type=int
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--http',
|
|
||||||
help='HTTP protocol version',
|
|
||||||
dest='http',
|
|
||||||
type=lambda it: HTTPModes(it),
|
|
||||||
choices=[str(mode) for mode in HTTPModes],
|
|
||||||
default = 'auto',
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--threading-mode',
|
|
||||||
help='Threading mode',
|
|
||||||
dest='threading_mode',
|
|
||||||
type=lambda it: ThreadModes(it),
|
|
||||||
choices=[str(mode) for mode in ThreadModes],
|
|
||||||
default=ThreadModes.workers
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
'--loop',
|
|
||||||
help='Loop',
|
|
||||||
dest='loop',
|
|
||||||
type=lambda it: Loops(it),
|
|
||||||
choices=[str(mode) for mode in Loops]
|
|
||||||
)
|
|
||||||
args = parser.parse_args(args)
|
args = parser.parse_args(args)
|
||||||
|
|
||||||
def parse(configuration: Path):
|
def parse(configuration: Path):
|
||||||
with open(configuration, 'r') as f:
|
with open(configuration, 'r') as f:
|
||||||
return yaml.safe_load(f)
|
return Configuration.from_dict(yaml.safe_load(f))
|
||||||
|
|
||||||
def assign(it: Configuration):
|
conf = Maybe.of_nullable(args.configuration).map(parse).or_else(Configuration.instance)
|
||||||
Configuration.instance = it
|
|
||||||
Maybe.of_nullable(args.configuration).map(parse).if_present(assign)
|
|
||||||
conf = Configuration.instance
|
|
||||||
|
|
||||||
|
|
||||||
granian_conf = asdict(conf).setdefault('granian', dict())
|
|
||||||
for k, v in vars(args).items():
|
|
||||||
if v is not None:
|
|
||||||
granian_conf[k] = v
|
|
||||||
if args.log_config_file:
|
|
||||||
with open(args.log_config_file, 'r') as f:
|
|
||||||
granian_conf['log_dictconfig'] = yaml.safe_load(f)
|
|
||||||
granian_conf = Configuration.GranianConfiguration.from_dict(granian_conf)
|
|
||||||
|
|
||||||
Granian(
|
Granian(
|
||||||
"bugis.asgi:application",
|
"bugis.asgi:application",
|
||||||
**asdict(granian_conf)
|
**asdict(conf.granian)
|
||||||
).serve()
|
).serve()
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
from os import environ
|
from os import environ
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from dataclasses import dataclass, field, asdict
|
from dataclasses import dataclass, field, asdict
|
||||||
|
|
||||||
import yaml
|
|
||||||
from granian.constants import Loops, Interfaces, ThreadModes, HTTPModes, StrEnum
|
from granian.constants import Loops, Interfaces, ThreadModes, HTTPModes, StrEnum
|
||||||
from granian.log import LogLevels
|
from granian.log import LogLevels
|
||||||
from granian.http import HTTP1Settings, HTTP2Settings
|
from granian.http import HTTP1Settings, HTTP2Settings
|
||||||
@@ -10,17 +8,17 @@ from typing import Optional, Sequence, Dict, Any
|
|||||||
from pwo import classproperty, Maybe
|
from pwo import classproperty, Maybe
|
||||||
from yaml import add_representer, SafeDumper, SafeLoader
|
from yaml import add_representer, SafeDumper, SafeLoader
|
||||||
|
|
||||||
def parse_log_config(conf_file=None) -> Dict[str, Any]:
|
|
||||||
if conf_file is None:
|
|
||||||
conf_file = environ.get("LOGGING_CONFIGURATION_FILE",
|
|
||||||
Path(__file__).parent / 'default-conf' / 'logging.yaml')
|
|
||||||
with open(conf_file, 'r') as file:
|
|
||||||
return yaml.safe_load(file)
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class Configuration:
|
class Configuration:
|
||||||
|
logging_configuration_file: str = environ.get("LOGGING_CONFIGURATION_FILE",
|
||||||
|
Path(__file__).parent / 'default-conf' / 'logging.yaml')
|
||||||
plant_uml_server_address: str = environ.get('PLANT_UML_SERVER_ADDRESS', None)
|
plant_uml_server_address: str = environ.get('PLANT_UML_SERVER_ADDRESS', None)
|
||||||
|
|
||||||
|
@classproperty
|
||||||
|
def instance(cls) -> 'Configuration':
|
||||||
|
return Configuration()
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class GranianConfiguration:
|
class GranianConfiguration:
|
||||||
address: str = '127.0.0.1'
|
address: str = '127.0.0.1'
|
||||||
@@ -79,7 +77,7 @@ class Configuration:
|
|||||||
http2_settings=Maybe.of_nullable(d.get('http2_settings')).map(lambda it: HTTP2Settings(**it)).or_else(None),
|
http2_settings=Maybe.of_nullable(d.get('http2_settings')).map(lambda it: HTTP2Settings(**it)).or_else(None),
|
||||||
log_enabled=d.get('log_enabled', None),
|
log_enabled=d.get('log_enabled', None),
|
||||||
log_level=Maybe.of_nullable(d.get('log_level')).map(lambda it: LogLevels(it)).or_else(None),
|
log_level=Maybe.of_nullable(d.get('log_level')).map(lambda it: LogLevels(it)).or_else(None),
|
||||||
log_dictconfig=parse_log_config(d.get('log_config_file')),
|
# log_dictconfig: Optional[Dict[str, Any]] = None,
|
||||||
log_access=d.get('log_access', None),
|
log_access=d.get('log_access', None),
|
||||||
log_access_format=d.get('log_access_format', None),
|
log_access_format=d.get('log_access_format', None),
|
||||||
ssl_cert=d.get('ssl_cert', None),
|
ssl_cert=d.get('ssl_cert', None),
|
||||||
@@ -140,5 +138,3 @@ class Configuration:
|
|||||||
return Configuration.from_dict(conf)
|
return Configuration.from_dict(conf)
|
||||||
finally:
|
finally:
|
||||||
loader.dispose()
|
loader.dispose()
|
||||||
|
|
||||||
instance = Configuration()
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
version: 1
|
version: 1
|
||||||
disable_existing_loggers: False
|
disable_existing_loggers: True
|
||||||
handlers:
|
handlers:
|
||||||
console:
|
console:
|
||||||
class : logging.StreamHandler
|
class : logging.StreamHandler
|
||||||
@@ -8,23 +8,28 @@ handlers:
|
|||||||
stream : ext://sys.stderr
|
stream : ext://sys.stderr
|
||||||
access:
|
access:
|
||||||
class : logging.StreamHandler
|
class : logging.StreamHandler
|
||||||
formatter: access
|
formatter: request
|
||||||
level : INFO
|
level : INFO
|
||||||
stream : ext://sys.stdout
|
stream : ext://sys.stderr
|
||||||
formatters:
|
formatters:
|
||||||
|
brief:
|
||||||
|
format: '%(message)s'
|
||||||
default:
|
default:
|
||||||
format: '{asctime}.{msecs:0<3.0f} [{levelname}] ({processName:s}/{threadName:s}) - {name} - {message}'
|
format: '{asctime} [{levelname}] ({processName:s}/{threadName:s}) - {name} - {message}'
|
||||||
|
style: '{'
|
||||||
|
datefmt: '%Y-%m-%d %H:%M:%S'
|
||||||
|
request:
|
||||||
|
format: '{asctime} {client[0]}:{client[1]} HTTP/{http_version} {method} {path} - {status}'
|
||||||
style: '{'
|
style: '{'
|
||||||
datefmt: '%Y-%m-%d %H:%M:%S'
|
datefmt: '%Y-%m-%d %H:%M:%S'
|
||||||
access:
|
|
||||||
format: '%(message)s'
|
|
||||||
loggers:
|
loggers:
|
||||||
root:
|
root:
|
||||||
handlers: [console]
|
handlers: [console]
|
||||||
_granian:
|
level: DEBUG
|
||||||
|
access:
|
||||||
|
handlers: [access]
|
||||||
level: INFO
|
level: INFO
|
||||||
propagate: False
|
watchdog.observers.inotify_buffer:
|
||||||
granian.access:
|
|
||||||
handlers: [ access ]
|
|
||||||
level: INFO
|
level: INFO
|
||||||
propagate: False
|
MARKDOWN:
|
||||||
|
level: INFO
|
||||||
@@ -1,19 +1,17 @@
|
|||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from aiofiles import open as async_open
|
from aiofiles import open as async_open
|
||||||
|
from aiohttp import ClientSession
|
||||||
from .configuration import Configuration
|
from .configuration import Configuration
|
||||||
|
from yarl import URL
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from _typeshed import StrOrBytesPath
|
from _typeshed import StrOrBytesPath
|
||||||
from httpx import AsyncClient, URL
|
|
||||||
from typing import Callable, Awaitable
|
|
||||||
from urllib.parse import urljoin
|
|
||||||
|
|
||||||
chunk_size = 0x10000
|
async def render_plant_uml(path: 'StrOrBytesPath') -> bytes:
|
||||||
async def render_plant_uml(client: AsyncClient, path: 'StrOrBytesPath', send : Callable[[bytes], Awaitable[None]]):
|
async with ClientSession() as session:
|
||||||
url = URL(urljoin(Configuration.instance.plant_uml_server_address, 'svg'))
|
url = URL(Configuration.instance.plant_uml_server_address) / 'svg'
|
||||||
async with async_open(path, 'rb') as file:
|
async with async_open(path, 'rb') as file:
|
||||||
source = await file.read()
|
source = await file.read()
|
||||||
response = await client.post(url, content=source)
|
async with session.post(url, data=source) as response:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
async for chunk in response.aiter_bytes(chunk_size=chunk_size):
|
return await response.read()
|
||||||
await send(chunk)
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from io import BytesIO
|
|||||||
from mimetypes import init as mimeinit, guess_type
|
from mimetypes import init as mimeinit, guess_type
|
||||||
from os import getcwd
|
from os import getcwd
|
||||||
from os.path import join, normpath, splitext, relpath, basename
|
from os.path import join, normpath, splitext, relpath, basename
|
||||||
from typing import Callable, TYPE_CHECKING, Optional, Awaitable, AsyncGenerator, Any, Mapping
|
from typing import Callable, TYPE_CHECKING, Optional, Awaitable, AsyncGenerator, Any
|
||||||
|
|
||||||
import pygraphviz as pgv
|
import pygraphviz as pgv
|
||||||
from aiofiles import open as async_open
|
from aiofiles import open as async_open
|
||||||
@@ -14,12 +14,10 @@ from aiofiles.base import AiofilesContextManager
|
|||||||
from aiofiles.os import listdir
|
from aiofiles.os import listdir
|
||||||
from aiofiles.ospath import exists, isdir, isfile, getmtime
|
from aiofiles.ospath import exists, isdir, isfile, getmtime
|
||||||
from aiofiles.threadpool.binary import AsyncBufferedReader
|
from aiofiles.threadpool.binary import AsyncBufferedReader
|
||||||
from httpx import AsyncClient
|
|
||||||
from pwo import Maybe
|
from pwo import Maybe
|
||||||
|
|
||||||
from .asgi_utils import encode_headers
|
from .asgi_utils import encode_headers
|
||||||
from .async_watchdog import FileWatcher
|
from .async_watchdog import FileWatcher
|
||||||
from .configuration import Configuration
|
|
||||||
from .md2html import compile_html, load_from_cache, STATIC_RESOURCES, MARDOWN_EXTENSIONS
|
from .md2html import compile_html, load_from_cache, STATIC_RESOURCES, MARDOWN_EXTENSIONS
|
||||||
from .plantuml import render_plant_uml
|
from .plantuml import render_plant_uml
|
||||||
|
|
||||||
@@ -58,10 +56,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
class Server:
|
class Server:
|
||||||
root_dir: 'StrOrBytesPath'
|
|
||||||
prefix: Optional['StrOrBytesPath']
|
|
||||||
_loop: AbstractEventLoop
|
_loop: AbstractEventLoop
|
||||||
_client: AsyncClient
|
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
root_dir: 'StrOrBytesPath' = getcwd(),
|
root_dir: 'StrOrBytesPath' = getcwd(),
|
||||||
@@ -72,16 +67,13 @@ class Server:
|
|||||||
self.file_watcher = FileWatcher(cwd)
|
self.file_watcher = FileWatcher(cwd)
|
||||||
self.prefix = prefix and normpath(f'{prefix.decode()}')
|
self.prefix = prefix and normpath(f'{prefix.decode()}')
|
||||||
self._loop = loop
|
self._loop = loop
|
||||||
self._client = AsyncClient()
|
|
||||||
|
|
||||||
async def handle_request(self,
|
async def handle_request(self,
|
||||||
method: str,
|
method: str,
|
||||||
url_path: str,
|
url_path: str,
|
||||||
etag: Optional[str],
|
etag: Optional[str],
|
||||||
query_string: Optional[str],
|
query_string: Optional[str], send,
|
||||||
send: Callable[[Mapping[str, Any]], Awaitable[None]],
|
pathsend: bool = False):
|
||||||
pathsend: bool = False
|
|
||||||
):
|
|
||||||
if method != 'GET':
|
if method != 'GET':
|
||||||
await send({
|
await send({
|
||||||
'type': 'http.response.start',
|
'type': 'http.response.start',
|
||||||
@@ -180,8 +172,9 @@ class Server:
|
|||||||
'type': 'http.response.body',
|
'type': 'http.response.body',
|
||||||
'body': body
|
'body': body
|
||||||
})
|
})
|
||||||
elif Configuration.instance.plant_uml_server_address and is_plant_uml(path):
|
elif is_plant_uml(path):
|
||||||
logger.debug("Starting PlantUML rendering for file '%s'", path)
|
logger.debug("Starting PlantUML rendering for file '%s'", path)
|
||||||
|
body = await render_plant_uml(path)
|
||||||
logger.debug("Completed PlantUML rendering for file '%s'", path)
|
logger.debug("Completed PlantUML rendering for file '%s'", path)
|
||||||
await send({
|
await send({
|
||||||
'type': 'http.response.start',
|
'type': 'http.response.start',
|
||||||
@@ -192,15 +185,9 @@ class Server:
|
|||||||
'Cache-Control': 'no-cache'
|
'Cache-Control': 'no-cache'
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
await render_plant_uml(self._client, path, lambda chunk: send({
|
|
||||||
'type': 'http.response.body',
|
|
||||||
'body': chunk,
|
|
||||||
'more_body': True
|
|
||||||
}))
|
|
||||||
await send({
|
await send({
|
||||||
'type': 'http.response.body',
|
'type': 'http.response.body',
|
||||||
'body': '',
|
'body': body
|
||||||
'more_body': False
|
|
||||||
})
|
})
|
||||||
else:
|
else:
|
||||||
async def read_file(file_path, buffer_size=0x10000):
|
async def read_file(file_path, buffer_size=0x10000):
|
||||||
@@ -405,4 +392,3 @@ class Server:
|
|||||||
|
|
||||||
async def stop(self):
|
async def stop(self):
|
||||||
await self.file_watcher.stop()
|
await self.file_watcher.stop()
|
||||||
await self._client.aclose()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user