diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs index 6413d30a0..2349078d5 100644 --- a/.git-blame-ignore-revs +++ b/.git-blame-ignore-revs @@ -6,3 +6,5 @@ a81731039c118543398c90869e608dde0acaf32c af2c352786f952d819e69b928bf6c99dcde5954b # Compile requirements to update comments 5821434b42203b1080dcdc480d1ffcf07fe53a90 +# Format with Ruff +e72aba6d7e5891ac777f533eef6f7520410c9e0b diff --git a/Makefile b/Makefile index 0d51fe336..bb1c8cd56 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,6 @@ APP_BIN := $(ENV_DIR)/bin/promgen CELERY_BIN := $(ENV_DIR)/bin/celery SPHINX := $(ENV_DIR)/bin/sphinx-build RUFF_BIN := $(ENV_DIR)/bin/ruff -BLACK_BIN := $(ENV_DIR)/bin/black DOCKER_TAG := promgen:local SYSTEM_PYTHON ?= python3.9 @@ -144,13 +143,11 @@ docs: $(SPHINX) $(RUFF_BIN): $(PIP_BIN) $(PIP_BIN) install ruff -$(BLACK_BIN): $(PIP_BIN) - $(PIP_BIN) install black .PHONY: format -format: $(RUFF_BIN) $(BLACK_BIN) +format: $(RUFF_BIN) $(RUFF_BIN) check promgen --fix - $(BLACK_BIN) promgen + $(RUFF_BIN) format promgen #### Other assorted commands diff --git a/promgen/celery.py b/promgen/celery.py index bda7fd434..40ebd83a9 100644 --- a/promgen/celery.py +++ b/promgen/celery.py @@ -6,10 +6,8 @@ import socket import celery - from celery.signals import celeryd_after_setup - logger = logging.getLogger(__name__) diff --git a/promgen/context_processors.py b/promgen/context_processors.py index af6259ce8..b841e3d7b 100644 --- a/promgen/context_processors.py +++ b/promgen/context_processors.py @@ -1,10 +1,10 @@ # Copyright (c) 2017 LINE Corporation # These sources are released under the terms of the MIT license: see LICENSE -from promgen import models, util - from django.conf import settings +from promgen import models, util + def settings_in_view(request): return { diff --git a/promgen/discovery/default.py b/promgen/discovery/default.py index 7e2256c05..412455862 100644 --- a/promgen/discovery/default.py +++ b/promgen/discovery/default.py @@ -5,8 +5,7 @@ from django.shortcuts import get_object_or_404 -from promgen import models -from promgen import discovery +from promgen import discovery, models logger = logging.getLogger(__name__) diff --git a/promgen/errors.py b/promgen/errors.py index a6795d0c1..64631329f 100644 --- a/promgen/errors.py +++ b/promgen/errors.py @@ -1,6 +1,7 @@ from enum import Enum -from django.utils.translation import gettext as _ + from django.core.exceptions import ValidationError +from django.utils.translation import gettext as _ class ValidationErrorEnum(Enum): diff --git a/promgen/forms.py b/promgen/forms.py index 993e47f5f..2479debd3 100644 --- a/promgen/forms.py +++ b/promgen/forms.py @@ -8,7 +8,7 @@ from django import forms from django.core.exceptions import ValidationError -from promgen import models, plugins, prometheus, validators, errors +from promgen import errors, models, plugins, prometheus, validators class ImportConfigForm(forms.Form): diff --git a/promgen/management/commands/import-probe.py b/promgen/management/commands/import-probe.py index e53d399ba..8fae1ee63 100644 --- a/promgen/management/commands/import-probe.py +++ b/promgen/management/commands/import-probe.py @@ -6,7 +6,6 @@ import sys import yaml - from django.core import exceptions from django.core.management.base import BaseCommand diff --git a/promgen/migrations/0001_squashed_0044_common-rules.py b/promgen/migrations/0001_squashed_0044_common-rules.py index 5f7d377c2..fcf4013c0 100644 --- a/promgen/migrations/0001_squashed_0044_common-rules.py +++ b/promgen/migrations/0001_squashed_0044_common-rules.py @@ -1,10 +1,11 @@ # Generated by Django 1.11.10 on 2018-02-07 06:56 -from django.conf import settings import django.contrib.sites.models import django.core.validators -from django.db import migrations, models import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + import promgen.models import promgen.validators diff --git a/promgen/migrations/0003_alert_queue.py b/promgen/migrations/0003_alert_queue.py index c8f052761..19c89f3b2 100644 --- a/promgen/migrations/0003_alert_queue.py +++ b/promgen/migrations/0003_alert_queue.py @@ -1,8 +1,8 @@ # Generated by Django 2.0.7 on 2018-07-26 08:15 -from django.db import migrations, models import django.db.models.deletion import django.utils.timezone +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/promgen/migrations/0003_default-group.py b/promgen/migrations/0003_default-group.py index 2d5ebe936..2eff59d7a 100644 --- a/promgen/migrations/0003_default-group.py +++ b/promgen/migrations/0003_default-group.py @@ -1,7 +1,7 @@ # Generated by Django 2.0.7 on 2018-07-31 08:09 -from django.db import migrations from django.conf import settings +from django.db import migrations def create_group(apps, schema_editor): diff --git a/promgen/migrations/0005_project_owner.py b/promgen/migrations/0005_project_owner.py index 8f1c66494..51ffd3665 100644 --- a/promgen/migrations/0005_project_owner.py +++ b/promgen/migrations/0005_project_owner.py @@ -1,8 +1,8 @@ # Generated by Django 2.1.2 on 2018-11-16 02:23 +import django.db.models.deletion from django.conf import settings from django.db import migrations, models -import django.db.models.deletion class Migration(migrations.Migration): diff --git a/promgen/migrations/0007_message_filter.py b/promgen/migrations/0007_message_filter.py index 3241835e5..6f9d69af8 100644 --- a/promgen/migrations/0007_message_filter.py +++ b/promgen/migrations/0007_message_filter.py @@ -1,7 +1,7 @@ # Generated by Django 2.1.2 on 2019-03-06 06:20 -from django.db import migrations, models import django.db.models.deletion +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/promgen/migrations/0008_shard_service.py b/promgen/migrations/0008_shard_service.py index edf751578..87cc8d086 100644 --- a/promgen/migrations/0008_shard_service.py +++ b/promgen/migrations/0008_shard_service.py @@ -1,7 +1,7 @@ # Generated by Django 2.1.8 on 2019-04-22 07:15 -from django.db import migrations, models import django.db.models.deletion +from django.db import migrations, models def project_to_shard(apps, schema_editor): diff --git a/promgen/migrations/0009_blackbox-module.py b/promgen/migrations/0009_blackbox-module.py index 45f9ccc02..92013b32b 100644 --- a/promgen/migrations/0009_blackbox-module.py +++ b/promgen/migrations/0009_blackbox-module.py @@ -1,7 +1,7 @@ # Generated by Django 2.1.8 on 2019-05-31 04:46 -from django.db import migrations, models import django.db.models.deletion +from django.db import migrations, models def default_probe(apps, schema_editor): diff --git a/promgen/migrations/0011_notifier_counts.py b/promgen/migrations/0011_notifier_counts.py index fbf5fe106..441539766 100644 --- a/promgen/migrations/0011_notifier_counts.py +++ b/promgen/migrations/0011_notifier_counts.py @@ -1,7 +1,7 @@ # Generated by Django 2.2.4 on 2019-11-28 02:21 -from django.db import migrations, models import django.db.models.deletion +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/promgen/migrations/0012_validation_updates.py b/promgen/migrations/0012_validation_updates.py index 25f63ae06..88898ffde 100644 --- a/promgen/migrations/0012_validation_updates.py +++ b/promgen/migrations/0012_validation_updates.py @@ -3,8 +3,8 @@ # Generated by Django 2.2.9 on 2020-01-16 06:35 import django.core.validators -from django.db import migrations, models import django.db.models.deletion +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/promgen/migrations/0015_internal.py b/promgen/migrations/0015_internal.py index 4dcd90fbb..6727f0d41 100644 --- a/promgen/migrations/0015_internal.py +++ b/promgen/migrations/0015_internal.py @@ -1,7 +1,7 @@ # Generated by Django 2.2.10 on 2020-02-21 05:56 -from django.db import migrations, models import django.db.models.deletion +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/promgen/migrations/0016_alertlabel.py b/promgen/migrations/0016_alertlabel.py index 21b61d83b..f09ba1277 100644 --- a/promgen/migrations/0016_alertlabel.py +++ b/promgen/migrations/0016_alertlabel.py @@ -1,7 +1,7 @@ # Generated by Django 2.2.8 on 2020-02-21 06:03 -from django.db import migrations, models import django.db.models.deletion +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/promgen/notification/user.py b/promgen/notification/user.py index eddae1fd4..c68634fb4 100644 --- a/promgen/notification/user.py +++ b/promgen/notification/user.py @@ -5,6 +5,7 @@ from django import forms from django.contrib.auth.models import User + from promgen import models from promgen.notification import NotificationBase diff --git a/promgen/prometheus.py b/promgen/prometheus.py index 144826309..5fd5b1984 100644 --- a/promgen/prometheus.py +++ b/promgen/prometheus.py @@ -11,7 +11,6 @@ import pytz import yaml from dateutil import parser - from django.core.exceptions import ValidationError from django.utils import timezone diff --git a/promgen/proxy.py b/promgen/proxy.py index c08c3fa59..3ace74422 100644 --- a/promgen/proxy.py +++ b/promgen/proxy.py @@ -11,9 +11,10 @@ from django.http import HttpResponse, JsonResponse from django.views.generic import View from django.views.generic.base import TemplateView -from promgen import forms, models, prometheus, util from requests.exceptions import HTTPError +from promgen import forms, models, prometheus, util + logger = logging.getLogger(__name__) @@ -213,7 +214,7 @@ def post(self, request): return JsonResponse( { "messages": [ - {"class": "alert alert-danger", "message": e.response.text} + {"class": "alert alert-danger", "message": e.response.text}, ] }, status=e.response.status_code, diff --git a/promgen/rest.py b/promgen/rest.py index 623c0afe2..fd0e368d7 100644 --- a/promgen/rest.py +++ b/promgen/rest.py @@ -1,13 +1,12 @@ # Copyright (c) 2019 LINE Corporation # These sources are released under the terms of the MIT license: see LICENSE +from django.http import HttpResponse +from django.views.generic import View from rest_framework import permissions, viewsets from rest_framework.decorators import action from rest_framework.response import Response -from django.http import HttpResponse -from django.views.generic import View - from promgen import filters, models, prometheus, renderers, serializers, tasks diff --git a/promgen/serializers.py b/promgen/serializers.py index 97bd4e927..7f32faa71 100644 --- a/promgen/serializers.py +++ b/promgen/serializers.py @@ -1,8 +1,7 @@ import collections -from rest_framework import serializers - from django.db.models import prefetch_related_objects +from rest_framework import serializers import promgen.templatetags.promgen as macro from promgen import models, shortcuts diff --git a/promgen/settings.py b/promgen/settings.py index c4db32473..0374d4324 100644 --- a/promgen/settings.py +++ b/promgen/settings.py @@ -19,7 +19,6 @@ import environ import yaml - from django.urls import reverse_lazy from promgen import PROMGEN_CONFIG_FILE @@ -175,8 +174,8 @@ if "SENTRY_DSN" in os.environ: import sentry_sdk - from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.celery import CeleryIntegration + from sentry_sdk.integrations.django import DjangoIntegration os.environ.setdefault("SENTRY_RELEASE", PROMGEN_VERSION) # By default we want to keep sentry's default of NOT sending user diff --git a/promgen/shortcuts.py b/promgen/shortcuts.py index ef4c60555..8c76e9f3e 100644 --- a/promgen/shortcuts.py +++ b/promgen/shortcuts.py @@ -5,6 +5,7 @@ from django.conf import settings from django.shortcuts import resolve_url + from promgen import models diff --git a/promgen/signals.py b/promgen/signals.py index 0de0f69da..d21947cbf 100644 --- a/promgen/signals.py +++ b/promgen/signals.py @@ -10,6 +10,7 @@ from django.core.cache import cache from django.db.models.signals import post_delete, post_save, pre_delete, pre_save from django.dispatch import Signal, receiver + from promgen import models, prometheus, tasks logger = logging.getLogger(__name__) diff --git a/promgen/tasks.py b/promgen/tasks.py index fb72ef5b5..9c03f4709 100644 --- a/promgen/tasks.py +++ b/promgen/tasks.py @@ -9,7 +9,7 @@ from celery import shared_task from requests.exceptions import RequestException -from promgen import models, prometheus, util, notification +from promgen import models, notification, prometheus, util logger = logging.getLogger(__name__) diff --git a/promgen/templatetags/promgen.py b/promgen/templatetags/promgen.py index 59f9414e2..fadd9f41e 100644 --- a/promgen/templatetags/promgen.py +++ b/promgen/templatetags/promgen.py @@ -8,13 +8,12 @@ from urllib.parse import urlencode import yaml -from pytz import timezone - from django import template from django.urls import reverse from django.utils.html import format_html from django.utils.safestring import mark_safe from django.utils.translation import ugettext as _ +from pytz import timezone from promgen import util diff --git a/promgen/tests/__init__.py b/promgen/tests/__init__.py index c5ec053c9..d66f81f79 100644 --- a/promgen/tests/__init__.py +++ b/promgen/tests/__init__.py @@ -2,12 +2,12 @@ # These sources are released under the terms of the MIT license: see LICENSE import json -from django.urls import reverse -import yaml +import yaml +from django.conf import settings from django.contrib.auth.models import Permission, User from django.test import TestCase -from django.conf import settings +from django.urls import reverse class Data: diff --git a/promgen/tests/notification/test_webhook.py b/promgen/tests/notification/test_webhook.py index 2d5914e9e..091fc03b6 100644 --- a/promgen/tests/notification/test_webhook.py +++ b/promgen/tests/notification/test_webhook.py @@ -3,9 +3,8 @@ from unittest import mock -from requests.exceptions import RequestException - from django.test import override_settings +from requests.exceptions import RequestException from promgen import models, rest, tests from promgen.notification.webhook import NotificationWebhook diff --git a/promgen/tests/test_alert_rules.py b/promgen/tests/test_alert_rules.py index bab402058..597360461 100644 --- a/promgen/tests/test_alert_rules.py +++ b/promgen/tests/test_alert_rules.py @@ -22,9 +22,7 @@ for: 1s labels: severity: high -""".lstrip().encode( - "utf-8" -) +""".lstrip().encode("utf-8") TEST_SETTINGS = tests.Data("examples", "promgen.yml").yaml() diff --git a/promgen/tests/test_routes.py b/promgen/tests/test_routes.py index dfa0befd8..84d19000b 100644 --- a/promgen/tests/test_routes.py +++ b/promgen/tests/test_routes.py @@ -3,7 +3,6 @@ from unittest import mock import requests - from django.test import override_settings from django.urls import reverse diff --git a/promgen/tests/test_silence.py b/promgen/tests/test_silence.py index 9a76ac034..fa94769a5 100644 --- a/promgen/tests/test_silence.py +++ b/promgen/tests/test_silence.py @@ -7,7 +7,7 @@ from django.test import override_settings from django.urls import reverse -from promgen import forms, tests, errors +from promgen import errors, forms, tests TEST_SETTINGS = tests.Data("examples", "promgen.yml").yaml() TEST_DURATION = tests.Data("examples", "silence.duration.json").json() @@ -41,9 +41,7 @@ def test_duration(self, mock_post): }, content_type="application/json", ) - mock_post.assert_called_with( - "http://alertmanager:9093/api/v2/silences", json=TEST_DURATION - ) + mock_post.assert_called_with("http://alertmanager:9093/api/v2/silences", json=TEST_DURATION) @override_settings(PROMGEN=TEST_SETTINGS) @mock.patch("promgen.util.post") @@ -62,9 +60,7 @@ def test_range(self, mock_post): content_type="application/json", ) - mock_post.assert_called_with( - "http://alertmanager:9093/api/v2/silences", json=TEST_RANGE - ) + mock_post.assert_called_with("http://alertmanager:9093/api/v2/silences", json=TEST_RANGE) @override_settings(PROMGEN=TEST_SETTINGS) def test_site_silence_errors(self): diff --git a/promgen/urls.py b/promgen/urls.py index eaeeed625..aec8bc576 100644 --- a/promgen/urls.py +++ b/promgen/urls.py @@ -16,12 +16,11 @@ 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ -from rest_framework import routers - from django.conf.urls import url from django.contrib import admin from django.urls import include, path from django.views.decorators.csrf import csrf_exempt +from rest_framework import routers from promgen import proxy, rest, views diff --git a/promgen/util.py b/promgen/util.py index f594f2862..20642d349 100644 --- a/promgen/util.py +++ b/promgen/util.py @@ -5,11 +5,9 @@ from urllib.parse import urlsplit import requests - from django.conf import settings from django.db.models import F - # Wrappers around request api to ensure we always attach our user agent # https://github.com/requests/requests/blob/master/requests/api.py diff --git a/promgen/validators.py b/promgen/validators.py index 7703f61e5..d1c4eaa1a 100644 --- a/promgen/validators.py +++ b/promgen/validators.py @@ -4,7 +4,6 @@ import re from dateutil import parser - from django.core.exceptions import ValidationError from django.core.validators import RegexValidator, URLValidator diff --git a/promgen/views.py b/promgen/views.py index a11d0caa9..32909654f 100644 --- a/promgen/views.py +++ b/promgen/views.py @@ -12,22 +12,6 @@ import prometheus_client import requests -from prometheus_client.core import CounterMetricFamily, GaugeMetricFamily - -import promgen.templatetags.promgen as macro -from promgen import ( - discovery, - forms, - mixins, - models, - plugins, - prometheus, - signals, - tasks, - util, -) -from promgen.shortcuts import resolve_domain - from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin @@ -43,6 +27,21 @@ from django.views.generic.base import RedirectView, TemplateView from django.views.generic.detail import SingleObjectMixin from django.views.generic.edit import CreateView, DeleteView, FormView +from prometheus_client.core import CounterMetricFamily, GaugeMetricFamily + +import promgen.templatetags.promgen as macro +from promgen import ( + discovery, + forms, + mixins, + models, + plugins, + prometheus, + signals, + tasks, + util, +) +from promgen.shortcuts import resolve_domain logger = logging.getLogger(__name__) diff --git a/pyproject.toml b/pyproject.toml index e62470332..2d6457ec5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,7 @@ Tracker = "https://github.com/line/promgen/issues" Documentation = "https://line.github.io/promgen/" [project.optional-dependencies] -dev = ["black", "codecov", "flake8", "unittest-xml-reporting"] +dev = ["codecov", "flake8", "unittest-xml-reporting"] docs = ["Sphinx", "sphinxcontrib-httpdomain"] mysql = ["mysqlclient"] @@ -61,25 +61,25 @@ slack = "promgen.notification.slack:NotificationSlack" user = "promgen.notification.user:NotificationUser" webhook = "promgen.notification.webhook:NotificationWebhook" -[tool.black] +[tool.ruff] line-length = 100 -target-version = ['py39'] -extend-exclude = ''' -( -promgen/migrations -|promgen/urls.py -) -''' -[tool.ruff] +[tool.ruff.lint] ignore = [ 'E741', # Ambiguous variable name - 'E501', # Let black handle line-length + 'E501', # We let the formatter fix line length but don't want to be warned +] + +select = ["E", "F", 'I'] + +[tool.ruff.format] +exclude = [ + "promgen/migrations/*", + "promgen/urls.py", ] -[tool.ruff.isort] +[tool.ruff.lint.isort] known-first-party = ["promgen"] -forced-separate = ["django"] [tool.codespell] skip = '*.min.js,*.min.css,*.css.map,.venv,dist,.git'