summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2016-12-18 10:40:06 +0000
committerGitHub <noreply@github.com>2016-12-18 10:40:06 +0000
commitc3a6cac24393a205ba20bef012a1f942c4ed99be (patch)
tree909d09e9d4784b184586750baa61c1b7f4d3c835
parentac7cfa19fd2b23e4d7ca8098661ca637bc4e0587 (diff)
parentf8635602757502fe27d63794c3a3511fc312f9ad (diff)
Merge pull request #929 from lucc/with-block-temp-files
Use with blocks to write to temp files
-rw-r--r--alot/commands/envelope.py11
-rw-r--r--alot/commands/thread.py11
-rw-r--r--alot/db/utils.py12
3 files changed, 14 insertions, 20 deletions
diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py
index 52ce5be9..c9d05b0d 100644
--- a/alot/commands/envelope.py
+++ b/alot/commands/envelope.py
@@ -375,12 +375,11 @@ class EditCommand(Command):
old_tmpfile = None
if self.envelope.tmpfile:
old_tmpfile = self.envelope.tmpfile
- self.envelope.tmpfile = tempfile.NamedTemporaryFile(delete=False,
- prefix='alot.',
- suffix='.eml')
- self.envelope.tmpfile.write(content.encode('utf-8'))
- self.envelope.tmpfile.flush()
- self.envelope.tmpfile.close()
+ with tempfile.NamedTemporaryFile(
+ delete=False, prefix='alot.', suffix='.eml') as tmpfile:
+ tmpfile.write(content.encode('utf-8'))
+ tmpfile.flush()
+ self.envelope.tmpfile = tmpfile
if old_tmpfile:
os.unlink(old_tmpfile.name)
cmd = globals.EditCommand(self.envelope.tmpfile.name,
diff --git a/alot/commands/thread.py b/alot/commands/thread.py
index 4b2221de..0c48c174 100644
--- a/alot/commands/thread.py
+++ b/alot/commands/thread.py
@@ -944,13 +944,10 @@ class OpenAttachmentCommand(Command):
filename = self.attachment.get_filename()
prefix, suffix = fn_hook(filename, prefix, suffix)
- tmpfile = tempfile.NamedTemporaryFile(delete=False,
- prefix=prefix,
- suffix=suffix)
-
- tempfile_name = tmpfile.name
- self.attachment.write(tmpfile)
- tmpfile.close()
+ with tempfile.NamedTemporaryFile(delete=False, prefix=prefix,
+ suffix=suffix) as tmpfile:
+ tempfile_name = tmpfile.name
+ self.attachment.write(tmpfile)
def afterwards():
os.unlink(tempfile_name)
diff --git a/alot/db/utils.py b/alot/db/utils.py
index 188a4b3a..ab20e26b 100644
--- a/alot/db/utils.py
+++ b/alot/db/utils.py
@@ -314,13 +314,11 @@ def extract_body(mail, types=None, field_key='copiousoutput'):
# open tempfile, respect mailcaps nametemplate
nametemplate = entry.get('nametemplate', '%s')
prefix, suffix = parse_mailcap_nametemplate(nametemplate)
- tmpfile = tempfile.NamedTemporaryFile(delete=False,
- prefix=prefix,
- suffix=suffix)
- # write payload to tmpfile
- tmpfile.write(raw_payload)
- tmpfile.close()
- tempfile_name = tmpfile.name
+ with tempfile.NamedTemporaryFile(
+ delete=False, prefix=prefix, suffix=suffix) \
+ as tmpfile:
+ tmpfile.write(raw_payload)
+ tempfile_name = tmpfile.name
else:
stdin = raw_payload