Skip to content

Commit

Permalink
Fixed #84
Browse files Browse the repository at this point in the history
  • Loading branch information
heuer committed Aug 31, 2020
2 parents ba0cbe9 + d3b9e69 commit 5cc9896
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.py text eol=lf
*.rst text eol=lf
*.ttf binary
LICEN[CS]E text eol=lf
8 changes: 7 additions & 1 deletion docs/man/segno.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ QR Code Options
Valid values for QR Codes: 0 \.\. 7
Valid values for Micro QR Codes: 0 \.\. 3

.. option:: --encoding ENCODING

Specifies the encoding of the provided content.
If not specified (default) either ISO 8859-1 is used or if it does
not fit, UTF-8 or Shift_JIS (for Kanji content) is used.

.. option:: --micro

Allow the creation of Micro QR Codes
Expand Down Expand Up @@ -250,7 +256,7 @@ SVG Options

Indicates the SVG version

.. option:: --encoding ENCODING
.. option:: --svgencoding ENCODING

Specifies the encoding of the document

Expand Down
11 changes: 9 additions & 2 deletions man/segno.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" Man page generated from reStructuredText.
.
.TH "SEGNO" "1" "Aug 27, 2020" "1.2.1" "Segno"
.TH "SEGNO" "1" "Aug 31, 2020" "1.2.2.dev" "Segno"
.SH NAME
segno \- Segno QR Code encoder
.
Expand Down Expand Up @@ -77,6 +77,13 @@ Valid values for Micro QR Codes: 0 .. 3
.UNINDENT
.INDENT 0.0
.TP
.B \-\-encoding ENCODING
Specifies the encoding of the provided content.
If not specified (default) either ISO 8859\-1 is used or if it does
not fit, UTF\-8 or Shift_JIS (for Kanji content) is used.
.UNINDENT
.INDENT 0.0
.TP
.B \-\-micro
Allow the creation of Micro QR Codes
.UNINDENT
Expand Down Expand Up @@ -301,7 +308,7 @@ Indicates the SVG version
.UNINDENT
.INDENT 0.0
.TP
.B \-\-encoding ENCODING
.B \-\-svgencoding ENCODING
Specifies the encoding of the document
.UNINDENT
.INDENT 0.0
Expand Down
8 changes: 7 additions & 1 deletion segno/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def _convert_scale(val):
choices=('numeric', 'alphanumeric', 'byte', 'kanji', 'hanzi'),
default=None,
type=lambda x: x.lower())
parser.add_argument('--encoding', help='Sets the encoding of the input. '
'If not set (default), a minimal encoding is chosen.',
default=None)
parser.add_argument('--micro', help='Allow the creation of Micro QR Codes',
dest='micro', action='store_true')
parser.add_argument('--no-micro', help='Disallow creation of Micro QR Codes (default)',
Expand Down Expand Up @@ -163,7 +166,7 @@ def _convert_scale(val):
svg_group.add_argument('--unit', help='Indicates SVG coordinate system unit')
svg_group.add_argument('--svgversion', help='Indicates the SVG version',
type=float)
svg_group.add_argument('--encoding', help='Specifies the encoding of the document',
svg_group.add_argument('--svgencoding', help='Specifies the encoding of the document',
default='utf-8')
svg_group.add_argument('--draw-transparent', help='Indicates that transparent paths should be drawn',
action='store_true')
Expand Down Expand Up @@ -234,6 +237,8 @@ def build_config(config, filename=None):
if config.pop('no_classes', False):
config['svgclass'] = None
config['lineclass'] = None
# encoding is used to provide the encoding to *create* a QR code
config['encoding'] = config.pop('svgencoding', 'utf-8')
if filename is not None:
ext = filename[filename.rfind('.') + 1:].lower()
if ext == 'svgz': # There is no svgz serializer, use same config as svg
Expand All @@ -258,6 +263,7 @@ def make_code(config):
make = segno.make
kw = dict(mode=config.pop('mode'), error=config.pop('error'),
version=config.pop('version'), mask=config.pop('pattern'),
encoding=config.pop('encoding'),
boost_error=config.pop('boost_error'))
if config.pop('seq'):
make = segno.make_sequence
Expand Down
29 changes: 29 additions & 0 deletions tests/issue-84/issue-84-iso-8859-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00001111111000110011111110000
00001000001001111010000010000
00001011101011010010111010000
00001011101011011010111010000
00001011101000101010111010000
00001000001001001010000010000
00001111111010101011111110000
00000000000001100000000000000
00000001101101111000011000000
00001100110111001110110100000
00001101101100111101011100000
00001011010100010100101100000
00000110111001100001110000000
00000000000011001000010110000
00001111111011100011101000000
00001000001001110000011000000
00001011101011101001010110000
00001011101010110011000000000
00001011101000010000111110000
00001000001000101101011110000
00001111111000100010100000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
29 changes: 29 additions & 0 deletions tests/issue-84/issue-84-utf-8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00001111111010111011111110000
00001000001011111010000010000
00001011101011011010111010000
00001011101001101010111010000
00001011101001010010111010000
00001000001010000010000010000
00001111111010101011111110000
00000000000010100000000000000
00000011101010010111001110000
00000111100111000011001000000
00001101001101011001010110000
00001101100100011011100100000
00001111111101000001010110000
00000000000010001100110000000
00001111111000110010001100000
00001000001001111110011110000
00001011101011010000000100000
00001011101010100011110000000
00001011101011101000001000000
00001000001000000111011000000
00001111111000011100100100000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
16 changes: 12 additions & 4 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_defaults():
assert args.pattern is None
assert args.version is None
assert args.scale == 1
assert args.encoding is None
assert not args.micro
assert args.output is None
assert args.border is None
Expand Down Expand Up @@ -55,7 +56,7 @@ def test_defaults():
# SVG
assert args.xmldecl
assert not args.no_classes
assert args.encoding == 'utf-8'
assert args.svgencoding == 'utf-8'
assert args.title is None
assert args.desc is None
assert args.svgns is True
Expand Down Expand Up @@ -374,16 +375,23 @@ def test_omit_classes():

def test_encoding():
args = cli.parse(['--output=x.svg', ''])
assert args.encoding == 'utf-8'
assert args.svgencoding == 'utf-8'
assert cli.build_config(args)['encoding'] == 'utf-8'


def test_encoding2():
args = cli.parse(['--encoding=ascii', '--output=x.svg', ''])
assert args.encoding == 'ascii'
args = cli.parse(['--svgencoding=ascii', '--output=x.svg', ''])
assert args.svgencoding == 'ascii'
assert cli.build_config(args)['encoding'] == 'ascii'


def test_encoding3():
# Ignore --encoding since it is used to *create* a QR code
args = cli.parse(['--encoding=latin1', '--output=x.svg', ''])
assert args.svgencoding == 'utf-8'
assert cli.build_config(args)['encoding'] == 'utf-8'


def test_title():
args = cli.parse(['--output=x.svg', ''])
assert args.title is None
Expand Down
62 changes: 62 additions & 0 deletions tests/test_issue84_cli_encoding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 - 2020 -- Lars Heuer
# All rights reserved.
#
# License: BSD License
#
"""\
Tests against issue #84.
<https://github.com/heuer/segno/issues/84>
"""
from __future__ import unicode_literals, absolute_import
import os
import tempfile
from segno import cli
import pytest


def test_issue_84_default_encoding():
with open(os.path.join(os.path.dirname(__file__), 'issue-84', 'issue-84-iso-8859-1.txt'), 'r') as f:
expected = f.read()
f = tempfile.NamedTemporaryFile('w', suffix='.txt', delete=False)
f.close()
try:
cli.main(['-o', f.name, 'Müller'])
with open(f.name, 'r') as f:
result = f.read()
assert expected == result
# Explicit but default encoding
cli.main(['-o', f.name, '--encoding', 'iso-8859-1', 'Müller'])
with open(f.name, 'r') as f:
result = f.read()
assert expected == result
# Explicit but default encoding
cli.main(['-o', f.name, '--encoding', 'latin1', 'Müller'])
with open(f.name, 'r') as f:
result = f.read()
assert expected == result
finally:
os.unlink(f.name)


def test_issue_84_utf8():
with open(os.path.join(os.path.dirname(__file__), 'issue-84', 'issue-84-utf-8.txt'), 'r') as f:
expected = f.read()
f = tempfile.NamedTemporaryFile('w', suffix='.txt', delete=False)
f.close()
try:
cli.main(['-o', f.name, '--encoding', 'utf-8', 'Müller'])
with open(f.name, 'r') as f:
result = f.read()
assert expected == result
cli.main(['-o', f.name, '--encoding', 'UTf-8', 'Müller'])
with open(f.name, 'r') as f:
result = f.read()
assert expected == result
finally:
os.unlink(f.name)


if __name__ == '__main__':
pytest.main([__file__])

0 comments on commit 5cc9896

Please sign in to comment.