From 13cba7ad0bfcbb8eacfffaf1913d11897c8f91c2 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 26 Jan 2021 11:38:03 +0100 Subject: 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. --- alot/addressbook/external.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'alot/addressbook/external.py') 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 # 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: -- cgit v1.2.3