Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INTERNAL] Run pyupgrade on code in prep for more lint cleanup #397

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# pyupgrade --py36-plus promgen/**/*.py
4d53038426aedf2abf337a2876d0d6ceccefc09b
2 changes: 1 addition & 1 deletion promgen/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class RuleAdmin(admin.ModelAdmin):
inlines = [RuleLabelInline, RuleAnnotationInline]

def get_queryset(self, request):
qs = super(RuleAdmin, self).get_queryset(request)
qs = super().get_queryset(request)
return qs.prefetch_related('content_object',)


Expand Down
3 changes: 1 addition & 2 deletions promgen/celery.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) 2017 LINE Corporation
# These sources are released under the terms of the MIT license: see LICENSE

from __future__ import absolute_import, unicode_literals

import logging
import socket
Expand All @@ -28,7 +27,7 @@

@app.task(bind=True)
def debug_task(self):
print("Request: {0!r}".format(self.request))
print(f"Request: {self.request!r}")


@celeryd_after_setup.connect
Expand Down
4 changes: 2 additions & 2 deletions promgen/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def directories(**kwargs):
path = pathlib.Path(util.setting(key)).parent
except TypeError:
yield checks.Warning(
"Missing setting for %s in %s " % (key, settings.PROMGEN_CONFIG_FILE),
f"Missing setting for {key} in {settings.PROMGEN_CONFIG_FILE} ",
id="promgen.W001",
)
else:
Expand All @@ -29,7 +29,7 @@ def promtool(**kwargs):
path = pathlib.Path(util.setting(key))
except TypeError:
yield checks.Warning(
"Missing setting for %s in %s " % (key, settings.PROMGEN_CONFIG_FILE),
f"Missing setting for {key} in {settings.PROMGEN_CONFIG_FILE} ",
id="promgen.W001",
)
else:
Expand Down
2 changes: 1 addition & 1 deletion promgen/discovery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
FARM_DEFAULT = 'promgen'


class DiscoveryBase(object):
class DiscoveryBase:
remote = True

