Skip to content

Commit

Permalink
[INTERNAL] Use ruff for formatting #434
Browse files Browse the repository at this point in the history
  • Loading branch information
kfdm authored Aug 6, 2023
2 parents bf7d0db + 186b87d commit d0c43fe
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 57 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
4d53038426aedf2abf337a2876d0d6ceccefc09b
# black
a81731039c118543398c90869e608dde0acaf32c
# ruff
af2c352786f952d819e69b928bf6c99dcde5954b
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ PIP_COMPILE := $(ENV_DIR)/bin/pip-compile
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
Expand Down Expand Up @@ -136,6 +138,17 @@ $(SPHINX): $(PIP_BIN)
docs: $(SPHINX)
$(SPHINX) -avb html docs dist/html

$(RUFF_BIN): $(PIP_BIN)
$(PIP_BIN) install ruff

$(BLACK_BIN): $(PIP_BIN)
$(PIP_BIN) install black

.PHONY: format
format: $(RUFF_BIN) $(BLACK_BIN)
$(RUFF_BIN) check promgen --fix
$(BLACK_BIN) promgen


#### Other assorted commands

Expand Down
4 changes: 2 additions & 2 deletions promgen/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# These sources are released under the terms of the MIT license: see LICENSE

import logging
import warnings

from django.apps import AppConfig
from django.db.models.signals import post_migrate
import warnings

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -57,7 +57,7 @@ class PromgenConfig(AppConfig):
default_auto_field = "django.db.models.AutoField"

def ready(self):
from promgen import checks, signals # NOQA
from promgen import celery, checks, signals # NOQA

post_migrate.connect(default_shard, sender=self)
post_migrate.connect(default_admin, sender=self)
Expand Down
4 changes: 2 additions & 2 deletions promgen/discovery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def fetch(self, farm):
"""
Return list of hosts for farm
"""
raise NotImplemented()
raise NotImplementedError()

def farms(self):
"""
Return a list of farm names
"""
raise NotImplemented()
raise NotImplementedError()
2 changes: 1 addition & 1 deletion promgen/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __call__(self, request):
}

for msg, func in triggers.items():
for (receiver, status) in func(self, request=request, force=True):
for receiver, status in func(self, request=request, force=True):
if status is False:
messages.warning(request, "Error queueing %s " % msg)
return response
Expand Down
1 change: 0 additions & 1 deletion promgen/migrations/0013_validation_fix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Generated by Django 2.2.10 on 2020-02-14 05:41

import re

import django.core.validators
import django.db.models.deletion
Expand Down
1 change: 0 additions & 1 deletion promgen/migrations/0014_exporter_scheme.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Generated by Django 2.2.10 on 2020-02-18 09:28

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):
Expand Down
2 changes: 1 addition & 1 deletion promgen/notification/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ def _send(self, address, data):
for sender in models.Sender.objects.filter(obj=user, enabled=True):
try:
sender.driver._send(sender.value, data)
except:
except Exception:
logger.exception("Error sending with %s", sender)
return True
1 change: 0 additions & 1 deletion promgen/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ def import_rules_v2(config, content_object=None):
counters = collections.defaultdict(int)
for group in config["groups"]:
for r in group["rules"]:

