Skip to content

Commit 493fc60

Browse files
committed
Use black as formatter for python files
1 parent 0d25aff commit 493fc60

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+910
-895
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ charset = utf-8
77
indent_style = space
88
indent_size = 2
99

10-
[configuration/**.py]
10+
[*.py]
1111
indent_size = 4

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ RUN apk add --no-cache \
1818
postgresql-dev \
1919
py3-pip \
2020
python3-dev \
21-
&& python3 -m venv /opt/netbox/venv \
22-
&& /opt/netbox/venv/bin/python3 -m pip install --upgrade \
21+
&& python3 -m venv /opt/netbox/venv \
22+
&& /opt/netbox/venv/bin/python3 -m pip install --upgrade \
2323
pip \
2424
setuptools \
2525
wheel

configuration/configuration.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
####
66

77
import re
8-
9-
from os.path import dirname, abspath, join
108
from os import environ
9+
from os.path import abspath, dirname, join
1110

1211
# For reference see https://netbox.readthedocs.io/en/stable/configuration/
1312
# Based on https://github.com/netbox-community/netbox/blob/master/netbox/netbox/configuration.example.py
@@ -39,16 +38,16 @@ def _read_secret(secret_name, default = None):
3938
# PostgreSQL database configuration. See the Django documentation for a complete list of available parameters:
4039
# https://docs.djangoproject.com/en/stable/ref/settings/#databases
4140
DATABASE = {
42-
'NAME': environ.get('DB_NAME', 'netbox'), # Database name
43-
'USER': environ.get('DB_USER', ''), # PostgreSQL username
41+
'NAME': environ.get('DB_NAME', 'netbox'), # Database name
42+
'USER': environ.get('DB_USER', ''), # PostgreSQL username
4443
'PASSWORD': _read_secret('db_password', environ.get('DB_PASSWORD', '')),
45-
# PostgreSQL password
46-
'HOST': environ.get('DB_HOST', 'localhost'), # Database server
47-
'PORT': environ.get('DB_PORT', ''), # Database port (leave blank for default)
44+
# PostgreSQL password
45+
'HOST': environ.get('DB_HOST', 'localhost'), # Database server
46+
'PORT': environ.get('DB_PORT', ''), # Database port (leave blank for default)
4847
'OPTIONS': {'sslmode': environ.get('DB_SSLMODE', 'prefer')},
49-
# Database connection SSLMODE
48+
# Database connection SSLMODE
5049
'CONN_MAX_AGE': int(environ.get('DB_CONN_MAX_AGE', '300')),
51-
# Max database connection age
50+
# Max database connection age
5251
}
5352

5453
# Redis database settings. Redis is used for caching and for queuing background tasks such as webhook events. A separate

configuration/ldap/ldap_config.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import ldap
2-
3-
from django_auth_ldap.config import LDAPSearch
41
from importlib import import_module
52
from os import environ
63

4+
import ldap
5+
from django_auth_ldap.config import LDAPSearch
6+
7+
78
# Read secret from file
89
def _read_secret(secret_name, default=None):
910
try:
@@ -47,9 +48,11 @@ def _import_group_type(group_type_name):
4748

4849
AUTH_LDAP_USER_SEARCH_BASEDN = environ.get('AUTH_LDAP_USER_SEARCH_BASEDN', '')
4950
AUTH_LDAP_USER_SEARCH_ATTR = environ.get('AUTH_LDAP_USER_SEARCH_ATTR', 'sAMAccountName')
50-
AUTH_LDAP_USER_SEARCH = LDAPSearch(AUTH_LDAP_USER_SEARCH_BASEDN,
51-
ldap.SCOPE_SUBTREE,
52-
"(" + AUTH_LDAP_USER_SEARCH_ATTR + "=%(user)s)")
51+
AUTH_LDAP_USER_SEARCH = LDAPSearch(
52+
AUTH_LDAP_USER_SEARCH_BASEDN,
53+
ldap.SCOPE_SUBTREE,
54+
"(" + AUTH_LDAP_USER_SEARCH_ATTR + "=%(user)s)"
55+
)
5356

5457
# This search ought to return all groups to which the user belongs. django_auth_ldap uses this to determine group
5558
# heirarchy.

docker/configuration.docker.py

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,58 @@
44
#
55
# They can be imported by other code (see `ldap_config.py` for an example).
66

7-
from os.path import abspath, isfile
8-
from os import scandir
97
import importlib.util
108
import sys
9+
from os import scandir
10+
from os.path import abspath, isfile
1111

1212

1313
def _filename(f):
14-
return f.name
14+
return f.name
1515

1616

1717
def _import(module_name, path, loaded_configurations):
18-
spec = importlib.util.spec_from_file_location('', path)
19-
module = importlib.util.module_from_spec(spec)
20-
spec.loader.exec_module(module)
21-
sys.modules[module_name] = module
18+
spec = importlib.util.spec_from_file_location("", path)
19+
module = importlib.util.module_from_spec(spec)
20+
spec.loader.exec_module(module)
21+
sys.modules[module_name] = module
2222

23-
loaded_configurations.insert(0, module)
23+
loaded_configurations.insert(0, module)
2424

25-
print(f"🧬 loaded config '{path}'")
25+
print(f"🧬 loaded config '{path}'")
2626

2727

2828
def read_configurations(config_module, config_dir, main_config):
29-
loaded_configurations = []
29+
loaded_configurations = []
3030

31-
main_config_path = abspath(f'{config_dir}/{main_config}.py')
32-
if isfile(main_config_path):
33-
_import(f'{config_module}.{main_config}', main_config_path, loaded_configurations)
34-
else:
35-
print(f"⚠️ Main configuration '{main_config_path}' not found.")
31+
main_config_path = abspath(f"{config_dir}/{main_config}.py")
32+
if isfile(main_config_path):
33+
_import(f"{config_module}.{main_config}", main_config_path, loaded_configurations)
34+
else:
35+
print(f"⚠️ Main configuration '{main_config_path}' not found.")
3636

37-
with scandir(config_dir) as it:
38-
for f in sorted(it, key=_filename):
39-
if not f.is_file():
40-
continue
37+
with scandir(config_dir) as it:
38+
for f in sorted(it, key=_filename):
39+
if not f.is_file():
40+
continue
4141

42-
if f.name.startswith('__'):
43-
continue
42+
if f.name.startswith("__"):
43+
continue
4444

45-
if not f.name.endswith('.py'):
46-
continue
45+
if not f.name.endswith(".py"):
46+
continue
4747

48-
if f.name == f'{config_dir}.py':
49-
continue
48+
if f.name == f"{config_dir}.py":
49+
continue
5050

51-
module_name = f"{config_module}.{f.name[:-len('.py')]}".replace(".", "_")
52-
_import(module_name, f.path, loaded_configurations)
51+
module_name = f"{config_module}.{f.name[:-len('.py')]}".replace(".", "_")
52+
_import(module_name, f.path, loaded_configurations)
5353

54-
if len(loaded_configurations) == 0:
55-
print(f"‼️ No configuration files found in '{config_dir}'.")
56-
raise ImportError(f"No configuration files found in '{config_dir}'.")
54+
if len(loaded_configurations) == 0:
55+
print(f"‼️ No configuration files found in '{config_dir}'.")
56+
raise ImportError(f"No configuration files found in '{config_dir}'.")
5757

58-
return loaded_configurations
58+
return loaded_configurations
5959

6060

6161
## Specific Parts
@@ -66,15 +66,16 @@ def read_configurations(config_module, config_dir, main_config):
6666

6767

6868
_loaded_configurations = read_configurations(
69-
config_dir = '/etc/netbox/config/',
70-
config_module = 'netbox.configuration',
71-
main_config = 'configuration')
69+
config_dir="/etc/netbox/config/",
70+
config_module="netbox.configuration",
71+
main_config="configuration",
72+
)
7273

7374

7475
def __getattr__(name):
75-
for config in _loaded_configurations:
76-
try:
77-
return getattr(config, name)
78-
except:
79-
pass
80-
raise AttributeError
76+
for config in _loaded_configurations:
77+
try:
78+
return getattr(config, name)
79+
except:
80+
pass
81+
raise AttributeError

docker/ldap_config.docker.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
from .configuration import read_configurations
22

33
_loaded_configurations = read_configurations(
4-
config_dir = '/etc/netbox/config/ldap/',
5-
config_module = 'netbox.configuration.ldap',
6-
main_config = 'ldap_config')
4+
config_dir="/etc/netbox/config/ldap/",
5+
config_module="netbox.configuration.ldap",
6+
main_config="ldap_config",
7+
)
78

89

910
def __getattr__(name):
10-
for config in _loaded_configurations:
11-
try:
12-
return getattr(config, name)
13-
except:
14-
pass
15-
raise AttributeError
11+
for config in _loaded_configurations:
12+
try:
13+
return getattr(config, name)
14+
except:
15+
pass
16+
raise AttributeError
17+
1618

1719
def __dir__():
18-
names = []
19-
for config in _loaded_configurations:
20-
names.extend(config.__dir__())
21-
return names
20+
names = []
21+
for config in _loaded_configurations:
22+
names.extend(config.__dir__())
23+
return names

initializers/custom_fields.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@
5353
# - Fifth Item
5454
# - Fourth Item
5555
# select_field_legacy_format:
56-
# type: select
57-
# label: Choose between items
58-
# required: false
59-
# filter_logic: loose
60-
# weight: 30
61-
# on_objects:
62-
# - dcim.models.Device
63-
# choices:
64-
# - value: A # this is the deprecated format.
65-
# - value: B # we only use it for the tests.
66-
# - value: C # please see above for the new format.
67-
# - value: "D like deprecated"
68-
# weight: 999
69-
# - value: E
56+
# type: select
57+
# label: Choose between items
58+
# required: false
59+
# filter_logic: loose
60+
# weight: 30
61+
# on_objects:
62+
# - dcim.models.Device
63+
# choices:
64+
# - value: A # this is the deprecated format.
65+
# - value: B # we only use it for the tests.
66+
# - value: C # please see above for the new format.
67+
# - value: "D like deprecated"
68+
# weight: 999
69+
# - value: E
7070
# boolean_field:
7171
# type: boolean
7272
# label: Yes Or No?

pyproject.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[tool.black]
2+
line-length = 100
3+
target-version = ['py38']
4+
include = '\.pyi?$'
5+
exclude = '''
6+
7+
(
8+
/(
9+
\.eggs # exclude a few common directories in the
10+
| \.git # root of the project
11+
| \.venv
12+
| \.netbox
13+
| \.vscode
14+
| configuration
15+
)/
16+
)
17+
'''
18+
19+
[tool.isort]
20+
profile = "black"
21+
multi_line_output = 3

startup_scripts/000_users.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44
from startup_script_utils import load_yaml, set_permissions
55
from users.models import Token
66

7-
users = load_yaml('/opt/netbox/initializers/users.yml')
7+
users = load_yaml("/opt/netbox/initializers/users.yml")
88
if users is None:
9-
sys.exit()
9+
sys.exit()
1010

1111
for username, user_details in users.items():
12-
if not User.objects.filter(username=username):
13-
user = User.objects.create_user(
14-
username = username,
15-
password = user_details.get('password', 0) or User.objects.make_random_password())
12+
if not User.objects.filter(username=username):
13+
user = User.objects.create_user(
14+
username=username,
15+
password=user_details.get("password", 0) or User.objects.make_random_password(),
16+
)
1617

17-
print("👤 Created user",username)
18+
print("👤 Created user", username)
1819

19-
if user_details.get('api_token', 0):
20-
Token.objects.create(user=user, key=user_details['api_token'])
20+
if user_details.get("api_token", 0):
21+
Token.objects.create(user=user, key=user_details["api_token"])
2122

22-
yaml_permissions = user_details.get('permissions', [])
23-
set_permissions(user.user_permissions, yaml_permissions)
23+
yaml_permissions = user_details.get("permissions", [])
24+
set_permissions(user.user_permissions, yaml_permissions)

startup_scripts/010_groups.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
from django.contrib.auth.models import Group, User
44
from startup_script_utils import load_yaml, set_permissions
55

6-
groups = load_yaml('/opt/netbox/initializers/groups.yml')
6+
groups = load_yaml("/opt/netbox/initializers/groups.yml")
77
if groups is None:
8-
sys.exit()
8+
sys.exit()
99

1010
for groupname, group_details in groups.items():
11-
group, created = Group.objects.get_or_create(name=groupname)
11+
group, created = Group.objects.get_or_create(name=groupname)
1212

13-
if created:
14-
print("👥 Created group", groupname)
13+
if created:
14+
print("👥 Created group", groupname)
1515

16-
for username in group_details.get('users', []):
17-
user = User.objects.get(username=username)
16+
for username in group_details.get("users", []):
17+
user = User.objects.get(username=username)
1818

19-
if user:
20-
user.groups.add(group)
19+
if user:
20+
user.groups.add(group)
2121

22-
yaml_permissions = group_details.get('permissions', [])
23-
set_permissions(group.permissions, yaml_permissions)
22+
yaml_permissions = group_details.get("permissions", [])
23+
set_permissions(group.permissions, yaml_permissions)

0 commit comments

Comments
 (0)