calling gnunet-uri
[oweals/gnunet.git] / src / util / gnunet-qr.py.in
1 #!/usr/bin/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         
32         silent = False
33         # Parse arguments
34         try:
35                 opts, args = getopt.gnu_getopt(sys.argv[1:], "c:hd:sv", ["config","help", "device","silent","verbose"])
36         except getopt.GetoptError as e:
37                 help ()
38                 print str (e)
39                 exit (1)
40
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                                 silent = True                           
53         # create a Processor
54         proc = zbar.Processor()
55
56         # configure the Processor
57         proc.parse_config('enable')
58
59         # initialize the Processor
60         try:
61                 proc.init(device)
62         except Exception as e:
63                 print 'Failed to open device ' + device
64                 exit (1)
65         
66         # enable the preview window
67         if (True == silent):
68                 proc.visible = True
69         else:
70                 proc.visible = False
71         
72         # read at least one barcode (or until window closed)
73         try:
74                 proc.process_one()
75         except Exception as e:
76                 # Window was closed without finding code
77                 exit (1)
78         
79         # hide the preview window
80         proc.visible = False
81         
82         # extract results
83         for symbol in proc.results:
84                 # do something useful with results
85                 if (verbose):
86                         print 'Found ', symbol.type, ' symbol ', '"%s"' % symbol.data
87                 args = list()
88                 args.append("gnunet-uri")
89                 if (configuration != ''):
90                         args.append ("-c " + str(configuration))
91                 args.append (url)
92                 if (verbose):
93                         for a in args:
94                                 cmd += str (a)
95                         print 'Running ' + cmd
96                         ls_output=subprocess.call(args)
97                         print ls_output
98                         exit (ls_output)
99                 
100