defaults = {
"clause": r["expr"],
"duration": r["for"],
Expand Down
3 changes: 0 additions & 3 deletions promgen/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
from urllib.parse import urljoin

import requests
from dateutil import parser
from django.conf import settings
from django.http import HttpResponse, JsonResponse
from django.template import defaultfilters
from django.views.generic import View
from django.views.generic.base import TemplateView
from promgen import forms, models, prometheus, util
Expand Down
2 changes: 1 addition & 1 deletion promgen/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from django.conf import settings
from django.contrib import messages
from django.contrib.auth.models import Group, User
from django.contrib.auth.models import Group
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
Expand Down
2 changes: 1 addition & 1 deletion promgen/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def assertMockCalls(self, mock_func, *args, **kwargs):
# We split them here, instead of passing to assertEquals, so that we have
# the arguments directly and not a call() object
call_args, call_kwargs = mock_func.call_args
self.assertEquals((call_args, call_kwargs), (args, kwargs))
self.assertEqual((call_args, call_kwargs), (args, kwargs))

def add_user_permissions(self, *args, user=None):
codenames = [p.split(".")[1] for p in args]
Expand Down
49 changes: 25 additions & 24 deletions promgen/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,8 @@
import requests
from prometheus_client.core import CounterMetricFamily, GaugeMetricFamily

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.contenttypes.models import ContentType
from django.db.models import Count, Q
from django.db.utils import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import ugettext as _
from django.views.generic import DetailView, ListView, UpdateView, View
from django.views.generic.base import RedirectView, TemplateView
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.edit import CreateView, DeleteView, FormView

import promgen.templatetags.promgen as macro
from promgen import (
celery,
discovery,
forms,
mixins,
Expand All @@ -45,6 +29,21 @@
)
from promgen.shortcuts import resolve_domain

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.contenttypes.models import ContentType
from django.db.models import Count, Q
from django.db.utils import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.translation import ugettext as _
from django.views.generic import DetailView, ListView, UpdateView, View
from django.views.generic.base import RedirectView, TemplateView
from django.views.generic.detail import SingleObjectMixin
from django.views.generic.edit import CreateView, DeleteView, FormView

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -763,7 +762,9 @@ def get_context_data(self, **kwargs):
# We use setdefault here, because we may have received existing forms in the case of a POST
# that has errors to be corrected
context.setdefault("label_form", forms.LabelFormSet(initial=self.object.labels))
context.setdefault("annotation_form", forms.AnnotationFormSet(initial=self.object.annotations))
context.setdefault(
"annotation_form", forms.AnnotationFormSet(initial=self.object.annotations)
)
return context

def form_invalid(self, **kwargs):
Expand Down Expand Up @@ -1197,7 +1198,7 @@ def form_valid(self, form):
counters = prometheus.import_rules_v2(rules)
messages.info(self.request, "Imported %s" % counters)
return redirect("rule-import")
except:
except Exception:
messages.error(self.request, "Error importing rules")
return self.form_invalid(form)

Expand Down Expand Up @@ -1276,15 +1277,15 @@ def post(self, request, pk):
# check our test query output for labels outside our expected match
if rule.content_type.model == "service":
# for a service rule, we expect the current service and all child projects as expected
expected_labels["service"] = set([rule.content_object.name])
expected_labels["project"] = set(
[project.name for project in rule.content_object.project_set.all()]
)
expected_labels["service"] = {rule.content_object.name}
expected_labels["project"] = {
project.name for project in rule.content_object.project_set.all()
}

if rule.content_type.model == "project":
# for a project rule we expect only the current project and the parent service
expected_labels["service"] = set([rule.content_object.service.name])
expected_labels["project"] = set([rule.content_object.name])
expected_labels["service"] = {rule.content_object.service.name}
expected_labels["project"] = {rule.content_object.name}

query = macro.rulemacro(rule, request.POST["query"])
# Since our rules affect all servers we use Promgen's proxy-query to test our rule
Expand Down
22 changes: 12 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
[tool.black]
line-length = 100
target-version = ['py36']
target-version = ['py39']
extend-exclude = '''
(
promgen/migrations
|promgen/urls.py
)
'''

[tool.isort]
forced_separate = "django,promgen"
known_django = "django"
known_first_party = "promgen"
sections = "FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER"
profile = "black"
float_to_top = true

[tool.ruff]
ignore = ['E501']
ignore = [
'E741', # Ambiguous variable name
'E501', # Let black handle line-length
]

[tool.ruff.isort]
known-first-party = ["promgen"]
forced-separate = ["django"]

[tool.codespell]
skip = '*.min.js,*.min.css,*.css.map,.venv,dist,.git'
9 changes: 0 additions & 9 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,3 @@ docs =
Sphinx
sphinxcontrib-httpdomain
mysql = mysqlclient==1.4.2

[flake8]
max-line-length = 100
extend-ignore = E203
ignore = E501
exclude = migrations

[codespell]
skip = *.min.js,*.min.css,*.css.map,.venv,dist,.git

0 comments on commit d0c43fe

Please sign in to comment.