# Path: pyani/download.py
# TAXONREGEX = re.compile(r"([0-9]\,?){1,}")
# class NCBIDownloadException(Exception):
# class FileExistsException(Exception):
# class PyaniIndexException(Exception):
# class ASMIDs(NamedTuple):
# class Classification(NamedTuple):
# class DLFileData(NamedTuple):
# class Hashstatus(NamedTuple):
# class DLStatus:
#     def __init__(self, msg: str = "Error downloading file from NCBI"):
#     def __init__(self, msg: str = "Specified file exists"):
#     def __init__(
#         self,
#         url: str,
#         hashurl: str,
#         outfname: Path,
#         outfhash: Path,
#         skipped: bool,
#         error: Optional[str] = None,
#     ):
# def last_exception() -> str:
# def make_asm_dict(taxon_ids: List[str], retries: int) -> Dict:
# def set_ncbi_email(email: str) -> None:
# def download_genome_and_hash(
#     outdir: Path,
#     timeout: int,
#     dlfiledata: DLFileData,
#     dltype: str = "RefSeq",
#     disable_tqdm: bool = False,
# ) -> namedlist:
# def entrez_retry(func):
#     def wrapper(*args, retries=1, **kwargs):
# def entrez_batch(func):
#     def wrapper(*args, expected=None, batchsize=None, **kwargs):
# def entrez_batched_webhistory(*args, **kwargs):
# def entrez_esearch(*args, **kwargs):
# def entrez_esummary(*args, **kwargs):
# def split_taxa(taxa: str) -> List[str]:
# def get_asm_uids(taxon_uid: str, retries: int) -> ASMIDs:
# def extract_filestem(esummary) -> str:
# def get_ncbi_esummary(asm_uid, retries, api_key=None) -> Tuple:
# def get_ncbi_classification(esummary) -> Classification:
# def compile_url(filestem: str, suffix: str, ftpstem: str) -> Tuple[str, str]:
# def download_url(
#     url: str, outfname: Path, timeout: int, disable_tqdm: bool = False
# ) -> None:
# def construct_output_paths(
#     filestem: str, suffix: str, outdir: Path
# ) -> Tuple[Path, Path]:
# def retrieve_genome_and_hash(
#     filestem: str,
#     suffix: str,
#     ftpstem: str,
#     outdir: Path,
#     timeout: int,
#     disable_tqdm: bool = False,
# ) -> DLStatus:
# def check_hash(fname: Path, hashfile: Path) -> Hashstatus:
# def extract_contigs(fname: Path, ename: Path) -> CompletedProcess:
# def create_labels(
#     classification: Classification, filestem: str, genomehash: str
# ) -> Tuple[str, str]:
# def create_hash(fname: Path) -> str:
# def extract_hash(hashfile: Path, name: str) -> str:
# 
# Path: pyani/download.py
# class ASMIDs(NamedTuple):
# 
#     """Matching Assembly ID information for a query taxID."""
# 
#     query: str
#     result_count: int
#     asm_ids: List[str]
# 
# class DLStatus:
# 
#     """Download status data."""
# 
#     def __init__(
#         self,
#         url: str,
#         hashurl: str,
#         outfname: Path,
#         outfhash: Path,
#         skipped: bool,
#         error: Optional[str] = None,
#     ):
#         self.url = url
#         self.hashurl = hashurl
#         self.outfname = outfname
#         self.outfhash = outfhash
#         self.skipped = skipped
#         self.error = error
# 
# Path: pyani/pyani_config.py
# BLASTALL_DEFAULT = Path("blastall")
# 
# BLASTN_DEFAULT = Path("blastn")
# 
# NUCMER_DEFAULT = Path("nucmer")
# 
# FRAGSIZE = 1020  # Default ANIb fragment size
# 
# Path: pyani/scripts/genbank_get_genomes_by_taxon.py
# class NCBIDownloadException(Exception):
#     def __init__(self):
# def parse_cmdline(argv=None):
# def last_exception():
# def set_ncbi_email(args: Namespace) -> None:
# def make_outdir(args: Namespace) -> None:
# def entrez_retry(args, func, *fnargs, **fnkwargs):
# def entrez_batch_webhistory(args, record, expected, batchsize, *fnargs, **fnkwargs):
# def get_asm_uids(args, taxon_uid):
# def extract_filestem(data):
# def get_ncbi_asm(args, asm_uid, fmt="fasta"):
# def retrieve_asm_contigs(
#     args,
#     filestem,
#     ftpstem="ftp://ftp.ncbi.nlm.nih.gov/genomes/all",
#     fmt="fasta",
# ):
# def extract_archive(archivepath):
# def write_contigs(args, asm_uid, contig_uids, batchsize=10000):
# def logreport_downloaded(accn, skiplist, accndict, uidaccndict):
# def run_main(args=None):


