Skip to content

Commit b3cf62d

Browse files
refactor (qr_gen.py): add error handling and accept url arg
1 parent 6fecae4 commit b3cf62d

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

boilerplate/qr_gen.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,42 @@
11
#!/usr/bin/env python3
22

3+
import sys
4+
from datetime import datetime
35
from pathlib import Path
4-
from pyqrcode import QRCode
6+
7+
try:
8+
from pyqrcode import QRCode
9+
except ImportError:
10+
print("pyqrcode module not found. Install it with 'python -m pip install pyqrcode pypng'")
11+
sys.exit(1)
512

613
home = str(Path.home())
14+
downloads = Path(f"{home}/Downloads")
15+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
16+
17+
if len(sys.argv) == 2 and sys.argv[1].startswith("http"):
18+
url = QRCode(sys.argv[1])
19+
else:
20+
url = QRCode("https://github.com/pythoninthegrass/python_template")
21+
22+
23+
def check_file():
24+
"""
25+
Check if QR code already exists in ~/Downloads folder.
26+
If it does, rename it with a timestamp.
27+
"""
28+
if Path(f"{home}/Downloads/qr.png").exists():
29+
print("QR code already exists in ~/Downloads")
30+
new_file_name = Path(f"{downloads}/qr_{timestamp}.png")
31+
Path(f"{downloads}/qr.png").rename(new_file_name)
32+
print(f"QR code copied to Downloads folder as 'qr_{timestamp}.png'")
33+
34+
35+
def main():
36+
check_file()
37+
url.png(Path(f"{home}/Downloads/qr.png"), scale=8)
38+
print("QR code saved to Downloads folder as 'qr.png'")
39+
740

8-
url = QRCode("https://github.com/pythoninthegrass/python_template")
9-
url.png(Path(f"{home}/Downloads/repo.png"), scale=8)
41+
if __name__ == "__main__":
42+
main()

0 commit comments

Comments
 (0)