diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..27ffc2f1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.pyc +build diff --git a/qrcode/main.py b/qrcode/main.py index 3024c66e..a09778f3 100644 --- a/qrcode/main.py +++ b/qrcode/main.py @@ -127,6 +127,35 @@ def best_mask_pattern(self): return pattern + def print_tty(self, out=None ): + """ + Output the QR Code to a TTY. (usefull for debugging?) + + If the data has not been compiled yet, make it first. + """ + if out is None: + import sys + out = sys.stdout + + if not out.isatty(): + raise OSError( "not a tty" ) + + if self.data_cache is None: + self.make() + + modcount = self.modules_count + out.write( "\x1b[1;47m" + (" "*(modcount*2+4)) + "\x1b[0m\n" ) + for r in range(modcount): + out.write( "\x1b[1;47m \x1b[0m" ) + for c in range(modcount): + if self.modules[r][c]: + out.write( " " ) + else: + out.write( "\x1b[1;47m \x1b[0m" ) + out.write( "\x1b[1;47m \x1b[0m\n" ) + out.write( "\x1b[1;47m" + (" "*(modcount*2+4)) + "\x1b[0m\n" ) + out.flush() + def make_image(self): """ Make a PIL image from the QR Code data. @@ -267,3 +296,4 @@ def map_data(self, data, mask_pattern): row -= inc inc = -inc break +# vim: expandtab tabstop=4 softtabstop=4 shiftwidth=4 textwidth=79: diff --git a/scripts/qr b/scripts/qr new file mode 100755 index 00000000..b89caf93 --- /dev/null +++ b/scripts/qr @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# qr - Convert stdin 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 a PNG image is written. +# +# Koen Bollen, 2012 +# + +import sys +import qrcode + +def main(): + qr = qrcode.QRCode() + qr.add_data( sys.stdin.read() ) + + if sys.stdout.isatty(): + qr.print_tty() + return + + img = qr.make_image() + img.save( sys.stdout, "PNG" ) + +if __name__ == "__main__": + main() + +# vim: expandtab tabstop=4 softtabstop=4 shiftwidth=4 textwidth=79: diff --git a/setup.py b/setup.py index 795cbdb6..8e1bc701 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,9 @@ packages=[ 'qrcode', ], + scripts = [ + 'scripts/qr', + ], package_data={'': ['LICENSE']}, classifiers=[ 'Development Status :: 3 - Alpha', @@ -26,3 +29,5 @@ 'Topic :: Software Development :: Libraries :: Python Modules', ], ) + +# vim: expandtab tabstop=4 softtabstop=4 shiftwidth=4 textwidth=79: