Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Update to python 3 and anki 2.1 #46

Merged
merged 5 commits into from
Oct 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2
jobs:
build:
docker:
- image: circleci/python:3.6.6-stretch
steps:
- checkout
- run: git submodule sync
- run: git submodule update --init
- run:
name: install dependencies
command: |
python3 -m venv venv
. venv/bin/activate
pip install -r requirements.txt
- run:
name: run tests
command: |
mkdir test-reports
. venv/bin/activate
py.test --verbose --junitxml=test-reports/junit.xml
- store_test_results:
path: test-reports/
- store_artifacts:
path: test-reports/
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ To run the existing tests:
.. code:: bash

$ python -m kanjicolorizer.colorizer
$ python -m unittest discover
$ python -m unittest discover -s kanjicolorizer

License
-------
Expand Down
1 change: 1 addition & 0 deletions anki/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import kanji_colorizer
15 changes: 7 additions & 8 deletions anki/kanji_colorizer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# kanji_colorizer.py is part of kanji-colorize which makes KanjiVG data
Expand Down Expand Up @@ -81,9 +81,8 @@
from aqt import mw
from aqt.utils import showInfo, askUser
from aqt.qt import *
from kanjicolorizer.colorizer import (KanjiVG, KanjiColorizer,
from .kanjicolorizer.colorizer import (KanjiVG, KanjiColorizer,
InvalidCharacterError)
import string

srcField = 'Kanji'
dstField = 'Diagram'
Expand Down Expand Up @@ -134,17 +133,17 @@ def addKanji(note, flag=False, currentFieldIndex=None):

oldDst = note[dstField]
dst=''
#srcTxt = string.replace(srcTxt, u'\uff5e', u'\u301c').encode('euc-jp')
for character in characters_to_colorize(unicode(srcTxt)):

for character in characters_to_colorize(str(srcTxt)):
# write to file; anki works in the media directory by default
try:
filename = KanjiVG(character).ascii_filename
except InvalidCharacterError:
# silently ignore non-Japanese characters
continue
char_svg = kc.get_colored_svg(character).encode('utf_8')
anki_fname = mw.col.media.writeData(unicode(filename, 'utf_8'), char_svg)
dst += '<img src="{!s}">'.format(anki_fname).encode('utf_8')
anki_fname = mw.col.media.writeData(filename, char_svg)
dst += '<img src="{!s}">'.format(anki_fname)

if dst != oldDst and dst != '':
note[dstField] = dst
Expand Down Expand Up @@ -180,5 +179,5 @@ def regenerate_all():

# add menu item
do_regenerate_all = QAction("Kanji Colorizer: (re)generate all", mw)
mw.connect(do_regenerate_all, SIGNAL("triggered()"), regenerate_all)
do_regenerate_all.triggered.connect(regenerate_all)
mw.form.menuTools.addAction(do_regenerate_all)
9 changes: 0 additions & 9 deletions circle.yml

This file was deleted.

2 changes: 1 addition & 1 deletion kanji_colorize.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

# kanjicolorizer.py is part of kanji-colorize which makes KanjiVG data
Expand Down
74 changes: 38 additions & 36 deletions kanjicolorizer/colorizer.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

# colorizer.py is part of kanji-colorize which makes KanjiVG data
Expand Down Expand Up @@ -26,13 +26,20 @@
# Note: this module is in the middle of being refactored.

import os
import colorsys
import re
import argparse
from codecs import open
from errno import ENOENT as FILE_NOT_FOUND
import sys

# Anki add-on compatibility
try:
import colorsys
except ModuleNotFoundError:
from . import colorsys # Anki add-on
try:
import argparse
except ModuleNotFoundError:
from . import argparse # Anki add-on


# Function that I want to have after refactoring, currently implemented using
# existing interface
Expand All @@ -42,7 +49,7 @@ def colorize(character, mode="spectrum", saturation=0.95, value=0.75,
"""
Returns a string containing the colorized svg for the character

>>> svg = colorize(u'a', mode='spectrum', image_size=100,
>>> svg = colorize('a', mode='spectrum', image_size=100,
... saturation=0.95, value=0.75)
>>> 'has been modified' in svg
True
Expand All @@ -69,12 +76,12 @@ class KanjiVG(object):
basic qualities of the character
'''
def __init__(self, character, variant=''):
u'''
'''
Create a new KanjiVG object

Either give just the character

>>> k1 = KanjiVG(u'漢')
>>> k1 = KanjiVG('漢')
>>> print(k1.character)
>>> k1.variant
Expand All @@ -83,7 +90,7 @@ def __init__(self, character, variant=''):
Or if the character has a variant, give that as a second
argument

>>> k2 = KanjiVG(u'字', 'Kaisho')
>>> k2 = KanjiVG('字', 'Kaisho')
>>> print(k2.character)
>>> k2.variant
Expand All @@ -92,10 +99,10 @@ def __init__(self, character, variant=''):
Raises InvalidCharacterError if the character and variant don't
correspond to known data

>>> k = KanjiVG((u'Л'))
>>> k = KanjiVG('Л')
Traceback (most recent call last):
...
InvalidCharacterError: (u'\\u041b', '')
kanjicolorizer.colorizer.InvalidCharacterError: ('\\u041b', '')

'''
self.character = character
Expand All @@ -104,33 +111,33 @@ def __init__(self, character, variant=''):
self.variant = ''
try:
with open(os.path.join(source_directory, self.ascii_filename),
encoding='utf-8') as f:
'r', 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)
raise InvalidCharacterError(character, variant) from e
else:
raise

@classmethod
def _create_from_filename(cls, filename):
u'''
'''
Alternate constructor that uses a KanjiVG filename; used by
get_all().

>>> k = KanjiVG._create_from_filename('00061.svg')
>>> k.character
u'a'
'a'
'''
m = re.match('^([0-9a-f]*)-?(.*?).svg$', filename)
return cls(unichr(int(m.group(1), 16)), m.group(2))
return cls(chr(int(m.group(1), 16)), m.group(2))

@property
def ascii_filename(self):
u'''
'''
An SVG filename in ASCII using the same format KanjiVG uses.

>>> k = KanjiVG(u'漢')
>>> k = KanjiVG('漢')
>>> k.ascii_filename
'06f22.svg'

Expand All @@ -149,10 +156,10 @@ def ascii_filename(self):

@property
def character_filename(self):
u'''
'''
An SVG filename that uses the unicode character

>>> k = KanjiVG(u'漢')
>>> k = KanjiVG('漢')
>>> print(k.character_filename)
漢.svg
'''
Expand All @@ -163,7 +170,7 @@ def character_filename(self):

@classmethod
def get_all(cls):
u'''
'''
Returns a complete list of KanjiVG objects; everything there is
data for

Expand All @@ -178,7 +185,7 @@ def get_all(cls):


class KanjiColorizer:
u"""
"""
Class that creates colored stroke order diagrams out of kanjivg
data, and writes them to file.

Expand All @@ -188,7 +195,7 @@ class KanjiColorizer:
Settings can set by initializing with a string in the same format as
the command line.
>>> test_output_dir = os.path.join('test', 'colorized-kanji')
>>> my_args = ' '.join(['--characters', u'aあ漢',
>>> my_args = ' '.join(['--characters', 'aあ漢',
... '--output', test_output_dir])
>>> kc = KanjiColorizer(my_args)

Expand Down Expand Up @@ -262,7 +269,7 @@ def _init_parser(self):
help="image size in pixels; they're square so this "
'will be both height and width '
'(default: %(default)s)')
self._parser.add_argument('--characters', type=unicode,
self._parser.add_argument('--characters', type=str,
help='a list of characters to include, without '
'spaces; if this option is used, no variants '
'will be included; if this option is not '
Expand All @@ -289,13 +296,8 @@ def read_cl_args(self):
>>> sys.argv = ['this.py', '--mode', 'contrast']
>>> kc.read_cl_args()
>>> kc.settings.mode
u'contrast'
'contrast'
"""
# Put argv in the correct encoding, with a default for the pytest case
for i in range(len(sys.argv)):
sys.argv[i] = sys.argv[i].decode(getattr(sys.stdin,
'encoding',
'UTF-8'))
self.settings = self._parser.parse_args()

def read_arg_string(self, argstring):
Expand All @@ -317,7 +319,7 @@ def get_colored_svg(self, character):
>>> kc = KanjiColorizer()
>>> svg = kc.get_colored_svg('a')
>>> svg.splitlines()[0]
u'<?xml version="1.0" encoding="UTF-8"?>'
'<?xml version="1.0" encoding="UTF-8"?>'
>>> svg.find('00061')
1780
>>> svg.find('has been modified')
Expand All @@ -336,7 +338,7 @@ def write_all(self):
Silently ignores invalid characters.

>>> test_output_dir = os.path.join('test', 'colorized-kanji')
>>> kc = KanjiColorizer(' '.join(['--characters', u'aあ漢',
>>> kc = KanjiColorizer(' '.join(['--characters', 'aあ漢',
... '--output', test_output_dir]))
>>> kc.write_all()

Expand Down Expand Up @@ -386,7 +388,7 @@ def write_all(self):
f.write(svg)

def _modify_svg(self, svg):
u"""
"""
Applies all desired changes to the SVG

>>> kc = KanjiColorizer('')
Expand All @@ -396,7 +398,7 @@ def _modify_svg(self, svg):
>>> desired_svg = open(
... os.path.join(
... 'test', 'default_results', 'kanji-colorize-spectrum',
... u'漢.svg'),
... '漢.svg'),
... 'r', encoding='utf-8').read()
>>> import difflib
>>> for line in difflib.context_diff(
Expand Down Expand Up @@ -625,7 +627,7 @@ def _hsv_to_rgbhexcode(self, h, s, v):
'#09bfbf'
"""
color = colorsys.hsv_to_rgb(h, s, v)
return '#%02x%02x%02x' % tuple([i * 255 for i in color])
return '#%02x%02x%02x' % tuple([int(i * 255) for i in color])

def _color_generator(self, n):
"""
Expand All @@ -644,11 +646,11 @@ def _color_generator(self, n):
"""
if (self.settings.mode == "contrast"):
angle = 0.618033988749895 # conjugate of the golden ratio
for i in 2 * range(n):
for i in 2 * list(range(n)):
yield self._hsv_to_rgbhexcode(i * angle,
self.settings.saturation, self.settings.value)
else: # spectrum is default
for i in 2 * range(n):
for i in 2 * list(range(n)):
yield self._hsv_to_rgbhexcode(float(i) / n,
self.settings.saturation, self.settings.value)

Expand Down
Loading