Exemplo n.º 1
0
    def test_optimize_size(self):
        text = 'A1abc12345123451234512345def1HELLOHELLOHELLOHELLOa' * 5

        qr = qrcode.QRCode()
        qr.add_data(text)
        qr.make()
        self.assertEqual(qr.version, 10)

        qr = qrcode.QRCode()
        qr.add_data(text, optimize=0)
        qr.make()
        self.assertEqual(qr.version, 11)
Exemplo n.º 2
0
 def test_render_pymaging_png(self):
     qr = qrcode.QRCode()
     qr.add_data(UNICODE_TEXT)
     img = qr.make_image(image_factory=qrcode.image.pure.PymagingImage)
     with warnings.catch_warnings():
         if six.PY3:
             warnings.simplefilter('ignore', DeprecationWarning)
         img.save(six.BytesIO())
Exemplo n.º 3
0
 def test_fit(self):
     qr = qrcode.QRCode()
     qr.add_data('a')
     qr.make()
     self.assertEqual(qr.version, 1)
     qr.add_data('bcdefghijklmno')
     qr.make()
     self.assertEqual(qr.version, 2)
Exemplo n.º 4
0
    def test_qrcode_factory(self):
        class MockFactory(BaseImage):
            drawrect = mock.Mock()

        qr = qrcode.QRCode(image_factory=MockFactory)
        qr.add_data(UNICODE_TEXT)
        qr.make_image()
        self.assertTrue(MockFactory.drawrect.called)
Exemplo n.º 5
0
 def test_print_ascii_stdout(self):
     qr = qrcode.QRCode()
     stdout_encoding = sys.stdout.encoding
     with mock.patch('sys.stdout') as fake_stdout:
         # Python 2.6 needs sys.stdout.encoding to be a real string.
         sys.stdout.encoding = stdout_encoding
         fake_stdout.isatty.return_value = None
         self.assertRaises(OSError, qr.print_ascii, tty=True)
         self.assertTrue(fake_stdout.isatty.called)
Exemplo n.º 6
0
 def test_optimize_short(self):
     qr = qrcode.QRCode()
     text = 'A1abc1234567def1HELLOa'
     qr.add_data(text, optimize=7)
     qr.make()
     self.assertEqual(len(qr.data_list), 3)
     self.assertEqual([d.mode for d in qr.data_list],
                      [MODE_8BIT_BYTE, MODE_NUMBER, MODE_8BIT_BYTE])
     self.assertEqual(qr.version, 2)
Exemplo n.º 7
0
 def test_optimize(self):
     qr = qrcode.QRCode()
     text = 'A1abc12345def1HELLOa'
     qr.add_data(text, optimize=4)
     qr.make()
     self.assertEqual([d.mode for d in qr.data_list], [
         MODE_8BIT_BYTE, MODE_NUMBER, MODE_8BIT_BYTE, MODE_ALPHA_NUM,
         MODE_8BIT_BYTE
     ])
     self.assertEqual(qr.version, 2)
Exemplo n.º 8
0
 def test_print_tty(self):
     qr = qrcode.QRCode()
     f = six.StringIO()
     f.isatty = lambda: True
     qr.print_tty(out=f)
     printed = f.getvalue()
     f.close()
     BOLD_WHITE_BG = '\x1b[1;47m'
     BLACK_BG = '\x1b[40m'
     WHITE_BLOCK = BOLD_WHITE_BG + '  ' + BLACK_BG
     EOL = '\x1b[0m\n'
     expected = (BOLD_WHITE_BG + '  ' * 23 + EOL + WHITE_BLOCK + '  ' * 7 +
                 WHITE_BLOCK)
     self.assertEqual(printed[:len(expected)], expected)
Exemplo n.º 9
0
    def test_print_ascii(self):
        qr = qrcode.QRCode(border=0)
        f = six.StringIO()
        qr.print_ascii(out=f)
        printed = f.getvalue()
        f.close()
        expected = u'\u2588\u2580\u2580\u2580\u2580\u2580\u2588'
        self.assertEqual(printed[:len(expected)], expected)

        f = six.StringIO()
        f.isatty = lambda: True
        qr.print_ascii(out=f, tty=True)
        printed = f.getvalue()
        f.close()
        expected = (u'\x1b[48;5;232m\x1b[38;5;255m' +
                    u'\xa0\u2584\u2584\u2584\u2584\u2584\xa0')
        self.assertEqual(printed[:len(expected)], expected)
Exemplo n.º 10
0
 def test_overflow(self):
     qr = qrcode.QRCode(version=1)
     qr.add_data('abcdefghijklmno')
     self.assertRaises(DataOverflowError, qr.make, fit=False)
Exemplo n.º 11
0
 def test_add_qrdata(self):
     qr = qrcode.QRCode(version=1)
     data = QRData('a')
     qr.add_data(data)
     qr.make(fit=False)
Exemplo n.º 12
0
 def test_large(self):
     qr = qrcode.QRCode(version=27)
     qr.add_data('a')
     qr.make(fit=False)
Exemplo n.º 13
0
 def test_invalid_version(self):
     qr = qrcode.QRCode(version=41)
     self.assertRaises(ValueError, qr.make, fit=False)
Exemplo n.º 14
0
 def test_negative_size_at_usage(self):
     qr = qrcode.QRCode()
     qr.box_size = -1
     self.assertRaises(ValueError, qr.make_image)
Exemplo n.º 15
0
 def test_basic(self):
     qr = qrcode.QRCode(version=1)
     qr.add_data('a')
     qr.make(fit=False)
Exemplo n.º 16
0
 def test_render_pil(self):
     qr = qrcode.QRCode()
     qr.add_data(UNICODE_TEXT)
     img = qr.make_image()
     img.save(six.BytesIO())
