# Path: cjktools/resources/kanjidic.py
# class Kanjidic(dict):
#     """
#     An interface to the kanjidic dictionary. Create a new instance to parse
#     the dictionary. Has a nice getitem interface to get the details of a
#     character.
#     """
# 
#     def __init__(self, kanjidic_files=None):
#         super(Kanjidic, self).__init__
# 
#         if kanjidic_files is None:
#             kanjidic_files = [
#                 cjkdata.get_resource('kanjidic'),
#                 cjkdata.get_resource('kanjd212'),
#             ]
# 
#         with ExitStack() as stack:
#             file_chain = (stack.enter_context(sopen(f, mode='r'))
#                           for f in kanjidic_files)
#             line_stream = reduce(chain, file_chain)
# 
#             self._parse_kanjidic(line_stream)
# 
#     @classmethod
#     def get_cached(cls):
#         cached = getattr(cls, '_cached', cls())
#         cls._cached = cached
# 
#         return cached
# 
#     def _parse_kanjidic(self, line_stream):
#         """
#         Parses the kanjidic file for its contents, updating this class
#         with a kanji -> info mapping.
# 
#         @param files: The files for kanjidic.
#         """
#         self._dictionary = {}
# 
#         for line in line_stream:
#             if line.startswith('#'):
#                 continue
# 
#             entry = self._parse_line(line)
#             self[entry.kanji] = entry
# 
#     def _parse_line(self, line):
#         "Parses a single line in the kanjdic file, returning an entry."
#         segment_pattern = re.compile('[^ {]+|{.*?}', re.UNICODE)
#         segments = segment_pattern.findall(line.strip())
#         segments.reverse()
# 
#         kanji = segments.pop()
#         jis_code = int(segments.pop(), 16)
#         info = {
#             'kanji':        kanji,
#             'gloss':        [],
#             'on_readings':  [],
#             'kun_readings': [],
#             'jis_code':     jis_code
#         }
# 
#         while segments:
#             s = segments.pop()
# 
#             if s.startswith('{'):
#                 info['gloss'].append(s[1:-1])
# 
#             elif (scripts.script_type(s) != scripts.Script.Ascii or
#                   s.startswith('-')):
#                 # It must be a reading.
#                 char = s[0]
#                 if char == '-':
#                     char = s[1]
# 
#                 if scripts.script_type(char) == scripts.Script.Katakana:
#                     info['on_readings'].append(s)
#                 elif scripts.script_type(char) == scripts.Script.Hiragana:
#                     info['kun_readings'].append(s)
#                 else:
#                     raise Exception("Unknown segment %s" % s)
# 
#             elif s in ('T1', 'T2'):
#                 continue
# 
#             else:
#                 # handle various codes
#                 code = s[0]
#                 remainder = s[1:]
#                 try:
#                     remainder = int(remainder)
#                 except:
#                     pass
# 
#                 info.setdefault(remappings.get(code, code), []).append(
#                     remainder)
# 
#         info['stroke_count'] = info['stroke_count'][0]
#         if 'frequency' in info:
#             info['frequency'] = info['frequency'][0]
# 
#         info['skip_code'] = tuple(int(i)
#                                   for i in info['skip_code'][0].split('-'))
# 
#         return KanjidicEntry(**info)


import unittest
from cjktools.resources.kanjidic import Kanjidic
# -*- coding: utf-8 -*-
#
#  test_kanjidic.py
#

from __future__ import unicode_literals



def suite():
    test_suite = unittest.TestSuite((
        unittest.makeSuite(KanjidicTestCase)
    ))
    return test_suite


class KanjidicTestCase(unittest.TestCase):
    def setUp(self):
        self.kd = Kanjidic()

    def test_get_cached(self):
        kd = Kanjidic.get_cached()


# Path: cjktools/resources/kanjidic.py
# class Kanjidic(dict):
#     """
#     An interface to the kanjidic dictionary. Create a new instance to parse
#     the dictionary. Has a nice getitem interface to get the details of a
#     character.
#     """
# 
#     def __init__(self, kanjidic_files=None):
#         super(Kanjidic, self).__init__
# 
#         if kanjidic_files is None:
#             kanjidic_files = [
#                 cjkdata.get_resource('kanjidic'),
#                 cjkdata.get_resource('kanjd212'),
#             ]
# 
#         with ExitStack() as stack:
#             file_chain = (stack.enter_context(sopen(f, mode='r'))
#                           for f in kanjidic_files)
#             line_stream = reduce(chain, file_chain)
# 
#             self._parse_kanjidic(line_stream)
# 
#     @classmethod
#     def get_cached(cls):
#         cached = getattr(cls, '_cached', cls())
#         cls._cached = cached
# 
#         return cached
# 
#     def _parse_kanjidic(self, line_stream):
#         """
#         Parses the kanjidic file for its contents, updating this class
#         with a kanji -> info mapping.
# 
#         @param files: The files for kanjidic.
#         """
#         self._dictionary = {}
# 
#         for line in line_stream:
#             if line.startswith('#'):
#                 continue
# 
#             entry = self._parse_line(line)
#             self[entry.kanji] = entry
# 
#     def _parse_line(self, line):
#         "Parses a single line in the kanjdic file, returning an entry."
#         segment_pattern = re.compile('[^ {]+|{.*?}', re.UNICODE)
#         segments = segment_pattern.findall(line.strip())
#         segments.reverse()
# 
#         kanji = segments.pop()
#         jis_code = int(segments.pop(), 16)
#         info = {
#             'kanji':        kanji,
#             'gloss':        [],
#             'on_readings':  [],
#             'kun_readings': [],
#             'jis_code':     jis_code
#         }
# 
#         while segments:
#             s = segments.pop()
# 
#             if s.startswith('{'):
#                 info['gloss'].append(s[1:-1])
# 
#             elif (scripts.script_type(s) != scripts.Script.Ascii or
#                   s.startswith('-')):
#                 # It must be a reading.
#                 char = s[0]
#                 if char == '-':
#                     char = s[1]
# 
#                 if scripts.script_type(char) == scripts.Script.Katakana:
#                     info['on_readings'].append(s)
#                 elif scripts.script_type(char) == scripts.Script.Hiragana:
#                     info['kun_readings'].append(s)
#                 else:
#                     raise Exception("Unknown segment %s" % s)
# 
#             elif s in ('T1', 'T2'):
#                 continue
# 
#             else:
#                 # handle various codes
#                 code = s[0]
#                 remainder = s[1:]
#                 try:
#                     remainder = int(remainder)
#                 except:
#                     pass
# 
#                 info.setdefault(remappings.get(code, code), []).append(
#                     remainder)
# 
#         info['stroke_count'] = info['stroke_count'][0]
#         if 'frequency' in info:
#             info['frequency'] = info['frequency'][0]
# 
#         info['skip_code'] = tuple(int(i)
#                                   for i in info['skip_code'][0].split('-'))
# 
#         return KanjidicEntry(**info)


# Path: cjktools/tests/resources/test_kanjidic.py
import unittest
from cjktools.resources.kanjidic import Kanjidic

# -*- coding: utf-8 -*-
#
#  test_kanjidic.py
#

from __future__ import unicode_literals



def suite():
    test_suite = unittest.TestSuite((
        unittest.makeSuite(KanjidicTestCase)
    ))
    return test_suite


class KanjidicTestCase(unittest.TestCase):
    def setUp(self):
        self.kd = Kanjidic()

    def test_get_cached(self):
        kd = Kanjidic.get_cached()


        self.assertIsInstance(kd, Kanjidic)