# Path: alot/helper.py
# def call_cmd(cmdlist, stdin=None):
#     """
#     get a shell commands output, error message and return value and immediately
#     return.
# 
#     .. warning::
# 
#         This returns with the first screen content for interactive commands.
# 
#     :param cmdlist: shellcommand to call, already splitted into a list accepted
#                     by :meth:`subprocess.Popen`
#     :type cmdlist: list of str
#     :param stdin: string to pipe to the process
#     :type stdin: str, bytes, or None
#     :return: triple of stdout, stderr, return value of the shell command
#     :rtype: str, str, int
#     """
#     termenc = urwid.util.detected_encoding
#     if isinstance(stdin, str):
#         stdin = stdin.encode(termenc)
#     try:
# 
#         logging.debug("Calling %s" % cmdlist)
#         proc = subprocess.Popen(
#             cmdlist,
#             stdout=subprocess.PIPE,
#             stderr=subprocess.PIPE,
#             stdin=subprocess.PIPE if stdin is not None else None)
#     except OSError as e:
#         out = b''
#         err = e.strerror
#         ret = e.errno
#     else:
#         out, err = proc.communicate(stdin)
#         ret = proc.returncode
# 
#     out = string_decode(out, termenc)
#     err = string_decode(err, termenc)
#     return out, err, ret
# 
# Path: alot/helper.py
# def split_commandstring(cmdstring):
#     """
#     split command string into a list of strings to pass on to subprocess.Popen
#     and the like. This simply calls shlex.split but works also with unicode
#     bytestrings.
#     """
#     assert isinstance(cmdstring, str)
#     return shlex.split(cmdstring)


import re
import logging
from ..helper import call_cmd
from ..helper import split_commandstring
from . import AddressBook, AddressbookError
                        `ignorecase` config option
                        (defaults to `re.IGNORECASE`)
                        Setting a value here will replace this.
        :type reflags: str
        :param external_filtering: if True the command is fired
                        with the given search string as parameter
                        and the result is not filtered further.
                        If set to False, the command is fired without
                        additional parameters and the result list is filtered
                        according to the search string.
        :type external_filtering: bool
        """
        AddressBook.__init__(self, **kwargs)
        self.commandline = commandline
        self.regex = regex
        if reflags:
            self.reflags = reflags
        self.external_filtering = external_filtering

    def get_contacts(self):
        return self._call_and_parse(self.commandline)

    def lookup(self, prefix):  # pragma: no cover
        if self.external_filtering:
            return self._call_and_parse(self.commandline + " " + prefix)
        else:
            return AddressBook.lookup(self, prefix)

    def _call_and_parse(self, commandline):
        cmdlist = split_commandstring(commandline)

# Path: alot/helper.py
# def call_cmd(cmdlist, stdin=None):
#     """
#     get a shell commands output, error message and return value and immediately
#     return.
# 
#     .. warning::
# 
#         This returns with the first screen content for interactive commands.
# 
#     :param cmdlist: shellcommand to call, already splitted into a list accepted
#                     by :meth:`subprocess.Popen`
#     :type cmdlist: list of str
#     :param stdin: string to pipe to the process
#     :type stdin: str, bytes, or None
#     :return: triple of stdout, stderr, return value of the shell command
#     :rtype: str, str, int
#     """
#     termenc = urwid.util.detected_encoding
#     if isinstance(stdin, str):
#         stdin = stdin.encode(termenc)
#     try:
# 
#         logging.debug("Calling %s" % cmdlist)
#         proc = subprocess.Popen(
#             cmdlist,
#             stdout=subprocess.PIPE,
#             stderr=subprocess.PIPE,
#             stdin=subprocess.PIPE if stdin is not None else None)
#     except OSError as e:
#         out = b''
#         err = e.strerror
#         ret = e.errno
#     else:
#         out, err = proc.communicate(stdin)
#         ret = proc.returncode
# 
#     out = string_decode(out, termenc)
#     err = string_decode(err, termenc)
#     return out, err, ret
# 
# Path: alot/helper.py
# def split_commandstring(cmdstring):
#     """
#     split command string into a list of strings to pass on to subprocess.Popen
#     and the like. This simply calls shlex.split but works also with unicode
#     bytestrings.
#     """
#     assert isinstance(cmdstring, str)
#     return shlex.split(cmdstring)


# Path: alot/addressbook/external.py
import re
import logging
from ..helper import call_cmd
from ..helper import split_commandstring
from . import AddressBook, AddressbookError

                        `ignorecase` config option
                        (defaults to `re.IGNORECASE`)
                        Setting a value here will replace this.
        :type reflags: str
        :param external_filtering: if True the command is fired
                        with the given search string as parameter
                        and the result is not filtered further.
                        If set to False, the command is fired without
                        additional parameters and the result list is filtered
                        according to the search string.
        :type external_filtering: bool
        """
        AddressBook.__init__(self, **kwargs)
        self.commandline = commandline
        self.regex = regex
        if reflags:
            self.reflags = reflags
        self.external_filtering = external_filtering

    def get_contacts(self):
        return self._call_and_parse(self.commandline)

    def lookup(self, prefix):  # pragma: no cover
        if self.external_filtering:
            return self._call_and_parse(self.commandline + " " + prefix)
        else:
            return AddressBook.lookup(self, prefix)

    def _call_and_parse(self, commandline):
        cmdlist = split_commandstring(commandline)

        resultstring, errmsg, retval = call_cmd(cmdlist)