Exemplo n.º 17
0
 def test_get_matrix_border(self):
     qr = qrcode.QRCode(border=1)
     qr.add_data('1')
     matrix = [row[1:-1] for row in qr.get_matrix()[1:-1]]
     self.assertEqual(matrix, qr.modules)
Exemplo n.º 18
0
 def test_mode_8bit(self):
     qr = qrcode.QRCode()
     qr.add_data(u'abcABC' + UNICODE_TEXT, optimize=0)
     qr.make()
     self.assertEqual(qr.version, 1)
     self.assertEqual(qr.data_list[0].mode, MODE_8BIT_BYTE)
Exemplo n.º 19
0
 def test_mode_alpha(self):
     qr = qrcode.QRCode()
     qr.add_data('ABCDEFGHIJ1234567890', optimize=0)
     qr.make()
     self.assertEqual(qr.version, 1)
     self.assertEqual(qr.data_list[0].mode, MODE_ALPHA_NUM)
Exemplo n.º 20
0
 def test_render_svg_fragment(self):
     qr = qrcode.QRCode()
     qr.add_data(UNICODE_TEXT)
     img = qr.make_image(image_factory=qrcode.image.svg.SvgFragmentImage)
     img.save(six.BytesIO())
Exemplo n.º 21
0
 def test_render_svg_with_background(self):
     qr = qrcode.QRCode()
     qr.add_data(UNICODE_TEXT)
     img = qr.make_image(image_factory=SvgImageWhite)
     img.save(six.BytesIO())
Exemplo n.º 22
0
 def test_render_pymaging_png_bad_kind(self):
     qr = qrcode.QRCode()
     qr.add_data(UNICODE_TEXT)
     img = qr.make_image(image_factory=qrcode.image.pure.PymagingImage)
     self.assertRaises(ValueError, img.save, six.BytesIO(), kind='FISH')
Exemplo n.º 23
0
 def test_mode_number(self):
     qr = qrcode.QRCode()
     qr.add_data('1234567890123456789012345678901234', optimize=0)
     qr.make()
     self.assertEqual(qr.version, 1)
     self.assertEqual(qr.data_list[0].mode, MODE_NUMBER)
Exemplo n.º 24
0
 def test_print_tty_stdout(self):
     qr = qrcode.QRCode()
     with mock.patch('sys.stdout') as fake_stdout:
         fake_stdout.isatty.return_value = None
         self.assertRaises(OSError, qr.print_tty)
         self.assertTrue(fake_stdout.isatty.called)
Exemplo n.º 25
0
 def test_regression_mode_comma(self):
     qr = qrcode.QRCode()
     qr.add_data(',', optimize=0)
     qr.make()
     self.assertEqual(qr.data_list[0].mode, MODE_8BIT_BYTE)
Exemplo n.º 26
0
 def test_get_matrix(self):
     qr = qrcode.QRCode(border=0)
     qr.add_data('1')
     self.assertEqual(qr.get_matrix(), qr.modules)
Exemplo n.º 27
0
 def test_mode_8bit_newline(self):
     qr = qrcode.QRCode()
     qr.add_data('ABCDEFGHIJ1234567890\n', optimize=0)
     qr.make()
     self.assertEqual(qr.data_list[0].mode, MODE_8BIT_BYTE)
Exemplo n.º 28
0
def main(args=None):
    if args is None:
        args = sys.argv[1:]
    from pkg_resources import get_distribution
    version = get_distribution('qrcode').version
    parser = optparse.OptionParser(usage=__doc__.strip(), version=version)
    parser.add_option(
        "--factory",
        help="Full python path to the image factory class to "
        "create the image with. You can use the following shortcuts to the "
        "built-in image factory classes: {0}.".format(", ".join(
            sorted(default_factories.keys()))))
    parser.add_option(
        "--optimize",
        type=int,
        help="Optimize the data by looking for chunks "
        "of at least this many characters that could use a more efficient "
        "encoding method. Use 0 to turn off chunk optimization.")
    parser.add_option(
        "--error-correction",
        type='choice',
        choices=sorted(error_correction.keys()),
        default='M',
        help="The error correction level to use. Choices are L (7%), "
        "M (15%, default), Q (25%), and H (30%).")
    opts, args = parser.parse_args(args)

    qr = qrcode.QRCode(
        error_correction=error_correction[opts.error_correction])

    if opts.factory:
        module = default_factories.get(opts.factory, opts.factory)
        if '.' not in module:
            parser.error("The image factory is not a full python path")
        module, name = module.rsplit('.', 1)
        imp = __import__(module, {}, [], [name])
        image_factory = getattr(imp, name)
    else:
        image_factory = None

    if args:
        data = args[0]
    else:
        # Use sys.stdin.buffer if available (Python 3) avoiding
        # UnicodeDecodeErrors.
        stdin_buffer = getattr(sys.stdin, 'buffer', sys.stdin)
        data = stdin_buffer.read()
    if opts.optimize is None:
        qr.add_data(data)
    else:
        qr.add_data(data, optimize=opts.optimize)

    if image_factory is None and os.isatty(sys.stdout.fileno()):
        qr.print_ascii(tty=True)
        return

    img = qr.make_image(image_factory=image_factory)

    sys.stdout.flush()
    # Use sys.stdout.buffer if available (Python 3), avoiding
    # UnicodeDecodeErrors.
    stdout_buffer = getattr(sys.stdout, 'buffer', None)
    if not stdout_buffer:
        if sys.platform == 'win32':  # pragma: no cover
            import msvcrt
            msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
        stdout_buffer = sys.stdout

    img.save(stdout_buffer)