mirror of
https://github.com/Kozea/Radicale.git
synced 2025-06-26 16:45:52 +00:00
More type hints
This commit is contained in:
parent
12fe5ce637
commit
cecb17df03
51 changed files with 1374 additions and 957 deletions
|
@ -26,20 +26,21 @@ import copy
|
|||
import xml.etree.ElementTree as ET
|
||||
from collections import OrderedDict
|
||||
from http import client
|
||||
from typing import Dict, Mapping, Optional
|
||||
from urllib.parse import quote
|
||||
|
||||
from radicale import pathutils
|
||||
from radicale import item, pathutils
|
||||
|
||||
MIMETYPES = {
|
||||
MIMETYPES: Mapping[str, str] = {
|
||||
"VADDRESSBOOK": "text/vcard",
|
||||
"VCALENDAR": "text/calendar"}
|
||||
|
||||
OBJECT_MIMETYPES = {
|
||||
OBJECT_MIMETYPES: Mapping[str, str] = {
|
||||
"VCARD": "text/vcard",
|
||||
"VLIST": "text/x-vlist",
|
||||
"VCALENDAR": "text/calendar"}
|
||||
|
||||
NAMESPACES = {
|
||||
NAMESPACES: Mapping[str, str] = {
|
||||
"C": "urn:ietf:params:xml:ns:caldav",
|
||||
"CR": "urn:ietf:params:xml:ns:carddav",
|
||||
"D": "DAV:",
|
||||
|
@ -48,15 +49,15 @@ NAMESPACES = {
|
|||
"ME": "http://me.com/_namespace/",
|
||||
"RADICALE": "http://radicale.org/ns/"}
|
||||
|
||||
NAMESPACES_REV = {}
|
||||
NAMESPACES_REV: Mapping[str, str] = {v: k for k, v in NAMESPACES.items()}
|
||||
|
||||
for short, url in NAMESPACES.items():
|
||||
NAMESPACES_REV[url] = short
|
||||
ET.register_namespace("" if short == "D" else short, url)
|
||||
|
||||
|
||||
def pretty_xml(element):
|
||||
def pretty_xml(element: ET.Element) -> str:
|
||||
"""Indent an ElementTree ``element`` and its children."""
|
||||
def pretty_xml_recursive(element, level):
|
||||
def pretty_xml_recursive(element: ET.Element, level: int) -> None:
|
||||
indent = "\n" + level * " "
|
||||
if len(element) > 0:
|
||||
if not (element.text or "").strip():
|
||||
|
@ -74,7 +75,7 @@ def pretty_xml(element):
|
|||
return '<?xml version="1.0"?>\n%s' % ET.tostring(element, "unicode")
|
||||
|
||||
|
||||
def make_clark(human_tag):
|
||||
def make_clark(human_tag: str) -> str:
|
||||
"""Get XML Clark notation from human tag ``human_tag``.
|
||||
|
||||
If ``human_tag`` is already in XML Clark notation it is returned as-is.
|
||||
|
@ -88,13 +89,13 @@ def make_clark(human_tag):
|
|||
ns_prefix, tag = human_tag.split(":", maxsplit=1)
|
||||
if not ns_prefix or not tag:
|
||||
raise ValueError("Invalid XML tag: %r" % human_tag)
|
||||
ns = NAMESPACES.get(ns_prefix)
|
||||
ns = NAMESPACES.get(ns_prefix, "")
|
||||
if not ns:
|
||||
raise ValueError("Unknown XML namespace prefix: %r" % human_tag)
|
||||
return "{%s}%s" % (ns, tag)
|
||||
|
||||
|
||||
def make_human_tag(clark_tag):
|
||||
def make_human_tag(clark_tag: str) -> str:
|
||||
"""Replace known namespaces in XML Clark notation ``clark_tag`` with
|
||||
prefix.
|
||||
|
||||
|
@ -111,31 +112,31 @@ def make_human_tag(clark_tag):
|
|||
ns, tag = clark_tag[len("{"):].split("}", maxsplit=1)
|
||||
if not ns or not tag:
|
||||
raise ValueError("Invalid XML tag: %r" % clark_tag)
|
||||
ns_prefix = NAMESPACES_REV.get(ns)
|
||||
ns_prefix = NAMESPACES_REV.get(ns, "")
|
||||
if ns_prefix:
|
||||
return "%s:%s" % (ns_prefix, tag)
|
||||
return clark_tag
|
||||
|
||||
|
||||
def make_response(code):
|
||||
def make_response(code: int) -> str:
|
||||
"""Return full W3C names from HTTP status codes."""
|
||||
return "HTTP/1.1 %i %s" % (code, client.responses[code])
|
||||
|
||||
|
||||
def make_href(base_prefix, href):
|
||||
def make_href(base_prefix: str, href: str) -> str:
|
||||
"""Return prefixed href."""
|
||||
assert href == pathutils.sanitize_path(href)
|
||||
return quote("%s%s" % (base_prefix, href))
|
||||
|
||||
|
||||
def webdav_error(human_tag):
|
||||
def webdav_error(human_tag: str) -> ET.Element:
|
||||
"""Generate XML error message."""
|
||||
root = ET.Element(make_clark("D:error"))
|
||||
root.append(ET.Element(make_clark(human_tag)))
|
||||
return root
|
||||
|
||||
|
||||
def get_content_type(item, encoding):
|
||||
def get_content_type(item: "item.Item", encoding: str) -> str:
|
||||
"""Get the content-type of an item with charset and component parameters.
|
||||
"""
|
||||
mimetype = OBJECT_MIMETYPES[item.name]
|
||||
|
@ -146,13 +147,14 @@ def get_content_type(item, encoding):
|
|||
return content_type
|
||||
|
||||
|
||||
def props_from_request(xml_request):
|
||||
def props_from_request(xml_request: Optional[ET.Element]
|
||||
) -> Dict[str, Optional[str]]:
|
||||
"""Return a list of properties as a dictionary.
|
||||
|
||||
Properties that should be removed are set to `None`.
|
||||
|
||||
"""
|
||||
result = OrderedDict()
|
||||
result: OrderedDict = OrderedDict()
|
||||
if xml_request is None:
|
||||
return result
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue