# Path: augur/util_support/color_parser_line.py
# class ColorParserLine:
#     def __init__(self, line):
#         self.line = line
# 
#     def pair(self):
#         if self.is_comment_or_blank():
#             return None
# 
#         if len(self.fields) != 3:
#             print("WARNING: Color map file contains invalid line:", self.line)
#             return None
# 
#         if not self.hex_code.startswith("#") or len(self.hex_code) != 7:
#             print(
#                 "WARNING: Color map file contained this invalid hex code: ",
#                 self.hex_code,
#             )
#             return None
# 
#         return self.trait, (self.trait_value, self.hex_code)
# 
#     def is_comment_or_blank(self):
#         return bool(self.line.startswith("#") or re.match(r"^\s*$", self.line))
# 
#     @property
#     @functools.lru_cache()
#     def fields(self):
#         return self.line.strip().split("\t")
# 
#     @property
#     def trait(self):
#         return self.fields[0].lower()
# 
#     @property
#     def trait_value(self):
#         return self.fields[1].lower()
# 
#     @property
#     def hex_code(self):
#         return self.fields[2]


from collections import defaultdict
from io import TextIOWrapper
from pkg_resources import resource_stream
from augur.util_support.color_parser_line import ColorParserLine
import functools



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]:

# Path: augur/util_support/color_parser_line.py
# class ColorParserLine:
#     def __init__(self, line):
#         self.line = line
# 
#     def pair(self):
#         if self.is_comment_or_blank():
#             return None
# 
#         if len(self.fields) != 3:
#             print("WARNING: Color map file contains invalid line:", self.line)
#             return None
# 
#         if not self.hex_code.startswith("#") or len(self.hex_code) != 7:
#             print(
#                 "WARNING: Color map file contained this invalid hex code: ",
#                 self.hex_code,
#             )
#             return None
# 
#         return self.trait, (self.trait_value, self.hex_code)
# 
#     def is_comment_or_blank(self):
#         return bool(self.line.startswith("#") or re.match(r"^\s*$", self.line))
# 
#     @property
#     @functools.lru_cache()
#     def fields(self):
#         return self.line.strip().split("\t")
# 
#     @property
#     def trait(self):
#         return self.fields[0].lower()
# 
#     @property
#     def trait_value(self):
#         return self.fields[1].lower()
# 
#     @property
#     def hex_code(self):
#         return self.fields[2]


# Path: augur/util_support/color_parser.py
from collections import defaultdict
from io import TextIOWrapper
from pkg_resources import resource_stream
from augur.util_support.color_parser_line import ColorParserLine
import functools




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: