summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>2024-01-30 15:40:58 -0500
committerSean Whitton <spwhitton@spwhitton.name>2024-04-06 16:16:00 +0800
commit793fafd0a98d3d428e07802eacfa0b359f6c9063 (patch)
tree3a7d5521e4abe73c2b2d9666994232f827d8be93
parent3f69d1912f91c6b1b51381de51f9a79f43f908ad (diff)
downloadmailscripts-793fafd0a98d3d428e07802eacfa0b359f6c9063.tar.gz
email-print-mime-structure, imap-dl: clean up types with mypy 1.9.0
Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
-rwxr-xr-xemail-print-mime-structure22
-rwxr-xr-ximap-dl14
2 files changed, 23 insertions, 13 deletions
diff --git a/email-print-mime-structure b/email-print-mime-structure
index b7646e0..3263da9 100755
--- a/email-print-mime-structure
+++ b/email-print-mime-structure
@@ -2,7 +2,7 @@
# PYTHON_ARGCOMPLETE_OK
# -*- coding: utf-8 -*-
-# Copyright (C) 2019 Daniel Kahn Gillmor
+# Copyright (C) 2019-2024 Daniel Kahn Gillmor
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -39,6 +39,7 @@ import subprocess
from argparse import ArgumentParser, Namespace
from typing import Optional, Union, List, Tuple, Any
+from types import ModuleType
from email.charset import Charset
from email.message import Message
@@ -47,8 +48,9 @@ try:
except ImportError:
pgpy = None
+argcomplete:Optional[ModuleType]
try:
- import argcomplete #type: ignore
+ import argcomplete
except ImportError:
argcomplete = None
@@ -74,7 +76,7 @@ class MimePrinter(object):
# FIXME: it looks like we are counting chars here, not bytes:
nbytes = len(z.as_string())
else:
- payload:Union[List[Message], str, bytes, None] = z.get_payload()
+ payload = z.get_payload()
if not isinstance(payload, (str,bytes)):
raise TypeError(f'expected payload to be either str or bytes, got {type(payload)}')
# FIXME: it looks like we are counting chars here, not bytes:
@@ -106,7 +108,7 @@ class MimePrinter(object):
else:
if z.get_content_type().lower() == 'application/pkcs7-mime' and \
str(z.get_param('smime-type')).lower() == 'signed-data':
- bodypart:Union[List[Message],str,bytes,None] = z.get_payload(decode=True)
+ bodypart = z.get_payload(decode=True)
if isinstance(bodypart, bytes):
unwrapped = self.pipe_transform(bodypart, ['certtool', '--p7-show-data', '--p7-info', '--inder'])
if unwrapped:
@@ -118,7 +120,7 @@ class MimePrinter(object):
def decrypt_part(self, msg:Message, flavor:EncType) -> Optional[Message]:
- ciphertext:Union[List[Message],str,bytes,None] = msg.get_payload(decode=True)
+ ciphertext = msg.get_payload(decode=True)
cryptopayload:Optional[Message] = None
if not isinstance(ciphertext, bytes):
logging.warning('encrypted part was not a leaf mime part somehow')
@@ -178,14 +180,18 @@ class MimePrinter(object):
prefix = prefix.rpartition('└')[0] + ' '
if prefix.endswith('├'):
prefix = prefix.rpartition('├')[0] + '│'
- parts:Union[List[Message], str, bytes, None] = z.get_payload()
+ parts = z.get_payload()
if not isinstance(parts, list):
raise TypeError(f'parts was {type(parts)}, expected List[Message]')
i = 0
while (i < len(parts)-1):
- self.print_tree(parts[i], prefix + '├', z, i+1)
+ msg = parts[i]
+ if isinstance(msg, Message):
+ self.print_tree(msg, prefix + '├', z, i+1)
i += 1
- self.print_tree(parts[i], prefix + '└', z, i+1)
+ msg = parts[i]
+ if isinstance(msg, Message):
+ self.print_tree(msg, prefix + '└', z, i+1)
# FIXME: show epilogue?
else:
self.print_part(z, prefix+'─╴', parent, num)
diff --git a/imap-dl b/imap-dl
index fac7487..824c21d 100755
--- a/imap-dl
+++ b/imap-dl
@@ -2,7 +2,7 @@
# PYTHON_ARGCOMPLETE_OK
# -*- coding: utf-8 -*-
-# Copyright (C) 2019-2020 Daniel Kahn Gillmor
+# Copyright (C) 2019-2024 Daniel Kahn Gillmor
# Copyright (C) 2020 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or modify
@@ -52,14 +52,17 @@ import statistics
import configparser
from typing import Dict, List, Optional, Tuple, Union
+from types import ModuleType
+argcomplete:Optional[ModuleType]
try:
- import argcomplete #type: ignore
+ import argcomplete
except ImportError:
argcomplete = None
+gssapi:Optional[ModuleType]
try:
- import gssapi # type: ignore
+ import gssapi
except ModuleNotFoundError:
gssapi = None
@@ -96,15 +99,16 @@ def auth_builtin(username:str, imap:imaplib.IMAP4,
except Exception as e:
raise Exception(f'login failed with {e} as user {username} on {server}')
-if gssapi:
+if gssapi is not None:
# imaplib auth methods need to be in the form of callables, and they all
# requre both additional parameters and storage beyond what the function
# interface provides.
class GSSAPI_handler():
- gss_vc:gssapi.SecurityContext
username:str
def __init__(self, server:str, username:str) -> None:
+ if gssapi is None:
+ raise Exception("Impossible state -- gssapi module is not loaded")
name = gssapi.Name(f'imap@{server}',
gssapi.NameType.hostbased_service)
self.gss_vc = gssapi.SecurityContext(usage="initiate", name=name)