'''
Expand Down
4 changes: 2 additions & 2 deletions promgen/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class ImportConfigForm(forms.Form):
def _choices():
return [('', '<Default>')] + sorted([(shard.name, 'Import into: ' + shard.name) for shard in models.Shard.objects.all()])
return [('', '<Default>')] + sorted((shard.name, 'Import into: ' + shard.name) for shard in models.Shard.objects.all())

config = forms.CharField(
widget=forms.Textarea(attrs={'rows': 5, 'class': 'form-control'}),
Expand Down Expand Up @@ -177,7 +177,7 @@ def clean(self):
# once we split on that, we want to make sure there are no invalid
# hostnames
hosts = set()
for hostname in re.split("[,\s]+", self.cleaned_data["hosts"]):
for hostname in re.split(r"[,\s]+", self.cleaned_data["hosts"]):
if hostname == "":
continue
validators.hostname(hostname)
Expand Down
2 changes: 1 addition & 1 deletion promgen/management/commands/alerts-index.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def handle(self, dryrun, **kargs):
for alert in models.Alert.objects.filter(alertlabel__isnull=True):
if dryrun:
labels = alert.json.get("commonLabels")
self.stderr.write("alert_id: %s, labels: %s" % (alert.pk, labels))
self.stderr.write(f"alert_id: {alert.pk}, labels: {labels}")
continue

tasks.index_alert.delay(alert.pk)
Expand Down
4 changes: 2 additions & 2 deletions promgen/management/commands/import-jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ def handle(self, target_file, replace_shard, **kwargs):

if imported:
counters = {key: len(imported[key]) for key in imported}
self.stdout.write('Imported {}'.format(counters))
self.stdout.write(f'Imported {counters}')

if skipped:
counters = {key: len(skipped[key]) for key in skipped}
self.stdout.write('Skipped {}'.format(counters))
self.stdout.write(f'Skipped {counters}')

trigger_write_config.send(self, force=True)
trigger_write_rules.send(self, force=True)
Expand Down
4 changes: 2 additions & 2 deletions promgen/management/commands/register-exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def handle(self, job, port, path, **kargs):
job=job, port=port, path=path
)
if created:
self.stdout.write('Created {}'.format(exporter))
self.stdout.write(f'Created {exporter}')
else:
self.stdout.write('Already exists {}'.format(exporter))
self.stdout.write(f'Already exists {exporter}')

6 changes: 2 additions & 4 deletions promgen/management/commands/register-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ def handle(self, shard, host, port, **kwargs):
defaults={'shard': shard}
)
if created:
self.stdout.write('Created {} on {}'.format(server, shard.name))
self.stdout.write(f'Created {server} on {shard.name}')
else:
old_shard = server.shard
server.shard = shard
server.save()
self.stdout.write('Moved {} from {} to {}'.format(
server, old_shard.name, shard.name
))
self.stdout.write(f'Moved {server} from {old_shard.name} to {shard.name}')
2 changes: 1 addition & 1 deletion promgen/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
_user = local()


class PromgenMiddleware(object):
class PromgenMiddleware:
def __init__(self, get_response):
self.get_response = get_response

Expand Down
10 changes: 4 additions & 6 deletions promgen/migrations/0001_squashed_0044_common-rules.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.10 on 2018-02-07 06:56
from __future__ import unicode_literals

from django.conf import settings
import django.contrib.sites.models
Expand Down Expand Up @@ -210,7 +208,7 @@ class Migration(migrations.Migration):
),
migrations.AlterUniqueTogether(
name='farm',
unique_together=set([('name', 'source')]),
unique_together={('name', 'source')},
),
migrations.AddField(
model_name='exporter',
Expand All @@ -219,14 +217,14 @@ class Migration(migrations.Migration):
),
migrations.AlterUniqueTogether(
name='prometheus',
unique_together=set([('host', 'port')]),
unique_together={('host', 'port')},
),
migrations.AlterUniqueTogether(
name='host',
unique_together=set([('name', 'farm')]),
unique_together={('name', 'farm')},
),
migrations.AlterUniqueTogether(
name='exporter',
unique_together=set([('job', 'port', 'project')]),
unique_together={('job', 'port', 'project')},
),
]
2 changes: 0 additions & 2 deletions promgen/migrations/0002_auto_20180316_0525.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.10 on 2018-03-16 05:25
from __future__ import unicode_literals

from django.db import migrations, models

Expand Down
34 changes: 17 additions & 17 deletions promgen/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def show_value(self):
show_value.short_description = 'Value'

def __str__(self):
return '{}:{}'.format(self.sender, self.show_value())
return f'{self.sender}:{self.show_value()}'

@classmethod
def driver_set(cls):
Expand Down Expand Up @@ -254,7 +254,7 @@ def get_absolute_url(self):
return reverse('project-detail', kwargs={'pk': self.pk})

def __str__(self):
return '{} » {}'.format(self.service, self.name)
return f'{self.service} » {self.name}'


class Farm(models.Model):
Expand All @@ -270,7 +270,7 @@ def get_absolute_url(self):

def refresh(self):
target = set()
current = set(host.name for host in self.host_set.all())
current = {host.name for host in self.host_set.all()}
for entry in plugins.discovery():
if self.source == entry.name:
target.update(entry.load()().fetch(self.name))
Expand All @@ -279,13 +279,13 @@ def refresh(self):
add = target - current

if add:
Audit.log('Adding {} to {}'.format(add, self), self)
Audit.log(f'Adding {add} to {self}', self)
Host.objects.bulk_create([
Host(name=name, farm_id=self.id) for name in add
])

if remove:
Audit.log('Removing {} from {}'.format(add, self), self)
Audit.log(f'Removing {add} from {self}', self)
Host.objects.filter(farm=self, name__in=remove).delete()

return add, remove
Expand All @@ -294,8 +294,7 @@ def refresh(self):
def fetch(cls, source):
for entry in plugins.discovery():
if entry.name == source:
for farm in entry.load()().farms():
yield farm
yield from entry.load()().farms()

@cached_property
def driver(self):
Expand All @@ -315,7 +314,7 @@ def driver_set(cls):
yield entry.name, entry.load()()

def __str__(self):
return '{} ({})'.format(self.name, self.source)
return f'{self.name} ({self.source})'


class Host(models.Model):
Expand All @@ -330,7 +329,7 @@ def get_absolute_url(self):
return reverse('host-detail', kwargs={'slug': self.name})

def __str__(self):
return '{} [{}]'.format(self.name, self.farm.name)
return f'{self.name} [{self.farm.name}]'


class BaseExporter(models.Model):
Expand Down Expand Up @@ -375,7 +374,7 @@ class Probe(models.Model):
description = models.TextField(blank=True)

def __str__(self):
return "{} » {}".format(self.module, self.description)
return f"{self.module} » {self.description}"


class URL(models.Model):
Expand All @@ -387,7 +386,7 @@ class Meta:
ordering = ["project__service", "project", "url"]

def __str__(self):
return "{} [{}]".format(self.project, self.url)
return f"{self.project} [{self.url}]"


class Rule(models.Model):
Expand Down Expand Up @@ -438,7 +437,7 @@ def annotations(self):
return _annotations

def __str__(self):
return '{} [{}]'.format(self.name, self.content_object.name)
return f'{self.name} [{self.content_object.name}]'

def get_absolute_url(self):
return reverse('rule-detail', kwargs={'pk': self.pk})
Expand Down Expand Up @@ -470,15 +469,16 @@ def copy_to(self, content_type, object_id):
orig_pk = self.pk
self.pk = None
self.parent_id = orig_pk
self.name = '{}_{}'.format(self.name, slugify(content_object.name)).replace('-', '_')
self.name = f'{self.name}_{slugify(content_object.name)}'.replace('-', '_')
self.content_type = content_type
self.object_id = object_id
# Enable the copy by default since it's more likely the user prefers
# to have their own copy enabled rather than the original one.
self.enabled = True
self.clause = self.clause.replace(macro.EXCLUSION_MACRO, '{}="{}",{}'.format(
content_type.model, content_object.name, macro.EXCLUSION_MACRO
))
self.clause = self.clause.replace(
macro.EXCLUSION_MACRO,
f'{content_type.model}="{content_object.name}",{macro.EXCLUSION_MACRO}',
)
self.save()

# Add a label to our new rule by default, to help ensure notifications
Expand Down Expand Up @@ -617,7 +617,7 @@ class Prometheus(models.Model):
port = models.IntegerField()

def __str__(self):
return '{}:{}'.format(self.host, self.port)
return f'{self.host}:{self.port}'

class Meta:
ordering = ['shard', 'host']
Expand Down
2 changes: 1 addition & 1 deletion promgen/notification/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FormSenderBase(forms.Form):
alias = forms.CharField(required=False)


class NotificationBase(object):
class NotificationBase:
"""
Base Notification class
"""
Expand Down
15 changes: 7 additions & 8 deletions promgen/notification/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@


def _choices():
for user in User.objects.filter(is_active=True).order_by('username'):
if user.first_name:
yield (user.username, '{user.username} ({user.first_name} {user.last_name})'.format(user=user))
elif user.email:
yield (user.username, '{user.username} ({user.email})'.format(user=user))
for u in User.objects.filter(is_active=True).order_by("username"):
if u.first_name:
yield (u.username, f"{u.username} ({u.first_name} {u.last_name})")
elif u.email:
yield (u.username, f"{u.username} ({u.email})")
else:
yield (user.username, user.username)
yield (u.username, u.username)


class FormUser(forms.Form):
Expand All @@ -42,8 +42,7 @@ def splay(self, address):
except User.DoesNotExist:
logger.error("Missing user %s", address)
else:
for sender in models.Sender.objects.filter(obj=user):
yield sender
yield from models.Sender.objects.filter(obj=user)

def _send(self, address, data):
user = User.objects.get(username=address)
Expand Down
2 changes: 1 addition & 1 deletion promgen/prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def render_config(service=None, project=None):

hosts = []
for host in exporter.project.farm.host_set.all():
hosts.append("{}:{}".format(host.name, exporter.port))
hosts.append(f"{host.name}:{exporter.port}")

data.append({"labels": labels, "targets": hosts})
return json.dumps(data, indent=2, sort_keys=True)
Expand Down
5 changes: 2 additions & 3 deletions promgen/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@ def proxy(self, request):
headers=self.headers,
)
)
for future in concurrent.futures.as_completed(futures):
yield future
yield from concurrent.futures.as_completed(futures)


class ProxyGraph(TemplateView):
template_name = "promgen/graph.html"

def get_context_data(self, **kwargs):
context = super(ProxyGraph, self).get_context_data(**kwargs)
context = super().get_context_data(**kwargs)
context["shard_list"] = models.Shard.objects.filter(proxy=True)
for k, v in self.request.GET.items():
_, k = k.split(".")
Expand Down
Loading