diff --git a/server/mypy.ini b/server/mypy.ini index 923c33937..c455093d8 100644 --- a/server/mypy.ini +++ b/server/mypy.ini @@ -6,7 +6,8 @@ exclude = .*/transports/.*/scripts/.* mypy_path = $MYPY_CONFIG_FILE_DIR/src -disable_error_code = import, no-any-return, misc, redundant-cast +# Call overload because ForeignKey fields are not being recognized with django-types +disable_error_code = import, no-any-return, misc, redundant-cast, call-overload strict = True implicit_reexport = true diff --git a/server/pyrightconfig.json b/server/pyrightconfig.json new file mode 100644 index 000000000..f1d767a19 --- /dev/null +++ b/server/pyrightconfig.json @@ -0,0 +1,13 @@ +{ + "include": [ + "src" + ], + "exclude": [ + "**/scripts", + "**/__pycache__", + ], + "typeCheckingMode": "strict", + "reportPrivateUsage": false, + "reportUnusedImport": true, + "reportMissingTypeStubs": false, +} \ No newline at end of file diff --git a/server/src/uds/REST/__init__.py b/server/src/uds/REST/__init__.py index 1115ccda0..c802a5431 100644 --- a/server/src/uds/REST/__init__.py +++ b/server/src/uds/REST/__init__.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (c) 2012-2019 Virtual Cable S.L. +# Copyright (c) 2012-2024 Virtual Cable S.L.U. # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, @@ -27,15 +27,9 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import logging -import typing -import collections.abc - -logger = logging.getLogger(__name__) - +# pyright: reportUnusedImport=false # Convenience imports, must be present before initializing handlers from .handlers import Handler - from .dispatcher import Dispatcher diff --git a/server/src/uds/REST/dispatcher.py b/server/src/uds/REST/dispatcher.py index a11e04155..a3441fd1d 100644 --- a/server/src/uds/REST/dispatcher.py +++ b/server/src/uds/REST/dispatcher.py @@ -82,7 +82,7 @@ class HandlerNode: return ret -class Dispatcher(View): # type: ignore +class Dispatcher(View): """ This class is responsible of dispatching REST requests """ diff --git a/server/src/uds/REST/handlers.py b/server/src/uds/REST/handlers.py index 97dbda035..6382c326b 100644 --- a/server/src/uds/REST/handlers.py +++ b/server/src/uds/REST/handlers.py @@ -30,7 +30,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import logging import codecs @@ -54,27 +53,24 @@ if typing.TYPE_CHECKING: logger = logging.getLogger(__name__) - class Handler: """ REST requests handler base class """ - raw: typing.ClassVar[ - bool - ] = False # If true, Handler will return directly an HttpResponse Object - name: typing.ClassVar[ - typing.Optional[str] - ] = None # If name is not used, name will be the class name in lower case - path: typing.ClassVar[ - typing.Optional[str] - ] = None # Path for this method, so we can do /auth/login, /auth/logout, /auth/auths in a simple way - authenticated: typing.ClassVar[ - bool - ] = True # By default, all handlers needs authentication. Will be overwriten if needs_admin or needs_staff, - needs_admin: typing.ClassVar[ - bool - ] = False # By default, the methods will be accessible by anyone if nothing else indicated + raw: typing.ClassVar[bool] = False # If true, Handler will return directly an HttpResponse Object + name: typing.ClassVar[typing.Optional[str]] = ( + None # If name is not used, name will be the class name in lower case + ) + path: typing.ClassVar[typing.Optional[str]] = ( + None # Path for this method, so we can do /auth/login, /auth/logout, /auth/auths in a simple way + ) + authenticated: typing.ClassVar[bool] = ( + True # By default, all handlers needs authentication. Will be overwriten if needs_admin or needs_staff, + ) + needs_admin: typing.ClassVar[bool] = ( + False # By default, the methods will be accessible by anyone if nothing else indicated + ) needs_staff: typing.ClassVar[bool] = False # By default, staff # For implementing help @@ -85,10 +81,11 @@ class Handler: _request: 'ExtendedHttpRequestWithUser' # It's a modified HttpRequest _path: str _operation: str - _params: dict[str, typing.Any] # This is a deserliazied object from request. Can be anything as 'a' or {'a': 1} or .... - _args: tuple[ - str, ... - ] # This are the "path" split by /, that is, the REST invocation arguments + _params: dict[ + str, typing.Any + ] # This is a deserliazied object from request. Can be anything as 'a' or {'a': 1} or .... + # These are the "path" split by /, that is, the REST invocation arguments + _args: list[str] _kwargs: dict[str, typing.Any] # This are the "path" split by /, that is, the REST invocation arguments _headers: dict[str, str] _session: typing.Optional[SessionStore] @@ -108,9 +105,7 @@ class Handler: *args: str, **kwargs: typing.Any, ): - logger.debug( - 'Data: %s %s %s', self.__class__, self.needs_admin, self.authenticated - ) + logger.debug('Data: %s %s %s', self.__class__, self.needs_admin, self.authenticated) if ( self.needs_admin or self.needs_staff ) and not self.authenticated: # If needs_admin, must also be authenticated @@ -122,13 +117,11 @@ class Handler: self._path = path self._operation = method self._params = params - self._args = args + self._args = list(args) # copy of args self._kwargs = kwargs self._headers = {} self._auth_token = None - if ( - self.authenticated - ): # Only retrieve auth related data on authenticated handlers + if self.authenticated: # Only retrieve auth related data on authenticated handlers try: self._auth_token = self._request.headers.get(consts.auth.AUTH_TOKEN_HEADER, '') self._session = SessionStore(session_key=self._auth_token) @@ -200,12 +193,12 @@ class Handler: return self._params @property - def args(self) -> tuple[str, ...]: + def args(self) -> list[str]: """ Returns the args object """ return self._args - + @property def session(self) -> 'SessionStore': if self._session is None: @@ -244,9 +237,7 @@ class Handler: staff_member = True # Make admins also staff members :-) # crypt password and convert to base64 - passwd = codecs.encode( - CryptoManager().symmetric_encrypt(password, scrambler), 'base64' - ).decode() + passwd = codecs.encode(CryptoManager().symmetric_encrypt(password, scrambler), 'base64').decode() session['REST'] = { 'auth': id_auth, @@ -334,15 +325,11 @@ class Handler: self._session.accessed = True self._session.save() except Exception: - logger.exception( - 'Got an exception setting session value %s to %s', key, value - ) + logger.exception('Got an exception setting session value %s to %s', key, value) def is_ip_allowed(self) -> bool: try: - return net.contains( - GlobalConfig.ADMIN_TRUSTED_SOURCES.get(True), self._request.ip - ) + return net.contains(GlobalConfig.ADMIN_TRUSTED_SOURCES.get(True), self._request.ip) except Exception: logger.warning( 'Error checking truted ADMIN source: "%s" does not seems to be a valid network string. Using Unrestricted access.', diff --git a/server/src/uds/REST/log.py b/server/src/uds/REST/log.py index 4b8dee958..496ec9c8f 100644 --- a/server/src/uds/REST/log.py +++ b/server/src/uds/REST/log.py @@ -30,7 +30,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc from uds import models from uds.core import consts @@ -47,7 +46,9 @@ if typing.TYPE_CHECKING: # If path has ".../services/[uuid]/..." we will replace uuid with "service name" sourrounded by [] # If path has ".../users/[uuid]/..." we will replace uuid with "user name" sourrounded by [] # If path has ".../groups/[uuid]/..." we will replace uuid with "group name" sourrounded by [] -UUID_REPLACER = ( +UUID_REPLACER: tuple[ + tuple[str, type[models.Provider | models.Service | models.ServicePool | models.User | models.Group]], ... +] = ( ('providers', models.Provider), ('services', models.Service), ('servicespools', models.ServicePool), diff --git a/server/src/uds/REST/methods/accounts.py b/server/src/uds/REST/methods/accounts.py index 769cc9f39..da99749ab 100644 --- a/server/src/uds/REST/methods/accounts.py +++ b/server/src/uds/REST/methods/accounts.py @@ -33,7 +33,6 @@ import datetime import logging import typing -import collections.abc from django.utils.translation import gettext_lazy as _ diff --git a/server/src/uds/REST/methods/accountsusage.py b/server/src/uds/REST/methods/accountsusage.py index 69e56419a..2e6166584 100644 --- a/server/src/uds/REST/methods/accountsusage.py +++ b/server/src/uds/REST/methods/accountsusage.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext as _ diff --git a/server/src/uds/REST/methods/actor_token.py b/server/src/uds/REST/methods/actor_token.py index 815188ca3..e00f44693 100644 --- a/server/src/uds/REST/methods/actor_token.py +++ b/server/src/uds/REST/methods/actor_token.py @@ -32,12 +32,12 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_lazy as _ from uds.core import types, consts from uds.core.types import permissions +from uds.core.util import ensure from uds.core.util.log import LogLevel from uds.models import Server from uds.core.exceptions.rest import NotFound, RequestError @@ -55,7 +55,7 @@ class ActorTokens(ModelHandler): model = Server model_filter = {'type': types.servers.ServerType.ACTOR} - table_title = typing.cast(str, _('Actor tokens')) + table_title = _('Actor tokens') table_fields = [ # {'token': {'title': _('Token')}}, {'stamp': {'title': _('Date'), 'type': 'datetime'}}, @@ -68,9 +68,9 @@ class ActorTokens(ModelHandler): {'log_level': {'title': _('Log level')}}, ] - def item_as_dict(self, item_: 'Model') -> dict[str, typing.Any]: - item = typing.cast(Server, item_) - data = item.data or {} + def item_as_dict(self, item: 'Model') -> dict[str, typing.Any]: + item = ensure.is_instance(item, Server) + data: dict[str, typing.Any] = item.data or {} log_level_int = data.get('log_level', 2) if log_level_int < 10000: # Old log level log_level = LogLevel.from_actor_level(log_level_int).name diff --git a/server/src/uds/REST/methods/actor_v3.py b/server/src/uds/REST/methods/actor_v3.py index e666de3ea..d41f990b3 100644 --- a/server/src/uds/REST/methods/actor_v3.py +++ b/server/src/uds/REST/methods/actor_v3.py @@ -401,7 +401,7 @@ class Initialize(ActorV3Action): # If not found an alias, try to locate on service table # Not on alias token, try to locate on Service table if not service: - service = typing.cast('Service', Service.objects.get(token=token)) + service = Service.objects.get(token=token) # If exists, create and alias for it # Get first mac and, if not exists, get first ip unique_id = self._params['id'][0].get('mac', self._params['id'][0].get('ip', '')) @@ -835,7 +835,7 @@ class Notify(ActorV3Action): logger.debug('Args: %s, Params: %s', self._args, self._params) try: action = NotifyActionType(self._params['action']) - token = self._params['token'] # pylint: disable=unused-variable # Just to check it exists + _token = self._params['token'] # Just to check it exists except Exception as e: # Requested login, logout or whatever raise exceptions.rest.RequestError('Invalid parameters') from e diff --git a/server/src/uds/REST/methods/authenticators.py b/server/src/uds/REST/methods/authenticators.py index b03bbfe49..6a6b2ef02 100644 --- a/server/src/uds/REST/methods/authenticators.py +++ b/server/src/uds/REST/methods/authenticators.py @@ -38,7 +38,6 @@ import typing from django.utils.translation import gettext from django.utils.translation import gettext_lazy as _ -from netaddr import N from uds.core import auths, consts, exceptions, types from uds.core.environment import Environment @@ -210,12 +209,14 @@ class Authenticators(ModelHandler): auth = item.get_instance() - canDoSearch = ( + # Cast to Any because we want to compare with the default method or if it's overriden + # Cast is neccesary to avoid mypy errors, for example + search_supported = ( type_ == 'user' - and (auth.search_users != auths.Authenticator.search_users) - or (auth.search_groups != auths.Authenticator.search_groups) + and (typing.cast(typing.Any, auth.search_users) != typing.cast(typing.Any, auths.Authenticator.search_users)) + or (typing.cast(typing.Any, auth.search_groups) != typing.cast(typing.Any, auths.Authenticator.search_groups)) ) - if canDoSearch is False: + if search_supported is False: raise self.not_supported_response() if type_ == 'user': diff --git a/server/src/uds/REST/methods/calendarrules.py b/server/src/uds/REST/methods/calendarrules.py index 902b3734f..b191056e8 100644 --- a/server/src/uds/REST/methods/calendarrules.py +++ b/server/src/uds/REST/methods/calendarrules.py @@ -30,7 +30,6 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import datetime import logging import typing diff --git a/server/src/uds/REST/methods/calendars.py b/server/src/uds/REST/methods/calendars.py index 9c22286e5..da7dc55bd 100644 --- a/server/src/uds/REST/methods/calendars.py +++ b/server/src/uds/REST/methods/calendars.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_lazy as _ from uds.models import Calendar @@ -60,7 +59,7 @@ class Calendars(ModelHandler): save_fields = ['name', 'comments', 'tags'] - table_title = typing.cast(str, _('Calendars')) + table_title = _('Calendars') table_fields = [ { 'name': { diff --git a/server/src/uds/REST/methods/client.py b/server/src/uds/REST/methods/client.py index 84e298e06..516fafab3 100644 --- a/server/src/uds/REST/methods/client.py +++ b/server/src/uds/REST/methods/client.py @@ -28,7 +28,6 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import logging import typing diff --git a/server/src/uds/REST/methods/config.py b/server/src/uds/REST/methods/config.py index e6120e55e..3295a435b 100644 --- a/server/src/uds/REST/methods/config.py +++ b/server/src/uds/REST/methods/config.py @@ -31,7 +31,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import logging from uds.core.util.config import Config as CfgConfig diff --git a/server/src/uds/REST/methods/connection.py b/server/src/uds/REST/methods/connection.py index 656ec2e45..c450380b9 100644 --- a/server/src/uds/REST/methods/connection.py +++ b/server/src/uds/REST/methods/connection.py @@ -30,7 +30,6 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import datetime import logging import typing @@ -39,7 +38,6 @@ from uds.core import exceptions, types from uds.core.managers.crypto import CryptoManager from uds.core.managers.userservice import UserServiceManager from uds.core.services.exceptions import ServiceNotReadyError -from uds.core.types.requests import ExtendedHttpRequestWithUser from uds.core.util.rest.tools import match from uds.REST import Handler from uds.web.util import services diff --git a/server/src/uds/REST/methods/gui_callback.py b/server/src/uds/REST/methods/gui_callback.py index c2387a618..1d4c3b355 100644 --- a/server/src/uds/REST/methods/gui_callback.py +++ b/server/src/uds/REST/methods/gui_callback.py @@ -30,9 +30,7 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import logging -import typing from uds.core import exceptions, types from uds.core.ui import gui diff --git a/server/src/uds/REST/methods/images.py b/server/src/uds/REST/methods/images.py index 2e889a72c..1ee4f1167 100644 --- a/server/src/uds/REST/methods/images.py +++ b/server/src/uds/REST/methods/images.py @@ -32,12 +32,10 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_lazy as _, gettext from uds.models import Image from uds.core import types -from uds.core.ui import gui from uds.core.util import ensure from uds.REST.model import ModelHandler @@ -59,7 +57,7 @@ class Images(ModelHandler): model = Image save_fields = ['name', 'data'] - table_title = typing.cast(str, _('Image Gallery')) + table_title = _('Image Gallery') table_fields = [ { 'thumb': { diff --git a/server/src/uds/REST/methods/login_logout.py b/server/src/uds/REST/methods/login_logout.py index 0212d085b..13726950f 100644 --- a/server/src/uds/REST/methods/login_logout.py +++ b/server/src/uds/REST/methods/login_logout.py @@ -35,7 +35,7 @@ import logging import time import typing -from uds.core import consts, exceptions, types +from uds.core import consts, exceptions from uds.core.auths.auth import authenticate from uds.core.managers.crypto import CryptoManager from uds.core.util.cache import Cache diff --git a/server/src/uds/REST/methods/meta_pools.py b/server/src/uds/REST/methods/meta_pools.py index 0518de3ce..fb9fe58a8 100644 --- a/server/src/uds/REST/methods/meta_pools.py +++ b/server/src/uds/REST/methods/meta_pools.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext from django.utils.translation import gettext_lazy as _ @@ -83,7 +82,7 @@ class MetaPools(ModelHandler): 'transport_grouping', ] - table_title = typing.cast(str, _('Meta Pools')) + table_title = _('Meta Pools') table_fields = [ {'name': {'title': _('Name')}}, {'comments': {'title': _('Comments')}}, @@ -116,7 +115,7 @@ class MetaPools(ModelHandler): # if item does not have an associated service, hide it (the case, for example, for a removed service) # Access from dict will raise an exception, and item will be skipped poolGroupId = None - poolGroupName = typing.cast(str, _('Default')) + poolGroupName = _('Default') poolGroupThumb = DEFAULT_THUMB_BASE64 if item.servicesPoolGroup is not None: poolGroupId = item.servicesPoolGroup.uuid @@ -204,7 +203,7 @@ class MetaPools(ModelHandler): }, { 'name': 'servicesPoolGroup_id', - 'choices': [gui.choice_image(-1, typing.cast(str, _('Default')), DEFAULT_THUMB_BASE64)] + 'choices': [gui.choice_image(-1, _('Default'), DEFAULT_THUMB_BASE64)] + gui.sorted_choices( [ gui.choice_image(v.uuid, v.name, v.thumb64) diff --git a/server/src/uds/REST/methods/meta_service_pools.py b/server/src/uds/REST/methods/meta_service_pools.py index eba2f8d33..b568670c9 100644 --- a/server/src/uds/REST/methods/meta_service_pools.py +++ b/server/src/uds/REST/methods/meta_service_pools.py @@ -31,7 +31,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext as _ @@ -147,7 +146,7 @@ class MetaAssignedService(DetailHandler): element['pool_name'] = item.deployed_service.name return element - def _getAssignedService(self, metaPool: models.MetaPool, userServiceId: str) -> models.UserService: + def _get_assigned_userservice(self, metaPool: models.MetaPool, userServiceId: str) -> models.UserService: """ Gets an assigned service and checks that it belongs to this metapool If not found, raises InvalidItemException @@ -185,7 +184,7 @@ class MetaAssignedService(DetailHandler): try: if not item: # All items - result = {} + result: dict[str, typing.Any] = {} for k, props in assignedUserServicesForPools(): result[k.uuid] = MetaAssignedService.item_as_dict(parent, k, props) @@ -193,7 +192,7 @@ class MetaAssignedService(DetailHandler): return MetaAssignedService.item_as_dict( parent, - self._getAssignedService(parent, item), + self._get_assigned_userservice(parent, item), props={ k: v for k, v in models.Properties.objects.filter( @@ -237,7 +236,7 @@ class MetaAssignedService(DetailHandler): def get_logs(self, parent: 'Model', item: str) -> list[typing.Any]: parent = ensure.is_instance(parent, models.MetaPool) try: - asignedService = self._getAssignedService(parent, item) + asignedService = self._get_assigned_userservice(parent, item) logger.debug('Getting logs for %s', asignedService) return log.get_logs(asignedService) except Exception: @@ -245,7 +244,7 @@ class MetaAssignedService(DetailHandler): def delete_item(self, parent: 'Model', item: str) -> None: parent = ensure.is_instance(parent, models.MetaPool) - userService = self._getAssignedService(parent, item) + userService = self._get_assigned_userservice(parent, item) if userService.user: logStr = 'Deleted assigned service {} to user {} by {}'.format( @@ -274,17 +273,17 @@ class MetaAssignedService(DetailHandler): raise self.invalid_item_response() fields = self.fields_from_params(['auth_id', 'user_id']) - service = self._getAssignedService(parent, item) + userservice = self._get_assigned_userservice(parent, item) user = models.User.objects.get(uuid=process_uuid(fields['user_id'])) logStr = 'Changing ownership of service from {} to {} by {}'.format( - service.user.pretty_name if service.user else 'unknown', user.pretty_name, self._user.pretty_name + userservice.user.pretty_name if userservice.user else 'unknown', user.pretty_name, self._user.pretty_name ) # If there is another service that has this same owner, raise an exception if ( - service.deployed_service.userServices.filter(user=user) - .exclude(uuid=service.uuid) + userservice.deployed_service.userServices.filter(user=user) + .exclude(uuid=userservice.uuid) .exclude(state__in=State.INFO_STATES) .count() > 0 @@ -293,8 +292,8 @@ class MetaAssignedService(DetailHandler): 'There is already another user service assigned to {}'.format(user.pretty_name) ) - service.user = user - service.save() + userservice.user = user + userservice.save() # Log change log.log(parent, log.LogLevel.INFO, logStr, log.LogSource.ADMIN) diff --git a/server/src/uds/REST/methods/mfas.py b/server/src/uds/REST/methods/mfas.py index 14f8f76b2..8b4230eb1 100644 --- a/server/src/uds/REST/methods/mfas.py +++ b/server/src/uds/REST/methods/mfas.py @@ -55,7 +55,7 @@ class MFA(ModelHandler): model = models.MFA save_fields = ['name', 'comments', 'tags', 'remember_device', 'validity'] - table_title = typing.cast(str, _('Multi Factor Authentication')) + table_title = _('Multi Factor Authentication') table_fields = [ {'name': {'title': _('Name'), 'visible': True, 'type': 'iconType'}}, {'type_name': {'title': _('Type')}}, diff --git a/server/src/uds/REST/methods/networks.py b/server/src/uds/REST/methods/networks.py index 99ae1cb76..9044e8264 100644 --- a/server/src/uds/REST/methods/networks.py +++ b/server/src/uds/REST/methods/networks.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_lazy as _, gettext @@ -59,7 +58,7 @@ class Networks(ModelHandler): model = Network save_fields = ['name', 'net_string', 'tags'] - table_title = typing.cast(str, _('Networks')) + table_title = _('Networks') table_fields = [ { 'name': { diff --git a/server/src/uds/REST/methods/notifiers.py b/server/src/uds/REST/methods/notifiers.py index a4bc8f381..e3d2e37a8 100644 --- a/server/src/uds/REST/methods/notifiers.py +++ b/server/src/uds/REST/methods/notifiers.py @@ -36,7 +36,6 @@ import collections.abc from django.utils.translation import gettext from django.utils.translation import gettext_lazy as _ -from traitlets import default from uds.core import messaging, types from uds.core.environment import Environment @@ -65,7 +64,7 @@ class Notifiers(ModelHandler): 'enabled', ] - table_title = typing.cast(str, _('Notifiers')) + table_title = _('Notifiers') table_fields = [ {'name': {'title': _('Name'), 'visible': True, 'type': 'iconType'}}, {'type_name': {'title': _('Type')}}, diff --git a/server/src/uds/REST/methods/op_calendars.py b/server/src/uds/REST/methods/op_calendars.py index c2939547e..1ffbf58f4 100644 --- a/server/src/uds/REST/methods/op_calendars.py +++ b/server/src/uds/REST/methods/op_calendars.py @@ -33,7 +33,6 @@ import json import logging import typing -import collections.abc from django.utils.translation import gettext as _ diff --git a/server/src/uds/REST/methods/osmanagers.py b/server/src/uds/REST/methods/osmanagers.py index f7d4c5f7c..970e9c241 100644 --- a/server/src/uds/REST/methods/osmanagers.py +++ b/server/src/uds/REST/methods/osmanagers.py @@ -54,7 +54,7 @@ class OsManagers(ModelHandler): model = OSManager save_fields = ['name', 'comments', 'tags'] - table_title = typing.cast(str, _('OS Managers')) + table_title = _('OS Managers') table_fields = [ {'name': {'title': _('Name'), 'visible': True, 'type': 'iconType'}}, {'type_name': {'title': _('Type')}}, diff --git a/server/src/uds/REST/methods/permissions.py b/server/src/uds/REST/methods/permissions.py index 585b99eb0..2dba4e526 100644 --- a/server/src/uds/REST/methods/permissions.py +++ b/server/src/uds/REST/methods/permissions.py @@ -81,7 +81,7 @@ class Permissions(Handler): def as_dict( perms: collections.abc.Iterable[models.Permissions], ) -> list[dict[str, str]]: - res = [] + res: list[dict[str, typing.Any]] = [] entity: typing.Optional[typing.Union[models.User, models.Group]] for perm in perms: if perm.user is None: @@ -130,8 +130,6 @@ class Permissions(Handler): """ logger.debug('Put args: %s', self._args) - la = len(self._args) - perm = uds.core.types.permissions.PermissionType.from_str(self._params.get('perm', '0')) def add_user_permission(cls_param: str, obj_param: str, user_param: str) -> list[dict[str, str]]: diff --git a/server/src/uds/REST/methods/providers.py b/server/src/uds/REST/methods/providers.py index 521f4c434..028a0d00e 100644 --- a/server/src/uds/REST/methods/providers.py +++ b/server/src/uds/REST/methods/providers.py @@ -66,7 +66,7 @@ class Providers(ModelHandler): save_fields = ['name', 'comments', 'tags'] - table_title = typing.cast(str, _('Service providers')) + table_title = _('Service providers') # Table info fields table_fields = [ @@ -81,7 +81,8 @@ class Providers(ModelHandler): # Field from where to get "class" and prefix for that class, so this will generate "row-state-A, row-state-X, .... table_row_style = types.ui.RowStyleInfo(prefix='row-maintenance-', field='maintenance_mode') - def item_as_dict(self, item: 'Provider') -> types.rest.ItemDictType: + def item_as_dict(self, item: 'Model') -> types.rest.ItemDictType: + item = ensure.is_instance(item, Provider) type_ = item.get_type() # Icon can have a lot of data (1-2 Kbytes), but it's not expected to have a lot of services providers, and even so, this will work fine diff --git a/server/src/uds/REST/methods/reports.py b/server/src/uds/REST/methods/reports.py index efa32e5d2..b533674a7 100644 --- a/server/src/uds/REST/methods/reports.py +++ b/server/src/uds/REST/methods/reports.py @@ -32,8 +32,6 @@ """ import logging import typing -import collections.abc -from click import style from django.utils.translation import gettext_lazy as _ diff --git a/server/src/uds/REST/methods/servers.py b/server/src/uds/REST/methods/servers.py index 78b8d6a2b..b89ddb5ac 100644 --- a/server/src/uds/REST/methods/servers.py +++ b/server/src/uds/REST/methods/servers.py @@ -69,7 +69,7 @@ class ServerRegisterBase(Handler): # Validate parameters try: try: - t = types.servers.ServerType(type) + types.servers.ServerType(type) # try to convert except ValueError: raise ValueError(_('Invalid type. Type must be an integer.')) if len(subtype) > 16: diff --git a/server/src/uds/REST/methods/servers_management.py b/server/src/uds/REST/methods/servers_management.py index 0d8f185e1..ae919029d 100644 --- a/server/src/uds/REST/methods/servers_management.py +++ b/server/src/uds/REST/methods/servers_management.py @@ -31,7 +31,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext from django.utils.translation import gettext_lazy as _ diff --git a/server/src/uds/REST/methods/services_pool_groups.py b/server/src/uds/REST/methods/services_pool_groups.py index ef694f91f..880bde706 100644 --- a/server/src/uds/REST/methods/services_pool_groups.py +++ b/server/src/uds/REST/methods/services_pool_groups.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext from django.utils.translation import gettext_lazy as _ @@ -65,7 +64,7 @@ class ServicesPoolGroups(ModelHandler): model = ServicePoolGroup save_fields = ['name', 'comments', 'image_id', 'priority'] - table_title = typing.cast(str, _('Services Pool Groups')) + table_title = _('Services Pool Groups') table_fields = [ {'priority': {'title': _('Priority'), 'type': 'numeric', 'width': '6em'}}, { diff --git a/server/src/uds/REST/methods/services_pools.py b/server/src/uds/REST/methods/services_pools.py index 6a09849b8..e68df3e92 100644 --- a/server/src/uds/REST/methods/services_pools.py +++ b/server/src/uds/REST/methods/services_pools.py @@ -33,7 +33,6 @@ import datetime import logging import typing -import collections.abc from django.db.models import Count, Q from django.utils.translation import gettext @@ -101,7 +100,7 @@ class ServicesPools(ModelHandler): remove_fields = ['osmanager_id', 'service_id'] - table_title = typing.cast(str, _('Service Pools')) + table_title = _('Service Pools') table_fields = [ {'name': {'title': _('Name')}}, {'state': {'title': _('Status'), 'type': 'dict', 'dict': State.literals_dict()}}, @@ -194,7 +193,7 @@ class ServicesPools(ModelHandler): # This needs a lot of queries, and really does not apport anything important to the report # elif UserServiceManager().canInitiateServiceFromDeployedService(item) is False: # state = State.SLOWED_DOWN - val = { + val: dict[str, typing.Any] = { 'id': item.uuid, 'name': item.name, 'short_name': item.short_name, @@ -614,39 +613,39 @@ class ServicesPools(ModelHandler): return item.fallbackAccess # Returns the action list based on current element, for calendar - def actionsList(self, item: 'Model') -> typing.Any: + def actionsList(self, item: 'Model') -> list[types.calendar.CalendarAction]: item = ensure.is_instance(item, ServicePool) - validActions: tuple[types.calendar.CalendarAction, ...] = () + valid_actions: list[types.calendar.CalendarAction] = [] itemInfo = item.service.get_type() if itemInfo.uses_cache is True: - validActions += ( + valid_actions += [ consts.calendar.CALENDAR_ACTION_INITIAL, consts.calendar.CALENDAR_ACTION_CACHE_L1, consts.calendar.CALENDAR_ACTION_MAX, - ) + ] if itemInfo.uses_cache_l2 is True: - validActions += (consts.calendar.CALENDAR_ACTION_CACHE_L2,) + valid_actions += [consts.calendar.CALENDAR_ACTION_CACHE_L2,] if itemInfo.publication_type is not None: - validActions += (consts.calendar.CALENDAR_ACTION_PUBLISH,) + valid_actions += [consts.calendar.CALENDAR_ACTION_PUBLISH,] # Transport & groups actions - validActions += ( + valid_actions += [ consts.calendar.CALENDAR_ACTION_ADD_TRANSPORT, consts.calendar.CALENDAR_ACTION_DEL_TRANSPORT, consts.calendar.CALENDAR_ACTION_DEL_ALL_TRANSPORTS, consts.calendar.CALENDAR_ACTION_ADD_GROUP, consts.calendar.CALENDAR_ACTION_DEL_GROUP, consts.calendar.CALENDAR_ACTION_DEL_ALL_GROUPS, - ) + ] # Advanced actions - validActions += ( + valid_actions += [ consts.calendar.CALENDAR_ACTION_IGNORE_UNUSED, consts.calendar.CALENDAR_ACTION_REMOVE_USERSERVICES, consts.calendar.CALENDAR_ACTION_REMOVE_STUCK_USERSERVICES, - ) - return validActions + ] + return valid_actions # Deprecated, use list_assignables def listAssignables(self, item: 'Model') -> typing.Any: diff --git a/server/src/uds/REST/methods/services_usage.py b/server/src/uds/REST/methods/services_usage.py index 1e1c744f8..d1b318984 100644 --- a/server/src/uds/REST/methods/services_usage.py +++ b/server/src/uds/REST/methods/services_usage.py @@ -33,7 +33,6 @@ import logging import typing -import collections.abc from django.utils.translation import gettext as _ from uds.core import types diff --git a/server/src/uds/REST/methods/system.py b/server/src/uds/REST/methods/system.py index bc0fbb97e..79cd221d9 100644 --- a/server/src/uds/REST/methods/system.py +++ b/server/src/uds/REST/methods/system.py @@ -170,10 +170,10 @@ class System(Handler): 'restrained_services_pools': restrained_services_pools, } - if len(self._args) in (2, 3): + if len(self.args) in (2, 3): # Extract pool if provided pool: typing.Optional[models.ServicePool] = None - if len(self._args) == 3: + if len(self.args) == 3: try: pool = models.ServicePool.objects.get(uuid=process_uuid(self._args[2])) except Exception: @@ -186,14 +186,14 @@ class System(Handler): self._user, typing.cast('Model', pool), types.permissions.PermissionType.READ ): raise exceptions.rest.AccessDenied() - if self._args[0] == 'stats': - if self._args[1] == 'assigned': + if self.args[0] == 'stats': + if self.args[1] == 'assigned': return get_servicepools_counters(pool, counters.types.stats.CounterType.ASSIGNED) - elif self._args[1] == 'inuse': + elif self.args[1] == 'inuse': return get_servicepools_counters(pool, counters.types.stats.CounterType.INUSE) - elif self._args[1] == 'cached': + elif self.args[1] == 'cached': return get_servicepools_counters(pool, counters.types.stats.CounterType.CACHED) - elif self._args[1] == 'complete': + elif self.args[1] == 'complete': return { 'assigned': get_servicepools_counters( pool, counters.types.stats.CounterType.ASSIGNED, since_days=7 diff --git a/server/src/uds/REST/methods/tickets.py b/server/src/uds/REST/methods/tickets.py index ee0bf01d1..9ce14eee2 100644 --- a/server/src/uds/REST/methods/tickets.py +++ b/server/src/uds/REST/methods/tickets.py @@ -33,7 +33,6 @@ import datetime import logging import typing -import collections.abc from uds.REST import Handler @@ -93,9 +92,7 @@ class Tickets(Handler): needs_admin = True # By default, staff is lower level needed @staticmethod - def result( - result: str = '', error: typing.Optional[str] = None - ) -> dict[str, typing.Any]: + def result(result: str = '', error: typing.Optional[str] = None) -> dict[str, typing.Any]: """ Returns a result for a Ticket request """ @@ -166,9 +163,7 @@ class Tickets(Handler): # Will raise an exception if no auth found if authId: - auth = models.Authenticator.objects.get( - uuid=process_uuid(authId.lower()) - ) + auth = models.Authenticator.objects.get(uuid=process_uuid(authId.lower())) elif authName: auth = models.Authenticator.objects.get(name=authName) else: @@ -217,42 +212,30 @@ class Tickets(Handler): pool: typing.Union[models.ServicePool, models.MetaPool] try: - pool = typing.cast( - models.MetaPool, models.MetaPool.objects.get(uuid=poolUuid) + pool = models.MetaPool.objects.get( + uuid=poolUuid ) # If not an metapool uuid, will process it as a servicePool if force: # First, add groups to metapool - for addGrp in set(groupIds) - set( - pool.assignedGroups.values_list('uuid', flat=True) - ): + for addGrp in set(groupIds) - set(pool.assignedGroups.values_list('uuid', flat=True)): pool.assignedGroups.add(auth.groups.get(uuid=addGrp)) # And now, to ALL metapool members for metaMember in pool.members.all(): # Now add groups to pools for addGrp in set(groupIds) - set( - metaMember.pool.assignedGroups.values_list( - 'uuid', flat=True - ) + metaMember.pool.assignedGroups.values_list('uuid', flat=True) ): - metaMember.pool.assignedGroups.add( - auth.groups.get(uuid=addGrp) - ) + metaMember.pool.assignedGroups.add(auth.groups.get(uuid=addGrp)) # For metapool, transport is ignored.. servicePoolId = 'M' + pool.uuid - except models.MetaPool.DoesNotExist: - pool = typing.cast( - models.ServicePool, - models.ServicePool.objects.get(uuid=poolUuid), - ) + pool = models.ServicePool.objects.get(uuid=poolUuid) # If forced that servicePool must honor groups if force: - for addGrp in set(groupIds) - set( - pool.assignedGroups.values_list('uuid', flat=True) - ): + for addGrp in set(groupIds) - set(pool.assignedGroups.values_list('uuid', flat=True)): pool.assignedGroups.add(auth.groups.get(uuid=addGrp)) servicePoolId = 'F' + pool.uuid diff --git a/server/src/uds/REST/methods/transports.py b/server/src/uds/REST/methods/transports.py index ea68aebf5..5898c7bac 100644 --- a/server/src/uds/REST/methods/transports.py +++ b/server/src/uds/REST/methods/transports.py @@ -64,7 +64,7 @@ class Transports(ModelHandler): 'label', ] - table_title = typing.cast(str, _('Transports')) + table_title = _('Transports') table_fields = [ {'priority': {'title': _('Priority'), 'type': 'numeric', 'width': '6em'}}, {'name': {'title': _('Name'), 'visible': True, 'type': 'iconType'}}, diff --git a/server/src/uds/REST/methods/tunnels.py b/server/src/uds/REST/methods/tunnels.py index bc16ea9cb..57b55356e 100644 --- a/server/src/uds/REST/methods/tunnels.py +++ b/server/src/uds/REST/methods/tunnels.py @@ -35,9 +35,9 @@ import typing from uds import models from uds.core import exceptions, types -from uds.core.auths.auth import is_source_trusted +from uds.core.auths.auth import is_trusted_source from uds.core.util import log, net -from uds.core.util.model import sql_datetime, sql_stamp_seconds +from uds.core.util.model import sql_stamp_seconds from uds.core.util.stats import events from uds.REST import Handler @@ -71,7 +71,7 @@ class TunnelTicket(Handler): ) if ( - not is_source_trusted(self._request.ip) + not is_trusted_source(self._request.ip) or len(self._args) != 3 or len(self._args[0]) != 48 ): diff --git a/server/src/uds/REST/methods/tunnels_management.py b/server/src/uds/REST/methods/tunnels_management.py index b5f012ce7..e7b3d2b34 100644 --- a/server/src/uds/REST/methods/tunnels_management.py +++ b/server/src/uds/REST/methods/tunnels_management.py @@ -31,13 +31,12 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext from django.utils.translation import gettext_lazy as _ import uds.core.types.permissions -from uds.core import types, consts, ui +from uds.core import types, consts from uds.core.util import permissions, validators, ensure from uds.core.util.model import process_uuid from uds import models @@ -45,7 +44,6 @@ from uds.REST.model import DetailHandler, ModelHandler # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module from django.db.models import Model logger = logging.getLogger(__name__) @@ -64,7 +62,7 @@ class TunnelServers(DetailHandler): q = parent.servers.all().order_by('hostname') else: q = parent.servers.filter(uuid=process_uuid(item)) - res = [] + res: list[dict[str, typing.Any]] = [] i = None for i in q: val = { @@ -150,7 +148,7 @@ class Tunnels(ModelHandler): detail = {'servers': TunnelServers} save_fields = ['name', 'comments', 'host:', 'port:0'] - table_title = typing.cast(str, _('Tunnels')) + table_title = _('Tunnels') table_fields = [ {'name': {'title': _('Name'), 'visible': True, 'type': 'iconType'}}, {'comments': {'title': _('Comments')}}, @@ -216,7 +214,7 @@ class Tunnels(ModelHandler): item = self._args[-1] - if item is None: + if not item: raise self.invalid_item_response('No server specified') try: @@ -226,7 +224,6 @@ class Tunnels(ModelHandler): except Exception: raise self.invalid_item_response() from None - # TODO: implement this return 'ok' def tunnels(self, parent: 'Model') -> typing.Any: diff --git a/server/src/uds/REST/methods/user_services.py b/server/src/uds/REST/methods/user_services.py index aef8e20aa..62b079144 100644 --- a/server/src/uds/REST/methods/user_services.py +++ b/server/src/uds/REST/methods/user_services.py @@ -322,7 +322,6 @@ class Groups(DetailHandler): def get_items(self, parent: 'Model', item: typing.Optional[str]) -> types.rest.ManyItemsDictType: parent = ensure.is_instance(parent, models.ServicePool) - group: models.Group return [ { 'id': group.uuid, @@ -334,7 +333,7 @@ class Groups(DetailHandler): 'type': 'meta' if group.is_meta else 'group', 'auth_name': group.manager.name, } - for group in parent.assignedGroups.all() + for group in typing.cast(collections.abc.Iterable[models.Group], parent.assignedGroups.all()) ] def get_title(self, parent: 'Model') -> str: diff --git a/server/src/uds/REST/model/__init__.py b/server/src/uds/REST/model/__init__.py index cfbf540fe..ed773d379 100644 --- a/server/src/uds/REST/model/__init__.py +++ b/server/src/uds/REST/model/__init__.py @@ -29,6 +29,7 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .base import BaseModelHandler from .detail import DetailHandler from .model import ModelHandler diff --git a/server/src/uds/REST/model/base.py b/server/src/uds/REST/model/base.py index aaa65bfe6..57d694c18 100644 --- a/server/src/uds/REST/model/base.py +++ b/server/src/uds/REST/model/base.py @@ -31,32 +31,25 @@ """ # pylint: disable=too-many-public-methods -import abc -import fnmatch import inspect import logging -import re import typing -import collections.abc -from types import GeneratorType -from django.db import IntegrityError, models +from django.db import models from django.utils.translation import gettext as _ from uds.core import consts from uds.core import exceptions from uds.core import types from uds.core.module import Module -from uds.core.util import log, permissions -from uds.core.util.model import process_uuid -from uds.models import ManagedObjectModel, Network, Tag, TaggingMixin -from uds.REST.utils import rest_result +from uds.core.util import permissions +from uds.models import ManagedObjectModel, Network from ..handlers import Handler # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.models import User + pass logger = logging.getLogger(__name__) diff --git a/server/src/uds/REST/model/detail.py b/server/src/uds/REST/model/detail.py index 761ee5e9a..e5032ec07 100644 --- a/server/src/uds/REST/model/detail.py +++ b/server/src/uds/REST/model/detail.py @@ -31,25 +31,16 @@ """ # pylint: disable=too-many-public-methods -import abc -import fnmatch -import inspect import logging -import re import typing import collections.abc -from types import GeneratorType -from django.db import IntegrityError, models +from django.db import models from django.utils.translation import gettext as _ from uds.core import consts -from uds.core import exceptions from uds.core import types -from uds.core.module import Module -from uds.core.util import log, permissions from uds.core.util.model import process_uuid -from uds.models import ManagedObjectModel, Network, Tag, TaggingMixin from uds.REST.utils import rest_result from .base import BaseModelHandler @@ -62,9 +53,6 @@ if typing.TYPE_CHECKING: logger = logging.getLogger(__name__) - - - # Details do not have types at all # so, right now, we only process details petitions for Handling & tables info # noinspection PyMissingConstructor @@ -94,7 +82,7 @@ class DetailHandler(BaseModelHandler): _parent: typing.Optional['ModelHandler'] _path: str _params: typing.Any # _params is deserialized object from request - _args: tuple[str, ...] + _args: list[str] _kwargs: dict[str, typing.Any] _user: 'User' @@ -114,7 +102,7 @@ class DetailHandler(BaseModelHandler): self._parent = parent_handler self._path = path self._params = params - self._args = args + self._args = list(args) self._kwargs = kwargs self._user = kwargs.get('user', None) @@ -349,4 +337,3 @@ class DetailHandler(BaseModelHandler): :return: a list of log elements (normally got using "uds.core.util.log.get_logs" method) """ raise self.invalid_method_response() - diff --git a/server/src/uds/REST/model/model.py b/server/src/uds/REST/model/model.py index 97cb4bbde..8523d4bc4 100644 --- a/server/src/uds/REST/model/model.py +++ b/server/src/uds/REST/model/model.py @@ -31,14 +31,11 @@ """ # pylint: disable=too-many-public-methods -import abc import fnmatch -import inspect import logging import re import typing import collections.abc -from types import GeneratorType from django.db import IntegrityError, models from django.utils.translation import gettext as _ @@ -48,15 +45,12 @@ from uds.core import exceptions from uds.core import types from uds.core.module import Module from uds.core.util import log, permissions -from uds.core.util.model import process_uuid -from uds.models import ManagedObjectModel, Network, Tag, TaggingMixin -from uds.REST.utils import rest_result +from uds.models import ManagedObjectModel, Tag, TaggingMixin from .base import BaseModelHandler # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.models import User from .detail import DetailHandler logger = logging.getLogger(__name__) diff --git a/server/src/uds/REST/processors.py b/server/src/uds/REST/processors.py index 4de1a6299..d62bd42c1 100644 --- a/server/src/uds/REST/processors.py +++ b/server/src/uds/REST/processors.py @@ -32,11 +32,9 @@ """ import collections.abc import datetime -from gc import collect import json import logging import time -import types import typing from django.http import HttpResponse diff --git a/server/src/uds/REST/utils.py b/server/src/uds/REST/utils.py index 3184b9074..a8ef43c7c 100644 --- a/server/src/uds/REST/utils.py +++ b/server/src/uds/REST/utils.py @@ -29,7 +29,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com ''' import typing -import collections.abc from uds.core.consts.system import VERSION from uds.core.util.model import sql_stamp_seconds diff --git a/server/src/uds/__init__.py b/server/src/uds/__init__.py index 543f19987..466a81b8c 100644 --- a/server/src/uds/__init__.py +++ b/server/src/uds/__init__.py @@ -30,6 +30,7 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false # Make sure that all services are "available" at service startup import logging diff --git a/server/src/uds/admin/views/__init__.py b/server/src/uds/admin/views/__init__.py index 20e6f5d68..58b7e40ed 100644 --- a/server/src/uds/admin/views/__init__.py +++ b/server/src/uds/admin/views/__init__.py @@ -50,8 +50,6 @@ if typing.TYPE_CHECKING: def index(request: 'HttpRequest') -> HttpResponse: # Gets csrf token csrf_token = csrf.get_token(request) - if csrf_token is not None: - csrf_token = str(csrf_token) return render( request, diff --git a/server/src/uds/auths/IP/__init__.py b/server/src/uds/auths/IP/__init__.py index 0efa93e89..d56e30e96 100644 --- a/server/src/uds/auths/IP/__init__.py +++ b/server/src/uds/auths/IP/__init__.py @@ -32,5 +32,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from uds.core.util.config import Config from .authenticator import IPAuth diff --git a/server/src/uds/auths/IP/authenticator.py b/server/src/uds/auths/IP/authenticator.py index 61caf287e..d41fbe841 100644 --- a/server/src/uds/auths/IP/authenticator.py +++ b/server/src/uds/auths/IP/authenticator.py @@ -33,11 +33,10 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core import auths, types, consts +from uds.core import auths, types from uds.core.ui import gui from uds.core.util import net @@ -88,13 +87,13 @@ class IPAuth(auths.Authenticator): ip = ip.split(':')[-1] return ip - def get_groups(self, username: str, groupsManager: 'auths.GroupsManager') -> None: + def get_groups(self, username: str, groups_manager: 'auths.GroupsManager') -> None: # these groups are a bit special. They are in fact ip-ranges, and we must check that the ip is in betwen # The ranges are stored in group names - for g in groupsManager.enumerate_groups_name(): + for g in groups_manager.enumerate_groups_name(): try: if net.contains(g, username): - groupsManager.validate(g) + groups_manager.validate(g) except Exception as e: logger.error('Invalid network for IP auth: %s', e) @@ -102,12 +101,12 @@ class IPAuth(auths.Authenticator): self, username: str, credentials: str, # pylint: disable=unused-argument - groupsManager: 'auths.GroupsManager', + groups_manager: 'auths.GroupsManager', request: 'types.requests.ExtendedHttpRequest', ) -> types.auth.AuthenticationResult: # If credentials is a dict, that can't be sent directly from web interface, we allow entering if username == self.getIp(request): - self.get_groups(username, groupsManager) + self.get_groups(username, groups_manager) return types.auth.SUCCESS_AUTH return types.auth.FAILED_AUTH diff --git a/server/src/uds/auths/InternalDB/__init__.py b/server/src/uds/auths/InternalDB/__init__.py index 943abaea7..33ae82956 100644 --- a/server/src/uds/auths/InternalDB/__init__.py +++ b/server/src/uds/auths/InternalDB/__init__.py @@ -27,9 +27,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - """ - @author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .authenticator import InternalDBAuth diff --git a/server/src/uds/auths/InternalDB/authenticator.py b/server/src/uds/auths/InternalDB/authenticator.py index 266ac5c2a..ca7daee7d 100644 --- a/server/src/uds/auths/InternalDB/authenticator.py +++ b/server/src/uds/auths/InternalDB/authenticator.py @@ -32,7 +32,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import logging import typing @@ -40,7 +39,7 @@ import dns.resolver import dns.reversename from django.utils.translation import gettext_noop as _ -from uds.core import auths, consts, exceptions, types +from uds.core import auths, types from uds.core.auths.auth import log_login from uds.core.managers.crypto import CryptoManager from uds.core.types.states import State @@ -100,7 +99,7 @@ class InternalDBAuth(auths.Authenticator): ip = request.ip_proxy if self.accepts_proxy.as_bool() else request.ip # pylint: disable=maybe-no-member if self.reverse_dns.as_bool(): try: - return str(dns.resolver.query(dns.reversename.from_address(ip).to_text(), 'PTR')[0]) + return str(dns.resolver.query(dns.reversename.from_address(ip).to_text(), 'PTR')[0]) # pyright: ignore[reportUnknownArgumentType] except Exception: # if we can't get the reverse, we will use the ip pass @@ -175,7 +174,7 @@ class InternalDBAuth(auths.Authenticator): log_login(request, self.db_obj(), username, 'Invalid password') return types.auth.FAILED_AUTH - def get_groups(self, username: str, groupsManager: 'auths.GroupsManager') -> None: + def get_groups(self, username: str, groups_manager: 'auths.GroupsManager') -> None: dbAuth = self.db_obj() try: user: 'models.User' = dbAuth.users.get(name=username.lower(), state=State.ACTIVE) @@ -188,7 +187,7 @@ class InternalDBAuth(auths.Authenticator): grps.extend([g.name for g in parent.groups.all()]) except Exception: pass - groupsManager.validate(grps) + groups_manager.validate(grps) def get_real_name(self, username: str) -> str: # Return the real name of the user, if it is set @@ -198,7 +197,7 @@ class InternalDBAuth(auths.Authenticator): except Exception: return super().get_real_name(username) - def create_user(self, usrData: dict[str, typing.Any]) -> None: + def create_user(self, user_data: dict[str, typing.Any]) -> None: pass @staticmethod diff --git a/server/src/uds/auths/OAuth2/__init__.py b/server/src/uds/auths/OAuth2/__init__.py index f1b86b55a..69ce83651 100644 --- a/server/src/uds/auths/OAuth2/__init__.py +++ b/server/src/uds/auths/OAuth2/__init__.py @@ -34,4 +34,5 @@ take care of registering it as provider Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .authenticator import OAuth2Authenticator diff --git a/server/src/uds/auths/OAuth2/authenticator.py b/server/src/uds/auths/OAuth2/authenticator.py index 236ce2282..1b82a815b 100644 --- a/server/src/uds/auths/OAuth2/authenticator.py +++ b/server/src/uds/auths/OAuth2/authenticator.py @@ -41,22 +41,18 @@ import datetime import urllib.parse from base64 import b64decode -import defusedxml.ElementTree as etree import jwt import requests from django.utils.translation import gettext from django.utils.translation import gettext_noop as _ from uds.core import auths, consts, exceptions, types -from uds.core.managers.crypto import CryptoManager from uds.core.ui import gui from uds.core.util import fields, model, auth as auth_utils if typing.TYPE_CHECKING: from django.http import HttpRequest - from cryptography.x509 import Certificate - logger = logging.getLogger(__name__) # Alphabet used for PKCE @@ -64,6 +60,7 @@ PKCE_ALPHABET: typing.Final[str] = string.ascii_letters + string.digits + '-._~' # Length of the State parameter STATE_LENGTH: typing.Final[int] = 16 + @dataclasses.dataclass class TokenInfo: access_token: str @@ -450,7 +447,9 @@ class OAuth2Authenticator(auths.Authenticator): if self.responseType.value in ('code', 'pkce', 'openid+code'): if self.commonGroups.value.strip() == '': - raise exceptions.ui.ValidationError(gettext('Common groups is required for "code" response types')) + raise exceptions.ui.ValidationError( + gettext('Common groups is required for "code" response types') + ) if self.tokenEndpoint.value.strip() == '': raise exceptions.ui.ValidationError( gettext('Token endpoint is required for "code" response types') diff --git a/server/src/uds/auths/Radius/__init__.py b/server/src/uds/auths/Radius/__init__.py index 185c3e90c..734bf01ee 100644 --- a/server/src/uds/auths/Radius/__init__.py +++ b/server/src/uds/auths/Radius/__init__.py @@ -26,12 +26,11 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - """ Sample authenticator. We import here the module, and uds.auths module will take care of registering it as provider Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .authenticator import RadiusAuth diff --git a/server/src/uds/auths/Radius/authenticator.py b/server/src/uds/auths/Radius/authenticator.py index 45b1b9ff0..2cfe3ed4e 100644 --- a/server/src/uds/auths/Radius/authenticator.py +++ b/server/src/uds/auths/Radius/authenticator.py @@ -32,11 +32,10 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core import auths, environment, types, consts +from uds.core import auths, environment, types from uds.core.auths.auth import log_login from uds.core.managers.crypto import CryptoManager from uds.core.ui import gui @@ -45,8 +44,7 @@ from . import client if typing.TYPE_CHECKING: from uds.core.types.requests import ExtendedHttpRequest - from uds.core.environment import Environment - + logger = logging.getLogger(__name__) @@ -146,7 +144,7 @@ class RadiusAuth(auths.Authenticator): self, username: str, credentials: str, - groupsManager: 'auths.GroupsManager', + groups_manager: 'auths.GroupsManager', request: 'ExtendedHttpRequest', ) -> types.auth.AuthenticationResult: try: @@ -181,15 +179,15 @@ class RadiusAuth(auths.Authenticator): storage[username] = groups # Validate groups - groupsManager.validate(groups) + groups_manager.validate(groups) return types.auth.SUCCESS_AUTH - def get_groups(self, username: str, groupsManager: 'auths.GroupsManager') -> None: + def get_groups(self, username: str, groups_manager: 'auths.GroupsManager') -> None: with self.storage.as_dict() as storage: - groupsManager.validate(storage.get(username, [])) + groups_manager.validate(storage.get(username, [])) - def create_user(self, usrData: dict[str, str]) -> None: + def create_user(self, user_data: dict[str, str]) -> None: pass def remove_user(self, username: str) -> None: diff --git a/server/src/uds/auths/Radius/client.py b/server/src/uds/auths/Radius/client.py index 0b599f737..90cc7eeb7 100644 --- a/server/src/uds/auths/Radius/client.py +++ b/server/src/uds/auths/Radius/client.py @@ -1,3 +1,36 @@ +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2021 Virtual Cable S.L.U. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnknownMemberType=false import dataclasses import io import logging diff --git a/server/src/uds/auths/RegexLdap/__init__.py b/server/src/uds/auths/RegexLdap/__init__.py index 56189c3bb..2e5df0779 100644 --- a/server/src/uds/auths/RegexLdap/__init__.py +++ b/server/src/uds/auths/RegexLdap/__init__.py @@ -26,10 +26,8 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - """ - @author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .authenticator import RegexLdap diff --git a/server/src/uds/auths/RegexLdap/authenticator.py b/server/src/uds/auths/RegexLdap/authenticator.py index 82ae1845f..eae685e5e 100644 --- a/server/src/uds/auths/RegexLdap/authenticator.py +++ b/server/src/uds/auths/RegexLdap/authenticator.py @@ -32,14 +32,12 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging -import re import typing import collections.abc -import ldap from django.utils.translation import gettext_noop as _ -from uds.core import auths, environment, exceptions, types, consts +from uds.core import auths, environment, exceptions, types from uds.core.auths.auth import log_login from uds.core.ui import gui from uds.core.util import ensure, ldaputil, auth as auth_utils, fields @@ -52,8 +50,6 @@ except Exception: # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds import models - from uds.core.environment import Environment from uds.core.types.requests import ExtendedHttpRequest logger = logging.getLogger(__name__) @@ -368,7 +364,7 @@ class RegexLdap(auths.Authenticator): self, username: str, credentials: str, - groupsManager: 'auths.GroupsManager', + groups_manager: 'auths.GroupsManager', request: 'ExtendedHttpRequest', ) -> types.auth.AuthenticationResult: """ @@ -404,7 +400,7 @@ class RegexLdap(auths.Authenticator): usr[self.mfa_attribute.value][0], ) - groupsManager.validate(self._get_groups(usr)) + groups_manager.validate(self._get_groups(usr)) return types.auth.SUCCESS_AUTH diff --git a/server/src/uds/auths/SAML/__init__.py b/server/src/uds/auths/SAML/__init__.py index ab0afa3e2..52fa66de7 100644 --- a/server/src/uds/auths/SAML/__init__.py +++ b/server/src/uds/auths/SAML/__init__.py @@ -29,5 +29,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from uds.core import managers from .saml import SAMLAuthenticator # import for registration on space, diff --git a/server/src/uds/auths/SAML/saml.py b/server/src/uds/auths/SAML/saml.py index e12060bbd..bbceb3e17 100644 --- a/server/src/uds/auths/SAML/saml.py +++ b/server/src/uds/auths/SAML/saml.py @@ -44,16 +44,15 @@ from onelogin.saml2.auth import OneLogin_Saml2_Auth from onelogin.saml2.idp_metadata_parser import OneLogin_Saml2_IdPMetadataParser from onelogin.saml2.settings import OneLogin_Saml2_Settings -from uds.core import auths, exceptions, types, consts +from uds.core import auths, exceptions, types from uds.core.managers.crypto import CryptoManager from uds.core.types.requests import ExtendedHttpRequest from uds.core.ui import gui -from uds.core.util import security, decorators, ensure, auth as auth_utils +from uds.core.util import security, decorators, auth as auth_utils from uds.core.util.model import sql_datetime # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from django.http import HttpRequest from urllib.parse import ParseResult from uds.core.types.requests import ExtendedHttpRequestWithUser @@ -822,4 +821,4 @@ class SAMLAuthenticator(auths.Authenticator): Clean ups storage data """ self.storage.remove(username) - self.mfa_clean(username) \ No newline at end of file + self.mfa_clean(username) diff --git a/server/src/uds/auths/Sample/__init__.py b/server/src/uds/auths/Sample/__init__.py index d3deab24a..435a7dd10 100644 --- a/server/src/uds/auths/Sample/__init__.py +++ b/server/src/uds/auths/Sample/__init__.py @@ -34,6 +34,5 @@ take care of registering it as provider Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .sample_auth import SampleAuth - -__updated__ = '2014-02-19' diff --git a/server/src/uds/auths/Sample/sample_auth.py b/server/src/uds/auths/Sample/sample_auth.py index 4ecdfcf71..d35262212 100644 --- a/server/src/uds/auths/Sample/sample_auth.py +++ b/server/src/uds/auths/Sample/sample_auth.py @@ -37,13 +37,12 @@ import collections.abc from django.utils.translation import gettext_noop as _ from uds.core.types.requests import ExtendedHttpRequest from uds.core.ui import gui -from uds.core import auths, exceptions, types, consts +from uds.core import auths, exceptions, types if typing.TYPE_CHECKING: from django.http import ( HttpRequest, ) # pylint: disable=ungrouped-imports - from uds.core.types.requests import ExtendedHttpRequestWithUser from uds.core.auths.groups_manager import GroupsManager @@ -161,7 +160,7 @@ class SampleAuth(auths.Authenticator): self, username: str, credentials: str, - groupsManager: 'GroupsManager', + groups_manager: 'GroupsManager', request: 'ExtendedHttpRequest', # pylint: disable=unused-argument ) -> types.auth.AuthenticationResult: """ @@ -212,13 +211,13 @@ class SampleAuth(auths.Authenticator): # two letters equals to the groups names known by UDS # For this, we will ask the groups manager for the groups names, and will check that and, # if the user match this criteria, will mark that group as valid - for g in groupsManager.enumerate_groups_name(): + for g in groups_manager.enumerate_groups_name(): if len(set(g.lower()).intersection(username.lower())) >= 2: - groupsManager.validate(g) + groups_manager.validate(g) return types.auth.SUCCESS_AUTH - def get_groups(self, username: str, groupsManager: 'auths.GroupsManager') -> None: + def get_groups(self, username: str, groups_manager: 'auths.GroupsManager') -> None: """ As with authenticator part related to groupsManager, this method will fill the groups to which the specified username belongs to. @@ -229,9 +228,9 @@ class SampleAuth(auths.Authenticator): In our case, we simply repeat the process that we also do at authenticate """ - for g in groupsManager.enumerate_groups_name(): + for g in groups_manager.enumerate_groups_name(): if len(set(g.lower()).intersection(username.lower())) >= 2: - groupsManager.validate(g) + groups_manager.validate(g) def get_javascript(self, request: 'HttpRequest') -> typing.Optional[str]: # pylint: disable=unused-argument """ @@ -263,7 +262,7 @@ class SampleAuth(auths.Authenticator): def auth_callback( self, parameters: 'types.auth.AuthCallbackParams', - gm: 'GroupsManager', + groups_manager: 'GroupsManager', request: 'types.requests.ExtendedHttpRequest', ) -> types.auth.AuthenticationResult: """ @@ -283,7 +282,7 @@ class SampleAuth(auths.Authenticator): return types.auth.AuthenticationResult(types.auth.AuthenticationState.SUCCESS, username=user) - def create_user(self, usrData: dict[str, str]) -> None: + def create_user(self, user_data: dict[str, str]) -> None: """ This method provides a "check oportunity" to authenticators for users created manually at administration interface. @@ -301,10 +300,10 @@ class SampleAuth(auths.Authenticator): """ from uds.core.types.states import State # pylint: disable=import-outside-toplevel - usrData['real_name'] = usrData['name'] + ' ' + usrData['name'] - usrData['state'] = State.INACTIVE + user_data['real_name'] = user_data['name'] + ' ' + user_data['name'] + user_data['state'] = State.INACTIVE - def modify_user(self, usrData: dict[str, str]) -> None: + def modify_user(self, user_data: dict[str, str]) -> None: """ This method provides a "check opportunity" to authenticator for users modified at administration interface. @@ -321,5 +320,5 @@ class SampleAuth(auths.Authenticator): Here, we will simply update the realName of the user, and (we have to take care this this kind of things) modify the userName to a new one, the original plus '-1' """ - usrData['real_name'] = usrData['name'] + ' ' + usrData['name'] - usrData['name'] += '-1' + user_data['real_name'] = user_data['name'] + ' ' + user_data['name'] + user_data['name'] += '-1' diff --git a/server/src/uds/auths/SimpleLDAP/__init__.py b/server/src/uds/auths/SimpleLDAP/__init__.py index b11b2db15..faad57f8a 100644 --- a/server/src/uds/auths/SimpleLDAP/__init__.py +++ b/server/src/uds/auths/SimpleLDAP/__init__.py @@ -26,10 +26,8 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - ''' - -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com ''' +# pyright: reportUnusedImport=false from .authenticator import SimpleLDAPAuthenticator diff --git a/server/src/uds/auths/SimpleLDAP/authenticator.py b/server/src/uds/auths/SimpleLDAP/authenticator.py index 819ba7539..a2edb6158 100644 --- a/server/src/uds/auths/SimpleLDAP/authenticator.py +++ b/server/src/uds/auths/SimpleLDAP/authenticator.py @@ -33,11 +33,11 @@ import logging import typing import collections.abc -import ldap +import ldap # pyright: ignore # Needed to import ldap.filter without errors import ldap.filter from django.utils.translation import gettext_noop as _ -from uds.core import auths, environment, types, consts, exceptions +from uds.core import auths, environment, types, exceptions from uds.core.auths.auth import log_login from uds.core.ui import gui from uds.core.util import ensure, fields, ldaputil, validators @@ -351,7 +351,7 @@ class SimpleLDAPAuthenticator(auths.Authenticator): self, username: str, credentials: str, - groupsManager: 'auths.GroupsManager', + groups_manager: 'auths.GroupsManager', request: 'ExtendedHttpRequest', ) -> types.auth.AuthenticationResult: ''' @@ -385,7 +385,7 @@ class SimpleLDAPAuthenticator(auths.Authenticator): user[self.mfa_attribute.as_str()][0], ) - groupsManager.validate(self._get_groups(user)) + groups_manager.validate(self._get_groups(user)) return types.auth.SUCCESS_AUTH diff --git a/server/src/uds/core/auths/__init__.py b/server/src/uds/core/auths/__init__.py index a6610fcb1..331aaf6a0 100644 --- a/server/src/uds/core/auths/__init__.py +++ b/server/src/uds/core/auths/__init__.py @@ -32,6 +32,7 @@ UDS authentication related interfaces and classes Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .authenticator import ( Authenticator, ) diff --git a/server/src/uds/core/auths/auth.py b/server/src/uds/core/auths/auth.py index f3312a6c4..e5e2a7344 100644 --- a/server/src/uds/core/auths/auth.py +++ b/server/src/uds/core/auths/auth.py @@ -180,7 +180,7 @@ def web_login_required( # Helper for checking if requests is from trusted source -def is_source_trusted(ip: str) -> bool: +def is_trusted_source(ip: str) -> bool: return net.contains(GlobalConfig.TRUSTED_SOURCES.get(True), ip) @@ -198,7 +198,7 @@ def needs_trusted_source( Wrapped function for decorator """ try: - if not is_source_trusted(request.ip): + if not is_trusted_source(request.ip): return HttpResponseForbidden() except Exception: logger.warning( @@ -240,7 +240,7 @@ def register_user( usr = authenticator.get_or_create_user(username, username) usr.real_name = authInstance.get_real_name(username) usr.save() - if usr is not None and State.from_str(usr.state).is_active(): + if usr and State.from_str(usr.state).is_active(): # Now we update database groups for this user usr.get_manager().recreate_groups(usr) # And add an login event @@ -380,10 +380,8 @@ def authenticate_info_url(authenticator: typing.Union[str, bytes, models.Authent name = authenticator elif isinstance(authenticator, bytes): name = authenticator.decode('utf8') - elif isinstance(authenticator, models.Authenticator): - name = authenticator.small_name else: - raise ValueError('Invalid authenticator type') + name = authenticator.small_name return reverse('page.auth.info', kwargs={'authenticator_name': name}) @@ -540,7 +538,7 @@ def log_login( def log_logout(request: 'ExtendedHttpRequest') -> None: if request.user: - if request.user.manager.id is not None: + if request.user.manager.id: log.log( request.user.manager, log.LogLevel.INFO, diff --git a/server/src/uds/core/auths/authenticator.py b/server/src/uds/core/auths/authenticator.py index ca9a5415c..085de0b70 100644 --- a/server/src/uds/core/auths/authenticator.py +++ b/server/src/uds/core/auths/authenticator.py @@ -322,7 +322,7 @@ class Authenticator(Module): self, username: str, credentials: str, - groupsManager: 'GroupsManager', + groups_manager: 'GroupsManager', request: 'types.requests.ExtendedHttpRequest', ) -> types.auth.AuthenticationResult: """ @@ -364,7 +364,7 @@ class Authenticator(Module): """ return types.auth.FAILED_AUTH - def is_ip_allowed(self, request: 'HttpRequest') -> bool: + def is_ip_allowed(self, request: 'types.requests.ExtendedHttpRequest') -> bool: """ Used by the login interface to determine if the authenticator is visible on the login page. """ @@ -372,7 +372,7 @@ class Authenticator(Module): if not self.db_obj().id: return True return self.db_obj().state != consts.auth.DISABLED and self.db_obj().is_ip_allowed( - typing.cast('types.requests.ExtendedHttpRequest', request).ip + request.ip ) def transformed_username( diff --git a/server/src/uds/core/auths/authfactory.py b/server/src/uds/core/auths/authfactory.py index d4a81ad7a..db42abd61 100644 --- a/server/src/uds/core/auths/authfactory.py +++ b/server/src/uds/core/auths/authfactory.py @@ -31,7 +31,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc from uds.core.util import factory diff --git a/server/src/uds/core/auths/group.py b/server/src/uds/core/auths/group.py index 6333a8441..7d18c96db 100644 --- a/server/src/uds/core/auths/group.py +++ b/server/src/uds/core/auths/group.py @@ -33,7 +33,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc # Imports for type checking if typing.TYPE_CHECKING: diff --git a/server/src/uds/core/auths/user.py b/server/src/uds/core/auths/user.py index cf888d316..ae5884042 100644 --- a/server/src/uds/core/auths/user.py +++ b/server/src/uds/core/auths/user.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from .group import Group from .groups_manager import GroupsManager diff --git a/server/src/uds/core/consts/__init__.py b/server/src/uds/core/consts/__init__.py index 780d36ea4..beb743293 100644 --- a/server/src/uds/core/consts/__init__.py +++ b/server/src/uds/core/consts/__init__.py @@ -30,7 +30,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc +# pyright: reportUnusedImport=false import time import typing from datetime import datetime diff --git a/server/src/uds/core/consts/actor.py b/server/src/uds/core/consts/actor.py index 57f9999ee..4cafe821e 100644 --- a/server/src/uds/core/consts/actor.py +++ b/server/src/uds/core/consts/actor.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc MANAGED: typing.Final[str] = 'managed' UNMANAGED: typing.Final[str] = 'unmanaged' # matches the definition of UDS Actors OFC diff --git a/server/src/uds/core/consts/auth.py b/server/src/uds/core/consts/auth.py index 65fc40d4e..e4af3a4be 100644 --- a/server/src/uds/core/consts/auth.py +++ b/server/src/uds/core/consts/auth.py @@ -30,9 +30,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -from re import X import typing -import collections.abc # Constants for Visibility VISIBLE: typing.Final[str] = 'v' diff --git a/server/src/uds/core/consts/cache.py b/server/src/uds/core/consts/cache.py index 502e1e45d..4a2f32e84 100644 --- a/server/src/uds/core/consts/cache.py +++ b/server/src/uds/core/consts/cache.py @@ -30,9 +30,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -from pickle import LONG import typing -import collections.abc # Default timeouts, in seconds diff --git a/server/src/uds/core/consts/net.py b/server/src/uds/core/consts/net.py index a4cd369fb..37a8b7289 100644 --- a/server/src/uds/core/consts/net.py +++ b/server/src/uds/core/consts/net.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc # Request related timeouts, etc.. DEFAULT_REQUEST_TIMEOUT: typing.Final[int] = 20 # In seconds diff --git a/server/src/uds/core/consts/os.py b/server/src/uds/core/consts/os.py index cd54d5962..e0151860d 100644 --- a/server/src/uds/core/consts/os.py +++ b/server/src/uds/core/consts/os.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import re import typing -import collections.abc from uds.core import types diff --git a/server/src/uds/core/consts/rest.py b/server/src/uds/core/consts/rest.py index 139c324c9..30d7bdf4b 100644 --- a/server/src/uds/core/consts/rest.py +++ b/server/src/uds/core/consts/rest.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc # REST API requests (..../REST/.../overview, ..../REST/.../types, etc) OVERVIEW: typing.Final[str] = 'overview' diff --git a/server/src/uds/core/consts/system.py b/server/src/uds/core/consts/system.py index ffde26e4d..ca8eea8ae 100644 --- a/server/src/uds/core/consts/system.py +++ b/server/src/uds/core/consts/system.py @@ -32,18 +32,17 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import time import typing -import collections.abc from django.conf import settings if typing.TYPE_CHECKING: - from uds.core.util.unique_id_generator import UniqueGenerator + pass # UDS Version related -VERSION = '4.0.0' -VERSION_STAMP = f'{time.strftime("%Y%m%d")}' +VERSION: typing.Final[str] = '4.0.0' +VERSION_STAMP: typing.Final[str] = f'{time.strftime("%Y%m%d")}' # Minimal uds client version required to connect to this server -REQUIRED_CLIENT_VERSION = '3.6.0' +REQUIRED_CLIENT_VERSION: typing.Final[str] = '3.6.0' # Max size of a rest request body MAX_REQUEST_SIZE: typing.Final[int] = int( diff --git a/server/src/uds/core/consts/ui.py b/server/src/uds/core/consts/ui.py index e79c02435..57eb97797 100644 --- a/server/src/uds/core/consts/ui.py +++ b/server/src/uds/core/consts/ui.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc # Old encryption key, log ago deprecated, but still here for reading old data UDSB: typing.Final[bytes] = b'udsprotect' diff --git a/server/src/uds/core/exceptions/__init__.py b/server/src/uds/core/exceptions/__init__.py index 9c3b86d62..dd948caa0 100644 --- a/server/src/uds/core/exceptions/__init__.py +++ b/server/src/uds/core/exceptions/__init__.py @@ -30,6 +30,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from . import actor from . import auth from . import service diff --git a/server/src/uds/core/jobs/__init__.py b/server/src/uds/core/jobs/__init__.py index 5babaccf0..53cff7663 100644 --- a/server/src/uds/core/jobs/__init__.py +++ b/server/src/uds/core/jobs/__init__.py @@ -32,8 +32,8 @@ UDS jobs related modules Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false import typing -import collections.abc from .job import Job from .delayed_task import DelayedTask diff --git a/server/src/uds/core/jobs/delayed_task.py b/server/src/uds/core/jobs/delayed_task.py index f78c576c6..1b4b4938c 100644 --- a/server/src/uds/core/jobs/delayed_task.py +++ b/server/src/uds/core/jobs/delayed_task.py @@ -31,7 +31,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import logging from uds.core.environment import Environmentable, Environment diff --git a/server/src/uds/core/jobs/delayed_task_runner.py b/server/src/uds/core/jobs/delayed_task_runner.py index 69e773b56..23075f134 100644 --- a/server/src/uds/core/jobs/delayed_task_runner.py +++ b/server/src/uds/core/jobs/delayed_task_runner.py @@ -36,7 +36,6 @@ from socket import gethostname from datetime import timedelta import logging import typing -import collections.abc from django.db import connections from django.db import transaction, OperationalError diff --git a/server/src/uds/core/jobs/job.py b/server/src/uds/core/jobs/job.py index 7fd23c83c..1a62bc70b 100644 --- a/server/src/uds/core/jobs/job.py +++ b/server/src/uds/core/jobs/job.py @@ -30,7 +30,6 @@ """ import logging import typing -import collections.abc from uds.core.environment import Environmentable from uds.core.util.config import Config diff --git a/server/src/uds/core/jobs/jobs_factory.py b/server/src/uds/core/jobs/jobs_factory.py index 208b37033..bee668a4b 100644 --- a/server/src/uds/core/jobs/jobs_factory.py +++ b/server/src/uds/core/jobs/jobs_factory.py @@ -33,7 +33,6 @@ import datetime import logging import typing -import collections.abc from uds.core.util import factory diff --git a/server/src/uds/core/jobs/scheduler.py b/server/src/uds/core/jobs/scheduler.py index fee6cab0b..573efdf76 100644 --- a/server/src/uds/core/jobs/scheduler.py +++ b/server/src/uds/core/jobs/scheduler.py @@ -31,7 +31,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import platform import threading import time diff --git a/server/src/uds/core/managers/__init__.py b/server/src/uds/core/managers/__init__.py index 589fff0e1..0d681f3cd 100644 --- a/server/src/uds/core/managers/__init__.py +++ b/server/src/uds/core/managers/__init__.py @@ -33,11 +33,9 @@ UDS managers (downloads, users publications, ...) Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc # Imports for type checking only (not on runtime), we have later to get rid of false "redefined outer names" for pylint if typing.TYPE_CHECKING: - from .crypto import CryptoManager from .task import TaskManager from .downloads import DownloadsManager from .log import LogManager @@ -62,11 +60,13 @@ def log_manager() -> 'LogManager': return LogManager() + def publication_manager() -> 'PublicationManager': from .publication import PublicationManager return PublicationManager() + def notifications_manager() -> 'NotificationsManager': from .notifications import NotificationsManager diff --git a/server/src/uds/core/managers/crypto.py b/server/src/uds/core/managers/crypto.py index 25574dd89..a224329ca 100644 --- a/server/src/uds/core/managers/crypto.py +++ b/server/src/uds/core/managers/crypto.py @@ -39,7 +39,6 @@ import re import string import logging import typing -import collections.abc import secrets # For password secrets diff --git a/server/src/uds/core/managers/downloads.py b/server/src/uds/core/managers/downloads.py index 3a80e62c5..ac588baf6 100644 --- a/server/src/uds/core/managers/downloads.py +++ b/server/src/uds/core/managers/downloads.py @@ -33,8 +33,6 @@ import os import logging -import typing -import collections.abc from wsgiref.util import FileWrapper from django.http import HttpResponse, Http404, HttpRequest diff --git a/server/src/uds/core/managers/log/__init__.py b/server/src/uds/core/managers/log/__init__.py index f18ba682a..28f8c63a9 100644 --- a/server/src/uds/core/managers/log/__init__.py +++ b/server/src/uds/core/managers/log/__init__.py @@ -25,5 +25,8 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false from .manager import LogManager diff --git a/server/src/uds/core/managers/log/manager.py b/server/src/uds/core/managers/log/manager.py index 8e2e3fb0d..e6e68de53 100644 --- a/server/src/uds/core/managers/log/manager.py +++ b/server/src/uds/core/managers/log/manager.py @@ -31,7 +31,6 @@ """ # import traceback import typing -import collections.abc import logging from uds.core.util import singleton @@ -44,7 +43,6 @@ from uds.core.types.log import LogObjectType # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from django.db.models import Model - from uds import models logger = logging.getLogger(__name__) @@ -71,8 +69,6 @@ class LogManager(metaclass=singleton.Singleton): # Ensure message fits on space message = str(message)[:4096] - qs = Log.objects.filter(owner_id=owner_id, owner_type=owner_type.value) - # now, we add new log try: Log.objects.create( diff --git a/server/src/uds/core/managers/notifications.py b/server/src/uds/core/managers/notifications.py index 0317f694a..23e2acd2b 100644 --- a/server/src/uds/core/managers/notifications.py +++ b/server/src/uds/core/managers/notifications.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.apps import apps from django.db import connections @@ -40,7 +39,7 @@ from uds.core.util import singleton from uds.core.util.log import LogLevel if typing.TYPE_CHECKING: - from ..messaging import provider + pass logger = logging.getLogger(__name__) diff --git a/server/src/uds/core/managers/publication.py b/server/src/uds/core/managers/publication.py index fab34d06a..143cf2740 100644 --- a/server/src/uds/core/managers/publication.py +++ b/server/src/uds/core/managers/publication.py @@ -30,7 +30,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import logging import datetime diff --git a/server/src/uds/core/managers/servers.py b/server/src/uds/core/managers/servers.py index 0d9ca9201..36523da3a 100644 --- a/server/src/uds/core/managers/servers.py +++ b/server/src/uds/core/managers/servers.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import datetime import logging import typing -import collections.abc from concurrent.futures import ThreadPoolExecutor from django.db import transaction diff --git a/server/src/uds/core/managers/task.py b/server/src/uds/core/managers/task.py index a447cdfa6..676c5c2f3 100644 --- a/server/src/uds/core/managers/task.py +++ b/server/src/uds/core/managers/task.py @@ -34,7 +34,6 @@ import time import signal import logging import typing -import collections.abc from django.db import connection from uds.core.jobs.scheduler import Scheduler @@ -105,7 +104,7 @@ class TaskManager(metaclass=singleton.Singleton): logger.info("Registering sheduled tasks") # Simply import this to make workers "register" themselves - from uds.core import workers # pylint: disable=unused-import, import-outside-toplevel + from uds.core import workers # pyright: ignore[reportUnusedImport] def add_other_tasks(self) -> None: logger.info("Registering other tasks") diff --git a/server/src/uds/core/managers/userservice.py b/server/src/uds/core/managers/userservice.py index 5dd4e6288..3445475a0 100644 --- a/server/src/uds/core/managers/userservice.py +++ b/server/src/uds/core/managers/userservice.py @@ -34,7 +34,6 @@ import logging import operator import random import typing -import collections.abc from django.db import transaction from django.db.models import Q @@ -460,7 +459,7 @@ class UserServiceManager(metaclass=singleton.Singleton): # Now find if there is a preparing one with transaction.atomic(): - caches = ( + caches = list( service_pool.cached_users_services() .select_for_update() .filter(cache_level=services.UserService.L1_CACHE, state=State.PREPARING)[:1] diff --git a/server/src/uds/core/managers/userservice_helpers/comms.py b/server/src/uds/core/managers/userservice_helpers/comms.py index cf432e20b..cfe07c8f6 100644 --- a/server/src/uds/core/managers/userservice_helpers/comms.py +++ b/server/src/uds/core/managers/userservice_helpers/comms.py @@ -28,7 +28,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import base64 import json import logging import os @@ -97,9 +96,9 @@ def _execute_actor_request( verify=verify, timeout=TIMEOUT, ) - if verify: + if not(isinstance(verify, bool)): try: - os.remove(typing.cast(str, verify)) + os.remove(verify) except Exception: logger.exception('removing verify') js = r.json() diff --git a/server/src/uds/core/managers/userservice_helpers/opchecker.py b/server/src/uds/core/managers/userservice_helpers/opchecker.py index 95f3ba4b3..3298a1465 100644 --- a/server/src/uds/core/managers/userservice_helpers/opchecker.py +++ b/server/src/uds/core/managers/userservice_helpers/opchecker.py @@ -33,7 +33,6 @@ import abc import logging import typing -import collections.abc from uds.core import services, types from uds.core.jobs.delayed_task import DelayedTask diff --git a/server/src/uds/core/messaging/__init__.py b/server/src/uds/core/messaging/__init__.py index 4d78c12c7..2cbd908b2 100644 --- a/server/src/uds/core/messaging/__init__.py +++ b/server/src/uds/core/messaging/__init__.py @@ -29,6 +29,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .provider import Notifier, LogLevel from .msgfactory import NotifierFactory diff --git a/server/src/uds/core/messaging/msgfactory.py b/server/src/uds/core/messaging/msgfactory.py index 3f3a17c3f..e0875096c 100644 --- a/server/src/uds/core/messaging/msgfactory.py +++ b/server/src/uds/core/messaging/msgfactory.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from uds.core.util import factory diff --git a/server/src/uds/core/messaging/processor.py b/server/src/uds/core/messaging/processor.py index 0b5e7eeb2..99682100a 100644 --- a/server/src/uds/core/messaging/processor.py +++ b/server/src/uds/core/messaging/processor.py @@ -32,7 +32,6 @@ import datetime import time import logging import typing -import collections.abc from uds.core.managers.task import BaseThread diff --git a/server/src/uds/core/messaging/provider.py b/server/src/uds/core/messaging/provider.py index 4d5f0b092..8f8379226 100644 --- a/server/src/uds/core/messaging/provider.py +++ b/server/src/uds/core/messaging/provider.py @@ -29,7 +29,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc from django.utils.translation import gettext_noop as _ diff --git a/server/src/uds/core/mfas/__init__.py b/server/src/uds/core/mfas/__init__.py index a70fa8a6d..24bf24f19 100644 --- a/server/src/uds/core/mfas/__init__.py +++ b/server/src/uds/core/mfas/__init__.py @@ -30,6 +30,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .mfa import MFA, LoginAllowed from .mfafactory import MFAsFactory diff --git a/server/src/uds/core/mfas/mfa.py b/server/src/uds/core/mfas/mfa.py index b48f22008..50acc836d 100644 --- a/server/src/uds/core/mfas/mfa.py +++ b/server/src/uds/core/mfas/mfa.py @@ -78,7 +78,7 @@ class LoginAllowed(enum.StrEnum): networks: typing.Optional[collections.abc.Iterable[str]] = None, ) -> bool: - if isinstance(action, str): + if not isinstance(action, LoginAllowed): action = LoginAllowed(action) return { diff --git a/server/src/uds/core/mfas/mfafactory.py b/server/src/uds/core/mfas/mfafactory.py index f7b0e1986..f73ac3959 100644 --- a/server/src/uds/core/mfas/mfafactory.py +++ b/server/src/uds/core/mfas/mfafactory.py @@ -31,7 +31,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc from uds.core.util import factory diff --git a/server/src/uds/core/module.py b/server/src/uds/core/module.py index 1bf815853..a99c2160e 100644 --- a/server/src/uds/core/module.py +++ b/server/src/uds/core/module.py @@ -35,7 +35,6 @@ import logging import os.path import sys import typing -import collections.abc from django.utils.translation import gettext as _ diff --git a/server/src/uds/core/osmanagers/__init__.py b/server/src/uds/core/osmanagers/__init__.py index e11692620..06e81a58e 100644 --- a/server/src/uds/core/osmanagers/__init__.py +++ b/server/src/uds/core/osmanagers/__init__.py @@ -29,8 +29,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ - -# pylint: disable=unused-import +# pyright: reportUnusedImport=false from .osmanager import OSManager from .osmfactory import OSManagersFactory diff --git a/server/src/uds/core/osmanagers/osmfactory.py b/server/src/uds/core/osmanagers/osmfactory.py index 50559fdbc..76d572a8c 100644 --- a/server/src/uds/core/osmanagers/osmfactory.py +++ b/server/src/uds/core/osmanagers/osmfactory.py @@ -31,7 +31,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc from uds.core.util import factory diff --git a/server/src/uds/core/reports/__init__.py b/server/src/uds/core/reports/__init__.py index 40219152a..e81de3e78 100644 --- a/server/src/uds/core/reports/__init__.py +++ b/server/src/uds/core/reports/__init__.py @@ -30,4 +30,5 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .report import Report diff --git a/server/src/uds/core/reports/graphs.py b/server/src/uds/core/reports/graphs.py index a2496ce8d..541f3c1b1 100644 --- a/server/src/uds/core/reports/graphs.py +++ b/server/src/uds/core/reports/graphs.py @@ -42,7 +42,7 @@ from matplotlib.figure import Figure from matplotlib import colormaps # This must be imported to allow 3d projections -from mpl_toolkits.mplot3d.axes3d import Axes3D # pylint: disable=unused-import +from mpl_toolkits.mplot3d.axes3d import Axes3D # pyright: ignore[reportUnusedImport] import numpy as np diff --git a/server/src/uds/core/reports/report.py b/server/src/uds/core/reports/report.py index 112bad240..33ea90daf 100644 --- a/server/src/uds/core/reports/report.py +++ b/server/src/uds/core/reports/report.py @@ -34,9 +34,8 @@ import codecs import datetime import logging import typing -import collections.abc -from weasyprint import HTML, CSS, default_url_fetcher +from weasyprint import HTML, CSS, default_url_fetcher # pyright: ignore[reportUnknownVariableType] from django.utils.translation import gettext, gettext_noop as _ from django.template import loader @@ -133,7 +132,7 @@ class Report(UserInterface): @classmethod def get_uuid(cls) -> str: - if cls.uuid is None: + if not cls.uuid: raise Exception(f'Class does not includes an uuid!!!: {cls}') return cls.uuid @@ -186,7 +185,7 @@ class Report(UserInterface): h = HTML(string=html, url_fetcher=report_fetcher) c = CSS(string=css) - return typing.cast(bytes, h.write_pdf(stylesheets=[c])) # Return a new bytes object + return typing.cast(bytes, h.write_pdf(stylesheets=[c])) # Return a new bytes object # pyright: ignore[reportUnknownMemberType] @staticmethod def template_as_pdf( diff --git a/server/src/uds/core/reports/stock.py b/server/src/uds/core/reports/stock.py index 5d9f12123..7fb191a87 100644 --- a/server/src/uds/core/reports/stock.py +++ b/server/src/uds/core/reports/stock.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from uds.core.util import utils diff --git a/server/src/uds/core/services/__init__.py b/server/src/uds/core/services/__init__.py index abbc26cc8..e953aa381 100644 --- a/server/src/uds/core/services/__init__.py +++ b/server/src/uds/core/services/__init__.py @@ -32,6 +32,8 @@ UDS Service modules interfaces and classes. Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false + from . import exceptions from .user_service import UserService diff --git a/server/src/uds/core/services/exceptions.py b/server/src/uds/core/services/exceptions.py index 99dc6117f..f2b54aa6c 100644 --- a/server/src/uds/core/services/exceptions.py +++ b/server/src/uds/core/services/exceptions.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc from uds.core.exceptions import UDSException diff --git a/server/src/uds/core/services/provider.py b/server/src/uds/core/services/provider.py index 4a02bfc51..bc16eb825 100644 --- a/server/src/uds/core/services/provider.py +++ b/server/src/uds/core/services/provider.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from uds.core import module, environment, consts from uds.core.util import log diff --git a/server/src/uds/core/services/provider_factory.py b/server/src/uds/core/services/provider_factory.py index c061b5524..d6816cbff 100644 --- a/server/src/uds/core/services/provider_factory.py +++ b/server/src/uds/core/services/provider_factory.py @@ -30,7 +30,6 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import typing import collections.abc import logging @@ -68,7 +67,7 @@ class ServiceProviderFactory(factory.ModuleFactory[ServiceProvider]): return # Fix offers by checking if they are valid - offers = [] + offers: list[type['Service']] = [] for s in type_.offers: if s.uses_cache_l2: s.uses_cache = True # Ensures uses cache is true diff --git a/server/src/uds/core/services/publication.py b/server/src/uds/core/services/publication.py index 10ab03ab1..5dee5914e 100644 --- a/server/src/uds/core/services/publication.py +++ b/server/src/uds/core/services/publication.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import abc import typing -import collections.abc from uds.core import types from uds.core.environment import Environmentable diff --git a/server/src/uds/core/services/specializations/fixed_machine/fixed_service.py b/server/src/uds/core/services/specializations/fixed_machine/fixed_service.py index 98a49d828..201c9591b 100644 --- a/server/src/uds/core/services/specializations/fixed_machine/fixed_service.py +++ b/server/src/uds/core/services/specializations/fixed_machine/fixed_service.py @@ -30,21 +30,15 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import abc import logging -import re import typing import collections.abc -from django.utils.translation import gettext_noop as _, gettext -from uds.core import services, types, consts, exceptions +from django.utils.translation import gettext_noop as _ +from uds.core import services, types from uds.core.ui import gui -from uds.core.util import validators, log -from uds.core.util.cache import Cache -from uds.core.util.decorators import cached -from uds.core.workers import initialize # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module from uds import models from .fixed_userservice import FixedUserService diff --git a/server/src/uds/core/services/specializations/fixed_machine/fixed_userservice.py b/server/src/uds/core/services/specializations/fixed_machine/fixed_userservice.py index 152523abe..1b9692b52 100644 --- a/server/src/uds/core/services/specializations/fixed_machine/fixed_userservice.py +++ b/server/src/uds/core/services/specializations/fixed_machine/fixed_userservice.py @@ -36,10 +36,8 @@ import logging import typing import collections.abc -from uds.core import services, consts, types -from uds.core.managers.userservice import UserServiceManager +from uds.core import services, types from uds.core.util import log, autoserializable -from uds.core.util.model import sql_stamp_seconds # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: diff --git a/server/src/uds/core/services/user_service.py b/server/src/uds/core/services/user_service.py index 37ca063dc..ea3f2bc1b 100644 --- a/server/src/uds/core/services/user_service.py +++ b/server/src/uds/core/services/user_service.py @@ -30,7 +30,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import typing from uds.core import types diff --git a/server/src/uds/core/transports/__init__.py b/server/src/uds/core/transports/__init__.py index f1c209610..7711bd939 100644 --- a/server/src/uds/core/transports/__init__.py +++ b/server/src/uds/core/transports/__init__.py @@ -32,8 +32,9 @@ UDS Service modules interfaces and classes. Author: Adolfo Gómez, dkmaster at dkmon dot com """ -from .transport import Transport +# pyright: reportUnusedImport=false from .transport_factory import TransportsFactory +from .transport import Transport def factory() -> TransportsFactory: diff --git a/server/src/uds/core/transports/transport.py b/server/src/uds/core/transports/transport.py index f745e09cb..bdc2fc7dc 100644 --- a/server/src/uds/core/transports/transport.py +++ b/server/src/uds/core/transports/transport.py @@ -33,7 +33,6 @@ import sys import codecs import logging -import json import typing import collections.abc @@ -201,7 +200,7 @@ class Transport(Module): def get_transport_script( self, - userService: 'models.UserService', + userservice: 'models.UserService', transport: 'models.Transport', ip: str, os: 'types.os.DetectedOsInfo', # pylint: disable=redefined-outer-name diff --git a/server/src/uds/core/types/__init__.py b/server/src/uds/core/types/__init__.py index 232d7e40b..b4331f882 100644 --- a/server/src/uds/core/types/__init__.py +++ b/server/src/uds/core/types/__init__.py @@ -29,8 +29,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ - -# pylint: disable=unused-import +# pyright: reportUnusedImport=false from . import ( auth, calendar, diff --git a/server/src/uds/core/types/auth.py b/server/src/uds/core/types/auth.py index 21bcd600c..0350490e4 100644 --- a/server/src/uds/core/types/auth.py +++ b/server/src/uds/core/types/auth.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing import dataclasses -import collections.abc import enum from django.urls import reverse diff --git a/server/src/uds/core/types/connections.py b/server/src/uds/core/types/connections.py index 1d5ae6a47..fe4844f10 100644 --- a/server/src/uds/core/types/connections.py +++ b/server/src/uds/core/types/connections.py @@ -29,9 +29,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import typing import dataclasses -import collections.abc from .services import ServiceType diff --git a/server/src/uds/core/types/core.py b/server/src/uds/core/types/core.py index ddc0d4f74..0fb254830 100644 --- a/server/src/uds/core/types/core.py +++ b/server/src/uds/core/types/core.py @@ -30,7 +30,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -from email import message import typing import dataclasses diff --git a/server/src/uds/core/types/errors.py b/server/src/uds/core/types/errors.py index 0f927f7d1..ead567d50 100644 --- a/server/src/uds/core/types/errors.py +++ b/server/src/uds/core/types/errors.py @@ -30,7 +30,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import stat import typing import enum import logging diff --git a/server/src/uds/core/types/log.py b/server/src/uds/core/types/log.py index 3e5cbfcea..63c1e0f9e 100644 --- a/server/src/uds/core/types/log.py +++ b/server/src/uds/core/types/log.py @@ -1,4 +1,3 @@ -import stat import typing import collections.abc import functools diff --git a/server/src/uds/core/types/os.py b/server/src/uds/core/types/os.py index 80a12a211..1960ba21e 100644 --- a/server/src/uds/core/types/os.py +++ b/server/src/uds/core/types/os.py @@ -32,8 +32,6 @@ """ import dataclasses import enum -import typing -import collections.abc @dataclasses.dataclass class DetectedOsInfo: diff --git a/server/src/uds/core/types/pools.py b/server/src/uds/core/types/pools.py index ca9cbe782..672febd20 100644 --- a/server/src/uds/core/types/pools.py +++ b/server/src/uds/core/types/pools.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import enum import typing import dataclasses -import collections.abc from django.utils.translation import gettext as _ diff --git a/server/src/uds/core/types/preferences.py b/server/src/uds/core/types/preferences.py index 882f9b562..6617a420e 100644 --- a/server/src/uds/core/types/preferences.py +++ b/server/src/uds/core/types/preferences.py @@ -30,11 +30,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ - -import typing -import collections.abc - - class CommonPrefs: SZ_PREF = 'screenSize' SZ_640x480 = '1' diff --git a/server/src/uds/core/types/requests.py b/server/src/uds/core/types/requests.py index 314c97c29..2ae7d8d04 100644 --- a/server/src/uds/core/types/requests.py +++ b/server/src/uds/core/types/requests.py @@ -30,7 +30,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc from django.http import HttpRequest @@ -44,7 +43,7 @@ class ExtendedHttpRequest(HttpRequest): ip_version: int ip_proxy: str os: 'types.os.DetectedOsInfo' - user: typing.Optional['User'] # pyright: ignore[reportIncompatibleVariableOverride] + user: typing.Optional['User'] # type: ignore authorized: bool diff --git a/server/src/uds/core/types/services.py b/server/src/uds/core/types/services.py index ebdff17ed..6a4189fe3 100644 --- a/server/src/uds/core/types/services.py +++ b/server/src/uds/core/types/services.py @@ -30,12 +30,8 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import dataclasses -import typing -import collections.abc import enum -from attr import field - class ServiceType(enum.StrEnum): VDI = 'VDI' diff --git a/server/src/uds/core/ui/__init__.py b/server/src/uds/core/ui/__init__.py index 6e7090850..41407a117 100644 --- a/server/src/uds/core/ui/__init__.py +++ b/server/src/uds/core/ui/__init__.py @@ -1,8 +1,39 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2012-2023 Virtual Cable S.L.U. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false """ User interface part of UDS modules. This module contains the definition of UserInterface, needed to describe the interaction between an UDS module and the administration interface """ - -from .user_interface import gui, UserInterface \ No newline at end of file +from .user_interface import gui, UserInterface diff --git a/server/src/uds/core/ui/user_interface.py b/server/src/uds/core/ui/user_interface.py index f81716aa6..f5f27576c 100644 --- a/server/src/uds/core/ui/user_interface.py +++ b/server/src/uds/core/ui/user_interface.py @@ -37,7 +37,6 @@ import datetime import inspect import itertools import logging -from os import read import pickle # nosec: safe usage import re import time diff --git a/server/src/uds/core/util/autoserializable.py b/server/src/uds/core/util/autoserializable.py index fa2a35173..a2cdea0ff 100644 --- a/server/src/uds/core/util/autoserializable.py +++ b/server/src/uds/core/util/autoserializable.py @@ -41,7 +41,6 @@ from uds.core import services class UserDeploymentService(AutoSerializable, services.UserDeployment): ... - """ import dataclasses @@ -56,11 +55,9 @@ import hashlib import struct import abc -# Import the cryptography library from cryptography import fernet from django.conf import settings -from requests import get from uds.core.serializable import Serializable @@ -201,7 +198,9 @@ class _SerializableField(typing.Generic[T]): def __set__(self, instance: 'AutoSerializable', value: T) -> None: # If type is float and value is int, convert it # Or if type is int and value is float, convert it - if typing.cast(typing.Type[typing.Any], self.obj_type) in (float, int) and isinstance(value, (float, int)): + if typing.cast(typing.Type[typing.Any], self.obj_type) in (float, int) and isinstance( + value, (float, int) + ): value = self.obj_type(value) # type: ignore if not isinstance(value, self.obj_type): # Try casting to load values (maybe a namedtuple, i.e.) @@ -374,7 +373,7 @@ class PasswordField(StringField): def __init__(self, default: str = '', crypt_key: str = ''): super().__init__(default) - self._crypt_key = crypt_key or settings.SECRET_KEY[:32] + self._crypt_key = crypt_key or settings.SECRET_KEY[:32] # If no SECRET_KEY, will raise an exception... def _encrypt(self, value: str) -> bytes: """Encrypt a password @@ -581,10 +580,7 @@ class AutoSerializable(Serializable, metaclass=_FieldNameSetter): def __str__(self) -> str: return ', '.join( - [ - f"{k}={v.obj_type.__name__}({v.__get__(self)})" - for k, v in self._autoserializable_fields() - ] + [f"{k}={v.obj_type.__name__}({v.__get__(self)})" for k, v in self._autoserializable_fields()] ) diff --git a/server/src/uds/core/util/calendar/__init__.py b/server/src/uds/core/util/calendar/__init__.py index 3bde8da86..d5a54ee2d 100644 --- a/server/src/uds/core/util/calendar/__init__.py +++ b/server/src/uds/core/util/calendar/__init__.py @@ -30,11 +30,10 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnknownMemberType=false import datetime import hashlib -import time import typing -import collections.abc import logging import bitarray @@ -99,7 +98,7 @@ class CalendarChecker: _end = end if r_end is None or end < r_end else r_end - for val in rr.between(_start, _end, inc=True): + for val in typing.cast(list[datetime.datetime], rr.between(_start, _end, inc=True)): if val.date() != data_date: diff = int((start - val).total_seconds() / 60) pos = 0 @@ -116,18 +115,19 @@ class CalendarChecker: return data def _update_events( - self, check_from: datetime.datetime, startEvent: bool = True + self, check_from: datetime.datetime, start_event: bool = True ) -> typing.Optional[datetime.datetime]: - next_event = None + next_event: 'datetime.datetime|None' = None + event: typing.Optional[datetime.datetime] = None for rule in self.calendar.rules.all(): # logger.debug('RULE: start = {}, checkFrom = {}, end'.format(rule.start.date(), checkFrom.date())) if rule.end is not None and rule.end < check_from.date(): continue # logger.debug('Rule in check interval...') - if startEvent: - event = rule.as_rrule().after(check_from) # At start + if start_event: + event = typing.cast(datetime.datetime|None, rule.as_rrule().after(check_from)) # At start else: - event = rule.as_rrule_end().after(check_from) # At end + event = typing.cast(datetime.datetime|None, rule.as_rrule_end().after(check_from)) # At end if event and (next_event is None or next_event > event): next_event = event diff --git a/server/src/uds/core/util/config.py b/server/src/uds/core/util/config.py index cd2c02056..f63b151e9 100644 --- a/server/src/uds/core/util/config.py +++ b/server/src/uds/core/util/config.py @@ -187,11 +187,11 @@ class Config: self.set(self._default) self._data = self._default except Exception as e: # On migration, this could happen - logger.info('Error accessing db config %s.%s', self._section.name(), self._key) + logger.info('Error accessing db config %s.%s: %s', self._section.name(), self._key, e) # logger.exception(e) self._data = self._default - return typing.cast(str, self._data) + return self._data def set_params(self, params: typing.Any) -> None: _config_params[self._section.name() + self._key] = params @@ -793,12 +793,12 @@ class GlobalConfig: logger.debug('Get later: %s', c) c.get() - _for_recovering_later[:] = [] + _for_recovering_later[:] = [] # pyright: ignore[reportUnknownArgumentType] for c, v in _for_saving_later: logger.debug('Saving delayed value: %s', c) c.set(v) - _for_saving_later[:] = [] + _for_saving_later[:] = [] # pyright: ignore[reportUnknownArgumentType] # Process some global config parameters # GlobalConfig.UDS_THEME.setParams(['html5', 'semantic']) diff --git a/server/src/uds/core/util/connection.py b/server/src/uds/core/util/connection.py deleted file mode 100644 index 21524c9c1..000000000 --- a/server/src/uds/core/util/connection.py +++ /dev/null @@ -1,36 +0,0 @@ -# -*- coding: utf-8 -*- - -# -# Copyright (c) 2012-2021 Virtual Cable S.L.U. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without modification, -# are permitted provided that the following conditions are met: -# -# * Redistributions of source code must retain the above copyright notice, -# this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors -# may be used to endorse or promote products derived from this software -# without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -""" -@author: Adolfo Gómez, dkmaster at dkmon dot com -""" - -# Import alias -# pylint: disable=unused-import -from .net import test_connectivity diff --git a/server/src/uds/core/util/fields.py b/server/src/uds/core/util/fields.py index b6d0a74bf..83750048c 100644 --- a/server/src/uds/core/util/fields.py +++ b/server/src/uds/core/util/fields.py @@ -55,7 +55,10 @@ def _server_group_values( types_: collections.abc.Iterable[types.servers.ServerType], subtype: typing.Optional[str] = None ) -> list[types.ui.ChoiceItem]: fltr = models.ServerGroup.objects.filter( - functools.reduce(lambda x, y: x | y, [Q(type=type_) for type_ in types_]) + functools.reduce( + lambda x, y: x | y, # pyright: ignore[reportUnknownLambdaType,reportUnknownArgumentType] + [Q(type=type_) for type_ in types_], # pyright: ignore[reportUnknownArgumentType] + ) ) if subtype is not None: fltr = fltr.filter(subtype=subtype) @@ -131,7 +134,9 @@ def get_server_group_from_field(fld: ui.gui.ChoiceField) -> models.ServerGroup: # Ticket validity time field (for http related tunnels) -def tunnel_ticket_validity_field(order: int = 90, tab: 'types.ui.Tab|None' = types.ui.Tab.ADVANCED) -> ui.gui.NumericField: +def tunnel_ticket_validity_field( + order: int = 90, tab: 'types.ui.Tab|None' = types.ui.Tab.ADVANCED +) -> ui.gui.NumericField: return ui.gui.NumericField( length=3, label=_('Ticket Validity'), @@ -148,7 +153,9 @@ def tunnel_ticket_validity_field(order: int = 90, tab: 'types.ui.Tab|None' = typ # Tunnel wait time (for uds client related tunnels) -def tunnel_wait_time_field(order: int = 2, tab: 'types.ui.Tab|None' = types.ui.Tab.TUNNEL) -> ui.gui.NumericField: +def tunnel_wait_time_field( + order: int = 2, tab: 'types.ui.Tab|None' = types.ui.Tab.TUNNEL +) -> ui.gui.NumericField: return ui.gui.NumericField( length=3, label=_('Tunnel wait time'), @@ -288,11 +295,12 @@ def concurrent_removal_limit_field( old_field_name='maxRemovingServices', ) + def services_limit_field( order: int = 4, tab: 'types.ui.Tab|str|None' = types.ui.Tab.ADVANCED, ) -> ui.gui.NumericField: - return ui.gui.NumericField( + return ui.gui.NumericField( order=order, label=_("Max. Allowed services"), min_value=0, @@ -307,7 +315,8 @@ def services_limit_field( def remove_duplicates_field( - order: int = 102, tab: 'types.ui.Tab|str|None' = types.ui.Tab.ADVANCED, + order: int = 102, + tab: 'types.ui.Tab|str|None' = types.ui.Tab.ADVANCED, old_field_name: typing.Optional[str] = None, ) -> ui.gui.CheckBoxField: return ui.gui.CheckBoxField( diff --git a/server/src/uds/core/util/hash.py b/server/src/uds/core/util/hash.py index 3d4d0ed91..c026eda9c 100644 --- a/server/src/uds/core/util/hash.py +++ b/server/src/uds/core/util/hash.py @@ -31,7 +31,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc hasher: typing.Any diff --git a/server/src/uds/core/util/html.py b/server/src/uds/core/util/html.py index c350aa11a..a2b42d169 100644 --- a/server/src/uds/core/util/html.py +++ b/server/src/uds/core/util/html.py @@ -32,7 +32,6 @@ import datetime import logging import typing -import collections.abc from django.utils.translation import get_language from django.utils import formats diff --git a/server/src/uds/core/util/ldaputil.py b/server/src/uds/core/util/ldaputil.py index df490f611..f992cd92d 100644 --- a/server/src/uds/core/util/ldaputil.py +++ b/server/src/uds/core/util/ldaputil.py @@ -40,13 +40,18 @@ import ldap.filter # Import for local use, and reexport from ldap import ( - SCOPE_BASE, # pyright: ignore - SCOPE_SUBTREE, # pyright: ignore - SCOPE_ONELEVEL, # pyright: ignore - ALREADY_EXISTS, # pyright: ignore + SCOPE_BASE as S_BASE, # pyright: ignore + SCOPE_SUBTREE as S_SUBTREE, # pyright: ignore + SCOPE_ONELEVEL as S_ONELEVEL, # pyright: ignore + ALREADY_EXISTS as S_ALREADY_EX, # pyright: ignore # SCOPE_SUBORDINATE, # pyright: ignore ) +SCOPE_BASE: int = S_BASE # pyright: ignore +SCOPE_SUBTREE: int = S_SUBTREE # pyright: ignore +SCOPE_ONELEVEL: int = S_ONELEVEL # pyright: ignore +ALREADY_EXISTS: int = S_ALREADY_EX # pyright: ignore + from django.utils.translation import gettext as _ from django.conf import settings diff --git a/server/src/uds/core/util/log.py b/server/src/uds/core/util/log.py index 07d9611e7..e7b10728a 100644 --- a/server/src/uds/core/util/log.py +++ b/server/src/uds/core/util/log.py @@ -34,7 +34,6 @@ import os import logging import logging.handlers import typing -import collections.abc import enum import re @@ -268,6 +267,6 @@ class UDSLogHandler(logging.handlers.RotatingFileHandler): priority = 4 if record.levelno == logging.WARNING else 3 if record.levelno == logging.ERROR else 2 if journal is not None: - journal.send(MESSAGE=msg, PRIORITY=priority, SYSLOG_IDENTIFIER=identificator) + journal.send(MESSAGE=msg, PRIORITY=priority, SYSLOG_IDENTIFIER=identificator) # pyright: ignore[reportUnknownMemberType] return super().emit(record) diff --git a/server/src/uds/core/util/model.py b/server/src/uds/core/util/model.py index 6585238a9..c658dc127 100644 --- a/server/src/uds/core/util/model.py +++ b/server/src/uds/core/util/model.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import logging from threading import Lock import datetime diff --git a/server/src/uds/core/util/net.py b/server/src/uds/core/util/net.py index deb826ea3..5ae828f75 100644 --- a/server/src/uds/core/util/net.py +++ b/server/src/uds/core/util/net.py @@ -30,7 +30,6 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import ipaddress import logging import re diff --git a/server/src/uds/core/util/objtype.py b/server/src/uds/core/util/objtype.py index eb1880ed1..3b35923a8 100644 --- a/server/src/uds/core/util/objtype.py +++ b/server/src/uds/core/util/objtype.py @@ -33,7 +33,6 @@ import logging import typing import dataclasses -import collections.abc import enum from uds import models diff --git a/server/src/uds/core/util/os_detector.py b/server/src/uds/core/util/os_detector.py index 7430604a1..aadf7cba5 100644 --- a/server/src/uds/core/util/os_detector.py +++ b/server/src/uds/core/util/os_detector.py @@ -30,14 +30,10 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import enum -import re import logging import typing import collections.abc -from numpy import mat - from uds.core import types, consts logger = logging.getLogger(__name__) diff --git a/server/src/uds/core/util/permissions.py b/server/src/uds/core/util/permissions.py index f77571404..b8478fddb 100644 --- a/server/src/uds/core/util/permissions.py +++ b/server/src/uds/core/util/permissions.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc # from django.utils.translation import gettext as _ diff --git a/server/src/uds/core/util/properties.py b/server/src/uds/core/util/properties.py index 4aeee8f63..a20bce90e 100644 --- a/server/src/uds/core/util/properties.py +++ b/server/src/uds/core/util/properties.py @@ -31,8 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc -import contextlib import logging from django.db.models import signals diff --git a/server/src/uds/core/util/resolver.py b/server/src/uds/core/util/resolver.py index 333b5d771..8ba1fae2f 100644 --- a/server/src/uds/core/util/resolver.py +++ b/server/src/uds/core/util/resolver.py @@ -28,10 +28,8 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import time import typing import collections.abc -import functools import dns.resolver import dns.reversename diff --git a/server/src/uds/core/util/security.py b/server/src/uds/core/util/security.py index f5945058f..7e5f37e0c 100644 --- a/server/src/uds/core/util/security.py +++ b/server/src/uds/core/util/security.py @@ -1,11 +1,42 @@ -import collections.abc +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2012-2024 Virtual Cable S.L.U. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" import ipaddress import logging import random import secrets import ssl import typing -from datetime import datetime, timedelta +import datetime import certifi import requests @@ -34,7 +65,7 @@ try: # Ensure that we do not get warnings about self signed certificates and so import requests.packages.urllib3 # type: ignore - requests.packages.urllib3.disable_warnings() + requests.packages.urllib3.disable_warnings() # pyright: ignore except Exception: # nosec: simple check for disabling warnings, # Igonre if we cannot disable warnings pass @@ -58,7 +89,7 @@ def create_self_signed_cert(ip: str) -> tuple[str, str, str]: san = x509.SubjectAlternativeName([x509.IPAddress(ipaddress.ip_address(ip))]) basic_contraints = x509.BasicConstraints(ca=True, path_length=0) - now = datetime.utcnow() + now = datetime.datetime.now(datetime.UTC) cert = ( x509.CertificateBuilder() .subject_name(name) @@ -66,7 +97,7 @@ def create_self_signed_cert(ip: str) -> tuple[str, str, str]: .public_key(key.public_key()) .serial_number(random.SystemRandom().randint(0, 1 << 64)) .not_valid_before(now) - .not_valid_after(now + timedelta(days=10 * 365)) + .not_valid_after(now + datetime.timedelta(days=10 * 365)) .add_extension(basic_contraints, False) .add_extension(san, False) .sign(key, hashes.SHA256(), default_backend()) diff --git a/server/src/uds/core/util/session_serializer.py b/server/src/uds/core/util/session_serializer.py index 7d448b631..6c4dd7ee9 100644 --- a/server/src/uds/core/util/session_serializer.py +++ b/server/src/uds/core/util/session_serializer.py @@ -31,7 +31,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import json diff --git a/server/src/uds/core/util/singleton.py b/server/src/uds/core/util/singleton.py index 090769ab9..d2e40117f 100644 --- a/server/src/uds/core/util/singleton.py +++ b/server/src/uds/core/util/singleton.py @@ -1,5 +1,4 @@ import typing -import collections.abc class Singleton(type): diff --git a/server/src/uds/core/util/state_queue.py b/server/src/uds/core/util/state_queue.py index 7e53a0af2..0d768d72a 100644 --- a/server/src/uds/core/util/state_queue.py +++ b/server/src/uds/core/util/state_queue.py @@ -31,7 +31,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc class StateQueue: diff --git a/server/src/uds/core/util/stats/__init__.py b/server/src/uds/core/util/stats/__init__.py index 3e59c1f8d..e26892b67 100644 --- a/server/src/uds/core/util/stats/__init__.py +++ b/server/src/uds/core/util/stats/__init__.py @@ -27,7 +27,8 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Auhor: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from . import counters from . import events diff --git a/server/src/uds/core/util/stats/counters.py b/server/src/uds/core/util/stats/counters.py index 91e71f557..b9e081059 100644 --- a/server/src/uds/core/util/stats/counters.py +++ b/server/src/uds/core/util/stats/counters.py @@ -28,7 +28,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import enum import datetime import logging import typing @@ -67,7 +66,7 @@ def _get_serv_pool_ids(service: 'Service') -> tuple[int, ...]: def _get_prov_serv_pool_ids(provider: 'Provider') -> tuple[int, ...]: - res: tuple[int, ...] = () + res: tuple[int, ...] = tuple() for i in provider.services.all(): res += _get_serv_pool_ids(i) return res diff --git a/server/src/uds/core/util/stats/events.py b/server/src/uds/core/util/stats/events.py index 97d56aa8c..fbb4b51b9 100644 --- a/server/src/uds/core/util/stats/events.py +++ b/server/src/uds/core/util/stats/events.py @@ -32,9 +32,7 @@ """ import dataclasses import datetime -import stat import time -import enum import logging import typing import collections.abc diff --git a/server/src/uds/core/util/storage.py b/server/src/uds/core/util/storage.py index 6e0e9c4c9..a1b23da7b 100644 --- a/server/src/uds/core/util/storage.py +++ b/server/src/uds/core/util/storage.py @@ -29,7 +29,6 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -from email.mime import base import pickle # nosec: This is e controled pickle use import base64 import hashlib diff --git a/server/src/uds/core/util/unique_id_generator.py b/server/src/uds/core/util/unique_id_generator.py index 02a04ad7a..0894e3b41 100644 --- a/server/src/uds/core/util/unique_id_generator.py +++ b/server/src/uds/core/util/unique_id_generator.py @@ -33,7 +33,6 @@ import logging import time import typing -import collections.abc from django.db import transaction, OperationalError, connection from django.db.utils import IntegrityError diff --git a/server/src/uds/core/util/unique_mac_generator.py b/server/src/uds/core/util/unique_mac_generator.py index 98c60e349..85098042f 100644 --- a/server/src/uds/core/util/unique_mac_generator.py +++ b/server/src/uds/core/util/unique_mac_generator.py @@ -28,11 +28,10 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import re -import typing from .unique_id_generator import UniqueGenerator diff --git a/server/src/uds/core/util/utils.py b/server/src/uds/core/util/utils.py index 579ae8e1d..b0687e42c 100644 --- a/server/src/uds/core/util/utils.py +++ b/server/src/uds/core/util/utils.py @@ -37,7 +37,6 @@ import logging import os import sys import typing -import collections.abc import unicodedata import django.template.defaultfilters as filters diff --git a/server/src/uds/core/util/validators.py b/server/src/uds/core/util/validators.py index 4c3cacc12..00fc26a29 100644 --- a/server/src/uds/core/util/validators.py +++ b/server/src/uds/core/util/validators.py @@ -33,7 +33,6 @@ import re import logging import typing -import collections.abc import json from django.utils.translation import gettext as _ diff --git a/server/src/uds/core/util/xml2dict.py b/server/src/uds/core/util/xml2dict.py index 42ac4d9a2..ba9aea149 100644 --- a/server/src/uds/core/util/xml2dict.py +++ b/server/src/uds/core/util/xml2dict.py @@ -27,8 +27,9 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: dkmaster@dkmon.com +Author: dkmaster@dkmon.com """ +# pyright: reportUnknownMemberType=false,reportUnknownVariableType=false,reportUnknownArgumentType=false import typing import collections.abc diff --git a/server/src/uds/core/workers/__init__.py b/server/src/uds/core/workers/__init__.py index d26db7035..8f1986acd 100644 --- a/server/src/uds/core/workers/__init__.py +++ b/server/src/uds/core/workers/__init__.py @@ -30,8 +30,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import typing -import collections.abc import logging from uds.core.util import modfinder diff --git a/server/src/uds/core/workers/notifications.py b/server/src/uds/core/workers/notifications.py index f32a2fe5d..6fa405d24 100644 --- a/server/src/uds/core/workers/notifications.py +++ b/server/src/uds/core/workers/notifications.py @@ -31,7 +31,7 @@ """ import logging -from uds.core.jobs import Job +# from uds.core.jobs import Job # from uds.core.util.config import GlobalConfig diff --git a/server/src/uds/core/workers/publication_cleaner.py b/server/src/uds/core/workers/publication_cleaner.py index bee6f42b1..4aefffcd0 100644 --- a/server/src/uds/core/workers/publication_cleaner.py +++ b/server/src/uds/core/workers/publication_cleaner.py @@ -31,7 +31,6 @@ """ from datetime import timedelta import logging -import typing import collections.abc from uds.core.managers import publication_manager diff --git a/server/src/uds/core/workers/service_pool_cleaner.py b/server/src/uds/core/workers/service_pool_cleaner.py index 933d831be..80c069e66 100644 --- a/server/src/uds/core/workers/service_pool_cleaner.py +++ b/server/src/uds/core/workers/service_pool_cleaner.py @@ -31,7 +31,6 @@ """ from datetime import timedelta import logging -import typing import collections.abc from django.db import transaction @@ -71,7 +70,7 @@ class DeployedServiceRemover(Job): def start_removal_of(self, service_pool: ServicePool) -> None: if ( - service_pool.service is None + not service_pool.service ): # Maybe an inconsistent value? (must not, but if no ref integrity in db, maybe someone hand-changed something.. ;)") logger.error('Found service pool %s without service', service_pool.name) service_pool.delete() # Just remove it "a las bravas", the best we can do diff --git a/server/src/uds/core/workers/servicepools_cache_updater.py b/server/src/uds/core/workers/servicepools_cache_updater.py index 1c77c17ff..43f48eb4b 100644 --- a/server/src/uds/core/workers/servicepools_cache_updater.py +++ b/server/src/uds/core/workers/servicepools_cache_updater.py @@ -31,7 +31,6 @@ """ import dataclasses import logging -from multiprocessing import pool import typing import collections.abc diff --git a/server/src/uds/core/workers/stats_collector.py b/server/src/uds/core/workers/stats_collector.py index 5a6e8613f..fa80ccd76 100644 --- a/server/src/uds/core/workers/stats_collector.py +++ b/server/src/uds/core/workers/stats_collector.py @@ -30,7 +30,6 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging -import typing import collections.abc from uds import models @@ -56,13 +55,13 @@ class DeployedServiceStatsCollector(Job): def run(self) -> None: logger.debug('Starting Deployed service stats collector') - servicePoolsToCheck: collections.abc.Iterable[ + service_pool_to_check: collections.abc.Iterable[ models.ServicePool ] = models.ServicePool.objects.filter(state=State.ACTIVE).iterator() stamp = model.sql_datetime() # Global counters totalAssigned, totalInUse, totalCached = 0, 0, 0 - for servicePool in servicePoolsToCheck: + for servicePool in service_pool_to_check: try: fltr = servicePool.assigned_user_services().exclude( state__in=State.INFO_STATES @@ -98,12 +97,12 @@ class DeployedServiceStatsCollector(Job): totalUsers, totalAssigned, totalWithService = 0, 0, 0 for auth in models.Authenticator.objects.all(): - fltr = auth.users.order_by().filter(userServices__isnull=False).exclude( + fltr_user = auth.users.filter(userServices__isnull=False).exclude( userServices__state__in=State.INFO_STATES - ) + ).order_by() users = auth.users.all().count() - users_with_service = fltr.values('id').distinct().count() # Use "values" to simplify query (only id) - number_assigned_services = fltr.values('id').count() + users_with_service = fltr_user.values('id').distinct().count() # Use "values" to simplify query (only id) + number_assigned_services = fltr_user.values('id').count() # Global counters totalUsers += users totalAssigned += number_assigned_services diff --git a/server/src/uds/core/workers/stuck_cleaner.py b/server/src/uds/core/workers/stuck_cleaner.py index ddf45812f..bab2e5170 100644 --- a/server/src/uds/core/workers/stuck_cleaner.py +++ b/server/src/uds/core/workers/stuck_cleaner.py @@ -31,7 +31,6 @@ """ from datetime import datetime, timedelta import logging -import typing import collections.abc from django.db.models import Q, Count diff --git a/server/src/uds/core/workers/system_cleaners.py b/server/src/uds/core/workers/system_cleaners.py index e01b5fbed..dc634c047 100644 --- a/server/src/uds/core/workers/system_cleaners.py +++ b/server/src/uds/core/workers/system_cleaners.py @@ -33,7 +33,6 @@ from importlib import import_module import logging import typing -import collections.abc from django.conf import settings from uds.core.util.cache import Cache diff --git a/server/src/uds/management/commands/export.py b/server/src/uds/management/commands/export.py index c122275b0..24636a296 100644 --- a/server/src/uds/management/commands/export.py +++ b/server/src/uds/management/commands/export.py @@ -45,7 +45,6 @@ from uds.core import consts if typing.TYPE_CHECKING: import argparse - from django.db.models import Model from uds.models.uuid_model import UUIDModel logger = logging.getLogger(__name__) diff --git a/server/src/uds/management/commands/fs.py b/server/src/uds/management/commands/fs.py index 3b8efe467..8a9edd258 100644 --- a/server/src/uds/management/commands/fs.py +++ b/server/src/uds/management/commands/fs.py @@ -26,10 +26,8 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ - +# pyright: reportUnusedImport=false # Placeholder, import the command from udsfs - -# pylint: disable=unused-import from .udsfs import Command diff --git a/server/src/uds/management/commands/showconfig.py b/server/src/uds/management/commands/showconfig.py index 995691426..2ee6389c4 100644 --- a/server/src/uds/management/commands/showconfig.py +++ b/server/src/uds/management/commands/showconfig.py @@ -33,7 +33,6 @@ import argparse import logging import typing -import collections.abc import csv import yaml diff --git a/server/src/uds/management/commands/tree.py b/server/src/uds/management/commands/tree.py index 23308e1d8..814157ef4 100644 --- a/server/src/uds/management/commands/tree.py +++ b/server/src/uds/management/commands/tree.py @@ -317,13 +317,13 @@ class Command(BaseCommand): tree[counter('ACCOUNTS')] = accounts # Service pool groups - servicePoolGroups = {} + service_pool_groups: dict[str, typing.Any] = {} for servicePoolGroup in models.ServicePoolGroup.objects.all(): - servicePoolGroups[servicePoolGroup.name] = { + service_pool_groups[servicePoolGroup.name] = { 'comments': servicePoolGroup.comments, 'service_pools': [sp.name for sp in servicePoolGroup.servicesPools.all()], } - tree[counter('SERVICEPOOLGROUPS')] = servicePoolGroups + tree[counter('SERVICEPOOLGROUPS')] = service_pool_groups # Gallery gallery: dict[str, typing.Any] = {} diff --git a/server/src/uds/management/commands/udsfs/__init__.py b/server/src/uds/management/commands/udsfs/__init__.py index 80980f70d..8c309164c 100644 --- a/server/src/uds/management/commands/udsfs/__init__.py +++ b/server/src/uds/management/commands/udsfs/__init__.py @@ -32,15 +32,12 @@ import argparse import errno import stat -import os.path import logging import typing -import collections.abc from django.core.management.base import BaseCommand -from uds import models from uds.core.util.fuse import FUSE, FuseOSError, Operations from . import types diff --git a/server/src/uds/management/commands/udsfs/events.py b/server/src/uds/management/commands/udsfs/events.py index 2028e945f..7ebf9c393 100644 --- a/server/src/uds/management/commands/udsfs/events.py +++ b/server/src/uds/management/commands/udsfs/events.py @@ -33,7 +33,6 @@ import stat import calendar import datetime import typing -import collections.abc import logging from django.db.models import QuerySet diff --git a/server/src/uds/management/commands/udsfs/stats.py b/server/src/uds/management/commands/udsfs/stats.py index 42aca5677..5e8ee38ca 100644 --- a/server/src/uds/management/commands/udsfs/stats.py +++ b/server/src/uds/management/commands/udsfs/stats.py @@ -156,7 +156,7 @@ class StatsFS(types.UDSFSInterface): if extension != 'csv': raise FileNotFoundError() - today_start = datetime.datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0) + today_start = datetime.datetime.now(datetime.UTC).replace(hour=0, minute=0, second=0, microsecond=0) return ( fnc, StatInterval( diff --git a/server/src/uds/management/commands/udsfs/types.py b/server/src/uds/management/commands/udsfs/types.py index 4a42535a1..94d313b31 100644 --- a/server/src/uds/management/commands/udsfs/types.py +++ b/server/src/uds/management/commands/udsfs/types.py @@ -33,7 +33,6 @@ import stat import time import logging import typing -import collections.abc logger = logging.getLogger(__name__) diff --git a/server/src/uds/mfas/Email/__init__.py b/server/src/uds/mfas/Email/__init__.py index be815d604..f1d937d6d 100644 --- a/server/src/uds/mfas/Email/__init__.py +++ b/server/src/uds/mfas/Email/__init__.py @@ -26,5 +26,5 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# pylint: disable=unused-import +# pyright: reportUnusedImport=false from . import mfa diff --git a/server/src/uds/mfas/Email/mfa.py b/server/src/uds/mfas/Email/mfa.py index d359fa213..4899c8651 100644 --- a/server/src/uds/mfas/Email/mfa.py +++ b/server/src/uds/mfas/Email/mfa.py @@ -29,25 +29,23 @@ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -from email.mime.text import MIMEText -from email.mime.multipart import MIMEMultipart +import logging import smtplib import ssl import typing -import collections.abc -import logging +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText -from django.utils.translation import gettext_noop as _, gettext +from django.utils.translation import gettext +from django.utils.translation import gettext_noop as _ from uds import models -from uds.core import mfas, exceptions, types -from uds.core.mfas.mfa import MFA +from uds.core import exceptions, mfas, types from uds.core.types.requests import ExtendedHttpRequest from uds.core.ui import gui -from uds.core.util import validators, decorators +from uds.core.util import decorators, validators if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequest logger = logging.getLogger(__name__) diff --git a/server/src/uds/mfas/Radius/__init__.py b/server/src/uds/mfas/Radius/__init__.py index be815d604..cfc0aacca 100644 --- a/server/src/uds/mfas/Radius/__init__.py +++ b/server/src/uds/mfas/Radius/__init__.py @@ -25,6 +25,8 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -# pylint: disable=unused-import +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false from . import mfa diff --git a/server/src/uds/mfas/Radius/mfa.py b/server/src/uds/mfas/Radius/mfa.py index a0ab12e3d..dafd72b55 100644 --- a/server/src/uds/mfas/Radius/mfa.py +++ b/server/src/uds/mfas/Radius/mfa.py @@ -26,12 +26,11 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Daniel Torregrosa -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Daniel Torregrosa +Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import logging from django.utils.translation import gettext_noop as _, gettext @@ -51,7 +50,6 @@ from uds.auths.Radius.client import ( from uds.core.auths.auth import web_password if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequest logger = logging.getLogger(__name__) diff --git a/server/src/uds/mfas/SMS/__init__.py b/server/src/uds/mfas/SMS/__init__.py index 35604031b..cfc0aacca 100644 --- a/server/src/uds/mfas/SMS/__init__.py +++ b/server/src/uds/mfas/SMS/__init__.py @@ -28,6 +28,5 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ - -# pylint: disable=unused-import +# pyright: reportUnusedImport=false from . import mfa diff --git a/server/src/uds/mfas/SMS/mfa.py b/server/src/uds/mfas/SMS/mfa.py index 4ad9e0c3a..e999561c1 100644 --- a/server/src/uds/mfas/SMS/mfa.py +++ b/server/src/uds/mfas/SMS/mfa.py @@ -30,7 +30,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import re import logging @@ -44,7 +43,6 @@ from uds.core.ui import gui from uds.core.util import security if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequest logger = logging.getLogger(__name__) diff --git a/server/src/uds/mfas/Sample/__init__.py b/server/src/uds/mfas/Sample/__init__.py index 35604031b..cfc0aacca 100644 --- a/server/src/uds/mfas/Sample/__init__.py +++ b/server/src/uds/mfas/Sample/__init__.py @@ -28,6 +28,5 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ - -# pylint: disable=unused-import +# pyright: reportUnusedImport=false from . import mfa diff --git a/server/src/uds/mfas/Sample/mfa.py b/server/src/uds/mfas/Sample/mfa.py index 3f0e48c06..8f2019d8a 100644 --- a/server/src/uds/mfas/Sample/mfa.py +++ b/server/src/uds/mfas/Sample/mfa.py @@ -31,15 +31,13 @@ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core import mfas, types, consts +from uds.core import mfas, types from uds.core.ui import gui if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequest logger = logging.getLogger(__name__) diff --git a/server/src/uds/mfas/TOTP/__init__.py b/server/src/uds/mfas/TOTP/__init__.py index 35604031b..cfc0aacca 100644 --- a/server/src/uds/mfas/TOTP/__init__.py +++ b/server/src/uds/mfas/TOTP/__init__.py @@ -28,6 +28,5 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ - -# pylint: disable=unused-import +# pyright: reportUnusedImport=false from . import mfa diff --git a/server/src/uds/mfas/TOTP/mfa.py b/server/src/uds/mfas/TOTP/mfa.py index 67f4bde00..303c1d63d 100644 --- a/server/src/uds/mfas/TOTP/mfa.py +++ b/server/src/uds/mfas/TOTP/mfa.py @@ -29,7 +29,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import logging import io import base64 @@ -45,7 +44,6 @@ from uds.core import mfas, exceptions, types from uds.core.ui import gui if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequest logger = logging.getLogger(__name__) diff --git a/server/src/uds/mfas/__init__.py b/server/src/uds/mfas/__init__.py index b5acf2927..d0c231471 100644 --- a/server/src/uds/mfas/__init__.py +++ b/server/src/uds/mfas/__init__.py @@ -39,14 +39,6 @@ The registration of modules is done locating subclases of :py:class:`uds.core.au Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import os.path -import pkgutil -import importlib -import sys -import typing -import collections.abc - - import logging from uds.core.util import modfinder diff --git a/server/src/uds/middleware/redirect.py b/server/src/uds/middleware/redirect.py index 1df9192ab..49f735476 100644 --- a/server/src/uds/middleware/redirect.py +++ b/server/src/uds/middleware/redirect.py @@ -31,7 +31,6 @@ import logging import typing -import collections.abc from django.urls import reverse from django.http import HttpResponsePermanentRedirect diff --git a/server/src/uds/middleware/request.py b/server/src/uds/middleware/request.py index 0243ef998..dd3f2c9a3 100644 --- a/server/src/uds/middleware/request.py +++ b/server/src/uds/middleware/request.py @@ -31,7 +31,6 @@ import datetime import logging import typing -import collections.abc from django.http import HttpResponseForbidden from django.utils import timezone diff --git a/server/src/uds/middleware/security.py b/server/src/uds/middleware/security.py index 9e2ca8a55..bf61eacea 100644 --- a/server/src/uds/middleware/security.py +++ b/server/src/uds/middleware/security.py @@ -32,14 +32,13 @@ import re import logging import typing -import collections.abc from django.http import HttpResponseForbidden from uds.core import consts from uds.core.util.config import GlobalConfig -from uds.core.auths.auth import is_source_trusted +from uds.core.auths.auth import is_trusted_source from . import builder @@ -56,7 +55,7 @@ bot = re.compile(r'bot|spider', re.IGNORECASE) def _process_request(request: 'ExtendedHttpRequest') -> typing.Optional['HttpResponse']: ua = request.META.get('HTTP_USER_AGENT', '') or 'Unknown' # If bot, break now - if bot.search(ua) or (ua == 'Unknown' and not is_source_trusted(request.ip)): + if bot.search(ua) or (ua == 'Unknown' and not is_trusted_source(request.ip)): # Return emty response if bot is detected logger.info( 'Denied Bot %s from %s to %s', @@ -91,11 +90,10 @@ def _process_response( ) -> 'HttpResponse': if GlobalConfig.ENHANCED_SECURITY.as_bool(): # Legacy browser support for X-XSS-Protection - response.headers.setdefault('X-XSS-Protection', '1; mode=block') + response['X-XSS-Protection'] = '1; mode=block' # Add Content-Security-Policy, see https://www.owasp.org/index.php/Content_Security_Policy - response.headers.setdefault( - 'Content-Security-Policy', - "default-src 'self' 'unsafe-inline' 'unsafe-eval' uds: udss:; img-src 'self' https: data:;", + response['Content-Security-Policy'] = ( + "default-src 'self' 'unsafe-inline' 'unsafe-eval' uds: udss:; img-src 'self' https: data:;" ) return response diff --git a/server/src/uds/middleware/xua.py b/server/src/uds/middleware/xua.py index ad3914f41..9ce0aa348 100644 --- a/server/src/uds/middleware/xua.py +++ b/server/src/uds/middleware/xua.py @@ -29,7 +29,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import logging from . import builder diff --git a/server/src/uds/migrations/0043_auto_20220704_2120.py b/server/src/uds/migrations/0043_auto_20220704_2120.py index c245dd1e3..fbe4386ef 100644 --- a/server/src/uds/migrations/0043_auto_20220704_2120.py +++ b/server/src/uds/migrations/0043_auto_20220704_2120.py @@ -1,4 +1,5 @@ # Generated by Django 3.2.10 on 2022-07-04 21:20 +# pyright: reportUnknownArgumentType=false, reportUnusedImport=false from django.db import migrations, models from django.db import connection diff --git a/server/src/uds/migrations/0046_registered_server_migration_and_more.py b/server/src/uds/migrations/0046_registered_server_migration_and_more.py index dd4ba0231..3fa092bcf 100644 --- a/server/src/uds/migrations/0046_registered_server_migration_and_more.py +++ b/server/src/uds/migrations/0046_registered_server_migration_and_more.py @@ -1,5 +1,5 @@ +# pyright: reportUnknownArgumentType=false import typing -import collections.abc import django.db.models.deletion from django.db import migrations, models diff --git a/server/src/uds/migrations/fixers/properties_v4.py b/server/src/uds/migrations/fixers/properties_v4.py index 358974554..86c2eae4a 100644 --- a/server/src/uds/migrations/fixers/properties_v4.py +++ b/server/src/uds/migrations/fixers/properties_v4.py @@ -1,5 +1,4 @@ import typing -import collections.abc import logging if typing.TYPE_CHECKING: diff --git a/server/src/uds/migrations/fixers/providers_v4/__init__.py b/server/src/uds/migrations/fixers/providers_v4/__init__.py index 24ab3698d..d4e970b34 100644 --- a/server/src/uds/migrations/fixers/providers_v4/__init__.py +++ b/server/src/uds/migrations/fixers/providers_v4/__init__.py @@ -29,7 +29,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc from . import rds ALL: typing.Final = (rds,) diff --git a/server/src/uds/migrations/fixers/providers_v4/_migrator.py b/server/src/uds/migrations/fixers/providers_v4/_migrator.py index 80e338e78..2369eb301 100644 --- a/server/src/uds/migrations/fixers/providers_v4/_migrator.py +++ b/server/src/uds/migrations/fixers/providers_v4/_migrator.py @@ -32,12 +32,11 @@ import datetime import logging import secrets import typing -import collections.abc import dns.resolver import dns.reversename -from uds.core import types, consts +from uds.core import types from uds.core.environment import Environment from uds.core.util import validators @@ -56,7 +55,7 @@ def migrate( ServerGroup: 'type[uds.models.ServerGroup]' = apps.get_model( 'uds', 'ServerGroup' ) - Server: 'type[uds.models.Server]' = apps.get_model('uds', 'Server') + # Server: 'type[uds.models.Server]' = apps.get_model('uds', 'Server') # For testing # from uds.models import Provider, Server, ServerGroup @@ -75,7 +74,7 @@ def migrate( validators.validate_ip(server) # Is Pure IP, try to get hostname try: - answers = dns.resolver.resolve(dns.reversename.from_address(server), 'PTR') + answers = typing.cast(list[typing.Any], dns.resolver.resolve(dns.reversename.from_address(server), 'PTR')) server_ip_hostname.append((server, str(answers[0]).rstrip('.'))) except Exception: # No problem, no reverse dns, hostname is the same as IP @@ -83,12 +82,12 @@ def migrate( except Exception: # Not pure IP, try to resolve it and get first IP try: - answers = dns.resolver.resolve(server, 'A') + answers = typing.cast(list[typing.Any], dns.resolver.resolve(server, 'A')) server_ip_hostname.append((str(answers[0]), server)) except Exception: # Try AAAA try: - answers = dns.resolver.resolve(server, 'AAAA') + answers = typing.cast(list[typing.Any], dns.resolver.resolve(server, 'AAAA')) server_ip_hostname.append((str(answers[0]), server)) except Exception: # Not found, continue, but do not add to servers and log it diff --git a/server/src/uds/migrations/fixers/providers_v4/rds.py b/server/src/uds/migrations/fixers/providers_v4/rds.py index c342b1325..ff40e29d6 100644 --- a/server/src/uds/migrations/fixers/providers_v4/rds.py +++ b/server/src/uds/migrations/fixers/providers_v4/rds.py @@ -1,46 +1,3 @@ -# -*- coding: utf-8 -*- -# -# Copyright (c) 2023 Virtual Cable S.L.U. -# All rights reserved. -# -# Redistribution and use in source and binary forms, with or without modification -# are permitted provided that the following conditions are met: -# -# * Redistributions of source code must retain the above copyright notice -# this list of conditions and the following disclaimer. -# * Redistributions in binary form must reproduce the above copyright notice -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. -# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors -# may be used to endorse or promote products derived from this software -# without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -""" -Author: Adolfo Gómez, dkmaster at dkmon dot com -""" -import datetime -import logging -import secrets - -import dns.resolver - -from uds.core import transports -from uds.core.ui import gui -from uds.core.util import validators - -logger = logging.getLogger(__name__) - - # Copy for migration # -*- coding: utf-8 -*- # @@ -74,18 +31,14 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc -from uds.core import services, consts +from uds.core import services from uds.core.ui import gui from . import _migrator logger = logging.getLogger(__name__) -if typing.TYPE_CHECKING: - import uds.models - RDS_SUBTYPE: typing.Final[str] = 'rds' diff --git a/server/src/uds/migrations/fixers/transports_v4/__init__.py b/server/src/uds/migrations/fixers/transports_v4/__init__.py index d9e58e229..d983d34aa 100644 --- a/server/src/uds/migrations/fixers/transports_v4/__init__.py +++ b/server/src/uds/migrations/fixers/transports_v4/__init__.py @@ -29,7 +29,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc from . import html5rdp, html5rds, html5ssh, html5vnc, nicedcv, rdp, rds, nomachine, spice, x2go ALL: typing.Final = (html5rdp, html5rds, html5ssh, html5vnc, nicedcv, rdp, rds, nomachine, spice, x2go) diff --git a/server/src/uds/migrations/fixers/transports_v4/_migrator.py b/server/src/uds/migrations/fixers/transports_v4/_migrator.py index 2dbc57c32..751b12aba 100644 --- a/server/src/uds/migrations/fixers/transports_v4/_migrator.py +++ b/server/src/uds/migrations/fixers/transports_v4/_migrator.py @@ -1,5 +1,4 @@ import typing -import collections.abc import logging from uds.core.environment import Environment diff --git a/server/src/uds/migrations/fixers/transports_v4/html5rdp.py b/server/src/uds/migrations/fixers/transports_v4/html5rdp.py index c19e34554..2b0f7fbba 100644 --- a/server/src/uds/migrations/fixers/transports_v4/html5rdp.py +++ b/server/src/uds/migrations/fixers/transports_v4/html5rdp.py @@ -32,7 +32,7 @@ import logging import typing from uds.core.ui import gui -from uds.core import transports, consts +from uds.core import transports from . import _migrator diff --git a/server/src/uds/migrations/fixers/transports_v4/html5rds.py b/server/src/uds/migrations/fixers/transports_v4/html5rds.py index 204d448f7..24d8f8848 100644 --- a/server/src/uds/migrations/fixers/transports_v4/html5rds.py +++ b/server/src/uds/migrations/fixers/transports_v4/html5rds.py @@ -31,7 +31,7 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import logging import typing -from uds.core import consts, transports +from uds.core import transports from uds.core.ui import gui from . import _migrator diff --git a/server/src/uds/migrations/fixers/transports_v4/html5ssh.py b/server/src/uds/migrations/fixers/transports_v4/html5ssh.py index 72bf89e55..b038e9680 100644 --- a/server/src/uds/migrations/fixers/transports_v4/html5ssh.py +++ b/server/src/uds/migrations/fixers/transports_v4/html5ssh.py @@ -31,7 +31,7 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import logging import typing -from uds.core import consts, transports +from uds.core import transports from uds.core.ui import gui from . import _migrator diff --git a/server/src/uds/migrations/fixers/transports_v4/html5vnc.py b/server/src/uds/migrations/fixers/transports_v4/html5vnc.py index bcd649373..f1d77ee23 100644 --- a/server/src/uds/migrations/fixers/transports_v4/html5vnc.py +++ b/server/src/uds/migrations/fixers/transports_v4/html5vnc.py @@ -31,7 +31,7 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import logging import typing -from uds.core import consts, transports +from uds.core import transports from uds.core.ui import gui from . import _migrator diff --git a/server/src/uds/migrations/fixers/transports_v4/nicedcv.py b/server/src/uds/migrations/fixers/transports_v4/nicedcv.py index 00c75e1b7..7d12ed829 100644 --- a/server/src/uds/migrations/fixers/transports_v4/nicedcv.py +++ b/server/src/uds/migrations/fixers/transports_v4/nicedcv.py @@ -31,7 +31,7 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import typing import logging -from uds.core import consts, transports +from uds.core import transports from uds.core.ui import gui from . import _migrator diff --git a/server/src/uds/migrations/fixers/transports_v4/nomachine.py b/server/src/uds/migrations/fixers/transports_v4/nomachine.py index 9f273258b..d5a76f098 100644 --- a/server/src/uds/migrations/fixers/transports_v4/nomachine.py +++ b/server/src/uds/migrations/fixers/transports_v4/nomachine.py @@ -31,7 +31,7 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import typing import logging -from uds.core import consts, transports +from uds.core import transports from uds.core.ui import gui from . import _migrator diff --git a/server/src/uds/migrations/fixers/transports_v4/rdp.py b/server/src/uds/migrations/fixers/transports_v4/rdp.py index 4ad98fc88..47a375483 100644 --- a/server/src/uds/migrations/fixers/transports_v4/rdp.py +++ b/server/src/uds/migrations/fixers/transports_v4/rdp.py @@ -31,7 +31,7 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import typing import logging -from uds.core import consts, transports +from uds.core import transports from uds.core.ui import gui from . import _migrator diff --git a/server/src/uds/migrations/fixers/transports_v4/rds.py b/server/src/uds/migrations/fixers/transports_v4/rds.py index 6f6164581..fd5c25787 100644 --- a/server/src/uds/migrations/fixers/transports_v4/rds.py +++ b/server/src/uds/migrations/fixers/transports_v4/rds.py @@ -31,7 +31,7 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import typing import logging -from uds.core import consts, transports +from uds.core import transports from uds.core.ui import gui from . import _migrator diff --git a/server/src/uds/migrations/fixers/transports_v4/spice.py b/server/src/uds/migrations/fixers/transports_v4/spice.py index d79615170..9f722a9cc 100644 --- a/server/src/uds/migrations/fixers/transports_v4/spice.py +++ b/server/src/uds/migrations/fixers/transports_v4/spice.py @@ -32,7 +32,7 @@ import typing import logging from uds.core.ui import gui -from uds.core import transports, consts +from uds.core import transports from . import _migrator diff --git a/server/src/uds/migrations/fixers/transports_v4/x2go.py b/server/src/uds/migrations/fixers/transports_v4/x2go.py index a773751a8..2a6f471d6 100644 --- a/server/src/uds/migrations/fixers/transports_v4/x2go.py +++ b/server/src/uds/migrations/fixers/transports_v4/x2go.py @@ -31,7 +31,7 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import typing import logging -from uds.core import consts, transports +from uds.core import transports from uds.core.types.preferences import CommonPrefs from uds.core.ui import gui diff --git a/server/src/uds/models/__init__.py b/server/src/uds/models/__init__.py index afc7cc94a..b7daf40f1 100644 --- a/server/src/uds/models/__init__.py +++ b/server/src/uds/models/__init__.py @@ -30,6 +30,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false # Imports all models so they are available for migrations, etc.. from .managed_object_model import ManagedObjectModel diff --git a/server/src/uds/models/account.py b/server/src/uds/models/account.py index 80041bb6a..8052c0ff7 100644 --- a/server/src/uds/models/account.py +++ b/server/src/uds/models/account.py @@ -30,7 +30,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import models diff --git a/server/src/uds/models/account_usage.py b/server/src/uds/models/account_usage.py index 6a976e3da..b686961a6 100644 --- a/server/src/uds/models/account_usage.py +++ b/server/src/uds/models/account_usage.py @@ -29,7 +29,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import logging from django.db import models diff --git a/server/src/uds/models/authenticator.py b/server/src/uds/models/authenticator.py index b52f27f56..82122d3ac 100644 --- a/server/src/uds/models/authenticator.py +++ b/server/src/uds/models/authenticator.py @@ -46,7 +46,7 @@ from .tag import TaggingMixin # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds.models import Group, Network, User, MFA - from django.db.models.manager import RelatedManager # type: ignore # MyPy complains because of django-stubs + from django.db.models.manager import RelatedManager logger = logging.getLogger(__name__) @@ -204,7 +204,7 @@ class Authenticator(ManagedObjectModel, TaggingMixin): except Exception: return returnValueIfNot - def is_ip_allowed(self, ipStr: str) -> bool: + def is_ip_allowed(self, ip_string: str) -> bool: """ Checks if this transport is valid for the specified IP. @@ -226,10 +226,10 @@ class Authenticator(ManagedObjectModel, TaggingMixin): """ if self.net_filtering == consts.auth.NO_FILTERING: return True - ip, version = net.ip_to_long(ipStr) + ip_number, version = net.ip_to_long(ip_string) # Allow exists = self.networks.filter( - start__lte=Network.hexlify(ip), end__gte=Network.hexlify(ip), version=version + start__lte=Network.hexlify(ip_number), end__gte=Network.hexlify(ip_number), version=version ).exists() if self.net_filtering == consts.auth.ALLOW: return exists diff --git a/server/src/uds/models/cache.py b/server/src/uds/models/cache.py index 6b46115a3..fb727253e 100644 --- a/server/src/uds/models/cache.py +++ b/server/src/uds/models/cache.py @@ -56,7 +56,7 @@ class Cache(models.Model): # "fake" relations declarations for type checking # objects: 'models.manager.Manager[Cache]' - class Meta: # pylint: disable=too-few-public-methods + class Meta(models.Model.Meta): """ Meta class to declare the name of the table at database """ diff --git a/server/src/uds/models/calendar.py b/server/src/uds/models/calendar.py index 153ca0aa8..1e2419409 100644 --- a/server/src/uds/models/calendar.py +++ b/server/src/uds/models/calendar.py @@ -31,7 +31,6 @@ Author:: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import models from .uuid_model import UUIDModel diff --git a/server/src/uds/models/calendar_rule.py b/server/src/uds/models/calendar_rule.py index f4ca580c6..1f6736d6c 100644 --- a/server/src/uds/models/calendar_rule.py +++ b/server/src/uds/models/calendar_rule.py @@ -36,7 +36,6 @@ import datetime import enum import logging import typing -import collections.abc from django.db import models from django.utils.translation import gettext_lazy as _ diff --git a/server/src/uds/models/config.py b/server/src/uds/models/config.py index 0439ccbd2..cbdb6bf43 100644 --- a/server/src/uds/models/config.py +++ b/server/src/uds/models/config.py @@ -53,7 +53,7 @@ class Config(models.Model): # "fake" declarations for type checking # objects: 'models.manager.Manager[Config]' - class Meta: # pylint: disable=too-few-public-methods + class Meta: # pyright: ignore """ Meta class to declare default order and unique multiple field index """ diff --git a/server/src/uds/models/delayed_task.py b/server/src/uds/models/delayed_task.py index a46cb92cb..dfcc546d0 100644 --- a/server/src/uds/models/delayed_task.py +++ b/server/src/uds/models/delayed_task.py @@ -61,7 +61,7 @@ class DelayedTask(models.Model): # "fake" declarations for type checking # objects: 'models.manager.Manager[DelayedTask]' - class Meta: # pylint: disable=too-few-public-methods + class Meta(models.Model.Meta): """ Meta class to declare default order and unique multiple field index """ diff --git a/server/src/uds/models/group.py b/server/src/uds/models/group.py index 26312a919..9f05abce8 100644 --- a/server/src/uds/models/group.py +++ b/server/src/uds/models/group.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import models diff --git a/server/src/uds/models/image.py b/server/src/uds/models/image.py index ca4208be0..adc101140 100644 --- a/server/src/uds/models/image.py +++ b/server/src/uds/models/image.py @@ -28,13 +28,10 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -from email.mime import base import io import base64 import logging -from operator import ne import typing -import collections.abc import PIL.Image diff --git a/server/src/uds/models/log.py b/server/src/uds/models/log.py index 6633830c7..b4c738ca8 100644 --- a/server/src/uds/models/log.py +++ b/server/src/uds/models/log.py @@ -64,7 +64,7 @@ class Log(models.Model): # "fake" declarations for type checking # objects: 'models.manager.Manager[Log]' - class Meta: # pylint: disable=too-few-public-methods + class Meta: # pyright: ignore """ Meta class to declare db table """ diff --git a/server/src/uds/models/mfa.py b/server/src/uds/models/mfa.py index 1e2062dd2..82c4cc11e 100644 --- a/server/src/uds/models/mfa.py +++ b/server/src/uds/models/mfa.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import models diff --git a/server/src/uds/models/notifications.py b/server/src/uds/models/notifications.py index 4f12dfbb8..2aa4950f3 100644 --- a/server/src/uds/models/notifications.py +++ b/server/src/uds/models/notifications.py @@ -30,7 +30,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import models, transaction @@ -61,7 +60,7 @@ class Notification(models.Model): # "fake" declarations for type checking # objects: 'models.BaseManager[Notification]' - class Meta: # pylint: disable=too-few-public-methods + class Meta: # pyright: ignore """ Meta class to declare db table """ diff --git a/server/src/uds/models/osmanager.py b/server/src/uds/models/osmanager.py index 2b1760637..535848a5d 100644 --- a/server/src/uds/models/osmanager.py +++ b/server/src/uds/models/osmanager.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import IntegrityError, models diff --git a/server/src/uds/models/permissions.py b/server/src/uds/models/permissions.py index 977aac735..63236e776 100644 --- a/server/src/uds/models/permissions.py +++ b/server/src/uds/models/permissions.py @@ -65,7 +65,7 @@ class Permissions(UUIDModel): ) # Future "permisions ends at this moment", not assigned right now user = models.ForeignKey( - User, + 'User', on_delete=models.CASCADE, related_name='permissions', null=True, @@ -73,7 +73,7 @@ class Permissions(UUIDModel): default=None, ) group = models.ForeignKey( - Group, + 'Group', on_delete=models.CASCADE, related_name='permissions', null=True, diff --git a/server/src/uds/models/properties.py b/server/src/uds/models/properties.py index 9a84a68fa..e0c4af1ed 100644 --- a/server/src/uds/models/properties.py +++ b/server/src/uds/models/properties.py @@ -49,7 +49,7 @@ class Properties(models.Model): key = models.CharField(max_length=64, db_index=True) value: typing.Any = models.JSONField(default=dict) - class Meta: # pylint: disable=too-few-public-methods + class Meta: # pyright: ignore """ Meta class to declare the name of the table at database """ diff --git a/server/src/uds/models/scheduler.py b/server/src/uds/models/scheduler.py index f565e4c20..7998fdc2f 100644 --- a/server/src/uds/models/scheduler.py +++ b/server/src/uds/models/scheduler.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import models @@ -76,7 +75,7 @@ class Scheduler(models.Model): # objects: 'models.manager.Manager[Scheduler]' id: int # Primary key (Autogenerated by model, just for type checking) - class Meta: # pylint: disable=too-few-public-methods + class Meta(models.Model.Meta): """ Meta class to declare default order and unique multiple field index """ diff --git a/server/src/uds/models/servers.py b/server/src/uds/models/servers.py index 92ab4dc01..d658247c0 100644 --- a/server/src/uds/models/servers.py +++ b/server/src/uds/models/servers.py @@ -46,7 +46,6 @@ from .uuid_model import UUIDModel if typing.TYPE_CHECKING: from uds.models.transport import Transport - from uds.models.user import User from uds.models.user_service import UserService @@ -190,7 +189,7 @@ class Server(UUIDModel, TaggingMixin, properties.PropertiesMixin): log_level = models.IntegerField(default=log.LogLevel.ERROR.value) # Extra data, for server type custom data use (i.e. actor keeps command related data here) - data = models.JSONField(null=True, blank=True, default=None) + data: typing.Any = models.JSONField(null=True, blank=True, default=None) # Group (of registered servers) this server belongs to # Note that only Tunnel servers can belong to more than one servergroup diff --git a/server/src/uds/models/service.py b/server/src/uds/models/service.py index 046d0543a..39a131730 100644 --- a/server/src/uds/models/service.py +++ b/server/src/uds/models/service.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import models diff --git a/server/src/uds/models/service_pool.py b/server/src/uds/models/service_pool.py index 45153ea12..5e4e3238f 100644 --- a/server/src/uds/models/service_pool.py +++ b/server/src/uds/models/service_pool.py @@ -188,7 +188,7 @@ class ServicePool(UUIDModel, TaggingMixin): None if there is no valid publication for this deployed service. """ try: - return self.publications.filter(state=types.states.State.USABLE)[0] # type: ignore # Slicing is not supported by pylance right now + return self.publications.filter(state=types.states.State.USABLE)[0] except Exception: return None @@ -282,7 +282,7 @@ class ServicePool(UUIDModel, TaggingMixin): return self.service.is_in_maintenance() if self.service else True def is_visible(self) -> bool: - return self.visible # type: ignore + return self.visible def is_usable(self) -> bool: return ( @@ -466,8 +466,6 @@ class ServicePool(UUIDModel, TaggingMixin): Ensures that at least a group of groups (database groups) has access to this Service Pool raise an InvalidUserException if fails check """ - from uds.core import auths # pylint: disable=import-outside-toplevel - if not set(groups) & set( self.assignedGroups.all() # pylint: disable=no-member ): # pylint: disable=no-member diff --git a/server/src/uds/models/service_pool_group.py b/server/src/uds/models/service_pool_group.py index dcee2aa2d..0838a69a8 100644 --- a/server/src/uds/models/service_pool_group.py +++ b/server/src/uds/models/service_pool_group.py @@ -43,7 +43,7 @@ from .uuid_model import UUIDModel from .image import Image if typing.TYPE_CHECKING: - from service_pool import ServicePool + from .service_pool import ServicePool logger = logging.getLogger(__name__) diff --git a/server/src/uds/models/service_pool_publication.py b/server/src/uds/models/service_pool_publication.py index 5053b8e8f..777c5aba7 100644 --- a/server/src/uds/models/service_pool_publication.py +++ b/server/src/uds/models/service_pool_publication.py @@ -33,7 +33,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import logging import typing -import collections.abc from django.db import models @@ -64,7 +63,7 @@ class ServicePoolPublicationChangelog(models.Model): # "fake" declarations for type checking # objects: 'models.manager.Manager[ServicePoolPublicationChangelog]' - class Meta(UUIDModel.Meta): # pylint: disable=too-few-public-methods + class Meta(UUIDModel.Meta): # pyright: ignore """ Meta class to declare default order and unique multiple field index """ diff --git a/server/src/uds/models/stats_counters.py b/server/src/uds/models/stats_counters.py index ee628eefd..7a7e2c719 100644 --- a/server/src/uds/models/stats_counters.py +++ b/server/src/uds/models/stats_counters.py @@ -54,7 +54,7 @@ class StatsCounters(models.Model): # "fake" declarations for type checking # objects: 'models.manager.Manager[StatsCounters]' - class Meta: # pylint: disable=too-few-public-methods + class Meta: # pyright: ignore """ Meta class to declare db table """ @@ -123,7 +123,7 @@ class StatsCounters(models.Model): interval = int(to - since) / max_intervals if interval > 0: - q = q.extra( # nosec: SQL injection is not possible here + q = q.extra( # type: ignore # nosec: SQL injection is not possible here select={ 'group_by_stamp': f'stamp - (stamp %% {interval})', # f'{floor}(stamp / {interval}) * {interval}', }, @@ -132,7 +132,7 @@ class StatsCounters(models.Model): fnc = models.Avg('value') if not kwargs.get('use_max') else models.Max('value') q = ( - q.order_by('group_by_stamp') + q.order_by('group_by_stamp') # type: ignore .values('group_by_stamp') .annotate( value=fnc, diff --git a/server/src/uds/models/stats_counters_accum.py b/server/src/uds/models/stats_counters_accum.py index aed4dd905..8677f4301 100644 --- a/server/src/uds/models/stats_counters_accum.py +++ b/server/src/uds/models/stats_counters_accum.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import enum import datetime import logging @@ -83,7 +82,7 @@ class StatsCountersAccum(models.Model): # "fake" declarations for type checking # objects: 'models.manager.Manager[StatsCountersAccum]' - class Meta: # pylint: disable=too-few-public-methods + class Meta: # pyright: ignore """ Meta class to declare db table """ @@ -144,7 +143,7 @@ class StatsCountersAccum(models.Model): interval = interval_type.seconds() # Get last stamp in table for this interval_type - start_record = ( + start_record: 'StatsCounters|StatsCountersAccum|None' = ( StatsCountersAccum.objects.filter(interval_type=interval_type) .order_by('stamp') .last() diff --git a/server/src/uds/models/stats_events.py b/server/src/uds/models/stats_events.py index 323b36926..acfe354ce 100644 --- a/server/src/uds/models/stats_events.py +++ b/server/src/uds/models/stats_events.py @@ -61,7 +61,7 @@ class StatsEvents(models.Model): # "fake" declarations for type checking # objects: 'models.manager.Manager[StatsEvents]' - class Meta: # pylint: disable=too-few-public-methods + class Meta: # pyright: ignore """ Meta class to declare db table """ @@ -137,7 +137,7 @@ class StatsEvents(models.Model): """ Returns the timestamp in ISO format (UTC) """ - stamp = datetime.datetime.utcfromtimestamp(self.stamp) + stamp = datetime.datetime.fromtimestamp(self.stamp, datetime.UTC) return stamp.isoformat() # returns CSV header diff --git a/server/src/uds/models/storage.py b/server/src/uds/models/storage.py index bd45859c5..c9791fec4 100644 --- a/server/src/uds/models/storage.py +++ b/server/src/uds/models/storage.py @@ -53,7 +53,7 @@ class Storage(models.Model): # "fake" declarations for type checking # objects: 'models.manager.Manager[Storage]' - class Meta: # pylint: disable=too-few-public-methods + class Meta: # pyright: ignore """ Meta class to declare the name of the table at database """ diff --git a/server/src/uds/models/tag.py b/server/src/uds/models/tag.py index 9049b7050..194b0a208 100644 --- a/server/src/uds/models/tag.py +++ b/server/src/uds/models/tag.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import typing -import collections.abc import logging @@ -101,5 +100,5 @@ class Tag(UUIDModel): class TaggingMixin(models.Model): tags: 'models.ManyToManyField[Tag, TaggingMixin]' = models.ManyToManyField(Tag) - class Meta: # pylint: disable=too-few-public-methods + class Meta: # pyright: ignore abstract = True diff --git a/server/src/uds/models/transport.py b/server/src/uds/models/transport.py index 3fffb2dff..f2686031d 100644 --- a/server/src/uds/models/transport.py +++ b/server/src/uds/models/transport.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import models diff --git a/server/src/uds/models/unique_id.py b/server/src/uds/models/unique_id.py index d676f9360..748634323 100644 --- a/server/src/uds/models/unique_id.py +++ b/server/src/uds/models/unique_id.py @@ -52,7 +52,7 @@ class UniqueId(models.Model): # "fake" declarations for type checking # objects: 'models.manager.Manager[UniqueId]' - class Meta: # pylint: disable=too-few-public-methods + class Meta(models.Model.Meta): """ Meta class to declare default order and unique multiple field index """ diff --git a/server/src/uds/models/user.py b/server/src/uds/models/user.py index efef189dc..33180d657 100644 --- a/server/src/uds/models/user.py +++ b/server/src/uds/models/user.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import models from django.db.models import Count, Q, signals @@ -48,7 +47,7 @@ from .uuid_model import UUIDModel if typing.TYPE_CHECKING: from uds.models import Group, UserService, Permissions from uds.core.types.requests import ExtendedHttpRequest - from django.db.models.manager import RelatedManager # type: ignore # MyPy complains because of django-stubs + from django.db.models.manager import RelatedManager logger = logging.getLogger(__name__) diff --git a/server/src/uds/models/user_service.py b/server/src/uds/models/user_service.py index a430efe3f..e6b94c4a8 100644 --- a/server/src/uds/models/user_service.py +++ b/server/src/uds/models/user_service.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.db import models from django.db.models import signals @@ -42,7 +41,6 @@ from uds.core.environment import Environment from uds.core.util import log, properties from uds.core.util.model import sql_datetime from uds.core.types.states import State -from uds.models import service_pool from uds.models.service_pool import ServicePool from uds.models.service_pool_publication import ServicePoolPublication from uds.models.user import User @@ -311,7 +309,7 @@ class UserService(UUIDModel, properties.PropertiesMixin): # If value is found on property, use it, else, try to recover it from storage if val is None: val = typing.cast(str, self.get_environment().storage.get(name)) - return val + return val or '' def set_connection_source(self, src: types.connections.ConnectionSource) -> None: """ diff --git a/server/src/uds/models/user_service_session.py b/server/src/uds/models/user_service_session.py index 81bf90868..c0c928f84 100644 --- a/server/src/uds/models/user_service_session.py +++ b/server/src/uds/models/user_service_session.py @@ -68,7 +68,7 @@ class UserServiceSession(models.Model): # pylint: disable=too-many-public-metho # "fake" declarations for type checking # objects: 'models.manager.Manager["UserServiceSession"]' - class Meta: # pylint: disable=too-few-public-methods + class Meta(models.Model.Meta): """ Meta class to declare default order and unique multiple field index """ diff --git a/server/src/uds/models/uuid_model.py b/server/src/uds/models/uuid_model.py index 3b0520ecd..6e48d7268 100644 --- a/server/src/uds/models/uuid_model.py +++ b/server/src/uds/models/uuid_model.py @@ -52,7 +52,7 @@ class UUIDModel(models.Model): # Just a fake declaration to allow type checking id: int - class Meta: # pylint: disable=too-few-public-methods + class Meta: # pyright: ignore abstract = True # Override default save to add uuid diff --git a/server/src/uds/notifiers/email/__init__.py b/server/src/uds/notifiers/email/__init__.py index b3da9df82..26d405d01 100644 --- a/server/src/uds/notifiers/email/__init__.py +++ b/server/src/uds/notifiers/email/__init__.py @@ -1,2 +1,35 @@ +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2012-2024 Virtual Cable S.L.U. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false # Make notifiers available to the UDS system. from .notifier import EmailNotifier diff --git a/server/src/uds/notifiers/email/notifier.py b/server/src/uds/notifiers/email/notifier.py index 31dbcfd7e..ba03afee9 100644 --- a/server/src/uds/notifiers/email/notifier.py +++ b/server/src/uds/notifiers/email/notifier.py @@ -36,7 +36,6 @@ import ssl from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -47,7 +46,7 @@ from uds.core.util import validators # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module + pass logger = logging.getLogger(__name__) diff --git a/server/src/uds/notifiers/telegram/__init__.py b/server/src/uds/notifiers/telegram/__init__.py index 5240f4b6e..01b76467d 100644 --- a/server/src/uds/notifiers/telegram/__init__.py +++ b/server/src/uds/notifiers/telegram/__init__.py @@ -1,2 +1,35 @@ +# -*- coding: utf-8 -*- + +# +# Copyright (c) 2012-2024 Virtual Cable S.L.U. +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, +# are permitted provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, +# this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, +# this list of conditions and the following disclaimer in the documentation +# and/or other materials provided with the distribution. +# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors +# may be used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false # Make notifiers available to the UDS system. from .notifier import TelegramNotifier diff --git a/server/src/uds/notifiers/telegram/jobs.py b/server/src/uds/notifiers/telegram/jobs.py index 8031a3657..8896282e9 100644 --- a/server/src/uds/notifiers/telegram/jobs.py +++ b/server/src/uds/notifiers/telegram/jobs.py @@ -10,7 +10,6 @@ """ import logging import typing -import collections.abc from uds.core import jobs diff --git a/server/src/uds/notifiers/telegram/notifier.py b/server/src/uds/notifiers/telegram/notifier.py index 407014f5d..901d2de2c 100644 --- a/server/src/uds/notifiers/telegram/notifier.py +++ b/server/src/uds/notifiers/telegram/notifier.py @@ -34,7 +34,6 @@ import logging import datetime import secrets import typing -import collections.abc import time from django.utils.translation import gettext_noop as _ @@ -48,7 +47,7 @@ from . import telegram # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module + pass logger = logging.getLogger(__name__) diff --git a/server/src/uds/osmanagers/LinuxOsManager/__init__.py b/server/src/uds/osmanagers/LinuxOsManager/__init__.py index b35e90d44..28d07fc50 100644 --- a/server/src/uds/osmanagers/LinuxOsManager/__init__.py +++ b/server/src/uds/osmanagers/LinuxOsManager/__init__.py @@ -28,16 +28,15 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com -@author: Alexander Burmatov, thatman at altlinux dot org +@Author: Adolfo Gómez, dkmaster at dkmon dot com +@Author: Alexander Burmatov, thatman at altlinux dot org """ +# pyright: reportUnusedImport=false import os.path import typing -import collections.abc import sys from django.utils.translation import gettext_noop as _ -from uds.core.osmanagers.osmfactory import OSManagersFactory from uds.core.managers import downloads_manager from uds.core.consts.system import VERSION diff --git a/server/src/uds/osmanagers/LinuxOsManager/linux_ad_osmanager.py b/server/src/uds/osmanagers/LinuxOsManager/linux_ad_osmanager.py index 337a4a4ba..9beb90ff2 100644 --- a/server/src/uds/osmanagers/LinuxOsManager/linux_ad_osmanager.py +++ b/server/src/uds/osmanagers/LinuxOsManager/linux_ad_osmanager.py @@ -30,7 +30,6 @@ """ @author: Alexander Burmatov, thatman at altlinux dot org """ -import codecs import logging import typing import collections.abc @@ -38,16 +37,11 @@ import collections.abc from django.utils.translation import gettext_lazy from django.utils.translation import gettext_noop as _ -from uds.core import exceptions, types, consts -from uds.core.managers.crypto import CryptoManager -from uds.core.module import Module -from uds.core.ui import gui -from uds.core.workers import initialize +from uds.core import ui, types, exceptions from .linux_osmanager import LinuxOsManager if typing.TYPE_CHECKING: - from uds.core.environment import Environment from uds.models.user_service import UserService @@ -60,28 +54,28 @@ class LinuxOsADManager(LinuxOsManager): type_description = _('Os Manager to control Linux virtual machines with active directory') icon_file = 'losmanager.png' - domain = gui.TextField( + domain = ui.gui.TextField( length=64, label=_('Domain'), order=1, tooltip=_('Domain to join machines to (use FQDN form, Netbios name not supported for most operations)'), required=True, ) - account = gui.TextField( + account = ui.gui.TextField( length=64, label=_('Account'), order=2, tooltip=_('Account with rights to add machines to domain'), required=True, ) - password = gui.PasswordField( + password = ui.gui.PasswordField( length=64, label=_('Password'), order=3, tooltip=_('Password of the account'), required=True, ) - ou = gui.TextField( + ou = ui.gui.TextField( length=256, label=_('OU'), order=4, @@ -89,43 +83,43 @@ class LinuxOsADManager(LinuxOsManager): 'Organizational unit where to add machines in domain (not used if IPA is selected). i.e.: ou=My Machines,dc=mydomain,dc=local' ), ) - client_software = gui.ChoiceField( + client_software = ui.gui.ChoiceField( label=_('Client software'), order=5, tooltip=_('Use specific client software'), choices=[ - gui.choice_item('automatically', gettext_lazy('Automatically')), - gui.choice_item('sssd', gettext_lazy('SSSD')), - gui.choice_item('winbind', gettext_lazy('Winbind')), + ui.gui.choice_item('automatically', gettext_lazy('Automatically')), + ui.gui.choice_item('sssd', gettext_lazy('SSSD')), + ui.gui.choice_item('winbind', gettext_lazy('Winbind')), ], tab=types.ui.Tab.ADVANCED, default='automatically', ) - membership_software = gui.ChoiceField( + membership_software = ui.gui.ChoiceField( label=_('Membership software'), order=6, tooltip=_('Use specific membership software'), choices=[ - gui.choice_item('automatically', gettext_lazy('Automatically')), - gui.choice_item('samba', gettext_lazy('Samba')), - gui.choice_item('adcli', gettext_lazy('AdCli')), + ui.gui.choice_item('automatically', gettext_lazy('Automatically')), + ui.gui.choice_item('samba', gettext_lazy('Samba')), + ui.gui.choice_item('adcli', gettext_lazy('AdCli')), ], tab=types.ui.Tab.ADVANCED, default='automatically', ) - server_software = gui.ChoiceField( + server_software = ui.gui.ChoiceField( label=_('Server software'), order=7, tooltip=_('Use specific server software'), choices=[ # gui.choice_item('automatically', gettext_lazy('Automatically')), - gui.choice_item('active-directory', gettext_lazy('Active Directory')), - gui.choice_item('freeipa', gettext_lazy('FreeIPA')), + ui.gui.choice_item('active-directory', gettext_lazy('Active Directory')), + ui.gui.choice_item('freeipa', gettext_lazy('FreeIPA')), ], tab=types.ui.Tab.ADVANCED, default='active-directory', ) - remove_on_exit = gui.CheckBoxField( + remove_on_exit = ui.gui.CheckBoxField( label=_('Machine clean'), order=7, tooltip=_( @@ -134,14 +128,14 @@ class LinuxOsADManager(LinuxOsManager): tab=types.ui.Tab.ADVANCED, default=True, ) - use_ssl = gui.CheckBoxField( + use_ssl = ui.gui.CheckBoxField( label=_('Use SSL'), order=8, tooltip=_('If checked, a ssl connection to Active Directory will be used'), tab=types.ui.Tab.ADVANCED, default=True, ) - automatic_id_mapping = gui.CheckBoxField( + automatic_id_mapping = ui.gui.CheckBoxField( label=_('Automatic ID mapping'), order=9, tooltip=_('If checked, automatic ID mapping'), diff --git a/server/src/uds/osmanagers/LinuxOsManager/linux_osmanager.py b/server/src/uds/osmanagers/LinuxOsManager/linux_osmanager.py index a1d9280ad..fa9771019 100644 --- a/server/src/uds/osmanagers/LinuxOsManager/linux_osmanager.py +++ b/server/src/uds/osmanagers/LinuxOsManager/linux_osmanager.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright (c) 2012-2019 Virtual Cable S.L. +# Copyright (c) 2012-2024 Virtual Cable S.L.U. # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, @@ -28,25 +28,21 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing import collections.abc -from django.utils.translation import gettext_lazy from django.utils.translation import gettext_noop as _ -from uds.core import osmanagers, types, consts +from uds.core import osmanagers, types from uds.core.types.services import ServiceType as serviceTypes from uds.core.ui import gui from uds.core.util import fields, log from uds.core.types.states import State -from uds.core.workers import initialize if typing.TYPE_CHECKING: - from uds.core.environment import Environment - from uds.core.module import Module from uds.models.user_service import UserService logger = logging.getLogger(__name__) diff --git a/server/src/uds/osmanagers/LinuxOsManager/linux_randompass_osmanager.py b/server/src/uds/osmanagers/LinuxOsManager/linux_randompass_osmanager.py index 4cebac83f..714af0f5a 100644 --- a/server/src/uds/osmanagers/LinuxOsManager/linux_randompass_osmanager.py +++ b/server/src/uds/osmanagers/LinuxOsManager/linux_randompass_osmanager.py @@ -38,7 +38,6 @@ import typing import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core.module import Module from uds.core.ui import gui from uds.core import exceptions from uds.core.util import log @@ -49,8 +48,6 @@ logger = logging.getLogger(__name__) if typing.TYPE_CHECKING: from uds.models.user_service import UserService - from uds.core.environment import Environment - from uds.core.module import Module class LinuxRandomPassManager(LinuxOsManager): @@ -87,7 +84,7 @@ class LinuxRandomPassManager(LinuxOsManager): def gen_random_password(self, service: 'UserService') -> str: randomPass = service.recover_value('linOsRandomPass') - if randomPass is None: + if not randomPass: randomPass = ''.join( random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(16) ) @@ -101,19 +98,19 @@ class LinuxRandomPassManager(LinuxOsManager): return randomPass - def actor_data(self, userService: 'UserService') -> collections.abc.MutableMapping[str, typing.Any]: + def actor_data(self, userservice: 'UserService') -> collections.abc.MutableMapping[str, typing.Any]: return { 'action': 'rename', - 'name': userService.get_name(), + 'name': userservice.get_name(), # Repeat data, to keep compat with old versions of Actor # Will be removed in a couple of versions 'username': self.user_account.as_str(), 'password': '', # On linux, user password is not needed so we provide an empty one - 'new_password': self.gen_random_password(userService), + 'new_password': self.gen_random_password(userservice), 'custom': { 'username': self.user_account.as_str(), 'password': '', # On linux, user password is not needed so we provide an empty one - 'new_password': self.gen_random_password(userService), + 'new_password': self.gen_random_password(userservice), }, } diff --git a/server/src/uds/osmanagers/Test/__init__.py b/server/src/uds/osmanagers/Test/__init__.py index 1fa6dc854..716dd446b 100644 --- a/server/src/uds/osmanagers/Test/__init__.py +++ b/server/src/uds/osmanagers/Test/__init__.py @@ -29,4 +29,5 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .testing_osmanager import TestOSManager diff --git a/server/src/uds/osmanagers/Test/testing_osmanager.py b/server/src/uds/osmanagers/Test/testing_osmanager.py index 41af00116..089a0c696 100644 --- a/server/src/uds/osmanagers/Test/testing_osmanager.py +++ b/server/src/uds/osmanagers/Test/testing_osmanager.py @@ -44,7 +44,6 @@ from uds.core.util import log if typing.TYPE_CHECKING: from uds.models.user_service import UserService - from uds.core.module import Module logger = logging.getLogger(__name__) @@ -88,16 +87,16 @@ class TestOSManager(osmanagers.OSManager): def initialize(self, values: 'types.core.ValuesType') -> None: self.handles_unused_userservices = True - def release(self, userService: 'UserService') -> None: - logger.debug('User service %s released', userService) + def release(self, userservice: 'UserService') -> None: + logger.debug('User service %s released', userservice) - def is_removable_on_logout(self, userService: 'UserService') -> bool: + def is_removable_on_logout(self, userservice: 'UserService') -> bool: ''' Says if a machine is removable on logout ''' - if not userService.in_use: + if not userservice.in_use: if (self.on_logout.value == 'remove') or ( - not userService.is_publication_valid() and self.on_logout.value == 'keep' + not userservice.is_publication_valid() and self.on_logout.value == 'keep' ): return True @@ -109,8 +108,8 @@ class TestOSManager(osmanagers.OSManager): """ return userService.get_name() - def actor_data(self, userService: 'UserService') -> collections.abc.MutableMapping[str, typing.Any]: - return {'action': 'rename', 'name': userService.get_name()} + def actor_data(self, userservice: 'UserService') -> collections.abc.MutableMapping[str, typing.Any]: + return {'action': 'rename', 'name': userservice.get_name()} def handle_unused(self, userservice: 'UserService') -> None: """ diff --git a/server/src/uds/osmanagers/WindowsOsManager/__init__.py b/server/src/uds/osmanagers/WindowsOsManager/__init__.py index 35f32ce37..556ed2ee0 100644 --- a/server/src/uds/osmanagers/WindowsOsManager/__init__.py +++ b/server/src/uds/osmanagers/WindowsOsManager/__init__.py @@ -29,15 +29,14 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false import os.path import typing -import collections.abc import sys from django.utils.translation import gettext_noop as _ -from uds.core import osmanagers from uds.core import managers from uds.core.consts.system import VERSION diff --git a/server/src/uds/osmanagers/WindowsOsManager/windows.py b/server/src/uds/osmanagers/WindowsOsManager/windows.py index dfdc05a3b..581826c7d 100644 --- a/server/src/uds/osmanagers/WindowsOsManager/windows.py +++ b/server/src/uds/osmanagers/WindowsOsManager/windows.py @@ -8,15 +8,13 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import codecs import logging import typing import collections.abc -from django.utils.translation import gettext_lazy from django.utils.translation import gettext_noop as _ -from uds.core import exceptions, osmanagers, types, consts +from uds.core import osmanagers, types from uds.core.types.services import ServiceType as serviceTypes from uds.core.ui import gui from uds.core.util import log, fields @@ -25,7 +23,6 @@ from uds.models import TicketStore # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.models import UserService logger = logging.getLogger(__name__) diff --git a/server/src/uds/osmanagers/WindowsOsManager/windows_domain.py b/server/src/uds/osmanagers/WindowsOsManager/windows_domain.py index 90f13577f..4bf4edb5e 100644 --- a/server/src/uds/osmanagers/WindowsOsManager/windows_domain.py +++ b/server/src/uds/osmanagers/WindowsOsManager/windows_domain.py @@ -42,7 +42,7 @@ import ldap from django.utils.translation import gettext_noop as _ from uds.core.ui import gui from uds.core.managers.crypto import CryptoManager -from uds.core import environment, exceptions, consts, types +from uds.core import environment, exceptions, types from uds.core.util import fields, log, ldaputil from .windows import WindowsOsManager @@ -164,7 +164,7 @@ class WinDomainOsManager(WindowsOsManager): for server in reversed( sorted( - iter(dns.resolver.resolve('_ldap._tcp.' + self.domain.as_str(), 'SRV')), + iter(typing.cast(collections.abc.Iterable[typing.Any], dns.resolver.resolve('_ldap._tcp.' + self.domain.as_str(), 'SRV'))), key=key, ) ): @@ -279,7 +279,7 @@ class WinDomainOsManager(WindowsOsManager): f'Could not remove machine from domain (_ldap._tcp.{self.domain.as_str()} not found)', log.LogSource.OSMANAGER, ) - except ldaputil.ALREADY_EXISTS: + except ldaputil.ALREADY_EXISTS: # pyright: ignore # Already added this machine to this group, pass error = None break @@ -365,7 +365,7 @@ class WinDomainOsManager(WindowsOsManager): return str(e) try: - ldap_connection.search_st(self.ou.as_str(), ldaputil.SCOPE_BASE) + ldap_connection.search_st(self.ou.as_str(), ldaputil.SCOPE_BASE) # pyright: ignore except ldaputil.LDAPError as e: return _('Check error: {}').format(e) diff --git a/server/src/uds/reports/__init__.py b/server/src/uds/reports/__init__.py index 999344ea7..453dd480f 100644 --- a/server/src/uds/reports/__init__.py +++ b/server/src/uds/reports/__init__.py @@ -40,7 +40,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from uds.core import reports from uds.core.util import modfinder diff --git a/server/src/uds/reports/auto/__init__.py b/server/src/uds/reports/auto/__init__.py index f1110e375..fc2daabb5 100644 --- a/server/src/uds/reports/auto/__init__.py +++ b/server/src/uds/reports/auto/__init__.py @@ -30,13 +30,12 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import abc import datetime import logging import typing import collections.abc -from django.utils.translation import gettext, gettext_noop as _ +from django.utils.translation import gettext_noop as _ from uds.core.ui import gui from uds.core.reports import Report diff --git a/server/src/uds/reports/auto/fields.py b/server/src/uds/reports/auto/fields.py index 54f21d066..f01314033 100644 --- a/server/src/uds/reports/auto/fields.py +++ b/server/src/uds/reports/auto/fields.py @@ -30,10 +30,8 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import datetime import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ from uds.core import types diff --git a/server/src/uds/reports/lists/__init__.py b/server/src/uds/reports/lists/__init__.py index 8519c2f73..565eced00 100644 --- a/server/src/uds/reports/lists/__init__.py +++ b/server/src/uds/reports/lists/__init__.py @@ -27,9 +27,9 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ - +# pyright: reportUnusedImport=false # Make reports visible to autoloader from . import users from . import audit \ No newline at end of file diff --git a/server/src/uds/reports/lists/audit.py b/server/src/uds/reports/lists/audit.py index 390a656de..f753d5ab9 100644 --- a/server/src/uds/reports/lists/audit.py +++ b/server/src/uds/reports/lists/audit.py @@ -31,12 +31,10 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import csv -import datetime import io import logging import re import typing -import collections.abc from django.utils.translation import gettext from django.utils.translation import gettext_lazy as _ diff --git a/server/src/uds/reports/stats/__init__.py b/server/src/uds/reports/stats/__init__.py index c14e35dc8..86ca39361 100644 --- a/server/src/uds/reports/stats/__init__.py +++ b/server/src/uds/reports/stats/__init__.py @@ -27,8 +27,9 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false # Make reports visible to autoloader # from . import usage diff --git a/server/src/uds/reports/stats/auth_stats.py b/server/src/uds/reports/stats/auth_stats.py index 14f4688a5..f843d2918 100644 --- a/server/src/uds/reports/stats/auth_stats.py +++ b/server/src/uds/reports/stats/auth_stats.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext, gettext_lazy as _ @@ -64,7 +63,7 @@ class AuthenticatorsStats(StatsReportAuto): def generate(self) -> bytes: - stats = [] + stats: list[dict[str, typing.Any]] = [] for a in self.get_model_records(): # Will show a.name on every change... stats.append({'date': a.name, 'users': None}) diff --git a/server/src/uds/reports/stats/pool_users_summary.py b/server/src/uds/reports/stats/pool_users_summary.py index 1c82e4643..a909ab87a 100644 --- a/server/src/uds/reports/stats/pool_users_summary.py +++ b/server/src/uds/reports/stats/pool_users_summary.py @@ -30,18 +30,15 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import csv -import datetime import io import logging import typing -import collections.abc from django.utils.translation import gettext from django.utils.translation import gettext_lazy as _ from uds.core.managers.stats import StatsManager from uds.core.ui import gui -from uds.core.util import dateutils from uds.core.util.stats import events from uds.models import ServicePool diff --git a/server/src/uds/reports/stats/pools_performance.py b/server/src/uds/reports/stats/pools_performance.py index 8237289ce..0f35d4cd6 100644 --- a/server/src/uds/reports/stats/pools_performance.py +++ b/server/src/uds/reports/stats/pools_performance.py @@ -45,7 +45,7 @@ from django.utils.translation import gettext_lazy as _ from uds.core.managers.stats import StatsManager from uds.core.reports import graphs from uds.core.ui import gui -from uds.core.util import dateutils, utils +from uds.core.util import utils from uds.core.util.stats import events from uds.models import ServicePool @@ -114,8 +114,8 @@ class PoolPerformanceReport(StatsReport): reportData: list[dict[str, typing.Any]] = [] for p in self.list_pools(): - dataUsers = [] - dataAccesses = [] + dataUsers: list[tuple[int, int]] = [] + dataAccesses: list[tuple[int, int]] = [] for interval in samplingIntervals: key = (interval[0] + interval[1]) // 2 q = ( @@ -167,28 +167,29 @@ class PoolPerformanceReport(StatsReport): # surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) # @UndefinedVariable # logger.debug('PoolsData: %s', poolsData) + def _tick_fnc1(l: int) -> str: + return filters.date(datetime.datetime.fromtimestamp(l), xLabelFormat) if int(l) >= 0 else '' - X = [v[0] for v in poolsData[0]['dataUsers']] + x = [v[0] for v in poolsData[0]['dataUsers']] data = { 'title': _('Distinct Users'), - 'x': X, - 'xtickFnc': lambda l: ( - filters.date(datetime.datetime.fromtimestamp(X[int(l)]), xLabelFormat) if int(l) >= 0 else '' - ), + 'x': x, + 'xtickFnc': _tick_fnc1, 'xlabel': _('Date'), 'y': [{'label': p['name'], 'data': [v[1] for v in p['dataUsers']]} for p in poolsData], 'ylabel': _('Users'), } graphs.bar_chart(SIZE, data, graph1) + + def _tick_fnc2(l: int) -> str: + return filters.date(datetime.datetime.fromtimestamp(l), xLabelFormat) if int(l) >= 0 else '' - X = [v[0] for v in poolsData[0]['dataAccesses']] + x = [v[0] for v in poolsData[0]['dataAccesses']] data = { 'title': _('Accesses'), - 'x': X, - 'xtickFnc': lambda l: ( - filters.date(datetime.datetime.fromtimestamp(X[int(l)]), xLabelFormat) if int(l) >= 0 else '' - ), + 'x': x, + 'xtickFnc': _tick_fnc2, 'xlabel': _('Date'), 'y': [{'label': p['name'], 'data': [v[1] for v in p['dataAccesses']]} for p in poolsData], 'ylabel': _('Accesses'), diff --git a/server/src/uds/reports/stats/pools_usage_day.py b/server/src/uds/reports/stats/pools_usage_day.py index aaa9f8310..fc1085e7e 100644 --- a/server/src/uds/reports/stats/pools_usage_day.py +++ b/server/src/uds/reports/stats/pools_usage_day.py @@ -34,14 +34,12 @@ import io import datetime import logging import typing -import collections.abc from django.utils.translation import gettext, gettext_lazy as _ from uds.core.ui import gui from uds.core.util.stats import counters from uds.core.reports import graphs -from uds.core.util import dateutils from uds.models import ServicePool @@ -73,7 +71,7 @@ class CountersPoolAssigned(StatsReport): # Generate the sampling intervals and get dataUsers from db start = self.start_date.as_date() - data = [] + data: list[dict[str, typing.Any]] = [] pool: ServicePool for poolUuid in self.pools.value: diff --git a/server/src/uds/reports/stats/usage_by_pool.py b/server/src/uds/reports/stats/usage_by_pool.py index bb45fe6cc..3102ec7a7 100644 --- a/server/src/uds/reports/stats/usage_by_pool.py +++ b/server/src/uds/reports/stats/usage_by_pool.py @@ -34,14 +34,13 @@ import datetime import io import logging import typing -import collections.abc from django.utils.translation import gettext from django.utils.translation import gettext_lazy as _ from uds.core.managers.stats import StatsManager from uds.core.ui import gui -from uds.core.util import dateutils, stats +from uds.core.util import stats from uds.models import ServicePool from .base import StatsReport @@ -78,7 +77,7 @@ class UsageByPool(StatsReport): pools = ServicePool.objects.all() else: pools = ServicePool.objects.filter(uuid__in=self.pool.value) - data = [] + data: list[dict[str, typing.Any]] = [] for pool in pools: items = ( StatsManager.manager() @@ -101,7 +100,7 @@ class UsageByPool(StatsReport): logins[i.fld4] = i.stamp else: if i.fld4 in logins: - stamp = logins[i.fld4] + stamp = typing.cast(int, logins[i.fld4]) del logins[i.fld4] total = i.stamp - stamp data.append( diff --git a/server/src/uds/reports/stats/user_access.py b/server/src/uds/reports/stats/user_access.py index 9c035314d..7e78b2a43 100644 --- a/server/src/uds/reports/stats/user_access.py +++ b/server/src/uds/reports/stats/user_access.py @@ -34,7 +34,6 @@ import datetime import io import logging import typing -import collections.abc import django.template.defaultfilters as filters from django.utils.translation import gettext @@ -43,7 +42,7 @@ from django.utils.translation import gettext_lazy as _ from uds.core.managers.stats import StatsManager from uds.core.reports import graphs from uds.core.ui import gui -from uds.core.util import dateutils, stats, utils +from uds.core.util import stats, utils from .base import StatsReport @@ -149,12 +148,15 @@ class StatsReportLogin(StatsReport): # User access by date graph # graph1 = io.BytesIO() + + def _tick_fnc1(l: int) -> str: + return filters.date(datetime.datetime.fromtimestamp(l), xLabelFormat) - X = [v[0] for v in data] - d = { + x = [v[0] for v in data] + d: dict[str, typing.Any] = { 'title': _('Users Access (global)'), - 'x': X, - 'xtickFnc': lambda l: filters.date(datetime.datetime.fromtimestamp(l), xLabelFormat), + 'x': x, + 'xtickFnc': _tick_fnc1, 'xlabel': _('Date'), 'y': [{'label': 'Users', 'data': [v[1] for v in data]}], 'ylabel': 'Users', @@ -167,12 +169,9 @@ class StatsReportLogin(StatsReport): graph3 = io.BytesIO() graph4 = io.BytesIO() dataWeek, dataHour, dataWeekHour = self.get_week_hourly_data() - - X = list(range(7)) - d = { - 'title': _('Users Access (by week)'), - 'x': X, - 'xtickFnc': lambda l: [ + + def _tick_fnc2(l: int) -> str: + return [ _('Monday'), _('Tuesday'), _('Wednesday'), @@ -180,7 +179,13 @@ class StatsReportLogin(StatsReport): _('Friday'), _('Saturday'), _('Sunday'), - ][l], + ][l] + + x = list(range(7)) + d = { + 'title': _('Users Access (by week)'), + 'x': x, + 'xtickFnc': _tick_fnc2, 'xlabel': _('Day of week'), 'y': [{'label': 'Users', 'data': list(dataWeek)}], 'ylabel': 'Users', @@ -188,35 +193,30 @@ class StatsReportLogin(StatsReport): graphs.bar_chart(SIZE, d, graph2) - X = list(range(24)) + x = list(range(24)) d = { 'title': _('Users Access (by hour)'), - 'x': X, + 'x': x, 'xlabel': _('Hour'), 'y': [{'label': 'Users', 'data': list(dataHour)}], 'ylabel': 'Users', } graphs.bar_chart(SIZE, d, graph3) + + def _tick_fnc3(l: int) -> str: + return str(l) - X = list(range(24)) + x = list(range(24)) Y = list(range(7)) d = { 'title': _('Users Access (by hour)'), - 'x': X, + 'x': x, 'xlabel': _('Hour'), - 'xtickFnc': lambda l: l, + 'xtickFnc': _tick_fnc3, 'y': Y, 'ylabel': _('Day of week'), - 'ytickFnc': lambda l: [ - _('Monday'), - _('Tuesday'), - _('Wednesday'), - _('Thursday'), - _('Friday'), - _('Saturday'), - _('Sunday'), - ][l], + 'ytickFnc': _tick_fnc2, 'z': dataWeekHour, 'zlabel': _('Users'), } diff --git a/server/src/uds/services/OVirt/__init__.py b/server/src/uds/services/OVirt/__init__.py index 85c64b575..08051fb2a 100644 --- a/server/src/uds/services/OVirt/__init__.py +++ b/server/src/uds/services/OVirt/__init__.py @@ -26,7 +26,10 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false from uds.core import managers from .provider import OVirtProvider diff --git a/server/src/uds/services/OVirt/client/__init__.py b/server/src/uds/services/OVirt/client/__init__.py index 29d384ebf..da1a8bb85 100644 --- a/server/src/uds/services/OVirt/client/__init__.py +++ b/server/src/uds/services/OVirt/client/__init__.py @@ -26,6 +26,8 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -import logging - +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false from .ovirt import Client diff --git a/server/src/uds/services/OVirt/client/ovirt.py b/server/src/uds/services/OVirt/client/ovirt.py index 0b00bd311..166a69895 100644 --- a/server/src/uds/services/OVirt/client/ovirt.py +++ b/server/src/uds/services/OVirt/client/ovirt.py @@ -28,6 +28,8 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnknownMemberType=false, reportAttributeAccessIssue=false + import threading import logging import typing @@ -40,7 +42,7 @@ from uds.core import types # Sometimes, we import ovirtsdk4 but "types" does not get imported... event can't be found???? # With this seems to work propertly try: - from ovirtsdk4 import types as ovirtTypes + from ovirtsdk4 import types as ovirtTypes # pyright: ignore[reportUnusedImport] except Exception: # nosec just to bring on the types if they exist pass @@ -181,7 +183,9 @@ class Client: api = self._api() - vms: collections.abc.Iterable[typing.Any] = api.system_service().vms_service().list() # pyright: ignore + vms: collections.abc.Iterable[typing.Any] = ( + api.system_service().vms_service().list() + ) # pyright: ignore logger.debug('oVirt VMS: %s', vms) @@ -347,7 +351,9 @@ class Client: d: typing.Any = datacenter_service.get() # pyright: ignore storage = [] - for dd in typing.cast(collections.abc.Iterable[typing.Any], datacenter_service.storage_domains_service().list()): # pyright: ignore + for dd in typing.cast( + collections.abc.Iterable[typing.Any], datacenter_service.storage_domains_service().list() + ): # pyright: ignore try: active = dd.status.value except Exception: @@ -364,7 +370,7 @@ class Client: } ) - res = { + res: dict[str, typing.Any] = { 'name': d.name, 'id': d.id, 'storage_type': d.local and 'local' or 'shared', @@ -410,7 +416,9 @@ class Client: api = self._api() - dd: typing.Any = api.system_service().storage_domains_service().service(storageId).get() # pyright: ignore + dd: typing.Any = ( + api.system_service().storage_domains_service().service(storageId).get() + ) # pyright: ignore res = { 'id': dd.id, @@ -429,10 +437,10 @@ class Client: self, name: str, comments: str, - machineId: str, - clusterId: str, - storageId: str, - displayType: str, + machine_id: str, + cluster_id: str, + storage_id: str, + display_type: str, ) -> str: """ Publish the machine (makes a template from it so we can create COWs) and returns the template id of @@ -452,10 +460,10 @@ class Client: "n: %s, c: %s, vm: %s, cl: %s, st: %s, dt: %s", name, comments, - machineId, - clusterId, - storageId, - displayType, + machine_id, + cluster_id, + storage_id, + display_type, ) try: @@ -466,9 +474,11 @@ class Client: # cluster = ov.clusters_service().service('00000002-0002-0002-0002-0000000002e4') # .get() # vm = ov.vms_service().service('e7ff4e00-b175-4e80-9c1f-e50a5e76d347') # .get() - vms = api.system_service().vms_service().service(machineId) + vms: typing.Any = api.system_service().vms_service().service(machine_id) - cluster: typing.Any = api.system_service().clusters_service().service(clusterId).get() # pyright: ignore + cluster: typing.Any = ( + api.system_service().clusters_service().service(cluster_id).get() + ) # pyright: ignore vm: typing.Any = vms.get() # pyright: ignore if vm is None: @@ -498,7 +508,7 @@ class Client: finally: lock.release() - def get_template_state(self, templateId: str) -> str: + def get_template_state(self, template_id: str) -> str: """ Returns current template state. This method do not uses cache at all (it always tries to get template state from oVirt server) @@ -517,7 +527,7 @@ class Client: try: template: typing.Any = ( - api.system_service().templates_service().service(templateId).get() # pyright: ignore + api.system_service().templates_service().service(template_id).get() # pyright: ignore ) if not template: @@ -650,7 +660,7 @@ class Client: finally: lock.release() - def start_machine(self, machineId: str) -> None: + def start_machine(self, machine_id: str) -> None: """ Tries to start a machine. No check is done, it is simply requested to oVirt. @@ -666,7 +676,7 @@ class Client: api = self._api() - vmService: typing.Any = api.system_service().vms_service().service(machineId) + vmService: typing.Any = api.system_service().vms_service().service(machine_id) if vmService.get() is None: raise Exception('Machine not found') @@ -808,7 +818,9 @@ class Client: if display.certificate is not None: cert_subject = display.certificate.subject else: - for i in typing.cast(collections.abc.Iterable[typing.Any], api.system_service().hosts_service().list()): + for i in typing.cast( + collections.abc.Iterable[typing.Any], api.system_service().hosts_service().list() + ): for k in typing.cast( collections.abc.Iterable[typing.Any], api.system_service() diff --git a/server/src/uds/services/OVirt/helpers.py b/server/src/uds/services/OVirt/helpers.py index 5e96e8c00..5e1d4e6de 100644 --- a/server/src/uds/services/OVirt/helpers.py +++ b/server/src/uds/services/OVirt/helpers.py @@ -6,7 +6,6 @@ Created on Nov 15, 2012-2019 import logging import typing -import collections.abc from uds.core import types from uds.core.ui.user_interface import gui diff --git a/server/src/uds/services/OVirt/jobs.py b/server/src/uds/services/OVirt/jobs.py index 0cd57a321..a9151de1d 100644 --- a/server/src/uds/services/OVirt/jobs.py +++ b/server/src/uds/services/OVirt/jobs.py @@ -31,7 +31,6 @@ """ import logging import typing -import collections.abc from uds.core import jobs diff --git a/server/src/uds/services/OVirt/provider.py b/server/src/uds/services/OVirt/provider.py index 18a742476..b5bd549f9 100644 --- a/server/src/uds/services/OVirt/provider.py +++ b/server/src/uds/services/OVirt/provider.py @@ -38,7 +38,6 @@ from django.utils.translation import gettext_noop as _ from uds.core import services, types, consts from uds.core.ui import gui from uds.core.util import validators, fields -from uds.core.util.cache import Cache from uds.core.util.decorators import cached from . import client diff --git a/server/src/uds/services/OVirt/publication.py b/server/src/uds/services/OVirt/publication.py index 6cd1a8c02..8b3bac539 100644 --- a/server/src/uds/services/OVirt/publication.py +++ b/server/src/uds/services/OVirt/publication.py @@ -30,7 +30,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import logging import typing from datetime import datetime diff --git a/server/src/uds/services/OVirt/service.py b/server/src/uds/services/OVirt/service.py index f5fb3363e..105fc3cad 100644 --- a/server/src/uds/services/OVirt/service.py +++ b/server/src/uds/services/OVirt/service.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright (c) 2012-2019 Virtual Cable S.L. +# Copyright (c) 2012-2024 Virtual Cable S.L.U. # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, @@ -33,7 +33,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import re import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -48,7 +47,6 @@ from . import helpers # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from .provider import OVirtProvider - from uds.core.module import Module logger = logging.getLogger(__name__) @@ -104,7 +102,6 @@ class OVirtLinkedService(services.Service): # pylint: disable=too-many-public-m allowed_protocols = types.transports.Protocol.generic_vdi(types.transports.Protocol.SPICE) services_type_provided = types.services.ServiceType.VDI - # Now the form part cluster = gui.ChoiceField( label=_("Cluster"), @@ -134,7 +131,7 @@ class OVirtLinkedService(services.Service): # pylint: disable=too-many-public-m order=102, tooltip=_('Minimal free space in GB'), required=True, - old_field_name='minSpaceGB' + old_field_name='minSpaceGB', ) machine = gui.ChoiceField( @@ -167,7 +164,7 @@ class OVirtLinkedService(services.Service): # pylint: disable=too-many-public-m tooltip=_('Physical memory guaranteed to machines'), tab=_('Machine'), required=True, - old_field_name='memoryGuaranteed' + old_field_name='memoryGuaranteed', ) usb = gui.ChoiceField( @@ -197,7 +194,7 @@ class OVirtLinkedService(services.Service): # pylint: disable=too-many-public-m lenname = fields.lenname_field(order=116, tab=_('Machine')) parent_uuid = gui.HiddenField() - + def initialize(self, values: 'types.core.ValuesType') -> None: """ We check here form values to see if they are valid. @@ -208,9 +205,7 @@ class OVirtLinkedService(services.Service): # pylint: disable=too-many-public-m if values: validators.validate_basename(self.basename.value, self.lenname.as_int()) if int(self.memory.value) < 256 or int(self.guaranteed_memory.value) < 256: - raise exceptions.ui.ValidationError( - _('The minimum allowed memory is 256 Mb') - ) + raise exceptions.ui.ValidationError(_('The minimum allowed memory is 256 Mb')) if int(self.guaranteed_memory.value) > int(self.memory.value): self.guaranteed_memory.value = self.memory.value @@ -223,7 +218,7 @@ class OVirtLinkedService(services.Service): # pylint: disable=too-many-public-m # This is that value is always '', so if we want to change something, we have to do it # at defValue self.parent_uuid.value = self.provider().get_uuid() - + # This is not the same case, values is not the "value" of the field, but # the list of values shown because this is a "ChoiceField" self.machine.set_choices(gui.choice_item(m['id'], m['name']) for m in self.provider().list_machines()) @@ -432,9 +427,7 @@ class OVirtLinkedService(services.Service): # pylint: disable=too-many-public-m """ return self.display.value - def get_console_connection( - self, machineId: str - ) -> typing.Optional[types.services.ConsoleConnectionInfo]: + def get_console_connection(self, machineId: str) -> typing.Optional[types.services.ConsoleConnectionInfo]: return self.provider().get_console_connection(machineId) def is_avaliable(self) -> bool: diff --git a/server/src/uds/services/OpenGnsys/__init__.py b/server/src/uds/services/OpenGnsys/__init__.py index 077cc2d1c..05e2aff45 100644 --- a/server/src/uds/services/OpenGnsys/__init__.py +++ b/server/src/uds/services/OpenGnsys/__init__.py @@ -25,6 +25,10 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false from uds.core import managers diff --git a/server/src/uds/services/OpenGnsys/deployment.py b/server/src/uds/services/OpenGnsys/deployment.py index 584ba734f..397d3cbfa 100644 --- a/server/src/uds/services/OpenGnsys/deployment.py +++ b/server/src/uds/services/OpenGnsys/deployment.py @@ -29,7 +29,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -from enum import auto import enum import pickle # nosec: not insecure, we are loading our own data import logging @@ -46,7 +45,6 @@ if typing.TYPE_CHECKING: from uds import models from .service import OGService from .publication import OpenGnsysPublication - from uds.core.util.storage import Storage logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/OpenGnsys/helpers.py b/server/src/uds/services/OpenGnsys/helpers.py index 5c9463f5e..d972ca4ae 100644 --- a/server/src/uds/services/OpenGnsys/helpers.py +++ b/server/src/uds/services/OpenGnsys/helpers.py @@ -28,14 +28,12 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import logging import typing from django.utils.translation import gettext as _ from uds.core import types -from uds.core.environment import Environment from uds.core.ui import gui from uds import models @@ -47,8 +45,6 @@ logger = logging.getLogger(__name__) def get_resources(parameters: typing.Any) -> types.ui.CallbackResultType: - from .provider import OGProvider - logger.debug('Parameters received by getResources Helper: %s', parameters) provider = typing.cast( 'OGProvider', models.Provider.objects.get(id=parameters['parent_uuid']).get_instance() diff --git a/server/src/uds/services/OpenGnsys/jobs.py b/server/src/uds/services/OpenGnsys/jobs.py index 3a8babed9..b1980364b 100644 --- a/server/src/uds/services/OpenGnsys/jobs.py +++ b/server/src/uds/services/OpenGnsys/jobs.py @@ -33,7 +33,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import datetime import logging import typing -import collections.abc from uds.core import jobs from uds import models @@ -44,7 +43,7 @@ from .service import OGService # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module + pass logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/OpenGnsys/og/__init__.py b/server/src/uds/services/OpenGnsys/og/__init__.py index 668f19a18..36a336309 100644 --- a/server/src/uds/services/OpenGnsys/og/__init__.py +++ b/server/src/uds/services/OpenGnsys/og/__init__.py @@ -36,7 +36,6 @@ import logging import typing import collections.abc -from requests import get from uds.core import consts from uds.core.util.decorators import ensure_connected diff --git a/server/src/uds/services/OpenGnsys/og/fake.py b/server/src/uds/services/OpenGnsys/og/fake.py index c49501b08..978fe7b53 100644 --- a/server/src/uds/services/OpenGnsys/og/fake.py +++ b/server/src/uds/services/OpenGnsys/og/fake.py @@ -34,7 +34,6 @@ import copy import random import logging import typing -import collections.abc from . import urls diff --git a/server/src/uds/services/OpenGnsys/provider.py b/server/src/uds/services/OpenGnsys/provider.py index 28d9ddb35..77c16ac1d 100644 --- a/server/src/uds/services/OpenGnsys/provider.py +++ b/server/src/uds/services/OpenGnsys/provider.py @@ -33,7 +33,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -41,7 +40,6 @@ from uds.core import types, consts from uds.core.services import ServiceProvider from uds.core.ui import gui from uds.core.util import fields, validators -from uds.core.util.cache import Cache from uds.core.util.decorators import cached from . import og @@ -50,7 +48,6 @@ from .service import OGService # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds.core.environment import Environment - from uds.core.module import Module logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/OpenGnsys/publication.py b/server/src/uds/services/OpenGnsys/publication.py index 6ad9026b7..6d8316205 100644 --- a/server/src/uds/services/OpenGnsys/publication.py +++ b/server/src/uds/services/OpenGnsys/publication.py @@ -31,12 +31,10 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from uds.core.services import Publication from uds.core import types from uds.core.util import autoserializable -from uds.core.util.model import sql_datetime # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: diff --git a/server/src/uds/services/OpenGnsys/service.py b/server/src/uds/services/OpenGnsys/service.py index d3a754eb9..525649325 100644 --- a/server/src/uds/services/OpenGnsys/service.py +++ b/server/src/uds/services/OpenGnsys/service.py @@ -31,11 +31,10 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core import types, services, consts +from uds.core import types, services from uds.core.ui import gui from uds.core.util import fields @@ -46,7 +45,6 @@ from .publication import OpenGnsysPublication # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from .provider import OGProvider - from uds.core.module import Module logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/OpenNebula/__init__.py b/server/src/uds/services/OpenNebula/__init__.py index 8a8a49e2d..4e7520ea6 100644 --- a/server/src/uds/services/OpenNebula/__init__.py +++ b/server/src/uds/services/OpenNebula/__init__.py @@ -26,6 +26,9 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false from .provider import OpenNebulaProvider diff --git a/server/src/uds/services/OpenNebula/deployment.py b/server/src/uds/services/OpenNebula/deployment.py index 3b1874a95..ebf249ef8 100644 --- a/server/src/uds/services/OpenNebula/deployment.py +++ b/server/src/uds/services/OpenNebula/deployment.py @@ -30,7 +30,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -from enum import auto import enum import pickle # nosec: not insecure, we are loading our own data import logging @@ -47,7 +46,6 @@ if typing.TYPE_CHECKING: from uds import models from .service import OpenNebulaLiveService from .publication import OpenNebulaLivePublication - from uds.core.util.storage import Storage logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/OpenNebula/on/__init__.py b/server/src/uds/services/OpenNebula/on/__init__.py index d0402e453..336fde61b 100644 --- a/server/src/uds/services/OpenNebula/on/__init__.py +++ b/server/src/uds/services/OpenNebula/on/__init__.py @@ -1,7 +1,11 @@ +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false from . import storage from . import template from . import vm from . import client from . import types -from .common import sanitizeName +from .common import sanitized_name diff --git a/server/src/uds/services/OpenNebula/on/client.py b/server/src/uds/services/OpenNebula/on/client.py index 4cfb3745d..76befa6df 100644 --- a/server/src/uds/services/OpenNebula/on/client.py +++ b/server/src/uds/services/OpenNebula/on/client.py @@ -286,7 +286,7 @@ class OpenNebulaClient: # pylint: disable=too-many-public-methods ) @ensure_connected - def enumVMs(self) -> collections.abc.Iterable[types.VirtualMachineType]: + def enumerate_machines(self) -> collections.abc.Iterable[types.VirtualMachineType]: """ Invoke vm pools info, with this parameters: 1.- Session string diff --git a/server/src/uds/services/OpenNebula/on/common.py b/server/src/uds/services/OpenNebula/on/common.py index d449de9ad..44c684650 100644 --- a/server/src/uds/services/OpenNebula/on/common.py +++ b/server/src/uds/services/OpenNebula/on/common.py @@ -39,7 +39,7 @@ import logging logger = logging.getLogger(__name__) # module = sys.modules[__name__] -def sanitizeName(name: str) -> str: +def sanitized_name(name: str) -> str: """ machine names with [a-zA-Z0-9_-] """ diff --git a/server/src/uds/services/OpenNebula/on/template.py b/server/src/uds/services/OpenNebula/on/template.py index eddfb239d..b2611ec7d 100644 --- a/server/src/uds/services/OpenNebula/on/template.py +++ b/server/src/uds/services/OpenNebula/on/template.py @@ -38,7 +38,7 @@ import collections.abc from defusedxml import minidom from . import types -from .common import sanitizeName +from .common import sanitized_name # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: @@ -114,7 +114,7 @@ def create( # raise Exception('The base machines images are not in READY state') # Now clone the image - imgName = sanitizeName(name + ' DSK ' + str(counter)) + imgName = sanitized_name(name + ' DSK ' + str(counter)) newId = api.cloneImage( imgId, imgName, toDataStore ) # api.call('image.clone', int(imgId), imgName, int(toDataStore)) diff --git a/server/src/uds/services/OpenNebula/on/types.py b/server/src/uds/services/OpenNebula/on/types.py index a210303f0..8296992a2 100644 --- a/server/src/uds/services/OpenNebula/on/types.py +++ b/server/src/uds/services/OpenNebula/on/types.py @@ -31,10 +31,9 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import enum import typing -import collections.abc -class VmState(enum.Enum): # pylint: disable=too-few-public-methods +class VmState(enum.Enum): INIT = 0 PENDING = 1 HOLD = 2 diff --git a/server/src/uds/services/OpenNebula/on/vm.py b/server/src/uds/services/OpenNebula/on/vm.py index 539cc3487..62ffff209 100644 --- a/server/src/uds/services/OpenNebula/on/vm.py +++ b/server/src/uds/services/OpenNebula/on/vm.py @@ -192,19 +192,18 @@ def enumerate_machines( 'id' 'cluster_id' ''' - yield from api.enumVMs() - + yield from api.enumerate_machines() def get_network_info( api: 'client.OpenNebulaClient', - machineId: str, + machine_id: str, networkId: typing.Optional[str] = None, ) -> tuple[str, str]: ''' Get the MAC and the IP for the network and machine. If network is None, for the first network ''' # md = minidom.parseString(api.call('vm.info', int(machineId))) - md = minidom.parseString(api.VMInfo(machineId).xml) + md: typing.Any = minidom.parseString(api.VMInfo(machine_id).xml or '') # pyright: ignore[reportUnknownMemberType] node = md try: @@ -237,7 +236,7 @@ def get_console_connection( If machine is not running or there is not a display, will return NONE SPICE connections should check that 'type' is 'SPICE' ''' - md = minidom.parseString(api.VMInfo(machineId).xml) + md: typing.Any = minidom.parseString(api.VMInfo(machineId).xml or '') # pyright: ignore[reportUnknownMemberType] try: graphics = md.getElementsByTagName('GRAPHICS')[0] diff --git a/server/src/uds/services/OpenNebula/provider.py b/server/src/uds/services/OpenNebula/provider.py index 0a5a4f158..ba6689548 100644 --- a/server/src/uds/services/OpenNebula/provider.py +++ b/server/src/uds/services/OpenNebula/provider.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com ''' import collections.abc -import dis import logging import typing @@ -48,8 +47,7 @@ from .service import OpenNebulaLiveService # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.environment import Environment - from uds.core.module import Module + pass logger = logging.getLogger(__name__) @@ -144,7 +142,7 @@ class OpenNebulaProvider(ServiceProvider): # pylint: disable=too-many-public-me self._api = None def sanitized_name(self, name: str) -> str: - return on.sanitizeName(name) + return on.sanitized_name(name) def test_connection(self) -> types.core.TestResult: ''' diff --git a/server/src/uds/services/OpenNebula/publication.py b/server/src/uds/services/OpenNebula/publication.py index 229b497d0..84d5d4f6e 100644 --- a/server/src/uds/services/OpenNebula/publication.py +++ b/server/src/uds/services/OpenNebula/publication.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from uds.core.services import Publication from uds.core import types diff --git a/server/src/uds/services/OpenNebula/service.py b/server/src/uds/services/OpenNebula/service.py index 14cd8a523..a1af456e5 100644 --- a/server/src/uds/services/OpenNebula/service.py +++ b/server/src/uds/services/OpenNebula/service.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ from uds.core import services, types @@ -46,7 +45,6 @@ from .deployment import OpenNebulaLiveDeployment if typing.TYPE_CHECKING: from . import on from .provider import OpenNebulaProvider - from uds.core.module import Module logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/OpenStack/__init__.py b/server/src/uds/services/OpenStack/__init__.py index 8d92fafc2..ea3009a04 100644 --- a/server/src/uds/services/OpenStack/__init__.py +++ b/server/src/uds/services/OpenStack/__init__.py @@ -26,6 +26,10 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false from .provider_legacy import ProviderLegacy from .provider import OpenStackProvider diff --git a/server/src/uds/services/OpenStack/helpers.py b/server/src/uds/services/OpenStack/helpers.py index 5e5042a73..27200a930 100644 --- a/server/src/uds/services/OpenStack/helpers.py +++ b/server/src/uds/services/OpenStack/helpers.py @@ -31,7 +31,6 @@ ''' import logging import typing -import collections.abc from django.utils.translation import gettext as _ from uds.core import types diff --git a/server/src/uds/services/OpenStack/openstack/__init__.py b/server/src/uds/services/OpenStack/openstack/__init__.py index a0ad1b227..7f0036b4a 100644 --- a/server/src/uds/services/OpenStack/openstack/__init__.py +++ b/server/src/uds/services/OpenStack/openstack/__init__.py @@ -30,6 +30,7 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .openstack_client import Client diff --git a/server/src/uds/services/OpenStack/provider.py b/server/src/uds/services/OpenStack/provider.py index 61af494ec..e4f196f89 100644 --- a/server/src/uds/services/OpenStack/provider.py +++ b/server/src/uds/services/OpenStack/provider.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -40,7 +39,6 @@ from uds.core import types, consts from uds.core.services import ServiceProvider from uds.core.ui import gui from uds.core.util import validators, fields -from uds.core.util.cache import Cache from uds.core.util.decorators import cached from . import openstack diff --git a/server/src/uds/services/OpenStack/provider_legacy.py b/server/src/uds/services/OpenStack/provider_legacy.py index 7c3c9c75c..766686fd9 100644 --- a/server/src/uds/services/OpenStack/provider_legacy.py +++ b/server/src/uds/services/OpenStack/provider_legacy.py @@ -34,7 +34,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com ''' import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -42,7 +41,6 @@ from uds.core import environment, types, consts from uds.core.services import ServiceProvider from uds.core.ui import gui from uds.core.util import validators -from uds.core.util.cache import Cache from uds.core.util.decorators import cached from . import openstack @@ -50,7 +48,7 @@ from .service import OpenStackLiveService # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module + pass logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/OpenStack/publication.py b/server/src/uds/services/OpenStack/publication.py index 585e8fe9e..09a89d245 100644 --- a/server/src/uds/services/OpenStack/publication.py +++ b/server/src/uds/services/OpenStack/publication.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from uds.core.services import Publication from uds.core import types diff --git a/server/src/uds/services/OpenStack/service.py b/server/src/uds/services/OpenStack/service.py index d3533e0aa..8595dc079 100644 --- a/server/src/uds/services/OpenStack/service.py +++ b/server/src/uds/services/OpenStack/service.py @@ -32,12 +32,11 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ from uds.core import services, types -from uds.core.util import utils, validators +from uds.core.util import validators from uds.core.ui import gui from .publication import OpenStackLivePublication diff --git a/server/src/uds/services/PhysicalMachines/__init__.py b/server/src/uds/services/PhysicalMachines/__init__.py index b5211051e..cb6512494 100644 --- a/server/src/uds/services/PhysicalMachines/__init__.py +++ b/server/src/uds/services/PhysicalMachines/__init__.py @@ -28,7 +28,7 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ - +# pyright: reportUnusedImport=false from .provider import PhysicalMachinesProvider diff --git a/server/src/uds/services/PhysicalMachines/deployment.py b/server/src/uds/services/PhysicalMachines/deployment.py index 9c0758adb..970d93e8b 100644 --- a/server/src/uds/services/PhysicalMachines/deployment.py +++ b/server/src/uds/services/PhysicalMachines/deployment.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc import dns.resolver diff --git a/server/src/uds/services/PhysicalMachines/provider.py b/server/src/uds/services/PhysicalMachines/provider.py index 3b36faaff..bc1cb3783 100644 --- a/server/src/uds/services/PhysicalMachines/provider.py +++ b/server/src/uds/services/PhysicalMachines/provider.py @@ -33,7 +33,6 @@ import configparser import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -42,7 +41,6 @@ from uds.core.ui.user_interface import gui from uds.core.util import log, net, resolver if typing.TYPE_CHECKING: - from uds.core.module import Module from .service_base import HostInfo logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/PhysicalMachines/service_base.py b/server/src/uds/services/PhysicalMachines/service_base.py index 1443643de..9be39e317 100644 --- a/server/src/uds/services/PhysicalMachines/service_base.py +++ b/server/src/uds/services/PhysicalMachines/service_base.py @@ -30,11 +30,8 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import dataclasses import logging -import stat import typing -import collections.abc from uds.core.util import security from uds.core import services diff --git a/server/src/uds/services/PhysicalMachines/service_multi.py b/server/src/uds/services/PhysicalMachines/service_multi.py index 36a85fc84..fe3e2b100 100644 --- a/server/src/uds/services/PhysicalMachines/service_multi.py +++ b/server/src/uds/services/PhysicalMachines/service_multi.py @@ -41,7 +41,7 @@ from django.utils.translation import gettext, gettext_lazy as _ from uds.core import exceptions, services, types from uds.core.ui import gui -from uds.core.util import ensure, log, net +from uds.core.util import log, net from uds.core.util.model import sql_stamp_seconds from .deployment import IPMachineUserService @@ -51,7 +51,6 @@ from .types import HostInfo # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models - from uds.core.module import Module logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/PhysicalMachines/service_single.py b/server/src/uds/services/PhysicalMachines/service_single.py index f9995e2d2..9c15d97d2 100644 --- a/server/src/uds/services/PhysicalMachines/service_single.py +++ b/server/src/uds/services/PhysicalMachines/service_single.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_lazy as _, gettext @@ -46,7 +45,7 @@ from .types import HostInfo # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module + pass logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/PhysicalMachines/types.py b/server/src/uds/services/PhysicalMachines/types.py index 3413e07f6..6c7d793f9 100644 --- a/server/src/uds/services/PhysicalMachines/types.py +++ b/server/src/uds/services/PhysicalMachines/types.py @@ -33,7 +33,6 @@ import dataclasses import logging import typing -import collections.abc logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/Proxmox/__init__.py b/server/src/uds/services/Proxmox/__init__.py index 0a2aae4a4..a4da375cf 100644 --- a/server/src/uds/services/Proxmox/__init__.py +++ b/server/src/uds/services/Proxmox/__init__.py @@ -23,8 +23,11 @@ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.""" +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false from uds.core import managers from .provider import ProxmoxProvider diff --git a/server/src/uds/services/Proxmox/client/types.py b/server/src/uds/services/Proxmox/client/types.py index 63d95481f..e3f3bef7d 100644 --- a/server/src/uds/services/Proxmox/client/types.py +++ b/server/src/uds/services/Proxmox/client/types.py @@ -1,5 +1,4 @@ import datetime -from os import name import re import typing import collections.abc @@ -34,7 +33,7 @@ def _from_dict( k: typing.cast(typing.Callable[..., typing.Any], CONVERSORS.get(type.__annotations__.get(k, str), lambda x: x))( dictionary.get(k, extra.get(k, None)) ) - for k in type._fields # type: ignore + for k in type._fields # pyright: ignore # _fields is a NamedTuple attribute that contains fields } ) diff --git a/server/src/uds/services/Proxmox/deployment_fixed.py b/server/src/uds/services/Proxmox/deployment_fixed.py index 80539eab6..7cefc4911 100644 --- a/server/src/uds/services/Proxmox/deployment_fixed.py +++ b/server/src/uds/services/Proxmox/deployment_fixed.py @@ -30,23 +30,17 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import pickle # nosec: controled data -import enum import logging import typing -import collections.abc -from uds.core import consts, services, types +from uds.core import types from uds.core.services.specializations.fixed_machine.fixed_userservice import FixedUserService, Operation -from uds.core.types.states import State -from uds.core.util import log, autoserializable -from uds.core.util.model import sql_stamp_seconds +from uds.core.util import autoserializable from . import client # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds import models from . import service_fixed logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/Proxmox/helpers.py b/server/src/uds/services/Proxmox/helpers.py index b48c32569..17f997079 100644 --- a/server/src/uds/services/Proxmox/helpers.py +++ b/server/src/uds/services/Proxmox/helpers.py @@ -27,7 +27,6 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import logging import typing @@ -54,7 +53,7 @@ def get_storage(parameters: typing.Any) -> types.ui.CallbackResultType: except Exception: return [] - res = [] + res: list[types.ui.ChoiceItem] = [] # Get storages for that datacenter for storage in sorted(provider.list_storages(vm_info.node), key=lambda x: int(not x.shared)): if storage.type in ('lvm', 'iscsi', 'iscsidirect'): diff --git a/server/src/uds/services/Proxmox/jobs.py b/server/src/uds/services/Proxmox/jobs.py index 3056a3d69..1e8f27b51 100644 --- a/server/src/uds/services/Proxmox/jobs.py +++ b/server/src/uds/services/Proxmox/jobs.py @@ -30,7 +30,6 @@ import time import logging import typing -import collections.abc from uds.core import jobs diff --git a/server/src/uds/services/Proxmox/provider.py b/server/src/uds/services/Proxmox/provider.py index 8d7628a33..b71391797 100644 --- a/server/src/uds/services/Proxmox/provider.py +++ b/server/src/uds/services/Proxmox/provider.py @@ -29,7 +29,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -38,7 +37,6 @@ from uds.core.ui import gui from uds.core.util import validators, fields from uds.core.util.decorators import cached from uds.core.util.unique_id_generator import UniqueIDGenerator -from uds.core.util.unique_mac_generator import UniqueMacGenerator from . import client from .service_linked import ProxmoxServiceLinked diff --git a/server/src/uds/services/Proxmox/publication.py b/server/src/uds/services/Proxmox/publication.py index 38f72c6ac..17c719c04 100644 --- a/server/src/uds/services/Proxmox/publication.py +++ b/server/src/uds/services/Proxmox/publication.py @@ -32,7 +32,6 @@ from datetime import datetime import time import logging import typing -import collections.abc from django.utils.translation import gettext as _ from uds.core import services, types @@ -41,7 +40,6 @@ from uds.core.util import autoserializable # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from .service_linked import ProxmoxServiceLinked - from . import client logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/Proxmox/service_fixed.py b/server/src/uds/services/Proxmox/service_fixed.py index a7de96f75..ba6919c21 100644 --- a/server/src/uds/services/Proxmox/service_fixed.py +++ b/server/src/uds/services/Proxmox/service_fixed.py @@ -35,12 +35,11 @@ import typing from django.utils.translation import gettext from django.utils.translation import gettext_noop as _ -from uds.core import consts, exceptions, services, types +from uds.core import exceptions, services, types from uds.core.services.specializations.fixed_machine.fixed_service import FixedService from uds.core.services.specializations.fixed_machine.fixed_userservice import FixedUserService from uds.core.ui import gui from uds.core.util import log -from uds.core.util.decorators import cached from . import helpers from .deployment_fixed import ProxmoxUserServiceFixed @@ -48,7 +47,6 @@ from .deployment_fixed import ProxmoxUserServiceFixed # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models - from uds.core.module import Module from . import client from .provider import ProxmoxProvider diff --git a/server/src/uds/services/Proxmox/service_linked.py b/server/src/uds/services/Proxmox/service_linked.py index dd4ca3566..d8ec9eef7 100644 --- a/server/src/uds/services/Proxmox/service_linked.py +++ b/server/src/uds/services/Proxmox/service_linked.py @@ -31,13 +31,11 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import logging import re import typing -import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core import services, types, consts +from uds.core import services, types from uds.core.ui import gui from uds.core.util import validators, log, fields -from uds.core.util.decorators import cached from . import helpers from .deployment_linked import ProxmoxUserserviceLinked @@ -45,8 +43,6 @@ from .publication import ProxmoxPublication # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module - from . import client from .provider import ProxmoxProvider diff --git a/server/src/uds/services/Sample/__init__.py b/server/src/uds/services/Sample/__init__.py index ed05c30f0..6a3edeaa9 100644 --- a/server/src/uds/services/Sample/__init__.py +++ b/server/src/uds/services/Sample/__init__.py @@ -26,6 +26,10 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false """ Sample Service module. diff --git a/server/src/uds/services/Sample/deployment_one.py b/server/src/uds/services/Sample/deployment_one.py index 1431ccb49..505b2e456 100644 --- a/server/src/uds/services/Sample/deployment_one.py +++ b/server/src/uds/services/Sample/deployment_one.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from uds.core import services, types @@ -41,8 +40,6 @@ from . import service as sample_service # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models - from .service import ServiceOne - from .publication import SamplePublication logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/Sample/deployment_two.py b/server/src/uds/services/Sample/deployment_two.py index 4b81ce274..74b18de6c 100644 --- a/server/src/uds/services/Sample/deployment_two.py +++ b/server/src/uds/services/Sample/deployment_two.py @@ -33,7 +33,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import codecs import logging import typing -import collections.abc from uds.core import services, types diff --git a/server/src/uds/services/Sample/provider.py b/server/src/uds/services/Sample/provider.py index 7cba6f480..49a6d531f 100644 --- a/server/src/uds/services/Sample/provider.py +++ b/server/src/uds/services/Sample/provider.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright (c) 2012-2019 Virtual Cable S.L. +# Copyright (c) 2012-2025 Virtual Cable S.L.U. # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, @@ -34,17 +34,15 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core import services, exceptions, consts, types +from uds.core import services, exceptions,types from uds.core.ui import gui from .service import ServiceOne, ServiceTwo # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.environment import Environment logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/Sample/publication.py b/server/src/uds/services/Sample/publication.py index 76cfad9d0..271868a4b 100644 --- a/server/src/uds/services/Sample/publication.py +++ b/server/src/uds/services/Sample/publication.py @@ -33,17 +33,12 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import random import string import logging -import typing -import collections.abc from django.utils.translation import gettext as _ from uds.core import services, types logger = logging.getLogger(__name__) -# Not imported at runtime, just for type checking -if typing.TYPE_CHECKING: - from .service import ServiceOne, ServiceTwo class SamplePublication(services.Publication): diff --git a/server/src/uds/services/Sample/service.py b/server/src/uds/services/Sample/service.py index 0b3a55f3d..79e343343 100644 --- a/server/src/uds/services/Sample/service.py +++ b/server/src/uds/services/Sample/service.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ from uds.core import services, exceptions, types @@ -45,8 +44,7 @@ from .deployment_two import SampleUserServiceTwo # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module - from uds.core.environment import Environment + pass logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/Test/__init__.py b/server/src/uds/services/Test/__init__.py index 6e31e90b8..87b3446b5 100644 --- a/server/src/uds/services/Test/__init__.py +++ b/server/src/uds/services/Test/__init__.py @@ -32,5 +32,5 @@ Simple "testing" provider. This package provides a simple test provider, suitable for automated tests. """ - +# pyright: reportUnusedImport=false from .provider import TestProvider diff --git a/server/src/uds/services/Test/deployment.py b/server/src/uds/services/Test/deployment.py index 19ae89892..1ffd8a8a1 100644 --- a/server/src/uds/services/Test/deployment.py +++ b/server/src/uds/services/Test/deployment.py @@ -33,17 +33,13 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import logging import dataclasses import typing -import collections.abc from uds.core import services, types -from . import service - # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models from .service import TestServiceNoCache, TestServiceCache - from .publication import TestPublication logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/Test/provider.py b/server/src/uds/services/Test/provider.py index 64f8e3d83..4b8f255a1 100644 --- a/server/src/uds/services/Test/provider.py +++ b/server/src/uds/services/Test/provider.py @@ -35,12 +35,10 @@ import random import string import dataclasses import typing -import collections.abc from django.utils.translation import gettext_noop as _ from uds.core import services, types -from uds.core import module from .service import TestServiceNoCache, TestServiceCache # Not imported at runtime, just for type checking diff --git a/server/src/uds/services/Test/publication.py b/server/src/uds/services/Test/publication.py index e6c8b8676..bd391ab46 100644 --- a/server/src/uds/services/Test/publication.py +++ b/server/src/uds/services/Test/publication.py @@ -35,7 +35,6 @@ import string import logging import dataclasses import typing -import collections.abc from django.utils.translation import gettext as _ from uds.core import services, types @@ -44,7 +43,7 @@ logger = logging.getLogger(__name__) # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from .service import TestServiceNoCache, TestServiceCache + pass class TestPublication(services.Publication): diff --git a/server/src/uds/services/Test/service.py b/server/src/uds/services/Test/service.py index 16b2f1551..11af79bb1 100644 --- a/server/src/uds/services/Test/service.py +++ b/server/src/uds/services/Test/service.py @@ -32,11 +32,9 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ from uds.core import services -from uds.core.ui import gui from .publication import TestPublication from .deployment import TestUserService diff --git a/server/src/uds/services/Xen/__init__.py b/server/src/uds/services/Xen/__init__.py index ef4f2fba8..b13b90a32 100644 --- a/server/src/uds/services/Xen/__init__.py +++ b/server/src/uds/services/Xen/__init__.py @@ -26,5 +26,8 @@ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - +""" +Author: Adolfo Gómez, dkmaster at dkmon dot com +""" +# pyright: reportUnusedImport=false from .provider import XenProvider diff --git a/server/src/uds/services/Xen/deployment.py b/server/src/uds/services/Xen/deployment.py index 0d0bf179e..85c5ae714 100644 --- a/server/src/uds/services/Xen/deployment.py +++ b/server/src/uds/services/Xen/deployment.py @@ -36,7 +36,6 @@ import typing import collections.abc from uds.core import services, consts, types -from uds.core.types.states import State from uds.core.util import autoserializable, log from .xen_client import XenPowerState @@ -46,7 +45,6 @@ if typing.TYPE_CHECKING: from uds import models from .service import XenLinkedService from .publication import XenPublication - from uds.core.util.storage import Storage logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/Xen/deployment_fixed.py b/server/src/uds/services/Xen/deployment_fixed.py index 42885219a..e07ee6f5d 100644 --- a/server/src/uds/services/Xen/deployment_fixed.py +++ b/server/src/uds/services/Xen/deployment_fixed.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from uds.core import types from uds.core.services.specializations.fixed_machine.fixed_userservice import FixedUserService, Operation diff --git a/server/src/uds/services/Xen/helpers.py b/server/src/uds/services/Xen/helpers.py index 26f2df19d..ec9b76af7 100644 --- a/server/src/uds/services/Xen/helpers.py +++ b/server/src/uds/services/Xen/helpers.py @@ -27,18 +27,14 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import logging -from multiprocessing import pool import typing from django.utils.translation import gettext as _ from uds.core import types -from uds.core.environment import Environment from uds.core.ui.user_interface import gui from uds import models -from uds.services.OpenNebula.on import vm logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/Xen/provider.py b/server/src/uds/services/Xen/provider.py index 86db9faaf..43380200d 100644 --- a/server/src/uds/services/Xen/provider.py +++ b/server/src/uds/services/Xen/provider.py @@ -37,7 +37,6 @@ from django.utils.translation import gettext_noop as _ from uds.core import types, consts from uds.core.services import ServiceProvider from uds.core.ui import gui -from uds.core.util.cache import Cache from uds.core.util.decorators import cached from uds.core.util import fields diff --git a/server/src/uds/services/Xen/publication.py b/server/src/uds/services/Xen/publication.py index 9f40782c9..139b19c59 100644 --- a/server/src/uds/services/Xen/publication.py +++ b/server/src/uds/services/Xen/publication.py @@ -32,12 +32,10 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com from datetime import datetime import logging import typing -import collections.abc from django.utils.translation import gettext as _ from uds.core import types from uds.core.services import Publication -from uds.core.types.states import State from uds.core.util import autoserializable # Not imported at runtime, just for type checking diff --git a/server/src/uds/services/Xen/service.py b/server/src/uds/services/Xen/service.py index af5cf790f..2722e175d 100644 --- a/server/src/uds/services/Xen/service.py +++ b/server/src/uds/services/Xen/service.py @@ -31,7 +31,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ from uds.core import services, exceptions, types @@ -44,7 +43,6 @@ from .deployment import XenLinkedDeployment # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from .provider import XenProvider - from uds.core.module import Module logger = logging.getLogger(__name__) diff --git a/server/src/uds/services/Xen/service_fixed.py b/server/src/uds/services/Xen/service_fixed.py index 27d2b29a8..7df1faf87 100644 --- a/server/src/uds/services/Xen/service_fixed.py +++ b/server/src/uds/services/Xen/service_fixed.py @@ -39,9 +39,8 @@ from uds.core import consts, exceptions, services, types from uds.core.services.specializations.fixed_machine.fixed_service import FixedService from uds.core.services.specializations.fixed_machine.fixed_userservice import FixedUserService from uds.core.ui import gui -from uds.core.util import log, validators +from uds.core.util import log from uds.core.util.decorators import cached -from uds.core.workers import initialize from . import helpers from .deployment_fixed import XenFixedUserService @@ -49,7 +48,6 @@ from .deployment_fixed import XenFixedUserService # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models - from uds.core.module import Module from .provider import XenProvider diff --git a/server/src/uds/services/Xen/xen_client/__init__.py b/server/src/uds/services/Xen/xen_client/__init__.py index bb6f60336..678a3e379 100644 --- a/server/src/uds/services/Xen/xen_client/__init__.py +++ b/server/src/uds/services/Xen/xen_client/__init__.py @@ -29,15 +29,12 @@ import ssl import xmlrpc.client import logging import typing -import collections.abc -from MySQLdb import connect -import server from uds.core import consts from uds.core.util.decorators import cached -import XenAPI +import XenAPI # pyright: ignore logger = logging.getLogger(__name__) @@ -51,7 +48,7 @@ class XenFault(Exception): def cache_key_helper(server_api: 'XenServer') -> str: - return server_api._url + return server_api._url # pyright: ignore[reportPrivateUsage] class XenFailure(XenAPI.Failure, XenFault): @@ -66,16 +63,16 @@ class XenFailure(XenAPI.Failure, XenFault): super(XenFailure, self).__init__(details) def isHandleInvalid(self) -> bool: - return self.details[0] == XenFailure.exHandleInvalid + return typing.cast(typing.Any, self.details[0]) == XenFailure.exHandleInvalid def needs_xen_tools(self) -> bool: - return self.details[0] == XenFailure.exVmMissingPVDrivers + return typing.cast(typing.Any, self.details[0]) == XenFailure.exVmMissingPVDrivers def bad_power_state(self) -> bool: - return self.details[0] == XenFailure.exBadVmPowerState + return typing.cast(typing.Any, self.details[0]) == XenFailure.exBadVmPowerState def is_slave(self) -> bool: - return self.details[0] == XenFailure.exHostIsSlave + return typing.cast(typing.Any, self.details[0]) == XenFailure.exHostIsSlave def as_human_readable(self) -> str: try: @@ -86,9 +83,9 @@ class XenFailure(XenAPI.Failure, XenFault): XenFailure.exSRError: 'Error on SR: {2}', XenFailure.exHandleInvalid: 'Invalid reference to {1}', } - err = errList.get(self.details[0], 'Error {0}') + err = errList.get(typing.cast(typing.Any, self.details[0]), 'Error {0}') - return err.format(*self.details) + return err.format(*typing.cast(list[typing.Any], self.details)) except Exception: return 'Unknown exception: {0}'.format(self.details) @@ -247,7 +244,7 @@ class XenServer: # pylint: disable=too-many-public-methods logger.info( '%s is an Slave, connecting to master at %s', self._host, - e.details[1], + typing.cast(typing.Any, e.details[1]) ) self._host = e.details[1] self.login(backup_checked=backup_checked) @@ -278,9 +275,8 @@ class XenServer: # pylint: disable=too-many-public-methods def get_task_info(self, task: str) -> dict[str, typing.Any]: progress = 0 - result = None + result: typing.Any = None destroy_task = False - connection_error = False try: status = self.task.get_status(task) logger.debug('Task %s in state %s', task, status) @@ -294,20 +290,19 @@ class XenServer: # pylint: disable=too-many-public-methods result = XenFailure(self.task.get_error_info(task)) destroy_task = True except XenAPI.Failure as e: - logger.debug('XenServer Failure: %s', e.details[0]) + logger.debug('XenServer Failure: %s', typing.cast(str, e.details[0])) if e.details[0] == 'HANDLE_INVALID': result = None status = 'unknown' progress = 0 else: destroy_task = True - result = e.details[0] + result = typing.cast(str, e.details[0]) status = 'failure' except ConnectionError as e: logger.debug('Connection error: %s', e) result = 'Connection error' status = 'failure' - connection_error = True except Exception as e: logger.exception('Unexpected exception!') result = str(e) @@ -501,9 +496,9 @@ class XenServer: # pylint: disable=too-many-public-methods def remove_machine(self, vmid: str) -> None: logger.debug('Removing machine') - vdisToDelete = [] + vdis_to_delete: list[str] = [] for vdb in self.VM.get_VBDs(vmid): - vdi = None + vdi = '' try: vdi = self.VBD.get_VDI(vdb) if vdi == 'OpaqueRef:NULL': @@ -516,9 +511,9 @@ class XenServer: # pylint: disable=too-many-public-methods logger.debug('%s is read only, skipping', vdi) continue logger.debug('VDI to delete: %s', vdi) - vdisToDelete.append(vdi) + vdis_to_delete.append(vdi) self.VM.destroy(vmid) - for vdi in vdisToDelete: + for vdi in vdis_to_delete: self.VDI.destroy(vdi) def configure_machine( diff --git a/server/src/uds/transports/HTML5RDP/__init__.py b/server/src/uds/transports/HTML5RDP/__init__.py index 11374a594..4aac17973 100644 --- a/server/src/uds/transports/HTML5RDP/__init__.py +++ b/server/src/uds/transports/HTML5RDP/__init__.py @@ -28,7 +28,7 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ - +# pyright: reportUnusedImport=false from .html5rdp import HTML5RDPTransport diff --git a/server/src/uds/transports/HTML5RDP/html5rdp.py b/server/src/uds/transports/HTML5RDP/html5rdp.py index 9625121ac..98ccc7f7c 100644 --- a/server/src/uds/transports/HTML5RDP/html5rdp.py +++ b/server/src/uds/transports/HTML5RDP/html5rdp.py @@ -33,7 +33,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import logging import re import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -45,7 +44,6 @@ from uds.core.util.model import sql_datetime # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequestWithUser logger = logging.getLogger(__name__) diff --git a/server/src/uds/transports/HTML5SSH/__init__.py b/server/src/uds/transports/HTML5SSH/__init__.py index 64dd221ea..ad5dc9d4b 100644 --- a/server/src/uds/transports/HTML5SSH/__init__.py +++ b/server/src/uds/transports/HTML5SSH/__init__.py @@ -28,7 +28,7 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ - +# pyright: reportUnusedImport=false from .html5ssh import HTML5SSHTransport diff --git a/server/src/uds/transports/HTML5SSH/html5ssh.py b/server/src/uds/transports/HTML5SSH/html5ssh.py index 0862ffe43..862177fb9 100644 --- a/server/src/uds/transports/HTML5SSH/html5ssh.py +++ b/server/src/uds/transports/HTML5SSH/html5ssh.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -46,7 +45,6 @@ from ..HTML5RDP.html5rdp import HTML5RDPTransport # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequestWithUser logger = logging.getLogger(__name__) diff --git a/server/src/uds/transports/HTML5VNC/__init__.py b/server/src/uds/transports/HTML5VNC/__init__.py index 01b60b12e..a086623c6 100644 --- a/server/src/uds/transports/HTML5VNC/__init__.py +++ b/server/src/uds/transports/HTML5VNC/__init__.py @@ -28,8 +28,9 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false # Experimental VNC transport from .html5vnc import HTML5VNCTransport diff --git a/server/src/uds/transports/HTML5VNC/html5vnc.py b/server/src/uds/transports/HTML5VNC/html5vnc.py index 20d38b23b..d044758e3 100644 --- a/server/src/uds/transports/HTML5VNC/html5vnc.py +++ b/server/src/uds/transports/HTML5VNC/html5vnc.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -46,7 +45,6 @@ from ..HTML5RDP.html5rdp import HTML5RDPTransport # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequestWithUser logger = logging.getLogger(__name__) diff --git a/server/src/uds/transports/RDP/__init__.py b/server/src/uds/transports/RDP/__init__.py index 0f4ec019b..0102dbee1 100644 --- a/server/src/uds/transports/RDP/__init__.py +++ b/server/src/uds/transports/RDP/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright (c) 2012 Virtual Cable S.L. +# Copyright (c) 2012-2024 Virtual Cable S.L.U. # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, @@ -28,8 +28,8 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ - +# pyright: reportUnusedImport=false from .rdp import RDPTransport from .rdptunnel import TRDPTransport diff --git a/server/src/uds/transports/RDP/rdp.py b/server/src/uds/transports/RDP/rdp.py index dd91863f1..15401943a 100644 --- a/server/src/uds/transports/RDP/rdp.py +++ b/server/src/uds/transports/RDP/rdp.py @@ -36,7 +36,7 @@ import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core import types, consts +from uds.core import types from .rdp_base import BaseRDPTransport from .rdp_file import RDPFile @@ -44,7 +44,6 @@ from .rdp_file import RDPFile # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models - from uds.core import transports from uds.core.types.requests import ExtendedHttpRequestWithUser logger = logging.getLogger(__name__) @@ -102,7 +101,7 @@ class RDPTransport(BaseRDPTransport): def get_transport_script( # pylint: disable=too-many-locals self, - userService: 'models.UserService', + userservice: 'models.UserService', transport: 'models.Transport', ip: str, os: 'types.os.DetectedOsInfo', @@ -112,7 +111,7 @@ class RDPTransport(BaseRDPTransport): ) -> 'types.transports.TransportScript': # We use helper to keep this clean - ci = self.get_connection_info(userService, user, password) + ci = self.get_connection_info(userservice, user, password) # escape conflicting chars : Note, on 3.0 this should not be neccesary. Kept until more tests # password = password.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'") @@ -191,6 +190,6 @@ class RDPTransport(BaseRDPTransport): 'Os not valid for RDP Transport: %s', request.META.get('HTTP_USER_AGENT', 'Unknown'), ) - return super().get_transport_script(userService, transport, ip, os, user, password, request) + return super().get_transport_script(userservice, transport, ip, os, user, password, request) return self.get_script(os.os.os_name(), 'direct', sp) diff --git a/server/src/uds/transports/RDP/rdp_base.py b/server/src/uds/transports/RDP/rdp_base.py index c5721f801..3e1ad0a1e 100644 --- a/server/src/uds/transports/RDP/rdp_base.py +++ b/server/src/uds/transports/RDP/rdp_base.py @@ -32,11 +32,10 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ from uds.core.ui import gui -from uds.core import transports, types, consts +from uds.core import transports, types from uds.models import UserService # Not imported at runtime, just for type checking diff --git a/server/src/uds/transports/RDP/rdp_file.py b/server/src/uds/transports/RDP/rdp_file.py index 67ba6dabc..ed15c6ffe 100644 --- a/server/src/uds/transports/RDP/rdp_file.py +++ b/server/src/uds/transports/RDP/rdp_file.py @@ -36,7 +36,6 @@ Created on Jul 29, 2011 import urllib.parse import shlex import typing -import collections.abc from uds.core import types diff --git a/server/src/uds/transports/RDP/rdptunnel.py b/server/src/uds/transports/RDP/rdptunnel.py index 90c864435..271fcbc78 100644 --- a/server/src/uds/transports/RDP/rdptunnel.py +++ b/server/src/uds/transports/RDP/rdptunnel.py @@ -35,7 +35,7 @@ import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core import transports, types, consts +from uds.core import types from uds.core.ui import gui from uds.core.util import fields, validators from uds.models import TicketStore @@ -46,7 +46,6 @@ from .rdp_file import RDPFile # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequestWithUser logger = logging.getLogger(__name__) @@ -123,7 +122,7 @@ class TRDPTransport(BaseRDPTransport): def get_transport_script( # pylint: disable=too-many-locals self, - userService: 'models.UserService', + userservice: 'models.UserService', transport: 'models.Transport', ip: str, os: 'types.os.DetectedOsInfo', @@ -133,7 +132,7 @@ class TRDPTransport(BaseRDPTransport): ) -> 'types.transports.TransportScript': # We use helper to keep this clean - ci = self.get_connection_info(userService, user, password) + ci = self.get_connection_info(userservice, user, password) # escape conflicting chars : Note, on 3.0 this should not be neccesary. Kept until more tests # password = password.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'") @@ -144,7 +143,7 @@ class TRDPTransport(BaseRDPTransport): depth = self.color_depth.value ticket = TicketStore.create_for_tunnel( - userService=userService, + userService=userservice, port=self.rdp_port.as_int(), validity=self.tunnel_wait.as_int() + 60, # Ticket overtime ) @@ -222,6 +221,6 @@ class TRDPTransport(BaseRDPTransport): 'Os not valid for RDP Transport: %s', request.META.get('HTTP_USER_AGENT', 'Unknown'), ) - return super().get_transport_script(userService, transport, ip, os, user, password, request) + return super().get_transport_script(userservice, transport, ip, os, user, password, request) return self.get_script(os.os.os_name(), 'tunnel', sp) diff --git a/server/src/uds/transports/SPICE/__init__.py b/server/src/uds/transports/SPICE/__init__.py index e1681be9a..5c888235d 100644 --- a/server/src/uds/transports/SPICE/__init__.py +++ b/server/src/uds/transports/SPICE/__init__.py @@ -28,8 +28,9 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .spice import SPICETransport from .spicetunnel import TSPICETransport diff --git a/server/src/uds/transports/SPICE/remote_viewer_file.py b/server/src/uds/transports/SPICE/remote_viewer_file.py index b1ade54a5..48327641d 100644 --- a/server/src/uds/transports/SPICE/remote_viewer_file.py +++ b/server/src/uds/transports/SPICE/remote_viewer_file.py @@ -27,7 +27,6 @@ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import typing -import collections.abc TEMPLATE = '''[virt-viewer] type={type} diff --git a/server/src/uds/transports/SPICE/spice.py b/server/src/uds/transports/SPICE/spice.py index bc45ac4d6..0e35a318a 100644 --- a/server/src/uds/transports/SPICE/spice.py +++ b/server/src/uds/transports/SPICE/spice.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -44,7 +43,6 @@ from .spice_base import BaseSpiceTransport # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models - from uds.core import transports from uds.core.types.requests import ExtendedHttpRequestWithUser logger = logging.getLogger(__name__) @@ -75,7 +73,7 @@ class SPICETransport(BaseSpiceTransport): def get_transport_script( self, - userService: 'models.UserService', + userservice: 'models.UserService', transport: 'models.Transport', ip: str, os: 'types.os.DetectedOsInfo', @@ -84,7 +82,7 @@ class SPICETransport(BaseSpiceTransport): request: 'ExtendedHttpRequestWithUser', ) -> 'types.transports.TransportScript': try: - userservice_instance = userService.get_instance() + userservice_instance = userservice.get_instance() con: typing.Optional[types.services.ConsoleConnectionInfo] = ( userservice_instance.get_console_connection() ) @@ -125,4 +123,4 @@ class SPICETransport(BaseSpiceTransport): try: return self.get_script(os.os.os_name(), 'direct', sp) except Exception: - return super().get_transport_script(userService, transport, ip, os, user, password, request) + return super().get_transport_script(userservice, transport, ip, os, user, password, request) diff --git a/server/src/uds/transports/SPICE/spice_base.py b/server/src/uds/transports/SPICE/spice_base.py index ca1319f2c..855932a2b 100644 --- a/server/src/uds/transports/SPICE/spice_base.py +++ b/server/src/uds/transports/SPICE/spice_base.py @@ -32,11 +32,10 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ from uds.core.ui import gui -from uds.core import transports, types, consts +from uds.core import transports, types # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: @@ -156,7 +155,7 @@ class BaseSpiceTransport(transports.Transport): if con is None: return False - if con.proxy is not None: + if con.proxy: # if proxy is set, we can't use it return True # test ANY of the ports @@ -221,8 +220,8 @@ class BaseSpiceTransport(transports.Transport): def get_connection_info( self, - userService: typing.Union['models.UserService', 'models.ServicePool'], + userservice: typing.Union['models.UserService', 'models.ServicePool'], user: 'models.User', password: str, ) -> types.connections.ConnectionData: - return self.process_user_password(userService, user, password) + return self.process_user_password(userservice, user, password) diff --git a/server/src/uds/transports/SPICE/spicetunnel.py b/server/src/uds/transports/SPICE/spicetunnel.py index e5e8c67b8..8ea5998b7 100644 --- a/server/src/uds/transports/SPICE/spicetunnel.py +++ b/server/src/uds/transports/SPICE/spicetunnel.py @@ -31,11 +31,10 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core import exceptions, transports, types, consts +from uds.core import exceptions, types from uds.core.ui import gui from uds.core.util import fields, validators from uds.models import TicketStore @@ -46,7 +45,6 @@ from .spice_base import BaseSpiceTransport # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequestWithUser logger = logging.getLogger(__name__) @@ -92,7 +90,7 @@ class TSPICETransport(BaseSpiceTransport): def get_transport_script( # pylint: disable=too-many-locals self, - userService: 'models.UserService', + userservice: 'models.UserService', transport: 'models.Transport', ip: str, os: 'types.os.DetectedOsInfo', @@ -101,7 +99,7 @@ class TSPICETransport(BaseSpiceTransport): request: 'ExtendedHttpRequestWithUser', ) -> types.transports.TransportScript: try: - userServiceInstance = userService.get_instance() + userServiceInstance = userservice.get_instance() con = userServiceInstance.get_console_connection() except Exception: logger.exception('Error getting console connection data') @@ -122,19 +120,19 @@ class TSPICETransport(BaseSpiceTransport): if con.proxy: logger.exception('Proxied SPICE tunnels are not suppoorted') return super().get_transport_script( - userService, transport, ip, os, user, password, request + userservice, transport, ip, os, user, password, request ) if con.port: ticket = TicketStore.create_for_tunnel( - userService=userService, + userService=userservice, port=int(con.port), validity=self.tunnel_wait.as_int() + 60, # Ticket overtime ) if con.secure_port: ticket_secure = TicketStore.create_for_tunnel( - userService=userService, + userService=userservice, port=int(con.secure_port), host=con.address, validity=self.tunnel_wait.as_int() + 60, # Ticket overtime @@ -173,5 +171,5 @@ class TSPICETransport(BaseSpiceTransport): return self.get_script(os.os.os_name(), 'tunnel', sp) except Exception: return super().get_transport_script( - userService, transport, ip, os, user, password, request + userservice, transport, ip, os, user, password, request ) diff --git a/server/src/uds/transports/Test/__init__.py b/server/src/uds/transports/Test/__init__.py index 1ae9b8c72..f9c0859e2 100644 --- a/server/src/uds/transports/Test/__init__.py +++ b/server/src/uds/transports/Test/__init__.py @@ -28,7 +28,8 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .transport import TestTransport diff --git a/server/src/uds/transports/Test/transport.py b/server/src/uds/transports/Test/transport.py index 05bcec6b4..c2e383760 100644 --- a/server/src/uds/transports/Test/transport.py +++ b/server/src/uds/transports/Test/transport.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -42,7 +41,6 @@ from uds.core.ui import gui # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequestWithUser logger = logging.getLogger(__name__) diff --git a/server/src/uds/transports/URL/__init__.py b/server/src/uds/transports/URL/__init__.py index 6c651fc65..f6f58e400 100644 --- a/server/src/uds/transports/URL/__init__.py +++ b/server/src/uds/transports/URL/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright (c) 2012-202 Virtual Cable S.L.U. +# Copyright (c) 2012-2024 Virtual Cable S.L.U. # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, @@ -28,7 +28,8 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .url_custom import URLCustomTransport diff --git a/server/src/uds/transports/URL/url_custom.py b/server/src/uds/transports/URL/url_custom.py index a093a00e4..848d45dc3 100644 --- a/server/src/uds/transports/URL/url_custom.py +++ b/server/src/uds/transports/URL/url_custom.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ @@ -42,7 +41,6 @@ from uds.core.ui import gui # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequestWithUser logger = logging.getLogger(__name__) diff --git a/server/src/uds/transports/X2GO/__init__.py b/server/src/uds/transports/X2GO/__init__.py index 165cf1f68..e360a90b0 100644 --- a/server/src/uds/transports/X2GO/__init__.py +++ b/server/src/uds/transports/X2GO/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # -# Copyright (c) 2012 Virtual Cable S.L. +# Copyright (c) 2012-2024 Virtual Cable S.L.U. # All rights reserved. # # Redistribution and use in source and binary forms, with or without modification, @@ -28,8 +28,9 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ -@author: Adolfo Gómez, dkmaster at dkmon dot com +Author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false from .x2go import X2GOTransport from .x2gotunnel import TX2GOTransport diff --git a/server/src/uds/transports/X2GO/x2go.py b/server/src/uds/transports/X2GO/x2go.py index 83e1aea3b..089db3e0f 100644 --- a/server/src/uds/transports/X2GO/x2go.py +++ b/server/src/uds/transports/X2GO/x2go.py @@ -32,7 +32,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ from .x2go_base import BaseX2GOTransport @@ -41,7 +40,7 @@ from . import x2go_file # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models - from uds.core import transports, types + from uds.core import types from uds.core.types.requests import ExtendedHttpRequestWithUser logger = logging.getLogger(__name__) @@ -84,7 +83,7 @@ class X2GOTransport(BaseX2GOTransport): ) -> 'types.transports.TransportScript': ci = self.get_connection_info(userservice, user, password) - priv, pub = self.getAndPushKey(ci.username, userservice) + priv, _pub = self.getAndPushKey(ci.username, userservice) width, height = self.get_screen_size() rootless = False diff --git a/server/src/uds/transports/X2GO/x2go_base.py b/server/src/uds/transports/X2GO/x2go_base.py index 388d129ee..98627d24f 100644 --- a/server/src/uds/transports/X2GO/x2go_base.py +++ b/server/src/uds/transports/X2GO/x2go_base.py @@ -34,17 +34,16 @@ import io import logging import os import typing -import collections.abc import paramiko from django.utils.translation import gettext_lazy from django.utils.translation import gettext_noop as _ -from uds.core import consts, transports, types +from uds.core import transports, types from uds.core.managers.userservice import UserServiceManager from uds.core.types.preferences import CommonPrefs from uds.core.ui import gui -from uds.core.util import connection +from uds.core.util import net # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: @@ -207,7 +206,7 @@ class BaseX2GOTransport(transports.Transport): tab=types.ui.Tab.ADVANCED, ) - def is_ip_allowed(self, userService: 'models.UserService', ip: str) -> bool: + def is_ip_allowed(self, userservice: 'models.UserService', ip: str) -> bool: """ Checks if the transport is available for the requested destination ip Override this in yours transports @@ -216,7 +215,7 @@ class BaseX2GOTransport(transports.Transport): ready = self.cache.get(ip) if ready is None: # Check again for ready - if connection.test_connectivity(ip, 22): + if net.test_connectivity(ip, 22): self.cache.put(ip, 'Y', READY_CACHE_TIMEOUT) return True self.cache.put(ip, 'N', READY_CACHE_TIMEOUT) @@ -260,11 +259,11 @@ class BaseX2GOTransport(transports.Transport): def get_connection_info( self, - userService: typing.Union['models.UserService', 'models.ServicePool'], + userservice: typing.Union['models.UserService', 'models.ServicePool'], user: 'models.User', password: str, ) -> types.connections.ConnectionData: - return self.process_user_password(userService, user, password) + return self.process_user_password(userservice, user, password) def genKeyPairForSsh(self) -> tuple[str, str]: """ diff --git a/server/src/uds/transports/X2GO/x2gotunnel.py b/server/src/uds/transports/X2GO/x2gotunnel.py index 7ee52c517..2f531784e 100644 --- a/server/src/uds/transports/X2GO/x2gotunnel.py +++ b/server/src/uds/transports/X2GO/x2gotunnel.py @@ -31,11 +31,10 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_noop as _ -from uds.core import transports, types, consts +from uds.core import types from uds.core.ui import gui from uds.core.util import fields, validators from uds.models import TicketStore @@ -46,7 +45,6 @@ from .x2go_base import BaseX2GOTransport # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds import models - from uds.core.module import Module from uds.core.types.requests import ExtendedHttpRequestWithUser @@ -98,7 +96,7 @@ class TX2GOTransport(BaseX2GOTransport): def get_transport_script( self, - userService: 'models.UserService', + userservice: 'models.UserService', transport: 'models.Transport', ip: str, os: 'types.os.DetectedOsInfo', @@ -106,9 +104,9 @@ class TX2GOTransport(BaseX2GOTransport): password: str, request: 'ExtendedHttpRequestWithUser', ) -> 'types.transports.TransportScript': - ci = self.get_connection_info(userService, user, password) + ci = self.get_connection_info(userservice, user, password) - private_key, _public_key = self.getAndPushKey(ci.username, userService) + private_key, _public_key = self.getAndPushKey(ci.username, userservice) width, height = self.get_screen_size() @@ -133,7 +131,7 @@ class TX2GOTransport(BaseX2GOTransport): ) ticket = TicketStore.create_for_tunnel( - userService=userService, + userService=userservice, port=22, validity=self.tunnel_wait.as_int() + 60, # Ticket overtime ) @@ -154,4 +152,4 @@ class TX2GOTransport(BaseX2GOTransport): try: return self.get_script(os.os.os_name(), 'tunnel', sp) except Exception: - return super().get_transport_script(userService, transport, ip, os, user, password, request) + return super().get_transport_script(userservice, transport, ip, os, user, password, request) diff --git a/server/src/uds/web/forms/login_form.py b/server/src/uds/web/forms/login_form.py index 1bb946346..da24bdb78 100644 --- a/server/src/uds/web/forms/login_form.py +++ b/server/src/uds/web/forms/login_form.py @@ -31,7 +31,6 @@ """ import logging import typing -import collections.abc from django.utils.translation import gettext_lazy as _ from django import forms @@ -62,7 +61,7 @@ class LoginForm(forms.Form): # Parent init super(LoginForm, self).__init__(*args, **kwargs) - choices = [] + choices: list[tuple[str, str]] = [] for a in Authenticator.get_by_tag(tag): if not a.get_type(): # Not existing manager for the auth? diff --git a/server/src/uds/web/util/authentication.py b/server/src/uds/web/util/authentication.py index a2423fb61..54c93bda2 100644 --- a/server/src/uds/web/util/authentication.py +++ b/server/src/uds/web/util/authentication.py @@ -28,7 +28,6 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import logging import typing diff --git a/server/src/uds/web/util/configjs.py b/server/src/uds/web/util/configjs.py index 28fd251ed..ac206bf81 100644 --- a/server/src/uds/web/util/configjs.py +++ b/server/src/uds/web/util/configjs.py @@ -32,7 +32,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import json import logging import typing -import collections.abc from django import template from django.conf import settings diff --git a/server/src/uds/web/util/errors.py b/server/src/uds/web/util/errors.py index 248fded4a..0b53bbe68 100644 --- a/server/src/uds/web/util/errors.py +++ b/server/src/uds/web/util/errors.py @@ -33,7 +33,6 @@ import traceback import json import logging import typing -import collections.abc from django.utils.translation import gettext_lazy as _ from django.shortcuts import render @@ -43,7 +42,6 @@ from django.http import HttpResponseRedirect from django.urls import reverse from uds.core import types -from uds.models import ServicePool, Transport, UserService, Authenticator # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: diff --git a/server/src/uds/web/views/__init__.py b/server/src/uds/web/views/__init__.py index bdbbe2636..38b5aa001 100644 --- a/server/src/uds/web/views/__init__.py +++ b/server/src/uds/web/views/__init__.py @@ -28,6 +28,7 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ +# pyright: reportUnusedImport=false import logging diff --git a/server/src/uds/web/views/auth.py b/server/src/uds/web/views/auth.py index 10b4a2b66..5e753d4de 100644 --- a/server/src/uds/web/views/auth.py +++ b/server/src/uds/web/views/auth.py @@ -28,7 +28,6 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ -import collections.abc import logging import typing @@ -39,7 +38,7 @@ from django.utils.translation import gettext as _ from django.views.decorators.cache import never_cache from django.views.decorators.csrf import csrf_exempt -from uds.core import auths, consts, exceptions, types +from uds.core import auths, exceptions, types from uds.core.auths.auth import authenticate_via_callback, log_login, uds_cookie, web_login, web_logout from uds.core.managers.crypto import CryptoManager from uds.core.managers.userservice import UserServiceManager diff --git a/server/src/uds/web/views/download.py b/server/src/uds/web/views/download.py index e155ea57a..8e9c532c7 100644 --- a/server/src/uds/web/views/download.py +++ b/server/src/uds/web/views/download.py @@ -30,7 +30,6 @@ """ import logging import typing -import collections.abc from uds.core.auths.auth import web_login_required from uds.core.managers import downloads_manager diff --git a/server/src/uds/web/views/images.py b/server/src/uds/web/views/images.py index 1fdc117aa..711138195 100644 --- a/server/src/uds/web/views/images.py +++ b/server/src/uds/web/views/images.py @@ -30,7 +30,6 @@ """ import logging import typing -import collections.abc from django.http import HttpResponse from django.views.decorators.cache import cache_page diff --git a/server/tests/core/util/test_uniqueid.py b/server/tests/core/util/test_uniqueid.py index 92f36966f..d3750a357 100644 --- a/server/tests/core/util/test_uniqueid.py +++ b/server/tests/core/util/test_uniqueid.py @@ -60,7 +60,7 @@ class UniqueIdTest(UDSTestCase): name_generator: UniqueNameGenerator def setUp(self) -> None: - self.uniqueid_generator = UniqueIDGenerator('uidg1', 'test', 'test') + self.uniqueid_generator = UniqueIDGenerator('test', 'test') self.ugidGen = UniqueGIDGenerator('test') self.macs_generator = UniqueMacGenerator('test') self.name_generator = UniqueNameGenerator('test') diff --git a/server/tests/services/proxmox/fixtures.py b/server/tests/services/proxmox/fixtures.py index f37cb21d4..a3d0a7b47 100644 --- a/server/tests/services/proxmox/fixtures.py +++ b/server/tests/services/proxmox/fixtures.py @@ -33,8 +33,6 @@ Author: Adolfo Gómez, dkmaster at dkmon dot com import contextlib import typing import datetime -import collections.abc -import itertools from unittest import mock import uuid @@ -42,7 +40,6 @@ import uuid from uds.core import types, environment from uds.core.ui.user_interface import gui -from ...utils.test import UDSTestCase from ...utils.autospec import autospec, AutoSpecMethodInfo from uds.services.Proxmox import ( @@ -327,7 +324,7 @@ CLIENT_METHODS_INFO: typing.Final[list[AutoSpecMethodInfo]] = [ AutoSpecMethodInfo( 'get_machine_configuration', method=lambda vmid, **kwargs: VMS_CONFIGURATION[vmid - 1] # pyright: ignore ), # pyright: ignore - # set_machine_ha return None + # enable_machine_ha return None # start_machine AutoSpecMethodInfo('start_machine', return_value=UPID), # stop_machine diff --git a/server/tests/services/proxmox/test_provider.py b/server/tests/services/proxmox/test_provider.py index 8b55c086e..e67801b63 100644 --- a/server/tests/services/proxmox/test_provider.py +++ b/server/tests/services/proxmox/test_provider.py @@ -30,8 +30,6 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import typing -import collections.abc from unittest import mock from uds.core import types, ui, environment @@ -252,7 +250,7 @@ class TestProxmovProvider(UDSTestCase): api.enable_machine_ha.assert_called_once_with(1, True, 'group') provider.set_machine_mac(1, 'mac') - api.set_machine_ha.assert_called_once_with(1, 'mac') + api.set_machine_mac.assert_called_once_with(1, 'mac') provider.disable_machine_ha(1) api.disable_machine_ha.assert_called_once_with(1) diff --git a/server/tests/services/proxmox/test_userservice_linked.py b/server/tests/services/proxmox/test_userservice_linked.py index 7dfaeab4d..13e756081 100644 --- a/server/tests/services/proxmox/test_userservice_linked.py +++ b/server/tests/services/proxmox/test_userservice_linked.py @@ -30,18 +30,12 @@ """ Author: Adolfo Gómez, dkmaster at dkmon dot com """ -import stat import typing -import datetime -import collections.abc -import itertools from unittest import mock -from tests.web import user from uds import models -from uds.core import types, ui, environment -from uds.models import service -from uds.services.Proxmox.deployment_linked import ProxmoxUserserviceLinked, Operation +from uds.core import types +from uds.services.Proxmox.deployment_linked import Operation from . import fixtures @@ -66,6 +60,12 @@ def limit_iter(check: typing.Callable[[], bool], limit: int = 128) -> typing.Gen # We use transactions on some related methods (storage access, etc...) class TestProxmovLinkedService(UDSTransactionTestCase): + def setUp(self) -> None: + # Set machine state for fixture to started + fixtures.VMS_INFO = [ + fixtures.VMS_INFO[i]._replace(status='stopped') for i in range(len(fixtures.VMS_INFO)) + ] + def test_userservice_linked_cache_l1(self) -> None: """