import subprocess
import shutil
import os
import re
import platform
import pytest
from pathlib import Path
from typing import NamedTuple
from pyani import download
from pyani.download import ASMIDs, DLStatus
from pyani.pyani_config import (
    BLASTALL_DEFAULT,
    BLASTN_DEFAULT,
    NUCMER_DEFAULT,
    FRAGSIZE,
)
from pyani.scripts import genbank_get_genomes_by_taxon
# This conftest.py file should be found in the top directory of the tests
# module. The fixture data should be in a subdirectory named fixtures
TESTSPATH = Path(__file__).parents[0]
FIXTUREPATH = TESTSPATH / "fixtures"


# Convenience structs to emulate returned objects
class MockGenome(NamedTuple):
    """Mock genome object."""

    path: str


class MockProcess(NamedTuple):
    """Mock process object."""

    stdout: str
    stderr: str


class MockMatch(NamedTuple):
    """Mock match object."""

    def group(self):
        return ""


@pytest.fixture
def blastall_available():
    """Returns True if blastall can be run, False otherwise."""

# Path: pyani/download.py
# TAXONREGEX = re.compile(r"([0-9]\,?){1,}")
# class NCBIDownloadException(Exception):
# class FileExistsException(Exception):
# class PyaniIndexException(Exception):
# class ASMIDs(NamedTuple):
# class Classification(NamedTuple):
# class DLFileData(NamedTuple):
# class Hashstatus(NamedTuple):
# class DLStatus:
#     def __init__(self, msg: str = "Error downloading file from NCBI"):
#     def __init__(self, msg: str = "Specified file exists"):
#     def __init__(
#         self,
#         url: str,
#         hashurl: str,
#         outfname: Path,
#         outfhash: Path,
#         skipped: bool,
#         error: Optional[str] = None,
#     ):
# def last_exception() -> str:
# def make_asm_dict(taxon_ids: List[str], retries: int) -> Dict:
# def set_ncbi_email(email: str) -> None:
# def download_genome_and_hash(
#     outdir: Path,
#     timeout: int,
#     dlfiledata: DLFileData,
#     dltype: str = "RefSeq",
#     disable_tqdm: bool = False,
# ) -> namedlist:
# def entrez_retry(func):
#     def wrapper(*args, retries=1, **kwargs):
# def entrez_batch(func):
#     def wrapper(*args, expected=None, batchsize=None, **kwargs):
# def entrez_batched_webhistory(*args, **kwargs):
# def entrez_esearch(*args, **kwargs):
# def entrez_esummary(*args, **kwargs):
# def split_taxa(taxa: str) -> List[str]:
# def get_asm_uids(taxon_uid: str, retries: int) -> ASMIDs:
# def extract_filestem(esummary) -> str:
# def get_ncbi_esummary(asm_uid, retries, api_key=None) -> Tuple:
# def get_ncbi_classification(esummary) -> Classification:
# def compile_url(filestem: str, suffix: str, ftpstem: str) -> Tuple[str, str]:
# def download_url(
#     url: str, outfname: Path, timeout: int, disable_tqdm: bool = False
# ) -> None:
# def construct_output_paths(
#     filestem: str, suffix: str, outdir: Path
# ) -> Tuple[Path, Path]:
# def retrieve_genome_and_hash(
#     filestem: str,
#     suffix: str,
#     ftpstem: str,
#     outdir: Path,
#     timeout: int,
#     disable_tqdm: bool = False,
# ) -> DLStatus:
# def check_hash(fname: Path, hashfile: Path) -> Hashstatus:
# def extract_contigs(fname: Path, ename: Path) -> CompletedProcess:
# def create_labels(
#     classification: Classification, filestem: str, genomehash: str
# ) -> Tuple[str, str]:
# def create_hash(fname: Path) -> str:
# def extract_hash(hashfile: Path, name: str) -> str:
# 
# Path: pyani/download.py
# class ASMIDs(NamedTuple):
# 
#     """Matching Assembly ID information for a query taxID."""
# 
#     query: str
#     result_count: int
#     asm_ids: List[str]
# 
# class DLStatus:
# 
#     """Download status data."""
# 
#     def __init__(
#         self,
#         url: str,
#         hashurl: str,
#         outfname: Path,
#         outfhash: Path,
#         skipped: bool,
#         error: Optional[str] = None,
#     ):
#         self.url = url
#         self.hashurl = hashurl
#         self.outfname = outfname
#         self.outfhash = outfhash
#         self.skipped = skipped
#         self.error = error
# 
# Path: pyani/pyani_config.py
# BLASTALL_DEFAULT = Path("blastall")
# 
# BLASTN_DEFAULT = Path("blastn")
# 
# NUCMER_DEFAULT = Path("nucmer")
# 
# FRAGSIZE = 1020  # Default ANIb fragment size
# 
# Path: pyani/scripts/genbank_get_genomes_by_taxon.py
# class NCBIDownloadException(Exception):
#     def __init__(self):
# def parse_cmdline(argv=None):
# def last_exception():
# def set_ncbi_email(args: Namespace) -> None:
# def make_outdir(args: Namespace) -> None:
# def entrez_retry(args, func, *fnargs, **fnkwargs):
# def entrez_batch_webhistory(args, record, expected, batchsize, *fnargs, **fnkwargs):
# def get_asm_uids(args, taxon_uid):
# def extract_filestem(data):
# def get_ncbi_asm(args, asm_uid, fmt="fasta"):
# def retrieve_asm_contigs(
#     args,
#     filestem,
#     ftpstem="ftp://ftp.ncbi.nlm.nih.gov/genomes/all",
#     fmt="fasta",
# ):
# def extract_archive(archivepath):
# def write_contigs(args, asm_uid, contig_uids, batchsize=10000):
# def logreport_downloaded(accn, skiplist, accndict, uidaccndict):
# def run_main(args=None):


# Path: tests/conftest.py
import subprocess
import shutil
import os
import re
import platform
import pytest
from pathlib import Path
from typing import NamedTuple
from pyani import download
from pyani.download import ASMIDs, DLStatus
from pyani.pyani_config import (
    BLASTALL_DEFAULT,
    BLASTN_DEFAULT,
    NUCMER_DEFAULT,
    FRAGSIZE,
)
from pyani.scripts import genbank_get_genomes_by_taxon

# This conftest.py file should be found in the top directory of the tests
# module. The fixture data should be in a subdirectory named fixtures
TESTSPATH = Path(__file__).parents[0]
FIXTUREPATH = TESTSPATH / "fixtures"


# Convenience structs to emulate returned objects
class MockGenome(NamedTuple):
    """Mock genome object."""

    path: str


class MockProcess(NamedTuple):
    """Mock process object."""

    stdout: str
    stderr: str


class MockMatch(NamedTuple):
    """Mock match object."""

    def group(self):
        return ""


@pytest.fixture
def blastall_available():
    """Returns True if blastall can be run, False otherwise."""

    cmd = str(BLASTALL_DEFAULT)