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