forked from lincolnloop/python-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_scripts.py
More file actions
executable file
·88 lines (74 loc) · 2.68 KB
/
console_scripts.py
File metadata and controls
executable file
·88 lines (74 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
"""
qr - Convert stdin (or the first argument) to a QR Code.
When stdout is a tty the QR Code is printed to the terminal and when stdout is
a pipe to a file an image is written. The default image format is PNG.
"""
import sys
import optparse
import qrcode
default_factories = {
'pil': 'qrcode.image.pil.PilImage',
'pymaging': 'qrcode.image.pure.PymagingImage',
'svg': 'qrcode.image.svg.SvgImage',
'svg-fragment': 'qrcode.image.svg.SvgFragmentImage',
'svg-path': 'qrcode.image.svg.SvgPathImage',
}
error_correction = {
'L': qrcode.ERROR_CORRECT_L,
'M': qrcode.ERROR_CORRECT_M,
'Q': qrcode.ERROR_CORRECT_Q,
'H': qrcode.ERROR_CORRECT_H,
}
def main(args=sys.argv[1:]):
parser = optparse.OptionParser(usage=__doc__.strip())
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=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:
data = sys.stdin.read()
if opts.optimize is None:
qr.add_data(data)
else:
qr.add_data(data, optimize=opts.optimize)
if image_factory is None and sys.stdout.isatty():
qr.print_ascii(tty=True)
return
img = qr.make_image(image_factory=image_factory)
sys.stdout.flush()
if sys.version_info[0] >= 3:
buff = sys.stdout.buffer
else:
if sys.platform == 'win32':
import os
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
buff = sys.stdout
img.save(buff)
if __name__ == "__main__":
main()