# Path: cjktools/resources/dict_format.py
# class RegexFormat(DictionaryFormat):
#     """
#     Describes a dictionary format, by providing regular expressions
#     for the header, the entry lines, and senses within an entry line. The
#     entry line pattern should have named values called 'word', 'reading',
#     and 'senses'.
#     """
# 
#     def __init__(self, name, header_pattern, line_pattern, sense_pattern):
#         self.name = name
#         self.header_pattern = re.compile(header_pattern, re.UNICODE)
#         self.line_pattern = re.compile(line_pattern, re.UNICODE)
#         self.sense_pattern = re.compile(sense_pattern, re.UNICODE)
# 
#     def match_header(self, header_line):
#         return bool(self.header_pattern.match(header_line))
# 
#     def parse_line(self, entry_line):
#         match = self.line_pattern.match(entry_line)
#         if not match:
#             raise FormatError("Bad line: %s" % entry_line)
# 
#         match_dict = match.groupdict()
# 
#         word = match_dict['word'].replace(' ', '')
#         reading = (match_dict.get('reading') or word).replace(' ', '')
#         readings = [reading]
#         sense_line = match_dict.get('senses')
# 
#         senses = []
#         for match in self.sense_pattern.finditer(sense_line):
#             senses.append(match.groupdict()['sense'])
# 
#         if not senses:
#             raise FormatError("No senses for word: %s" % word)
# 
#         return DictionaryEntry(word, readings, senses)
# 
# class UnknownFormatError(Exception):
#     """
#     Indicates that the dictionary which we attempted to parse had an
#     unknown format.
#     """
#     pass


from .dict_format import RegexFormat, UnknownFormatError
# -*- coding: utf-8 -*-
#
#  auto_format.py
#  cjktools
#

"""
A module of methods to automatically detect the format given a dictionary
file.
"""
from __future__ import unicode_literals



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


# Path: cjktools/resources/dict_format.py
# class RegexFormat(DictionaryFormat):
#     """
#     Describes a dictionary format, by providing regular expressions
#     for the header, the entry lines, and senses within an entry line. The
#     entry line pattern should have named values called 'word', 'reading',
#     and 'senses'.
#     """
# 
#     def __init__(self, name, header_pattern, line_pattern, sense_pattern):
#         self.name = name
#         self.header_pattern = re.compile(header_pattern, re.UNICODE)
#         self.line_pattern = re.compile(line_pattern, re.UNICODE)
#         self.sense_pattern = re.compile(sense_pattern, re.UNICODE)
# 
#     def match_header(self, header_line):
#         return bool(self.header_pattern.match(header_line))
# 
#     def parse_line(self, entry_line):
#         match = self.line_pattern.match(entry_line)
#         if not match:
#             raise FormatError("Bad line: %s" % entry_line)
# 
#         match_dict = match.groupdict()
# 
#         word = match_dict['word'].replace(' ', '')
#         reading = (match_dict.get('reading') or word).replace(' ', '')
#         readings = [reading]
#         sense_line = match_dict.get('senses')
# 
#         senses = []
#         for match in self.sense_pattern.finditer(sense_line):
#             senses.append(match.groupdict()['sense'])
# 
#         if not senses:
#             raise FormatError("No senses for word: %s" % word)
# 
#         return DictionaryEntry(word, readings, senses)
# 
# class UnknownFormatError(Exception):
#     """
#     Indicates that the dictionary which we attempted to parse had an
#     unknown format.
#     """
#     pass


# Path: cjktools/resources/auto_format.py
from .dict_format import RegexFormat, UnknownFormatError

# -*- coding: utf-8 -*-
#
#  auto_format.py
#  cjktools
#

"""
A module of methods to automatically detect the format given a dictionary
file.
"""
from __future__ import unicode_literals



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))