# Path: augur/util_support/color_parser.py
# class ColorParser:
#     def __init__(self, *, mapping_filename, use_defaults=True):
#         self.mapping_filename = mapping_filename
#         self.use_defaults = use_defaults
# 
#     @property
#     @functools.lru_cache()
#     def mapping(self):
#         colors = {}
# 
#         if self.use_defaults:
#             with resource_stream("augur", "data/colors.tsv") as stream:
#                 with TextIOWrapper(stream, "utf-8") as defaults:
#                     colors = {**colors, **self.parse_file(defaults)}
# 
#         if self.mapping_filename:
#             with open(self.mapping_filename, encoding="utf-8") as mapping:
#                 colors = {**colors, **self.parse_file(mapping)}
# 
#         return colors
# 
#     def parse_file(self, file):
#         file_mapping = defaultdict(list)
#         for pair in [ColorParserLine(line).pair() for line in file]:
#             if pair is None:
#                 continue
# 
#             file_mapping[pair[0]].append(pair[1])
# 
#         return file_mapping


from augur.util_support.color_parser import ColorParser
import pytest



@pytest.fixture()
def prepare_file(tmpdir):
    def _prepare_file(contents):
        with open(f"{tmpdir}/colors.tsv", "w") as file:
            for line in contents.split("\n"):
                file.write(line.strip() + "\n")

    return _prepare_file


class TestColorParser:
    def test_color_file_not_found(self):
        with pytest.raises(FileNotFoundError):
            ColorParser(mapping_filename="/does/not/exist.txt").mapping

    def test_color_file_no_defaults(self, tmpdir, prepare_file):
        prepare_file(
            """
            strain\tstrainA\t#ff0000
            strain\tstrainB\t#00ff00
            strain\tstrainC\t#0000ff
            """
        )


# Path: augur/util_support/color_parser.py
# class ColorParser:
#     def __init__(self, *, mapping_filename, use_defaults=True):
#         self.mapping_filename = mapping_filename
#         self.use_defaults = use_defaults
# 
#     @property
#     @functools.lru_cache()
#     def mapping(self):
#         colors = {}
# 
#         if self.use_defaults:
#             with resource_stream("augur", "data/colors.tsv") as stream:
#                 with TextIOWrapper(stream, "utf-8") as defaults:
#                     colors = {**colors, **self.parse_file(defaults)}
# 
#         if self.mapping_filename:
#             with open(self.mapping_filename, encoding="utf-8") as mapping:
#                 colors = {**colors, **self.parse_file(mapping)}
# 
#         return colors
# 
#     def parse_file(self, file):
#         file_mapping = defaultdict(list)
#         for pair in [ColorParserLine(line).pair() for line in file]:
#             if pair is None:
#                 continue
# 
#             file_mapping[pair[0]].append(pair[1])
# 
#         return file_mapping


# Path: tests/util_support/test_color_parser.py
from augur.util_support.color_parser import ColorParser
import pytest




@pytest.fixture()
def prepare_file(tmpdir):
    def _prepare_file(contents):
        with open(f"{tmpdir}/colors.tsv", "w") as file:
            for line in contents.split("\n"):
                file.write(line.strip() + "\n")

    return _prepare_file


class TestColorParser:
    def test_color_file_not_found(self):
        with pytest.raises(FileNotFoundError):
            ColorParser(mapping_filename="/does/not/exist.txt").mapping

    def test_color_file_no_defaults(self, tmpdir, prepare_file):
        prepare_file(
            """
            strain\tstrainA\t#ff0000
            strain\tstrainB\t#00ff00
            strain\tstrainC\t#0000ff
            """
        )


        assert ColorParser(