diff options
author | Anton Khirnov <anton@khirnov.net> | 2021-01-26 11:38:03 +0100 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2021-01-26 11:57:08 +0100 |
commit | 13cba7ad0bfcbb8eacfffaf1913d11897c8f91c2 (patch) | |
tree | 9bfb117a8b4f1d9d1b1eff9fc1b0fb07006793d6 /alot/addressbook/external.py | |
parent | 0e7103d9e388c684097ea5bc998e598903889ef6 (diff) |
addressbook/external: stop using call_cmd()
It does not actually save any code. The new code also uses a shell as is
documented.
Remove call_cmd(), as it no longer has any callers.
Diffstat (limited to 'alot/addressbook/external.py')
-rw-r--r-- | alot/addressbook/external.py | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/alot/addressbook/external.py b/alot/addressbook/external.py index cd300a42..c01a525e 100644 --- a/alot/addressbook/external.py +++ b/alot/addressbook/external.py @@ -1,13 +1,14 @@ # Copyright (C) 2011-2015 Patrick Totzke <patricktotzke@gmail.com> # This file is released under the GNU GPL, version 3 or a later revision. # For further details see the COPYING file -import re -from ..helper import call_cmd -from ..helper import split_commandstring -from . import AddressBook, AddressbookError import logging +import re +import subprocess +from urwid.util import detected_encoding + +from . import AddressBook, AddressbookError class ExternalAddressbook(AddressBook): """:class:`AddressBook` that parses a shell command's output""" @@ -49,20 +50,24 @@ class ExternalAddressbook(AddressBook): else: return AddressBook.lookup(self, prefix) - def _call_and_parse(self, commandline): - cmdlist = split_commandstring(commandline) - resultstring, errmsg, retval = call_cmd(cmdlist) - if retval != 0: - msg = 'abook command "%s" returned with ' % commandline - msg += 'return code %d' % retval - if errmsg: - msg += ':\n%s' % errmsg + def _call_and_parse(self, cmd): + try: + result = subprocess.run(cmd, shell = True, check = True, + capture_output = True, + stdin = subprocess.DEVNULL) + except subprocess.CalledProcessError as e: + msg = 'abook command "%s" failed with code %s' % (e.cmd, e.returncode) + if e.stderr: + msg += '; stderr:\n%s' % \ + e.stderr.decode(detected_encoding, errors = 'backslashreplace') raise AddressbookError(msg) - if not resultstring: + output = result.stdout.decode(detected_encoding, errors = 'backslashreplace') + if not output: logging.debug("No contacts in address book (empty string)") return [] - lines = resultstring.splitlines() + + lines = output.splitlines() res = [] logging.debug("Apply %s on %d results" % (self.regex, len(lines))) for l in lines: |