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