Skip to content

Commit

Permalink
Adjust code based on feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenepix committed Nov 24, 2024
1 parent 6beeb6e commit 043bb8e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
6 changes: 3 additions & 3 deletions backend/nango/management/commands/nango.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""CLI to run the Nango's bridge."""
"""CLI to run Nango's bridge."""

from __future__ import annotations

Expand All @@ -10,8 +10,8 @@
class Command(BaseCommand):
"""Cli to manage the bridge."""

def add_arguments(self, parser: CommandParser) -> None:
"""."""
def add_arguments(self, parser: CommandParser) -> None: # noqa: D102
...

def handle(self, *args: list, **options: dict) -> None: # noqa: ARG002
"""Handle argument and run Nango's cogs."""
Expand Down
2 changes: 1 addition & 1 deletion backend/nango/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def setup_django() -> None:
"""Setup django environment."""
"""Setup Django's environment."""
import os
import sys
from pathlib import Path
Expand Down
39 changes: 19 additions & 20 deletions backend/nango/utils/cogs_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import TYPE_CHECKING

from django.apps import apps
from django.conf import settings

from nango import cogs

Expand All @@ -15,22 +16,29 @@
class CogsRunner:
"""Load and execute cogs.
For the order execution, follow the nango.cogs.__all__ order.
For execution order, follow the nango.cogs.__all__ order.
Settings:
--------
Nango's settings can be defined for each cog at the model level.
A model can have a staticmethod called `nango` that returns a None (for no generation) or a dict (with settings for each cogs).
"""

model_filter_keywords: list[str] = ("django", "django_celery_beat", "rest_framework")

def get_cogs_classes(self) -> list[Callable]:
def get_cog_classes(self) -> list[Callable]:
"""Return cogs's classes to execute."""
return [getattr(import_module("nango.cogs"), cog_str) for cog_str in cogs.__all__]

def get_local_model_classes(self) -> list[models.Model]:
"""Get all models for apps defined in settings.LOCAL_APPS."""
model_list: list[models.Model] = []

for app_name in settings.LOCAL_APPS:
app_config = apps.app_configs.get(app_name)
model_list.extend(app_config.get_models())
return model_list

def get_settings_from_model(self, model: models.Model) -> dict[str, any]:
"""Return settings define in a model."""
"""Return settings defined in a model."""
model_nango_method: Callable | None = getattr(model, "nango", None)

if not isinstance(model_nango_method, Callable):
Expand All @@ -43,27 +51,18 @@ def get_settings_from_model(self, model: models.Model) -> dict[str, any]:

return model_nango_method()

def _is_filtered_model(self, model: models.Model) -> bool:
"""Indicate if a model is filter or not.
If the model is filter, we don't run it.
"""
return any(keyword in str(model) for keyword in self.model_filter_keywords)

def run_cogs(self) -> None:
"""Run all existing cogs."""
cogs_classes = self.get_cogs_classes()

for model in apps.get_models():
if self._is_filtered_model(model):
continue

for model in self.get_local_model_classes():
for cog in cogs_classes:
settings = self.get_settings_from_model(model)
if settings is None:
# This model doesn't want any cogs.
model_nango_settings = self.get_settings_from_model(model)

# This model doesn't want any cogs.
if model_nango_settings is None:
continue

# Run the cogs
instantiated_cog = cog(model=model, settings=settings)
instantiated_cog = cog(model=model, settings=model_nango_settings)
instantiated_cog.run()

0 comments on commit 043bb8e

Please sign in to comment.