Skip to content

Commit 7db680a

Browse files
committed
Handle some edge cases.
1 parent 8a4c7d0 commit 7db680a

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

extract.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ def process_zip_file(filename, dest):
1414
if len(f.namelist()) != 1:
1515
raise Exception("ZIP archive '{}' has not precisely one file!".format(filename))
1616
xmlfile = f.namelist()[0]
17+
if '/' in xmlfile:
18+
xmlfile = xmlfile.rsplit('/', 1)[1]
1719
if '/' in xmlfile or '\\' in xmlfile:
1820
raise Exception("ZIP archive '{}' contains a file '{}' in a subfolder!".format(filename, xmlfile))
1921
fn, ext = os.path.splitext(xmlfile)
2022
if ext != '.xml':
2123
raise Exception("ZIP archive '{}' contains a non-XML file '{}'!".format(filename, xmlfile))
2224
bn = os.path.splitext(os.path.basename(real_filename))[0]
23-
if fn != bn:
24-
raise Exception("ZIP archive '{}' contains a XML file called '{}', but it should contain one called '{}'!".format(filename, fn, bn))
25+
bns = [bn, os.path.splitext(bn)[0]]
26+
if fn not in bns:
27+
raise Exception("ZIP archive '{}' contains a XML file called '{}', but it should contain one called {}!".format(filename, fn, ' or '.join(["'{}'".format(bn) for bn in bns])))
2528
new_filename = '{}-{}'.format(message_id, xmlfile)
2629
if os.path.exists(os.path.join(dest, new_filename)):
2730
raise Exception("Destination file '{}' already exists!".format(new_filename))

fetch.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
# Found attachment. Write to disk.
4646
filename = '{0}-{1}'.format(message_id, part.get_filename())
4747
if os.path.exists(filename):
48-
print('{0} already exists!'.format(filename))
48+
print(' WARNING: {0} already exists!'.format(filename))
4949
continue
5050
with open(filename, 'wb') as f:
5151
f.write(part.get_content())
@@ -54,3 +54,5 @@
5454
# On success, mark email as read
5555
if success:
5656
client.add_flags(message_id, imapclient.SEEN, silent=True)
57+
else:
58+
print(' WARNING: cound not extract attachment!')

0 commit comments

Comments
 (0)