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