Skip to content

Commit 22bf8ee

Browse files
authored
Merge pull request #5 from OUDON/feat/console-script
feat: Add the `rmqr` command
2 parents 74ef2f8 + 154d702 commit 22bf8ee

File tree

4 files changed

+111
-3
lines changed

4 files changed

+111
-3
lines changed

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,32 @@ pip install rmqrcode
1616

1717

1818
## 📕 Basic Usage
19+
### CLI
20+
Generate an rMQR Code image from your command line, use `rmqr` command:
21+
```sh
22+
rmqr 'Text data' 'my_qr.png'
23+
```
24+
25+
See the help to list the options:
26+
```sh
27+
➜ rmqr -h
28+
usage: rmqr [-h] [--ecc {M,H}] [--version VERSION] [--fit-strategy {min_width,min_height,balanced}]
29+
DATA OUTPUT
30+
31+
positional arguments:
32+
DATA Data to encode.
33+
OUTPUT Output file path
34+
35+
optional arguments:
36+
-h, --help show this help message and exit
37+
--ecc {M,H} Error correction level. (default: M)
38+
--version VERSION rMQR Code version like 'R11x139'.
39+
--fit-strategy {min_width,min_height,balanced}
40+
Strategy how to determine rMQR Code size.
41+
```
42+
1943
### Generate rMQR Code
44+
Alternatively, you can also use in python scripts:
2045
```py
2146
from rmqrcode import rMQR
2247
import rmqrcode
@@ -60,7 +85,7 @@ qr.make("https://oudon.xyz")
6085
```
6186

6287
`R11x139` means 11 rows and 139 columns. The following table shows available combinations.
63-
88+
6489
| |27|43|59|77|99|139|
6590
|-|:-:|:-:|:-:|:-:|:-:|:-:|
6691
|R7|||||||

setup.cfg

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ dev =
2525
pytest
2626

2727
[options.packages.find]
28-
where = src
28+
where = src
29+
30+
[options.entry_points]
31+
console_scripts =
32+
rmqr = rmqrcode.console:main

src/rmqrcode/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
from .rmqrcode import DataTooLongError
44
from .rmqrcode import IllegalVersionError
55
from .qr_image import QRImage
6-
from .format.error_correction_level import ErrorCorrectionLevel
6+
from .format.error_correction_level import ErrorCorrectionLevel

src/rmqrcode/console.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
import rmqrcode
3+
from rmqrcode import rMQR
4+
from rmqrcode import QRImage
5+
from rmqrcode import ErrorCorrectionLevel
6+
from rmqrcode import FitStrategy
7+
from rmqrcode import DataTooLongError
8+
from rmqrcode import IllegalVersionError
9+
10+
import argparse
11+
import sys
12+
13+
14+
def _show_error_and_exit(msg):
15+
print(msg, file=sys.stderr)
16+
sys.exit(1)
17+
18+
19+
def _make_qr(data, ecc, version, fit_strategy):
20+
if version == None:
21+
qr = rMQR.fit(data, ecc=ecc, fit_strategy=fit_strategy)
22+
else:
23+
try:
24+
qr = rMQR(version, ecc)
25+
except IllegalVersionError:
26+
_show_error_and_exit("Error: Illegal version.")
27+
qr.make(data)
28+
29+
return qr
30+
31+
32+
33+
def _save_image(qr, output):
34+
image = QRImage(qr)
35+
try:
36+
image.save(output)
37+
except FileNotFoundError as e:
38+
_show_error_and_exit(f"Error: {e}")
39+
40+
41+
def main():
42+
parser = _init_argparser()
43+
args = parser.parse_args()
44+
45+
if args.ecc == 'M':
46+
ecc = ErrorCorrectionLevel.M
47+
elif args.ecc == 'H':
48+
ecc = ErrorCorrectionLevel.H
49+
50+
fit_strategy = FitStrategy.BALANCED
51+
if args.fit_strategy == 'min_width':
52+
fit_strategy = FitStrategy.MINIMIZE_WIDTH
53+
elif args.fit_strategy == 'min_height':
54+
fit_strategy = FitStrategy.MINIMIZE_HEIGHT
55+
56+
qr = _make_qr(
57+
args.DATA,
58+
ecc=ecc,
59+
version=args.version,
60+
fit_strategy=fit_strategy
61+
)
62+
63+
_save_image(qr, args.OUTPUT)
64+
65+
print(f"{qr}")
66+
67+
68+
def _init_argparser():
69+
parser = argparse.ArgumentParser()
70+
parser.add_argument('DATA', type=str, help="Data to encode.")
71+
parser.add_argument('OUTPUT', type=str, help="Output file path")
72+
parser.add_argument('--ecc', help="Error correction level. (default: M)", type=str, choices=["M", "H"], default='M')
73+
parser.add_argument('--version', help="rMQR Code version like 'R11x139'.")
74+
parser.add_argument('--fit-strategy', choices=["min_width", "min_height", "balanced"], help="Strategy how to determine rMQR Code size.", dest="fit_strategy")
75+
return parser
76+
77+
78+
if __name__ == "__main__":
79+
main()

0 commit comments

Comments
 (0)