Skip to content

Commit

Permalink
Raise exceptions when trying to initialize nonexistent kanji
Browse files Browse the repository at this point in the history
  • Loading branch information
cayennes committed Jun 16, 2012
1 parent 2d0f862 commit 7e96cde
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
53 changes: 48 additions & 5 deletions kanjicolorizer/colorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import re
import argparse
import codecs
from errno import ENOENT as FILE_NOT_FOUND

# Setup

Expand Down Expand Up @@ -64,12 +65,26 @@ def __init__(self, character, variant=None):
>>> k2.variant
'Kaisho'
Raises InvalidCharacterError if the character and variant don't
correspond to known data
>>> k = KanjiVG((u'Л'))
Traceback (most recent call last):
...
InvalidCharacterError: (u'\\u041b', None)
'''
self.character = character
self.variant = variant
with codecs.open(os.path.join(source_directory,
self.ascii_filename), encoding='utf-8') as f:
self.svg = f.read()
try:
with codecs.open(os.path.join(source_directory,
self.ascii_filename), encoding='utf-8') as f:
self.svg = f.read()
except IOError as e: # file not found
if e.errno == FILE_NOT_FOUND:
raise InvalidCharacterError(character, variant)
else:
raise

@classmethod
def _create_from_filename(cls, filename):
Expand All @@ -91,11 +106,19 @@ def ascii_filename(self):
>>> k = KanjiVG(u'漢')
>>> k.ascii_filename
'06f22.svg'
May raise InvalidCharacterError for some kinds of invalid
character/variant combinations; this should only happen during
object initialization.
'''
try:
code = '%05x' % ord(self.character)
except TypeError: # character not a character
raise InvalidCharacterError(self.character, self.variant)
if not self.variant:
return '%05x.svg' % ord(self.character)
return code + '.svg'
else:
return '%05x-%s.svg' % (ord(self.character), self.variant)
return '%s-%s.svg' % (code, self.variant)

@property
def character_filename(self):
Expand Down Expand Up @@ -552,6 +575,26 @@ def _color_generator(self, n):
yield self._hsv_to_rgbhexcode(float(i) / n,
self.settings.saturation, self.settings.value)


# Exceptions

class Error(Exception):
'''
Base class for this module's exceptions
'''
pass


class InvalidCharacterError(Error):
'''
Exception thrown when trying to initialize or use a character that
there isn't data for
'''
pass


# Test if run

if __name__ == "__main__":
import doctest
import sys
Expand Down
58 changes: 58 additions & 0 deletions kanjicolorizer/tests/test_colorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
# <http://www.gnu.org/licenses/>.

import unittest
from mock import patch
from kanjicolorizer import colorizer
from kanjicolorizer.colorizer import KanjiVG


Expand Down Expand Up @@ -62,5 +64,61 @@ def test_valid_variant_contains_named_stroke_group(self):
k = KanjiVG(u'字', 'Kaisho')
self.assertIn('kvg:StrokePaths_05b57-Kaisho', k.svg)

def test_with_invalid_character_raises_correct_ex_args(self):
with self.assertRaises(colorizer.InvalidCharacterError) as cm:
KanjiVG(u'Л')
# args set
self.assertEqual(cm.exception.args[0], u'Л')
self.assertEqual(cm.exception.args[1], None)
# message contains the useful information
self.assertIn(repr(u'Л'), repr(cm.exception))
self.assertIn(repr(None), repr(cm.exception))

def test_with_multiple_characters_raises_correct_exception(self):
self.assertRaises(
colorizer.InvalidCharacterError,
KanjiVG,
(u'漢字'))

def test_with_nonexistent_variant_raises_correct_ex_args(self):
with self.assertRaises(colorizer.InvalidCharacterError) as cm:
KanjiVG(u'字', 'gobbledygook')
# args set
self.assertEqual(cm.exception.args[0], u'字')
self.assertEqual(cm.exception.args[1], 'gobbledygook')
# message contains the useful information
self.assertIn(repr(u'字'), repr(cm.exception))
self.assertIn(repr('gobbledygook'), repr(cm.exception))

def test_with_mismatched_variant_raises_correct_exception(self):
self.assertRaises(
colorizer.InvalidCharacterError,
KanjiVG,
(u'漢', 'Kaisho'))

def test_empty_variant_raises_correct_exception(self):
self.assertRaises(
colorizer.InvalidCharacterError,
KanjiVG,
(u'字', ''))

def test_with_too_few_parameters_raises_correct_exception(self):
self.assertRaises(
colorizer.InvalidCharacterError,
KanjiVG,
())

def test_permission_denied_error_propogated(self):
'''
Errors other than file not found are unknown problems; the
exception should not be caught or changed
'''
with patch('__builtin__.open') as mock_open:
mock_open.side_effect = IOError(31, 'Permission denied')
self.assertRaises(
IOError,
KanjiVG,
('a'))

if __name__ == "__main__":
unittest.main()

0 comments on commit 7e96cde

Please sign in to comment.