From a15140b3fc1ee6e31e6e0c750620998e86daecc2 Mon Sep 17 00:00:00 2001 From: Alw3ys Date: Sun, 11 Feb 2024 23:31:24 +0100 Subject: [PATCH] chore(release): 0.0.18a10 --- README.md | 24 +++++++------ dosei/__init__.py | 1 - dosei/integrations/__init__.py | 0 dosei/integrations/fastapi.py | 42 ----------------------- dosei/main.py | 40 --------------------- dosei_sdk/__init__.py | 1 + {dosei => dosei_sdk}/app.py | 3 +- {dosei => dosei_sdk}/importer.py | 0 dosei_sdk/main.py | 30 ++++++++++++++++ {dosei => dosei_sdk}/schemas/cron_jobs.py | 0 pyproject.toml | 11 +++--- tests/example_project/main.py | 2 +- tests/fastapi/dosei_config.py | 5 ++- tests/fastapi/test_fastapi.py | 7 ++-- tests/test_app.py | 4 +-- tests/test_middleware.py | 34 ------------------ 16 files changed, 57 insertions(+), 147 deletions(-) delete mode 100644 dosei/__init__.py delete mode 100644 dosei/integrations/__init__.py delete mode 100644 dosei/integrations/fastapi.py delete mode 100644 dosei/main.py create mode 100644 dosei_sdk/__init__.py rename {dosei => dosei_sdk}/app.py (95%) rename {dosei => dosei_sdk}/importer.py (100%) create mode 100644 dosei_sdk/main.py rename {dosei => dosei_sdk}/schemas/cron_jobs.py (100%) delete mode 100644 tests/test_middleware.py diff --git a/README.md b/README.md index 044d61b..547d106 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,11 @@ # Dosei Python SDK -`dosei` is the official Dosei SDK for Python +`dosei-sdk` is the official Dosei SDK for Python [![pypi version](https://img.shields.io/pypi/v/dosei.svg)](https://pypi.org/pypi/dosei/) -[![Downloads](https://static.pepy.tech/badge/dosei/week)](https://pypi.org/pypi/dosei/) -[![License: MIT](https://img.shields.io/badge/license-Apache--2.0-yellow)](https://www.apache.org/licenses/LICENSE-2.0) -[![Twitter](https://img.shields.io/twitter/url/https/x.com/dosei_ai.svg?style=social&label=Follow%20%40dosei_ai)](https://x.com/dosei_ai) -[![](https://dcbadge.vercel.app/api/server/BP5aUkhcAh?compact=true&style=flat)](https://discord.com/invite/BP5aUkhcAh) +[![](https://img.shields.io/discord/1144175748559683615?logo=discord&logoColor=7289DA&label=Discord)](https://discord.com/invite/BP5aUkhcAh) +[![X (formerly Twitter)](https://img.shields.io/twitter/follow/dosei_ai?style=flat&logo=x)](https://x.com/dosei_ai) +[![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-white)](https://www.apache.org/licenses/LICENSE-2.0) ## Getting Started @@ -15,17 +14,20 @@ - [Python 3.11.2](https://www.python.org/downloads/) ### Install -You can install and configure `dosei` using this command: +You can install and configure `dosei-sdk` using this command: + ```bash -pip install -U dosei +pip install -U dosei-sdk ``` ### Configure ```python -from fastapi import FastAPI -import dosei.integrations.fastapi as dosei_fastapi_integration +from dosei_sdk import Dosei + +dosei = Dosei() -app = FastAPI() -dosei_fastapi_integration.init(app) +@dosei.cron_job("* * * * *") +def cron_job(): + print("hello world!") ``` diff --git a/dosei/__init__.py b/dosei/__init__.py deleted file mode 100644 index e3f4506..0000000 --- a/dosei/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from dosei.app import Dosei diff --git a/dosei/integrations/__init__.py b/dosei/integrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/dosei/integrations/fastapi.py b/dosei/integrations/fastapi.py deleted file mode 100644 index 1a08035..0000000 --- a/dosei/integrations/fastapi.py +++ /dev/null @@ -1,42 +0,0 @@ -from fastapi import FastAPI - - -def get_fastapi_app(app): - while hasattr(app, "app"): - app = app.app - return app - - -class FastAPIMiddleware: - _self = None - _initialized = False - - def __new__(cls, app): - if cls._self is None: - cls._self = super(FastAPIMiddleware, cls).__new__(cls) - return cls._self - - def __init__(self, app): - if self._initialized: - return - self.app = get_fastapi_app(app) - self.deprecated_endpoints = [route.path for route in self.app.routes if getattr(route, 'deprecated', False)] - self._initialized = True - - async def __call__(self, scope, receive, send): - if scope['type'] == 'http': - method = scope.get("method") - path = scope.get("path") - if path in self.deprecated_endpoints: - print(f"Deprecated endpoint accessed: {method} {path}") - - await self.app(scope, receive, send) - - -def init(app: FastAPI): - # TODO: This doesn't work, is breaking tests somehow, implement somewhere in the future - # for app in gc.get_objects(): - # if isinstance(app, FastAPI): - # app.add_middleware(FastAPIMiddleware) - # return - app.add_middleware(FastAPIMiddleware) diff --git a/dosei/main.py b/dosei/main.py deleted file mode 100644 index 66c0603..0000000 --- a/dosei/main.py +++ /dev/null @@ -1,40 +0,0 @@ -import os -import sys - -from dosei import Dosei -from dosei.importer import import_from_string, ImportFromStringError - -dosei_config_init = "dosei_config:dosei" - -# Add current dir to PYTHONPATH -sys.path.insert(0, "") - - -def run(func): - if func: - return Dosei.call_func(import_from_string(func)) - try: - app: Dosei = import_from_string(dosei_config_init) - except ImportFromStringError: - raise ImportFromStringError(f"Couldn't find a dosei_config.py file in \"{os.getcwd()}\"") - if app.command is None: - raise ImportFromStringError('Command "run" not found.') - os.system(app.command) - - -def dev(): - try: - app: Dosei = import_from_string(dosei_config_init) - except ImportFromStringError: - raise ImportFromStringError(f"Couldn't find a dosei_config.py file in \"{os.getcwd()}\"") - if app.dev is None: - raise ImportFromStringError('Command "dev" not found.') - os.system(app.dev) - - -def export(): - try: - app: Dosei = import_from_string(dosei_config_init) - return app.export() - except ImportFromStringError: - raise ImportFromStringError(f"Couldn't find a dosei_config.py file in \"{os.getcwd()}\"") diff --git a/dosei_sdk/__init__.py b/dosei_sdk/__init__.py new file mode 100644 index 0000000..ef53f90 --- /dev/null +++ b/dosei_sdk/__init__.py @@ -0,0 +1 @@ +from dosei_sdk.app import Dosei diff --git a/dosei/app.py b/dosei_sdk/app.py similarity index 95% rename from dosei/app.py rename to dosei_sdk/app.py index 299d05c..b7dec49 100644 --- a/dosei/app.py +++ b/dosei_sdk/app.py @@ -29,8 +29,7 @@ class Dosei(BaseModel): _app_export_path: str = ".dosei/app.json" name: Optional[str] = None - command: Optional[str] = None - dev: Optional[str] = None + run: Optional[str] = None port: Optional[int] = None cron_jobs: Optional[List[CronJob]] = [] diff --git a/dosei/importer.py b/dosei_sdk/importer.py similarity index 100% rename from dosei/importer.py rename to dosei_sdk/importer.py diff --git a/dosei_sdk/main.py b/dosei_sdk/main.py new file mode 100644 index 0000000..3f60c61 --- /dev/null +++ b/dosei_sdk/main.py @@ -0,0 +1,30 @@ +import os +import sys + +from dosei_sdk import Dosei +from dosei_sdk.importer import import_from_string, ImportFromStringError + +dosei_init = "dosei:dosei" + +# Add current dir to PYTHONPATH +sys.path.insert(0, "") + + +def run(func): + if func: + return Dosei.call_func(import_from_string(func)) + try: + app: Dosei = import_from_string(dosei_init) + except ImportFromStringError: + raise ImportFromStringError(f"Couldn't find a dosei.py file in \"{os.getcwd()}\"") + if app.command is None: + raise ImportFromStringError('Command "run" not found.') + os.system(app.run) + + +def export(): + try: + app: Dosei = import_from_string(dosei_init) + return app.export() + except ImportFromStringError: + raise ImportFromStringError(f"Couldn't find a dosei.py file in \"{os.getcwd()}\"") diff --git a/dosei/schemas/cron_jobs.py b/dosei_sdk/schemas/cron_jobs.py similarity index 100% rename from dosei/schemas/cron_jobs.py rename to dosei_sdk/schemas/cron_jobs.py diff --git a/pyproject.toml b/pyproject.toml index c900803..56f8995 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,23 +1,20 @@ [tool.poetry] -name = "dosei" -version = "0.0.18a9" +name = "dosei-sdk" +version = "0.0.18a10" description = "Dosei Python SDK" authors = ["Alvaro Molina "] license = "Apache-2.0" readme = "README.md" -[tool.poetry.scripts] -dosei = 'dosei.main:cli' - [tool.poetry.dependencies] python = "^3.11" -fastapi = "^0.109.2" -pytest-trio = "^0.8.0" croniter = "^2.0.1" pydantic = "^2.6.1" [tool.poetry.group.dev.dependencies] +pytest-trio = "^0.8.0" +fastapi = "^0.109.2" pytest = "^7.4.3" httpx = "^0.25.0" diff --git a/tests/example_project/main.py b/tests/example_project/main.py index 78bb193..44ceb36 100644 --- a/tests/example_project/main.py +++ b/tests/example_project/main.py @@ -1,4 +1,4 @@ -from dosei import Dosei +from dosei_sdk import Dosei dosei = Dosei() diff --git a/tests/fastapi/dosei_config.py b/tests/fastapi/dosei_config.py index d34689e..6d61a2b 100644 --- a/tests/fastapi/dosei_config.py +++ b/tests/fastapi/dosei_config.py @@ -1,10 +1,9 @@ import os -from dosei import Dosei +from dosei_sdk import Dosei port = os.getenv('PORT', 8080) dosei = Dosei( name="hello-world", - command=f"uvicorn helloworld.main:app --host 0.0.0.0 --port {port}", - dev=f"uvicorn helloworld.main:app --port {port} --reload", + run=f"uvicorn helloworld.main:app --host 0.0.0.0 --port {port}", port=port ) diff --git a/tests/fastapi/test_fastapi.py b/tests/fastapi/test_fastapi.py index 7b60f42..9b963a0 100644 --- a/tests/fastapi/test_fastapi.py +++ b/tests/fastapi/test_fastapi.py @@ -1,10 +1,9 @@ -from dosei import Dosei -from dosei.importer import import_from_string +from dosei_sdk import Dosei +from dosei_sdk.importer import import_from_string def test_fastapi(): app: Dosei = import_from_string("tests.fastapi.dosei_config:dosei") assert app.name == "hello-world" assert app.port == 8080 - assert app.command == f"uvicorn helloworld.main:app --host 0.0.0.0 --port {app.port}" - assert app.dev == f"uvicorn helloworld.main:app --port {app.port} --reload" + assert app.run == f"uvicorn helloworld.main:app --host 0.0.0.0 --port {app.port}" diff --git a/tests/test_app.py b/tests/test_app.py index 2d75934..de5a594 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,7 +1,7 @@ -from dosei import Dosei +from dosei_sdk import Dosei import pytest -from dosei.importer import import_from_string +from dosei_sdk.importer import import_from_string dosei = Dosei() diff --git a/tests/test_middleware.py b/tests/test_middleware.py deleted file mode 100644 index 0357168..0000000 --- a/tests/test_middleware.py +++ /dev/null @@ -1,34 +0,0 @@ -from fastapi import FastAPI, Response -from fastapi.testclient import TestClient -import dosei.integrations.fastapi as dosei_fastapi_integration - -app = FastAPI() -dosei_fastapi_integration.init(app) - -client = TestClient(app) - - -@app.get("/deprecated", deprecated=True) -def deprecated() -> Response: - return Response() - - -@app.get("/not-deprecated") -def not_deprecated() -> Response: - return Response() - - -def test_deprecated(): - response = client.get("/deprecated") - assert response.status_code == 200 - - instance = dosei_fastapi_integration.FastAPIMiddleware(app) - assert "/deprecated" in instance.deprecated_endpoints - - -def test_not_deprecated(): - response = client.get("/not-deprecated") - assert response.status_code == 200 - - instance = dosei_fastapi_integration.FastAPIMiddleware(app) - assert "/not-deprecated" not in instance.deprecated_endpoints