summaryrefslogtreecommitdiff
path: root/alot
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2018-07-27 09:43:24 -0700
committerPatrick Totzke <patricktotzke@gmail.com>2019-05-11 17:37:57 +0100
commit5898d6eed5441e3fb487e44ef9e5960879d6fcd0 (patch)
tree4c3ac53fdf777ec1a1fcf4025375e9ae7cd2a2d0 /alot
parent7d4301db08fb9576aacceb5a283cc68666b2b8db (diff)
commands/globals: split out the code for loading a template
Diffstat (limited to 'alot')
-rw-r--r--alot/commands/globals.py47
1 files changed, 25 insertions, 22 deletions
diff --git a/alot/commands/globals.py b/alot/commands/globals.py
index 391f0913..ebb7f578 100644
--- a/alot/commands/globals.py
+++ b/alot/commands/globals.py
@@ -782,6 +782,30 @@ class ComposeCommand(Command):
class ApplyError(Exception):
pass
+ def _get_template(self, ui):
+ # get location of tempsdir, containing msg templates
+ tempdir = settings.get('template_dir')
+
+ path = os.path.expanduser(self.template)
+ if not os.path.dirname(path): # use tempsdir
+ if not os.path.isdir(tempdir):
+ ui.notify('no templates directory: %s' % tempdir,
+ priority='error')
+ raise self.ApplyError()
+ path = os.path.join(tempdir, path)
+
+ if not os.path.isfile(path):
+ ui.notify('could not find template: %s' % path,
+ priority='error')
+ raise self.ApplyError()
+ try:
+ with open(path, 'rb') as f:
+ template = helper.try_decode(f.read())
+ self.envelope.parse_template(template)
+ except Exception as e:
+ ui.notify(str(e), priority='error')
+ raise self.ApplyError()
+
async def apply(self, ui):
try:
await self.__apply(ui)
@@ -799,28 +823,7 @@ class ComposeCommand(Command):
else:
self.envelope = Envelope()
if self.template is not None:
- # get location of tempsdir, containing msg templates
- tempdir = settings.get('template_dir')
-
- path = os.path.expanduser(self.template)
- if not os.path.dirname(path): # use tempsdir
- if not os.path.isdir(tempdir):
- ui.notify('no templates directory: %s' % tempdir,
- priority='error')
- return
- path = os.path.join(tempdir, path)
-
- if not os.path.isfile(path):
- ui.notify('could not find template: %s' % path,
- priority='error')
- return
- try:
- with open(path, 'rb') as f:
- template = helper.try_decode(f.read())
- self.envelope.parse_template(template)
- except Exception as e:
- ui.notify(str(e), priority='error')
- return
+ self._get_template(ui)
# set forced headers
for key, value in self.headers.items():