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

Reformat and lint codebase with ruff #356

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ setuptools = "*"
# Generate fake data for tests
faker = "<14.0.0"
tblib = "*"
ruff ="*"

[requires]
python_version = "3.9"
Expand Down
26 changes: 25 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion adminapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Copyright (c) 2019 InnoGames GmbH
"""

from adminapi.request import send_request
from adminapi.exceptions import ApiError
from adminapi.request import send_request

API_CALL_ENDPOINT = '/call'

Expand Down
3 changes: 1 addition & 2 deletions adminapi/cmduser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"""

import os
from os.path import isfile
from os.path import expanduser
from os.path import expanduser, isfile


def get_auth_token():
Expand Down
10 changes: 5 additions & 5 deletions adminapi/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from types import GeneratorType

from adminapi import api
from adminapi.datatype import validate_value, json_to_datatype
from adminapi.datatype import json_to_datatype, validate_value
from adminapi.exceptions import AdminapiException, DatasetError
from adminapi.filters import Any, BaseFilter, ContainedOnlyBy
from adminapi.request import send_request, json_encode_extra
from adminapi.exceptions import DatasetError, AdminapiException
from adminapi.request import json_encode_extra, send_request

NEW_OBJECT_ENDPOINT = '/dataset/new_object'
COMMIT_ENDPOINT = '/dataset/commit'
Expand Down Expand Up @@ -460,9 +460,9 @@ def validate(self, key, value):
def set(self, key, value):
if isinstance(self[key], MultiAttr):
self[key].add(value)
elif type(self[key]) is bool:
elif isinstance(self[key], bool):
self[key] = bool(strtobool(value))
elif type(self[key]) is int:
elif isinstance(self[key], int):
self[key] = int(value)
else:
self[key] = value
Expand Down
4 changes: 2 additions & 2 deletions adminapi/datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
Copyright (c) 2019 InnoGames GmbH
"""
from datetime import date, datetime
from re import compile as re_compile
from ipaddress import IPv4Address, IPv4Network, IPv6Address, IPv6Network
from re import compile as re_compile

from netaddr import EUI

try:
from netaddr import mac_unix_expanded
except ImportError:
from netaddr import mac_unix as mac_unix_expanded

from adminapi.exceptions import DatatypeError, FilterValueError


# We use a set of regular expressions to cast to datatypes. This module
# is not aware of the attributes types of the server, neither it tries
# to match with them one to one. Its purpose is to provide convenience
Expand Down
3 changes: 2 additions & 1 deletion adminapi/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Copyright (c) 2019 InnoGames GmbH
"""

from re import compile as re_compile, error as re_error
from re import compile as re_compile
from re import error as re_error

from adminapi.datatype import STR_BASED_DATATYPES
from adminapi.exceptions import FilterValueError
Expand Down
2 changes: 1 addition & 1 deletion adminapi/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

from adminapi.datatype import DatatypeError, str_to_datatype
from adminapi.filters import BaseFilter, Any, Regexp, filter_classes
from adminapi.filters import Any, BaseFilter, Regexp, filter_classes

_trigger_re_chars = ('.*', '.+', '[', ']', '|', '\\', '$', '^', '<')

Expand Down
21 changes: 10 additions & 11 deletions adminapi/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,41 @@
Copyright (c) 2019 InnoGames GmbH
"""

import hmac
import json
import os
import time
from base64 import b64encode
from datetime import datetime, timezone
from hashlib import sha1
import hmac
from http.client import IncompleteRead
from socket import timeout
from ssl import SSLError
import time
import json
from base64 import b64encode
from datetime import datetime, timezone

from urllib.error import HTTPError, URLError
from urllib.parse import urlencode
from urllib.request import urlopen, Request
from urllib.request import Request, urlopen

from paramiko.agent import Agent
from paramiko.message import Message
from paramiko.ssh_exception import SSHException, PasswordRequiredException
from paramiko.ssh_exception import PasswordRequiredException, SSHException

from adminapi import VERSION

try:
from paramiko import RSAKey, ECDSAKey, Ed25519Key
from paramiko import ECDSAKey, Ed25519Key, RSAKey
key_classes = (RSAKey, ECDSAKey, Ed25519Key)
except ImportError:
# Ed25519Key requires paramiko >= 2.2
from paramiko import RSAKey, ECDSAKey
from paramiko import ECDSAKey, RSAKey
key_classes = (RSAKey, ECDSAKey)

from adminapi.cmduser import get_auth_token
from adminapi.filters import BaseFilter
from adminapi.exceptions import (
ApiError,
AuthenticationError,
ConfigurationError,
)
from adminapi.filters import BaseFilter


def load_private_key_file(private_key_path):
Expand Down
6 changes: 3 additions & 3 deletions adminapi/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from argparse import ArgumentParser
from typing import Text, NoReturn
from typing import NoReturn, Text

from adminapi.cli import parse_args

Expand All @@ -13,11 +13,11 @@ def error(self, message: Text) -> NoReturn:

class TestCommandlineInterface(unittest.TestCase):
def test_no_argument(self, *args):
with self.assertRaises(SystemExit) as e:
with self.assertRaises(SystemExit):
parse_args([])

def test_unknown_argument(self, *args):
with self.assertRaises(SystemExit) as e:
with self.assertRaises(SystemExit):
parse_args(['project=adminapi', '--attr', 'state', 'spaceship'])

def test_one_argument(self, *args):
Expand Down
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#
import os
import sys

sys.path.insert(0, os.path.abspath('../..'))

# -- Project information -----------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
line-length = 120
target-version = "py39"

[lint]
extend-select = ["I"]

[format]
quote-style = "single"

1 change: 1 addition & 0 deletions serveradmin/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'serveradmin.settings')


def main():
execute_from_command_line(sys.argv)

Expand Down
2 changes: 1 addition & 1 deletion serveradmin/access_control/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (c) 2019 InnoGames GmbH
"""

