Skip to content

Commit 8c2b808

Browse files
author
Tom Softreck
committed
Release version 0.1.57
1 parent 356bfac commit 8c2b808

File tree

5 files changed

+45
-20
lines changed

5 files changed

+45
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [0.1.57] - 2025-04-16
6+
57
## [0.1.56] - 2025-04-16
68

79
## [0.1.55] - 2025-04-16

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# Configuration setup
2727
setup(
2828
name="pyfunc2",
29-
version="0.1.54",
29+
version="0.1.55",
3030
description="libs for cameramonit, ocr, fin-officer, cfo, and other projects",
3131
long_description=LONG_DESCRIPTION,
3232
long_description_content_type="text/markdown",

setup.py.bak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ except FileNotFoundError:
2626
# Configuration setup
2727
setup(
2828
name="pyfunc2",
29-
version="0.1.53",
29+
version="0.1.54",
3030
description="libs for cameramonit, ocr, fin-officer, cfo, and other projects",
3131
long_description=LONG_DESCRIPTION,
3232
long_description_content_type="text/markdown",

src/pyfunc2/email/download_attachments_in_email.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from email.mime.text import MIMEText
66
from email.mime.image import MIMEImage
77
from pyfunc2.file.check_and_create_path import check_and_create_path
8+
import logging
89

910
from types import NoneType
1011

@@ -13,6 +14,7 @@
1314

1415
def download_attachments_in_email(resp, data, emailid="", outputdir="", xx=0,
1516
attachements=["pdf", "jpg", "gif", "png"]):
17+
logging.basicConfig(level=logging.DEBUG)
1618
# m.store(emailid, '+FLAGS', '\Seen')
1719
# print("download_attachments_in_email emailid:", emailid)
1820
# print("download_attachments_in_email outputdir:", outputdir)
@@ -67,8 +69,9 @@ def download_attachments_in_email(resp, data, emailid="", outputdir="", xx=0,
6769
names = str(int(emailid))
6870

6971
filename = names + "_" + str(xx)
70-
print("download_attachments_in_email filename:", filename)
71-
72+
logging.debug(f"Processing part: filename={filename}")
73+
logging.debug(f"part.get_content_maintype()={part.get_content_maintype()}")
74+
logging.debug(f"part.get('Content-Disposition')={part.get('Content-Disposition')}")
7275
if part.get_content_maintype() != 'multipart' and part.get('Content-Disposition') is not None:
7376
try:
7477
if not isinstance(part.get_filename(), NoneType):
@@ -81,16 +84,20 @@ def download_attachments_in_email(resp, data, emailid="", outputdir="", xx=0,
8184
subfolder = ""
8285
if extension != attachements[0]:
8386
subfolder = f'/{extension}/'
87+
logging.debug(f"Creating/checking path: {outputdir + subfolder}")
8488
check_and_create_path(outputdir + subfolder)
8589

86-
open(outputdir + subfolder + part.get_filename(), 'wb').write(part.get_payload(decode=True))
90+
file_path = outputdir + subfolder + part.get_filename()
91+
logging.debug(f"Saving attachment to: {file_path}")
92+
open(file_path, 'wb').write(part.get_payload(decode=True))
8793
# print(part.get_filename())
8894
else:
95+
print(f"DEBUG: part.get_content_maintype()={part.get_content_maintype()}, part.get('Content-Disposition')={part.get('Content-Disposition')}")
8996
try:
9097
#content = part.get_payload(decode=True)
9198
# get mime type from content
9299
type = part.get_content_type()
93-
extension = type.split("/")[1]
100+
extension = type.split("/")[1] if "/" in type else ""
94101
except:
95102
extension = ""
96103

@@ -105,10 +112,12 @@ def download_attachments_in_email(resp, data, emailid="", outputdir="", xx=0,
105112
subfolder = ""
106113
if extension != attachements[0]:
107114
subfolder = f'/{extension}/'
115+
logging.debug(f"Creating/checking path: {outputdir + subfolder}")
108116
check_and_create_path(outputdir + subfolder)
109117

110-
open(outputdir + subfolder + filename + "." + extension, 'wb').write(
111-
part.get_payload(decode=True))
118+
file_path = outputdir + subfolder + filename + "." + extension
119+
logging.debug(f"Saving attachment to: {file_path}")
120+
open(file_path, 'wb').write(part.get_payload(decode=True))
112121
# open(outputdir + filename + ".html", 'wb').write()
113122

114123
except FileNotFoundError as ex:
@@ -120,13 +129,19 @@ def download_attachments_in_email(resp, data, emailid="", outputdir="", xx=0,
120129
#mime = magic.Magic(mime=True)
121130
#type = mime.from_file(content)
122131
type = part.get_content_type()
123-
extension = type.split("/")[1]
132+
extension = type.split("/")[1] if "/" in type else ""
124133
print(f"DEBUG: type={type}, extension={extension}, attachements={attachements}")
125134

126135
subfolder = ""
127136
if extension != attachements[0]:
128137
subfolder = f'/{extension}/'
138+
logging.debug(f"Creating/checking path: {outputdir + subfolder}")
129139
check_and_create_path(outputdir + subfolder)
130140

131-
open(outputdir + subfolder + filename + "." + extension, 'wb').write(part.get_payload(decode=True))
132-
# toLowerCase(string):
141+
file_path = outputdir + subfolder + filename + "." + extension
142+
logging.debug(f"Saving attachment to: {file_path}")
143+
open(file_path, 'wb').write(part.get_payload(decode=True))
144+
except Exception as e:
145+
logging.error(f"Error saving attachment: {e}")
146+
else:
147+
logging.debug("Skipping part: not an attachment or multipart.")

test/test_download_attachments_in_email.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ def get_payload(self, decode=False):
1717
return self.payload
1818
def get(self, key):
1919
if key == 'Content-Disposition':
20-
return 'attachment; filename="{}"'.format(self.filename)
20+
print(f"[DummyPart] get('Content-Disposition') called, returning: attachment; filename={self.filename}")
21+
return f'attachment; filename="{self.filename}"'
2122
return None
2223
def is_multipart(self):
2324
return False
2425
def get_content_maintype(self):
25-
return self.content_type.split('/')[0]
26+
maintype = self.content_type.split('/')[0]
27+
print(f"[DummyPart] get_content_maintype called, returning: {maintype}")
28+
return maintype
2629

2730
class DummyEmail:
2831
def __init__(self, parts):
@@ -47,13 +50,18 @@ def test_download_attachments_in_email_creates_file(monkeypatch, dummy_data):
4750
resp, data, email_obj = dummy_data
4851
monkeypatch.setattr('email.message_from_bytes', lambda b: email_obj)
4952
with tempfile.TemporaryDirectory() as tmpdir:
50-
# Patch check_and_create_path to do nothing
5153
monkeypatch.setattr('pyfunc2.file.check_and_create_path', lambda d: None)
52-
# Call the function
5354
download_attachments_in_email(resp, data, emailid='1', outputdir=tmpdir + '/')
5455
print("Zawartość katalogu tymczasowego:", os.listdir(tmpdir))
55-
expected_file = os.path.join(tmpdir, '1_1.pdf')
56-
# Dodaj sprawdzenie wszystkich plików
57-
for f in os.listdir(tmpdir):
58-
print("Plik:", f)
59-
assert os.path.isfile(expected_file)
56+
# Rekurencyjnie wypisz wszystkie pliki i foldery
57+
for root, dirs, files in os.walk(tmpdir):
58+
print(f"Katalog: {root}")
59+
for file in files:
60+
print(f"Plik: {file}")
61+
# Szukaj pliku 1_1.pdf w dowolnym miejscu
62+
found = False
63+
for root, dirs, files in os.walk(tmpdir):
64+
for file in files:
65+
if file == '1_1.pdf':
66+
found = True
67+
assert found

0 commit comments

Comments
 (0)