gnunet-qr.py: align help with reality
[oweals/gnunet.git] / src / util / gnunet-qr.py
1 import sys
2 import getopt
3 import subprocess
4 from sys import argv
5 try:
6     import zbar
7 except ImportError as e:
8     print('Cannot run gnunet-qr, please install the zbar module.')
9     print('For Debian, you can obtain it as "python-zbar".')
10     print('Upstream: http://zbar.sourceforge.net/')
11     sys.exit(1)
12
13
14 def help():
15     print('gnunet-qr\n\
16 Scan a QR code using a video device and import\n\
17 Arguments mandatory for long options are also mandatory for short options.\n\
18   -c, --config FILENAME      use configuration file FILENAME\n\
19   -d, --device DEVICE        use device DEVICE\n\
20   -s, --silent               do not show preview windows\n\
21   -h, --help                 print this help\n\
22   -v, --verbose              be verbose\n\
23 Report bugs to gnunet-developers@gnu.org.\n\
24 GNUnet home page: https://gnunet.org/\n\
25 General help using GNU software: https://www.gnu.org/gethelp/')
26
27
28 if __name__ == '__main__':
29     configuration = ''
30     device = '/dev/video0'
31     url = ''
32     verbose = False
33     silent = False
34     # Parse arguments
35     try:
36         opts, args = getopt.gnu_getopt(sys.argv[1:], "c:hd:sv", ["config", "help", "device", "silent", "verbose"])
37     except getopt.GetoptError as e:
38         help()
39         print(str(e))
40         exit(1)
41     for o, a in opts:
42         if o in ("-h", "--help"):
43             help()
44             sys.exit(0)
45         elif o in ("-c", "--config"):
46             configuration = a
47         elif o in ("-d", "--device"):
48             device = a
49         elif o in ("-s", "--silent"):
50             silent = True
51         elif o in ("-v", "--verbose"):
52             verbose = True
53     if (True == verbose):
54         print('Initializing')
55     # create a Processor
56     proc = zbar.Processor()
57
58     # configure the Processor
59     proc.parse_config('enable')
60
61     # initialize the Processor
62     try:
63         if (True == verbose):
64             print('Opening video device ' + device)
65         proc.init(device)
66     except Exception as e:
67         print('Failed to open device ' + device)
68         exit(1)
69
70     # enable the preview window
71     # if (True == silent):
72     #       proc.visible = True
73     # else:
74     #               proc.visible = False
75
76     proc.visible = True
77     # read at least one barcode (or until window closed)
78     try:
79         if (True == verbose):
80             print('Capturing')
81         proc.process_one()
82     except Exception as e:
83         # Window was closed without finding code
84         exit(1)
85
86     # hide the preview window
87     proc.visible = False
88
89     # extract results
90     for symbol in proc.results:
91         # do something useful with results
92         if (True == verbose):
93             print('Found ', symbol.type, ' symbol ', '"%s"' % symbol.data)
94         args = list()
95         args.append("gnunet-uri")
96         if (configuration != ''):
97             args.append(str("-c " + str(configuration)))
98         args.append(str(symbol.data))
99         cmd = ''
100         for a in args:
101             cmd += " " + str(a)
102         if (verbose):
103             print('Running `' + cmd +'`')
104         res = subprocess.call(args)
105         if (0 != res):
106             print('Failed to add URI ' + str(symbol.data))
107         else:
108             print('Added URI ' + str(symbol.data))
109         exit(res)
110     exit(1)