Skip to content

Commit

Permalink
[qa] Automatic reformatting of code and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Jul 30, 2024
1 parent 1b0e6f3 commit 92b4f9c
Show file tree
Hide file tree
Showing 9 changed files with 2,073 additions and 1,774 deletions.
455 changes: 260 additions & 195 deletions CHANGES.rst

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Please refer to the `OpenWISP Contribution Guidelines <http://openwisp.io/docs/developer/contributing.html>`_.
Please refer to the `OpenWISP Contribution Guidelines
<http://openwisp.io/docs/developer/contributing.html>`_.
3,258 changes: 1,740 additions & 1,518 deletions README.rst

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions openwisp_controller/admin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Base admin classes and mixins
"""

from django.core.exceptions import PermissionDenied

from openwisp_users.multitenancy import (
Expand Down
12 changes: 6 additions & 6 deletions openwisp_controller/config/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,13 @@ def get_vpn_context(self):
if 'vni' in vpn_context_keys and (
vpnclient.vni or vpnclient.vpn._vxlan_vni
):
context[
vpn_context_keys['vni']
] = f'{vpnclient.vni or vpnclient.vpn._vxlan_vni}'
context[vpn_context_keys['vni']] = (
f'{vpnclient.vni or vpnclient.vpn._vxlan_vni}'
)
if vpnclient.secret:
context[
vpn_context_keys['zerotier_member_id']
] = vpnclient.zerotier_member_id
context[vpn_context_keys['zerotier_member_id']] = (
vpnclient.zerotier_member_id
)
context[vpn_context_keys['secret']] = vpnclient.secret
return context

Expand Down
114 changes: 61 additions & 53 deletions openwisp_controller/config/base/vpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def _validate_host(self):

def save(self, *args, **kwargs):
"""
Calls _auto_create_cert() if cert is not set
Calls _auto_create_cert() if cert is not set.
"""
config = {}
created = self._state.adding
Expand Down Expand Up @@ -328,9 +328,9 @@ def dhparam(cls, length):

@classmethod
def post_delete(cls, instance, **kwargs):
"""
class method for ``post_delete`` signal
for managing automatic deletion of vpn servers
"""Receiver for ``post_delete`` signal.
Manages automatic deletion of vpn servers.
"""
if not instance._is_backend_type('zerotier'):
return
Expand Down Expand Up @@ -496,9 +496,9 @@ def get_vpn_server_context(self):
context[context_keys['public_key']] = self.public_key
if self.ip:
context[context_keys['server_ip_address']] = self.ip.ip_address
context[
context_keys['server_ip_network']
] = f'{self.ip.ip_address}/{self.subnet.subnet.max_prefixlen}'
context[context_keys['server_ip_network']] = (
f'{self.ip.ip_address}/{self.subnet.subnet.max_prefixlen}'
)
context[context_keys['vpn_subnet']] = str(self.subnet.subnet)
if self._is_backend_type('zerotier') and self.network_id:
context[context_keys['network_name']] = self.name
Expand All @@ -510,9 +510,11 @@ def get_system_context(self):
return self.get_context()

def _is_backend_type(self, backend_type):
"""
returns true if the backend path used converted to lowercase
"""Returns True if backend contains specified backend_type.
Returns true if the backend path used converted to lowercase
contains ``backend_type``.
Checking for the exact path may not be the best choices
given backends can be extended and customized.
By using this method, customizations will just have
Expand All @@ -521,22 +523,30 @@ def _is_backend_type(self, backend_type):
return backend_type.lower() in self.backend.lower()

def _get_auto_context_keys(self):
"""
returns a dictionary which indicates the names of
"""Returns context keys automatically.
Returns a dictionary which indicates the names of
the configuration variables needed to access:
* path to CA file
* CA certificate in PEM format
* path to cert file
* cert in PEM format
* path to key file
* key in PEM format
* path to CA file
* CA certificate in PEM format
* path to cert file
* cert in PEM format
* path to key file
* key in PEM format
WireGuard:
* public key
* ip address
* public key
* ip address
VXLAN:
* vni (VXLAN Network Identifier)
ZeroTier
* network_id (ZeroTier Network Identifier)
* vni (VXLAN Network Identifier)
ZeroTier:
* network_id (ZeroTier Network Identifier)
"""
pk = self.pk.hex
context_keys = {
Expand Down Expand Up @@ -585,8 +595,9 @@ def _get_auto_context_keys(self):
return context_keys

def auto_client(self, auto_cert=True, template_backend_class=None):
"""
calls backend ``auto_client`` method and returns a configuration
"""Calls backend ``auto_client`` method.
Returns a configuration
dictionary that is suitable to be used as a template
if ``auto_cert`` is ``False`` the resulting configuration
won't include autogenerated key and certificate details
Expand Down Expand Up @@ -653,9 +664,9 @@ def _auto_create_cert_extra(self, cert):
return cert

def _generate_wireguard_keys(self):
"""
generates wireguard private and public keys
and set the respctive attributes
"""Generates wireguard private and public keys.
Also sets the respctive instance attributes.
"""
if not self.private_key or not self.public_key:
self.private_key, self.public_key = crypto.generate_wireguard_keys()
Expand All @@ -669,9 +680,10 @@ def get_config(self):
return config