from django.contrib.admin import site, ModelAdmin
from django.contrib.admin import ModelAdmin, site

from serveradmin.access_control.models import AccessControlGroup

Expand Down
26 changes: 22 additions & 4 deletions serveradmin/access_control/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand All @@ -21,9 +20,28 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=80, unique=True)),
('query', models.CharField(max_length=1000)),
('applications', models.ManyToManyField(blank=True, limit_choices_to={'disabled': False, 'superuser': False}, related_name='access_control_groups', to='apps.Application')),
('attributes', models.ManyToManyField(blank=True, related_name='access_control_groups', to='serverdb.Attribute')),
('members', models.ManyToManyField(blank=True, limit_choices_to={'is_active': True, 'is_superuser': False}, related_name='access_control_groups', to=settings.AUTH_USER_MODEL)),
(
'applications',
models.ManyToManyField(
blank=True,
limit_choices_to={'disabled': False, 'superuser': False},
related_name='access_control_groups',
to='apps.Application',
),
),
(
'attributes',
models.ManyToManyField(blank=True, related_name='access_control_groups', to='serverdb.Attribute'),
),
(
'members',
models.ManyToManyField(
blank=True,
limit_choices_to={'is_active': True, 'is_superuser': False},
related_name='access_control_groups',
to=settings.AUTH_USER_MODEL,
),
),
],
options={
'db_table': 'access_control_group',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

dependencies = [
('access_control', '0001_initial'),
]
Expand All @@ -14,11 +13,19 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='accesscontrolgroup',
name='is_whitelist',
field=models.BooleanField(default=True, help_text='If checked, the attribute list is treated as a whitelist, otherwise it is treated as a blacklist.'),
field=models.BooleanField(
default=True,
help_text='If checked, the attribute list is treated as a whitelist, otherwise it is treated as a blacklist.',
),
),
migrations.AlterField(
model_name='accesscontrolgroup',
name='attributes',
field=models.ManyToManyField(blank=True, help_text="Note that you currently can't select the special attributes like object_id, hostname, servertype or intern_ip here.", related_name='access_control_groups', to='serverdb.Attribute'),
field=models.ManyToManyField(
blank=True,
help_text="Note that you currently can't select the special attributes like object_id, hostname, servertype or intern_ip here.",
related_name='access_control_groups',
to='serverdb.Attribute',
),
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
('access_control', '0002_whitelist_blacklist_toggle'),
]
Expand Down
12 changes: 4 additions & 8 deletions serveradmin/access_control/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Copyright (c) 2019 InnoGames GmbH
"""

from django.db import models
from django.contrib.auth.models import User
from django.db import models

from adminapi.parse import parse_query
from serveradmin.apps.models import Application
Expand All @@ -31,8 +31,7 @@ class AccessControlGroup(models.Model):
null=False,
default=True,
help_text=(
"If checked, the attribute list is treated as a whitelist, "
"otherwise it is treated as a blacklist."
'If checked, the attribute list is treated as a whitelist, ' 'otherwise it is treated as a blacklist.'
),
)
attributes = models.ManyToManyField(
Expand All @@ -41,7 +40,7 @@ class AccessControlGroup(models.Model):
related_name='access_control_groups',
help_text=(
"Note that you currently can't select the special attributes "
"like object_id, hostname, servertype or intern_ip here."
'like object_id, hostname, servertype or intern_ip here.'
),
)

Expand Down Expand Up @@ -69,10 +68,7 @@ def get_permissible_attribute_ids(self):

if self.is_whitelist is False:
# Set of all attributes, excluding the blacklisted ones
return (
{a.pk for a in Attribute.objects.all()} |
set(Attribute.specials.keys())
).difference(
return ({a.pk for a in Attribute.objects.all()} | set(Attribute.specials.keys())).difference(
{a.pk for a in self.attributes.all()}
)

Expand Down
Loading
Loading