diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 617334c..c00e7dd 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -4,8 +4,6 @@ name: pytest on: - push: - branches: [ "main", "develop" ] pull_request: branches: [ "main", "develop" ] diff --git a/README.md b/README.md index 75f97e1..8c4e63f 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,32 @@ pip install rmqrcode ## 📕 Basic Usage +### CLI +Generate an rMQR Code image from your command line, use `rmqr` command: +```sh +rmqr 'Text data' 'my_qr.png' +``` + +See the help to list the options: +```sh +➜ rmqr -h +usage: rmqr [-h] [--ecc {M,H}] [--version VERSION] [--fit-strategy {min_width,min_height,balanced}] + DATA OUTPUT + +positional arguments: + DATA Data to encode. + OUTPUT Output file path + +optional arguments: + -h, --help show this help message and exit + --ecc {M,H} Error correction level. (default: M) + --version VERSION rMQR Code version like 'R11x139'. + --fit-strategy {min_width,min_height,balanced} + Strategy how to determine rMQR Code size. +``` + ### Generate rMQR Code +Alternatively, you can also use in python scripts: ```py from rmqrcode import rMQR import rmqrcode @@ -60,7 +85,7 @@ qr.make("https://oudon.xyz") ``` `R11x139` means 11 rows and 139 columns. The following table shows available combinations. - + | |27|43|59|77|99|139| |-|:-:|:-:|:-:|:-:|:-:|:-:| |R7|❌|✅|✅|✅|✅|✅| diff --git a/setup.cfg b/setup.cfg index 5086838..bdb177a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = rmqrcode -version = 0.1.1 +version = 0.1.2 author = Takahiro Tomita author_email = ttp8101@gmail.com description = An rMQR Code Generetor @@ -25,4 +25,8 @@ dev = pytest [options.packages.find] -where = src \ No newline at end of file +where = src + +[options.entry_points] +console_scripts = + rmqr = rmqrcode.console:main \ No newline at end of file diff --git a/src/rmqrcode/__init__.py b/src/rmqrcode/__init__.py index d9c4f19..2b40cee 100644 --- a/src/rmqrcode/__init__.py +++ b/src/rmqrcode/__init__.py @@ -3,4 +3,4 @@ from .rmqrcode import DataTooLongError from .rmqrcode import IllegalVersionError from .qr_image import QRImage -from .format.error_correction_level import ErrorCorrectionLevel +from .format.error_correction_level import ErrorCorrectionLevel \ No newline at end of file diff --git a/src/rmqrcode/console.py b/src/rmqrcode/console.py new file mode 100644 index 0000000..8e0324d --- /dev/null +++ b/src/rmqrcode/console.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +import rmqrcode +from rmqrcode import rMQR +from rmqrcode import QRImage +from rmqrcode import ErrorCorrectionLevel +from rmqrcode import FitStrategy +from rmqrcode import DataTooLongError +from rmqrcode import IllegalVersionError + +import argparse +import sys + + +def _show_error_and_exit(msg): + print(msg, file=sys.stderr) + sys.exit(1) + + +def _make_qr(data, ecc, version, fit_strategy): + if version == None: + qr = rMQR.fit(data, ecc=ecc, fit_strategy=fit_strategy) + else: + try: + qr = rMQR(version, ecc) + except IllegalVersionError: + _show_error_and_exit("Error: Illegal version.") + qr.make(data) + + return qr + + + +def _save_image(qr, output): + image = QRImage(qr) + try: + image.save(output) + except FileNotFoundError as e: + _show_error_and_exit(f"Error: {e}") + + +def main(): + parser = _init_argparser() + args = parser.parse_args() + + if args.ecc == 'M': + ecc = ErrorCorrectionLevel.M + elif args.ecc == 'H': + ecc = ErrorCorrectionLevel.H + + fit_strategy = FitStrategy.BALANCED + if args.fit_strategy == 'min_width': + fit_strategy = FitStrategy.MINIMIZE_WIDTH + elif args.fit_strategy == 'min_height': + fit_strategy = FitStrategy.MINIMIZE_HEIGHT + + qr = _make_qr( + args.DATA, + ecc=ecc, + version=args.version, + fit_strategy=fit_strategy + ) + + _save_image(qr, args.OUTPUT) + + print(f"{qr}") + + +def _init_argparser(): + parser = argparse.ArgumentParser() + parser.add_argument('DATA', type=str, help="Data to encode.") + parser.add_argument('OUTPUT', type=str, help="Output file path") + parser.add_argument('--ecc', help="Error correction level. (default: M)", type=str, choices=["M", "H"], default='M') + parser.add_argument('--version', help="rMQR Code version like 'R11x139'.") + parser.add_argument('--fit-strategy', choices=["min_width", "min_height", "balanced"], help="Strategy how to determine rMQR Code size.", dest="fit_strategy") + return parser + + +if __name__ == "__main__": + main() \ No newline at end of file