-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata2qr.py
executable file
·65 lines (59 loc) · 2.11 KB
/
data2qr.py
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
#!/usr/bin/env python
import os, traceback
import argparse
from data2qr import *
parser = argparse.ArgumentParser()
parser.add_argument('-m', dest='mode',
type=str, choices=['encode', 'd2q', 'decode', 'q2d', 'decode-raw'],
action='store', default='encode',
help='covert mode')
parser.add_argument('-o', dest='out_filename',
type=str,
action='store', default='a.out',
help='output file name')
#positional arguments
parser.add_argument('filenames', nargs='*', type=str,
help='file names')
args = parser.parse_args()
def write_file_script(filename, byte_arr):
content = data2code(byte_arr)
script = f"""
mkdir -p $(dirname {filename})
python -c 'from data2qr import *; f=open("{filename}", "wb"); f.write(code2data("{content}")); f.close()'
"""
return script
if args.mode in ('encode', 'd2q'):
big_str = ''
for filename in args.filenames:
with open(filename, 'rb') as f:
content = f.read()
script = write_file_script(filename, content)
big_str += script
encoded_str = data2code(big_str.encode('ascii'))
code2qrcode(encoded_str, n_jobs=-1)
elif args.mode in ('decode', 'q2d'):
decoded_filename = args.out_filename
try:
if os.path.exists(decoded_filename):
raise Exception(f'file {decoded_filename} already exists')
contents = []
for filename in args.filenames:
contents.append(qrcode2code(filename))
decoded_content = code2data(''.join(contents)).decode('ascii')
with open(decoded_filename, 'a') as f:
f.write(decoded_content)
except:
traceback.print_exc()
elif args.mode == 'decode-raw':
decoded_filename = args.out_filename
if os.path.exists(decoded_filename):
raise Exception(f'file {decoded_filename} already exists')
for filename in args.filenames:
try:
with open(filename, 'r') as f:
content = f.read()
decoded_content = code2data(content).decode('ascii')
with open(decoded_filename, 'a') as f:
f.write(decoded_content)
except:
traceback.print_exc()