1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-09-15 20:36:55 +00:00

More type hints

This commit is contained in:
Unrud 2021-07-26 20:56:46 +02:00 committed by Unrud
parent 12fe5ce637
commit cecb17df03
51 changed files with 1374 additions and 957 deletions

View file

@ -32,17 +32,21 @@ Take a look at the class ``BaseRights`` if you want to implement your own.
"""
from radicale import utils
from typing import Sequence
INTERNAL_TYPES = ("authenticated", "owner_write", "owner_only", "from_file")
from radicale import config, utils
INTERNAL_TYPES: Sequence[str] = ("authenticated", "owner_write", "owner_only",
"from_file")
def load(configuration):
def load(configuration: "config.Configuration") -> "BaseRights":
"""Load the rights module chosen in configuration."""
return utils.load_plugin(INTERNAL_TYPES, "rights", "Rights", configuration)
return utils.load_plugin(INTERNAL_TYPES, "rights", "Rights", BaseRights,
configuration)
def intersect(a, b):
def intersect(a: str, b: str) -> str:
"""Intersect two lists of rights.
Returns all rights that are both in ``a`` and ``b``.
@ -52,7 +56,8 @@ def intersect(a, b):
class BaseRights:
def __init__(self, configuration):
def __init__(self, configuration: "config.Configuration") -> None:
"""Initialize BaseRights.
``configuration`` see ``radicale.config`` module.
@ -62,7 +67,7 @@ class BaseRights:
"""
self.configuration = configuration
def authorization(self, user, path):
def authorization(self, user: str, path: str) -> str:
"""Get granted rights of ``user`` for the collection ``path``.
If ``user`` is empty, check for anonymous rights.

View file

@ -21,15 +21,16 @@ calendars and address books.
"""
from radicale import pathutils, rights
from radicale import config, pathutils, rights
class Rights(rights.BaseRights):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __init__(self, configuration: config.Configuration) -> None:
super().__init__(configuration)
self._verify_user = self.configuration.get("auth", "type") != "none"
def authorization(self, user, path):
def authorization(self, user: str, path: str) -> str:
if self._verify_user and not user:
return ""
sane_path = pathutils.strip_path(path)

View file

@ -37,16 +37,19 @@ Leading or ending slashes are trimmed from collection's path.
import configparser
import re
from radicale import pathutils, rights
from radicale import config, pathutils, rights
from radicale.log import logger
class Rights(rights.BaseRights):
def __init__(self, configuration):
_filename: str
def __init__(self, configuration: config.Configuration) -> None:
super().__init__(configuration)
self._filename = configuration.get("rights", "file")
def authorization(self, user, path):
def authorization(self, user: str, path: str) -> str:
user = user or ""
sane_path = pathutils.strip_path(path)
# Prevent "regex injection"
@ -54,8 +57,7 @@ class Rights(rights.BaseRights):
rights_config = configparser.ConfigParser()
try:
if not rights_config.read(self._filename):
raise RuntimeError("No such file: %r" %
self._filename)
raise RuntimeError("No such file: %r" % self._filename)
except Exception as e:
raise RuntimeError("Failed to load rights file %r: %s" %
(self._filename, e)) from e
@ -67,7 +69,7 @@ class Rights(rights.BaseRights):
user_match = re.fullmatch(user_pattern.format(), user)
collection_match = user_match and re.fullmatch(
collection_pattern.format(
*map(re.escape, user_match.groups()),
*(re.escape(s) for s in user_match.groups()),
user=escaped_user), sane_path)
except Exception as e:
raise RuntimeError("Error in section %r of rights file %r: "

View file

@ -26,7 +26,8 @@ from radicale import pathutils
class Rights(authenticated.Rights):
def authorization(self, user, path):
def authorization(self, user: str, path: str) -> str:
if self._verify_user and not user:
return ""
sane_path = pathutils.strip_path(path)

View file

@ -26,7 +26,8 @@ from radicale import pathutils
class Rights(authenticated.Rights):
def authorization(self, user, path):
def authorization(self, user: str, path: str) -> str:
if self._verify_user and not user:
return ""
sane_path = pathutils.strip_path(path)