Skip to content

Commit

Permalink
wip on vsajip#246
Browse files Browse the repository at this point in the history
  • Loading branch information
vergenzt committed Jan 27, 2025
1 parent 9265f6c commit 64ae800
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions gnupg.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from io import StringIO
import logging
import os
from queue import Queue
import re
import socket
from subprocess import Popen, PIPE
Expand Down Expand Up @@ -2214,6 +2215,40 @@ def decrypt_file(self, fileobj_or_path, always_trust=False, passphrase=None, out
# logger.debug('decrypt result[:100]: %r', result.data[:100])
return result

def decrypt_file_iter(self, fileobj_or_path, **kwargs):
orig_on_data = self.on_data
chunks_queue = Queue(maxsize=1)
def _on_data(chunk):
chunks_queue.put(chunk)
return orig_on_data(chunk) if orig_on_data else False

self.on_data = _on_data
try:

result_queue = Queue(maxsize=1)
def decrypt_in_thread():
try:
result = self.decrypt_file(fileobj_or_path, **kwargs)
result_queue.put(result)
except Exception as exc:
result_queue.put(exc)

decrypt_thread = threading.Thread(target=decrypt_in_thread)
decrypt_thread.start()

while result_queue.empty():
yield chunks_queue.get()

decrypt_thread.join()
result = result_queue.get()
if isinstance(result, Exception):
raise result
else:
return result

finally:
self.on_data = orig_on_data

def get_recipients(self, message, **kwargs):
""" Get the list of recipients for an encrypted message. This method delegates most of the work to
`get_recipients_file()`.
Expand Down

0 comments on commit 64ae800

Please sign in to comment.