# Path: cjktools/resources/auto_format.py
# def detect_format(header):
#     """
#     Reads the first line of the filename, and attempts to determine the
#     format of the dictionary. The matching format is returned, or an
#     UnknownFormatError is thrown.
#     """
#     for f in known_formats:
#         if f.match_header(header):
#             return f
# 
#     raise UnknownFormatError('unknown header line: %s' % repr(header))
# 
# def load_dictionary(istream):
#     """
#     Attempts to detect the format of and parse a dictionary, returning the
#     dictionary object on success.
#     """
#     lines = iter(istream)
#     header = next(lines)
#     return detect_format(header).parse_dictionary(lines)
# 
# Path: cjktools/resources/bilingual_dict.py
# class BilingualDictionary(dict):
#     """
#     An abstract bilingual dictionary, containing headwords in one
#     language and translated senses in another language. Homographs are
#     stored as senses of the same lexeme, rather than being separate.
#     """
# 
#     def __init__(self, fmt):
#         """
#         :param DictionaryFormat format:
#             The format object for this dictionary.
#         """
#         self.format = fmt
# 
#     def update(self, rhs_dictionary, clash_policy=ClashPolicy.Overwrite):
#         "Merges another dictionary into this one, in-place."
#         if clash_policy == ClashPolicy.Merge:
#             raise NotYetImplementedError
# 
#         return super(BilingualDictionary, self).update(rhs_dictionary)
# 
# Path: cjktools/resources/dict_format.py
# class UnknownFormatError(Exception):
#     """
#     Indicates that the dictionary which we attempted to parse had an
#     unknown format.
#     """
#     pass
# 
# Path: cjktools/tests/_common.py
# def to_unicode_stream(x):
#     o = StringIO(x)
# 
#     if isinstance(x, binary_type):
#         o = codecs.getreader('utf8')(o)
# 
#     return o


import unittest
from cjktools.resources.auto_format import detect_format, load_dictionary
from cjktools.resources.bilingual_dict import BilingualDictionary
from cjktools.resources.dict_format import UnknownFormatError
from .._common import to_unicode_stream
    return test_suite

EDICT_SAMPLE = \
"""　？？？ /EDICT, EDICT_SUB(P), EDICT2 Japanese-English Electronic Dictionary Files/Copyright Electronic Dictionary Research & Development Group - 2011/Created: 2011-07-03/
齧歯目 [げっしもく] /(n) (1) Rodentia/(adj-no) (2) rat-like/rodential/
齲歯 [うし] /(n,adj-no) cavity/tooth decay/decayed tooth/caries/
齲歯 [むしば] /(n,adj-no) cavity/tooth decay/decayed tooth/caries/
"""  # nopep8

JPLACES_SAMPLE = \
"""？？？？ /J_PLACES  A file of Japanese place names compiled from the files from the www.postal.mpt.go.jp site, Dec 1998 version/
秋葉原 [あきはばら] /Akihabara (loc)/
上野 [うえの] /Ueno (loc)/
"""  # nopep8


class AutoFormatTestCase(unittest.TestCase):
    def setUp(self):

        self.je_edict = to_unicode_stream(EDICT_SAMPLE)
        self.je_jplaces = to_unicode_stream(JPLACES_SAMPLE)
        self.invalid = to_unicode_stream('BAD HEADER は BAD HEADER')

    def test_formats(self):
        "Tests correct format detection for a variety of dictionaries."
        self.assertEqual(
            detect_format(next(self.je_edict)).name,
            'edict',
        )
        self.assertEqual(

# Path: cjktools/resources/auto_format.py
# def detect_format(header):
#     """
#     Reads the first line of the filename, and attempts to determine the
#     format of the dictionary. The matching format is returned, or an
#     UnknownFormatError is thrown.
#     """
#     for f in known_formats:
#         if f.match_header(header):
#             return f
# 
#     raise UnknownFormatError('unknown header line: %s' % repr(header))
# 
# def load_dictionary(istream):
#     """
#     Attempts to detect the format of and parse a dictionary, returning the
#     dictionary object on success.
#     """
#     lines = iter(istream)
#     header = next(lines)
#     return detect_format(header).parse_dictionary(lines)
# 
# Path: cjktools/resources/bilingual_dict.py
# class BilingualDictionary(dict):
#     """
#     An abstract bilingual dictionary, containing headwords in one
#     language and translated senses in another language. Homographs are
#     stored as senses of the same lexeme, rather than being separate.
#     """
# 
#     def __init__(self, fmt):
#         """
#         :param DictionaryFormat format:
#             The format object for this dictionary.
#         """
#         self.format = fmt
# 
#     def update(self, rhs_dictionary, clash_policy=ClashPolicy.Overwrite):
#         "Merges another dictionary into this one, in-place."
#         if clash_policy == ClashPolicy.Merge:
#             raise NotYetImplementedError
# 
#         return super(BilingualDictionary, self).update(rhs_dictionary)
# 
# Path: cjktools/resources/dict_format.py
# class UnknownFormatError(Exception):
#     """
#     Indicates that the dictionary which we attempted to parse had an
#     unknown format.
#     """
#     pass
# 
# Path: cjktools/tests/_common.py
# def to_unicode_stream(x):
#     o = StringIO(x)
# 
#     if isinstance(x, binary_type):
#         o = codecs.getreader('utf8')(o)
# 
#     return o


# Path: cjktools/tests/resources/test_auto_format.py
import unittest
from cjktools.resources.auto_format import detect_format, load_dictionary
from cjktools.resources.bilingual_dict import BilingualDictionary
from cjktools.resources.dict_format import UnknownFormatError
from .._common import to_unicode_stream

    return test_suite

EDICT_SAMPLE = \
"""　？？？ /EDICT, EDICT_SUB(P), EDICT2 Japanese-English Electronic Dictionary Files/Copyright Electronic Dictionary Research & Development Group - 2011/Created: 2011-07-03/
齧歯目 [げっしもく] /(n) (1) Rodentia/(adj-no) (2) rat-like/rodential/
齲歯 [うし] /(n,adj-no) cavity/tooth decay/decayed tooth/caries/
齲歯 [むしば] /(n,adj-no) cavity/tooth decay/decayed tooth/caries/
"""  # nopep8

JPLACES_SAMPLE = \
"""？？？？ /J_PLACES  A file of Japanese place names compiled from the files from the www.postal.mpt.go.jp site, Dec 1998 version/
秋葉原 [あきはばら] /Akihabara (loc)/
上野 [うえの] /Ueno (loc)/
"""  # nopep8


class AutoFormatTestCase(unittest.TestCase):
    def setUp(self):

        self.je_edict = to_unicode_stream(EDICT_SAMPLE)
        self.je_jplaces = to_unicode_stream(JPLACES_SAMPLE)
        self.invalid = to_unicode_stream('BAD HEADER は BAD HEADER')

    def test_formats(self):
        "Tests correct format detection for a variety of dictionaries."
        self.assertEqual(
            detect_format(next(self.je_edict)).name,
            'edict',
        )
        self.assertEqual(

            detect_format(next(self.je_jplaces)).name,