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