def _invalidate_peer_cache(self, update=False):
"""
Invalidates peer cache, if update=True is passed,
the peer cache will be regenerated
"""Invalidates peer cache.
If update=True is passed,
the peer cache will be regenerated.
"""
for backend in ['wireguard', 'vxlan']:
if self._is_backend_type(backend):
Expand All @@ -682,9 +694,10 @@ def _invalidate_peer_cache(self, update=False):
vpn_peers_changed.send(sender=self.__class__, instance=self)

def _get_peer_queryset(self):
"""
returns an iterator to iterate over tunnel peers
used to generate the list of peers of a tunnel (WireGuard/VXLAN)
"""Returns peer queryset.
Returns an iterator to iterate over tunnel peers
used to generate the list of peers of a tunnel (WireGuard/VXLAN).
"""
return (
self.vpnclient_set.select_related('config', 'ip')
Expand All @@ -702,9 +715,9 @@ def _get_peer_queryset(self):
)

def _add_wireguard(self, config):
"""
Adds wireguard peers and private key to the generated
configuration without the need of manual intervention.
"""Adds wireguard peers and private key to the generated configuration
without the need of manual intervention.
Modifies the config data structure as a side effect.
"""
try:
Expand All @@ -722,9 +735,7 @@ def _add_wireguard(self, config):

@cache_memoize(_PEER_CACHE_TIMEOUT, args_rewrite=_peer_cache_key)
def _get_wireguard_peers(self):
"""
Returns list of wireguard peers, the result is cached.
"""
"""Returns list of wireguard peers, the result is cached."""
peers = []
for vpnclient in self._get_peer_queryset():
if vpnclient.ip:
Expand All @@ -738,8 +749,8 @@ def _get_wireguard_peers(self):
return peers

def _add_vxlan(self, config):
"""
Adds VXLAN peers to the generated configuration
"""Adds VXLAN peers to the generated configuration
without the need of manual intervention.
Modifies the config data structure as a side effect.
"""
Expand Down Expand Up @@ -831,8 +842,8 @@ def zerotier_member_id(self):

@classmethod
def register_auto_ip_stopper(cls, func):
"""
Adds "func" to "_auto_ip_stopper_funcs".
"""Adds "func" to "_auto_ip_stopper_funcs".
These functions are called in the "_auto_ip" method.
Output from these functions are used to determine
skipping automatic IP assignment.
Expand All @@ -856,10 +867,7 @@ def _get_unique_checks(self, exclude=None, include_meta_constraints=False):
return unique_checks, date_checks

def save(self, *args, **kwargs):
"""
automatically provisions tunnel keys
and configuration if ``auto_cert`` is True
"""
"""Performs automatic provisioning if ``auto_cert`` is True."""
if self.auto_cert:
self._auto_x509()
self._auto_ip()
Expand All @@ -870,7 +878,7 @@ def save(self, *args, **kwargs):

def _auto_x509(self):
"""
automatically creates an x509 certificate
Automatically creates an x509 certificate.
"""
if not self.vpn._is_backend_type('openvpn') or self.cert:
return
Expand All @@ -879,7 +887,7 @@ def _auto_x509(self):

def _get_common_name(self):
"""
returns the common name for a new certificate
Returns the common name for a new certificate.
"""
d = self.config.device
end = 63 - len(d.mac_address)
Expand Down Expand Up @@ -908,10 +916,10 @@ def _post_save():

@classmethod
def post_delete(cls, instance, **kwargs):
"""
class method for ``post_delete`` signal
automatically deletes related certificates
and ip addresses if necessary
"""Receiver of ``post_delete`` signal.
Automatically deletes related certificates
and ip addresses if necessary.
"""
# only invalidates, does not regenerate the cache
# to avoid generating high load during bulk deletes
Expand Down
1 change: 0 additions & 1 deletion openwisp_controller/config/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@


class TestController(CreateConfigTemplateMixin, TestVpnX509Mixin, TestCase):

"""
tests for config.controller
"""
Expand Down
1 change: 1 addition & 0 deletions openwisp_controller/config/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
these mixins are reused also in openwisp2
change with care.
"""

from copy import deepcopy
from unittest import mock
from uuid import uuid4
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.docstrfmt]
extend_exclude = ["**/*.py", "README.rst"]

0 comments on commit 92b4f9c

Please sign in to comment.