From 7f85101b3cfece04743cd681c34d9d2ed3506d3f Mon Sep 17 00:00:00 2001 From: Douwe van der Meij Date: Sun, 28 Apr 2024 09:24:22 +0200 Subject: [PATCH] remove gcp/cloudstorage, sentry fixes, tox changes, github build changes --- .github/workflows/build.yml | 6 +- Makefile | 5 +- fractal/__init__.py | 2 +- fractal/contrib/fastapi/install.py | 25 +++++--- fractal/contrib/fastapi/routers/default.py | 8 ++- fractal/contrib/fastapi/routers/tokens.py | 2 +- fractal/contrib/gcp/cloudstorage/__init__.py | 0 .../contrib/gcp/cloudstorage/repositories.py | 64 ------------------- .../contrib/gcp/cloudstorage/requirements.txt | 1 - fractal/core/utils/application_context.py | 32 ++++++---- pyproject.toml | 2 +- tox.ini | 5 +- 12 files changed, 53 insertions(+), 99 deletions(-) delete mode 100644 fractal/contrib/gcp/cloudstorage/__init__.py delete mode 100644 fractal/contrib/gcp/cloudstorage/repositories.py delete mode 100644 fractal/contrib/gcp/cloudstorage/requirements.txt diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6fb2069..f7c6252 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.9] + python-version: ["3.8", "3.9", "3.10", "3.11"] env: USING_COVERAGE: "3.9" @@ -35,7 +35,7 @@ jobs: make tox - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1 + uses: codecov/codecov-action@v3 if: contains(env.USING_COVERAGE, matrix.python-version) with: - fail_ci_if_error: true + fail_ci_if_error: false diff --git a/Makefile b/Makefile index bfc09dc..9e1c378 100644 --- a/Makefile +++ b/Makefile @@ -7,9 +7,8 @@ coverage: ## Run tests with coverage pytest --cov fractal --cov-report=xml deps: ## Install dependencies - python -m pip install --upgrade pip - python -m pip install autoflake black coverage django fastapi flake8 flit fractal-repositories fractal-roles fractal-specifications fractal-tokens httpx isort mccabe mypy pre-commit pylint pytest pytest-cov pytest-asyncio pytest-lazy-fixture python-dotenv python-jose requests 'sqlalchemy<2.0' tox tox-gh-actions - python -m pip install cryptography + python -m pip install -U pip + python -m pip install -U autoflake black coverage cryptography django fastapi flake8 flit fractal-repositories fractal-roles fractal-specifications fractal-tokens httpx isort mccabe mypy pre-commit pylint 'pytest<7' pytest-cov pytest-asyncio pytest-lazy-fixture python-dotenv python-jose requests 'sqlalchemy<2.0' tox tox-gh-actions lint: ## Lint and static-check pre-commit run --all-files diff --git a/fractal/__init__.py b/fractal/__init__.py index 174de4e..2573224 100644 --- a/fractal/__init__.py +++ b/fractal/__init__.py @@ -1,6 +1,6 @@ """Fractal is a scaffolding toolkit for building SOLID logic for your Python applications.""" -__version__ = "4.1.3" +__version__ = "5.0.0" from abc import ABC diff --git a/fractal/contrib/fastapi/install.py b/fractal/contrib/fastapi/install.py index 46934dc..c52960a 100644 --- a/fractal/contrib/fastapi/install.py +++ b/fractal/contrib/fastapi/install.py @@ -22,10 +22,12 @@ def install_fastapi(settings: Settings): title=getattr(settings, "APP_NAME", "Fractal service"), version=getattr(settings, "APP_VERSION", "0.1.0"), root_path=getattr(settings, "OPENAPI_PREFIX_PATH", ""), - openapi_url="/openapi.json" - if str(getattr(settings, "APIDOCS_ENABLED", "true")).lower() - in ["1", "yes", "true"] - else None, + openapi_url=( + "/openapi.json" + if str(getattr(settings, "APIDOCS_ENABLED", "true")).lower() + in ["1", "yes", "true"] + else None + ), ) app.add_middleware( @@ -38,7 +40,12 @@ def install_fastapi(settings: Settings): if sentry: if sentry_dsn := getattr(settings, "SENTRY_DSN", ""): - sentry_sdk.init(dsn=sentry_dsn) + sentry_sdk.init( + dsn=sentry_dsn, + traces_sample_rate=getattr(settings, "SENTRY_TRACES_SAMPLE_RATE", 0.1), + send_default_pii=True, + enable_tracing=True, + ) @app.exception_handler(DomainException) def unicorn_domain_exception_handler(request: Request, exc: DomainException): @@ -48,8 +55,8 @@ def unicorn_domain_exception_handler(request: Request, exc: DomainException): else: logger.error(f"{exc.code} - {exc.message}") - if sentry: - sentry_sdk.capture_exception(exc) + # if sentry: + # sentry_sdk.capture_exception(exc) return JSONResponse( status_code=exc.status_code, @@ -64,8 +71,8 @@ def unicorn_domain_exception_handler(request: Request, exc: DomainException): def unicorn_exception_handler(request: Request, exc: Exception): logging.error(exc) - if sentry: - sentry_sdk.capture_exception(exc) + # if sentry: + # sentry_sdk.capture_exception(exc) return JSONResponse( status_code=500, diff --git a/fractal/contrib/fastapi/routers/default.py b/fractal/contrib/fastapi/routers/default.py index ee609a8..2e2c95a 100644 --- a/fractal/contrib/fastapi/routers/default.py +++ b/fractal/contrib/fastapi/routers/default.py @@ -25,9 +25,11 @@ def inject_default_routes(fractal: Fractal): @router.get(Routes.ROOT) def read_root(): return { - "FastAPI": fractal.settings.APP_NAME - if fractal.settings and hasattr(fractal.settings, "APP_NAME") - else "Fractal Service", + "FastAPI": ( + fractal.settings.APP_NAME + if fractal.settings and hasattr(fractal.settings, "APP_NAME") + else "Fractal Service" + ), } @router.get(Routes.INFO, responses={200: {"model": Info}}) diff --git a/fractal/contrib/fastapi/routers/tokens.py b/fractal/contrib/fastapi/routers/tokens.py index 43a4f80..9676dbb 100644 --- a/fractal/contrib/fastapi/routers/tokens.py +++ b/fractal/contrib/fastapi/routers/tokens.py @@ -19,7 +19,7 @@ @dataclass class TokenPayload(Model, BaseTokenPayload): - ... + pass @dataclass diff --git a/fractal/contrib/gcp/cloudstorage/__init__.py b/fractal/contrib/gcp/cloudstorage/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/fractal/contrib/gcp/cloudstorage/repositories.py b/fractal/contrib/gcp/cloudstorage/repositories.py deleted file mode 100644 index be2e208..0000000 --- a/fractal/contrib/gcp/cloudstorage/repositories.py +++ /dev/null @@ -1,64 +0,0 @@ -import uuid - -from google.cloud import storage - -from fractal import Settings -from fractal.contrib.gcp import SettingsMixin -from fractal.core.repositories import Entity, Repository - - -def get_cloudstorage_client(settings: Settings): - if not hasattr(settings, "cloudstorage_client"): - settings.cloudstorage_client = storage.Client() - return settings.cloudstorage_client - - -class CloudStorageRepositoryMixin(SettingsMixin, Repository[Entity]): - """ - https://github.com/googleapis/python-storage/tree/main/samples/snippets - """ - - entity = Entity - - def __init__(self, *args, **kwargs): - super(CloudStorageRepositoryMixin, self).__init__(*args, **kwargs) - - client = get_cloudstorage_client(self.settings) - if app_name := getattr(self.settings, "APP_NAME"): - self.bucket = client.bucket( - f"{app_name.lower()}-{self.entity.__name__.lower()}" - ) - else: - self.bucket = client.bucket(self.entity.__name__.lower()) - - def upload_file(self, data: bytes, content_type: str, reference: str = "") -> str: - # https://github.com/googleapis/python-storage/blob/main/samples/snippets/storage_upload_from_memory.py - if not reference: - reference = str(uuid.uuid4()) - blob = self.bucket.blob(reference) - blob.upload_from_string(data, content_type=content_type) - return reference - - def get_file(self, reference: str) -> bytes: - # https://github.com/googleapis/python-storage/blob/main/samples/snippets/storage_download_into_memory.py - blob = self.bucket.blob(reference) - return blob.download_as_string() - - def delete_file(self, reference: str) -> bool: - # https://github.com/googleapis/python-storage/blob/main/samples/snippets/storage_delete_file.py - blob = self.bucket.blob(reference) - blob.delete() - return True - - # def get_upload_url(self, reference: str) -> str: - # # https://cloud.google.com/storage/docs/access-control/signing-urls-with-helpers#storage-signed-url-object-python - # blob = self.bucket.blob(reference) - # - # return blob.generate_signed_url( - # version="v4", - # # This URL is valid for 15 minutes - # expiration=datetime.timedelta(minutes=15), - # # Allow PUT requests using this URL. - # method="PUT", - # content_type="application/octet-stream", - # ) diff --git a/fractal/contrib/gcp/cloudstorage/requirements.txt b/fractal/contrib/gcp/cloudstorage/requirements.txt deleted file mode 100644 index b2535e6..0000000 --- a/fractal/contrib/gcp/cloudstorage/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -google-cloud-storage diff --git a/fractal/core/utils/application_context.py b/fractal/core/utils/application_context.py index 6a8b59e..8de9837 100644 --- a/fractal/core/utils/application_context.py +++ b/fractal/core/utils/application_context.py @@ -94,9 +94,11 @@ def load_internal_services(self): """Load services for internal use of the domain.""" for name, service in self.registered_internal_services: _service = self.install_service( - service(self.settings) - if isinstance(service, FunctionType) - else service, + ( + service(self.settings) + if isinstance(service, FunctionType) + else service + ), name=name, ) setattr(self, name, _service()) @@ -108,9 +110,11 @@ def load_repositories(self): self, name, self.install_repository( - repository(self.settings) - if isinstance(repository, FunctionType) - else repository, + ( + repository(self.settings) + if isinstance(repository, FunctionType) + else repository + ), name=name, ), ) @@ -172,9 +176,11 @@ def load_egress_services(self): """Load services to external interfaces that are initiated by this service (outbound)""" for name, service in self.registered_egress_services: _service = self.install_service( - service(self.settings) - if isinstance(service, FunctionType) - else service, + ( + service(self.settings) + if isinstance(service, FunctionType) + else service + ), name=name, ) setattr(self, name, _service()) @@ -238,9 +244,11 @@ def load_ingress_services(self): """Load services to external interfaces that are initiated by the external services (inbound)""" for name, service in self.registered_ingress_services: _service = self.install_service( - service(self.settings) - if isinstance(service, FunctionType) - else service, + ( + service(self.settings) + if isinstance(service, FunctionType) + else service + ), name=name, ) setattr(self, name, _service()) diff --git a/pyproject.toml b/pyproject.toml index af8cf32..3bf9a53 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "fractal-toolkit" -version = "4.1.3" +version = "5.0.0" description = "Fractal is a scaffolding toolkit for building SOLID logic for your Python applications." authors = ["Douwe van der Meij "] diff --git a/tox.ini b/tox.ini index 7325a4b..b05646c 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,9 @@ [gh-actions] python = + 3.8: py38 3.9: py39 + 3.10: py310 + 3.11: py311 [tox] isolated_build = True @@ -23,7 +26,7 @@ deps = mccabe mypy pylint - pytest + pytest<7 pytest-asyncio pytest-lazy-fixture python-jose