Philipp Hörist pushed to branch master at gajim / gajim
Commits: 6670992e by lovetox at 2021-12-21T21:39:16+01:00 CertStore: Add type annotations
- - - - -
3 changed files:
- gajim/common/cert_store.py - gajim/common/helpers.py - pyrightconfig.json
Changes:
===================================== gajim/common/cert_store.py ===================================== @@ -12,8 +12,11 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
+from __future__ import annotations
import logging +from pathlib import Path +from typing import Optional
from gi.repository import GLib from gi.repository import Gio @@ -30,18 +33,18 @@ class CertificateStore: def __init__(self): self._path = configpaths.get('CERT_STORE') - self._certs = [] + self._certs: list[Gio.TlsCertificate] = []
self._load_certificates()
- def _get_random_path(self): + def _get_random_path(self) -> Path: filename = get_random_string() path = self._path / filename if path.exists(): return self._get_random_path() return path
- def _load_certificates(self): + def _load_certificates(self) -> None: for path in self._path.iterdir(): if path.is_dir(): continue @@ -59,7 +62,7 @@ def _load_certificates(self): def get_certificates(self): return list(self._certs)
- def add_certificate(self, certificate): + def add_certificate(self, certificate: Gio.TlsCertificate) -> None: log.info('Add certificate to trust store') self._certs.append(certificate) pem = certificate.props.certificate_pem @@ -69,7 +72,10 @@ def add_certificate(self, certificate): self._on_certificate_write_finished, path)
- def verify(self, certificate, tls_errors): + def verify(self, + certificate: Gio.TlsCertificate, + tls_errors: set[Gio.TlsCertificateFlags]): + if Gio.TlsCertificateFlags.UNKNOWN_CA in tls_errors: for trusted_certificate in self._certs: if trusted_certificate.is_same(certificate): @@ -81,9 +87,11 @@ def verify(self, certificate, tls_errors): return False
@staticmethod - def _on_certificate_write_finished(data, error, path): - if data is None: - log.error('Can't store certificate: %s', error) + def _on_certificate_write_finished(_successful: bool, + error: Optional[GLib.Error], + path: Path): + if error is not None: + log.error('Can't store certificate: %s', error.message) return
log.info('Certificate stored: %s', path)
===================================== gajim/common/helpers.py ===================================== @@ -28,6 +28,7 @@ from __future__ import annotations
from typing import Any +from typing import Callable from typing import Dict from typing import Optional from typing import List @@ -1256,7 +1257,12 @@ def notify(self, signal_name: str, *args: Any, **kwargs: Any): func(self, signal_name, *args, **kwargs)
-def write_file_async(path, data, callback, user_data=None): +def write_file_async( + path: Path, + data: bytes, + callback: Callable[[bool, Optional[GLib.Error], Any], Any], + user_data: Optional[Any] = None): + file = Gio.File.new_for_path(str(path)) file.create_async(Gio.FileCreateFlags.PRIVATE, GLib.PRIORITY_DEFAULT,
===================================== pyrightconfig.json ===================================== @@ -11,6 +11,7 @@ ], "typeCheckingMode": "strict", "include": [ - "gajim/gtk/chat_filter.py" + "gajim/gtk/chat_filter.py", + "gajim/common/cert_store.py" ], }
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/6670992e9fbff38f7e1b12ce1c65f77f8...