# Path: pyani/pyani_tools.py
# def termcolor(
#     logstr: str, color: Optional[str] = None, bold: Optional[bool] = False
# ) -> str:
#     """Return the passed logstr, wrapped in terminal colouring."""
#     # For terminal colouring
#     termcolors = {
#         "BLACK": 0,
#         "RED": 1,
#         "GREEN": 2,
#         "YELLOW": 3,
#         "BLUE": 4,
#         "MAGENTA": 5,
#         "CYAN": 6,
#         "WHITE": 7,
#     }
#     reset = "\033[0m"
# 
#     # Colour the string
#     if isinstance(color, str) and color.upper() in termcolors:
#         logstr = f"\033[1;{30 + termcolors[color.upper()]}m{logstr}{reset}"
# 
#     # Make the string bold
#     if bold is True:
#         logstr = f"\033[1m{logstr}"
#         if not logstr.endswith(reset):
#             logstr += reset
# 
#     return logstr


import hashlib
import logging
import re
import shlex
import subprocess
import os
import sys
import traceback
import urllib.request
from namedlist import namedlist
from pathlib import Path
from subprocess import CompletedProcess
from typing import Any, Dict, List, NamedTuple, Optional, Tuple
from urllib.error import HTTPError, URLError
from Bio import Entrez  # type: ignore
from tqdm import tqdm  # type: ignore
from pyani.pyani_tools import termcolor
    dlfiledata: DLFileData,
    dltype: str = "RefSeq",
    disable_tqdm: bool = False,
) -> namedlist:
    """Download genome and accompanying MD5 hash from NCBI.

    :param args:  Namespace for command-line arguments
    :param outdir:  Path to output directory for downloads
    :param timeout:  int: timeout for download attempt
    :param dlfiledata:  namedtuple of info for file to download
    :param dltype:  reference database to use: RefSeq or GenBank
    :param disable_tqdm:  disable progress bar

    This function tries the (assumed to be passed) RefSeq FTP URL first and,
    if that fails, then attempts to download the corresponding GenBank data.

    We attempt to gracefully skip genomes with download errors.
    """
    # Create logger
    logger = logging.getLogger(__name__)

    if dltype == "GenBank":
        filestem = re.sub("^GCF_", "GCA_", dlfiledata.filestem)
    else:
        filestem = dlfiledata.filestem
    dlstatus = retrieve_genome_and_hash(
        filestem, dlfiledata.suffix, dlfiledata.ftpstem, outdir, timeout, disable_tqdm
    )
    # Pylint is confused by the content of dlstatus (a namedlist)
    if dlstatus.error is not None:  # pylint: disable=no-member

# Path: pyani/pyani_tools.py
# def termcolor(
#     logstr: str, color: Optional[str] = None, bold: Optional[bool] = False
# ) -> str:
#     """Return the passed logstr, wrapped in terminal colouring."""
#     # For terminal colouring
#     termcolors = {
#         "BLACK": 0,
#         "RED": 1,
#         "GREEN": 2,
#         "YELLOW": 3,
#         "BLUE": 4,
#         "MAGENTA": 5,
#         "CYAN": 6,
#         "WHITE": 7,
#     }
#     reset = "\033[0m"
# 
#     # Colour the string
#     if isinstance(color, str) and color.upper() in termcolors:
#         logstr = f"\033[1;{30 + termcolors[color.upper()]}m{logstr}{reset}"
# 
#     # Make the string bold
#     if bold is True:
#         logstr = f"\033[1m{logstr}"
#         if not logstr.endswith(reset):
#             logstr += reset
# 
#     return logstr


# Path: pyani/download.py
import hashlib
import logging
import re
import shlex
import subprocess
import os
import sys
import traceback
import urllib.request
from namedlist import namedlist
from pathlib import Path
from subprocess import CompletedProcess
from typing import Any, Dict, List, NamedTuple, Optional, Tuple
from urllib.error import HTTPError, URLError
from Bio import Entrez  # type: ignore
from tqdm import tqdm  # type: ignore
from pyani.pyani_tools import termcolor

    dlfiledata: DLFileData,
    dltype: str = "RefSeq",
    disable_tqdm: bool = False,
) -> namedlist:
    """Download genome and accompanying MD5 hash from NCBI.

    :param args:  Namespace for command-line arguments
    :param outdir:  Path to output directory for downloads
    :param timeout:  int: timeout for download attempt
    :param dlfiledata:  namedtuple of info for file to download
    :param dltype:  reference database to use: RefSeq or GenBank
    :param disable_tqdm:  disable progress bar

    This function tries the (assumed to be passed) RefSeq FTP URL first and,
    if that fails, then attempts to download the corresponding GenBank data.

    We attempt to gracefully skip genomes with download errors.
    """
    # Create logger
    logger = logging.getLogger(__name__)

    if dltype == "GenBank":
        filestem = re.sub("^GCF_", "GCA_", dlfiledata.filestem)
    else:
        filestem = dlfiledata.filestem
    dlstatus = retrieve_genome_and_hash(
        filestem, dlfiledata.suffix, dlfiledata.ftpstem, outdir, timeout, disable_tqdm
    )
    # Pylint is confused by the content of dlstatus (a namedlist)
    if dlstatus.error is not None:  # pylint: disable=no-member

        logger.warning(termcolor("%s download failed: skipping!", "magenta"), dltype)