fix indentation, typo, improve logging
[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         for o,a in opts:
40                 if o in ("-h", "--help"):
41                         help ()            
42                         sys.exit (0)
43                 elif o in ("-c", "--config"):
44                         configuration = a
45                 elif o in ("-d", "--device"):
46                         device = a
47                 elif o in ("-s", "--silent"):
48                         silent = True
49                 elif o in ("-v", "--verbose"):
50                         verbose = True                  
51         if (True == verbose):
52                 print 'Initializing'
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                 if (True == verbose):
62                         print 'Opening video device ' + device
63                 proc.init(device)
64         except Exception as e:
65                 print 'Failed to open device ' + device
66                 exit (1)
67         
68         # enable the preview window
69         #if (True == silent):
70         #       proc.visible = True
71         #else:
72         #               proc.visible = False
73         
74         proc.visible = True
75         # read at least one barcode (or until window closed)
76         try:
77                 if (True == verbose):
78                         print 'Capturing'
79                 proc.process_one()
80         except Exception as e:
81                 # Window was closed without finding code
82                 exit (1)
83         
84         # hide the preview window
85         proc.visible = False
86         
87         # extract results
88         for symbol in proc.results:
89                 # do something useful with results
90                 if (True == verbose):
91                         print 'Found ', symbol.type, ' symbol ', '"%s"' % symbol.data
92                 args = list()
93                 args.append("gnunet-uri")
94                 if (configuration != ''):
95                         args.append (str("-c " + str(configuration)))
96                 args.append (str(symbol.data))
97                 cmd = ''
98                 for a in args:
99                         cmd += " " + str(a)
100                 if (verbose):
101                         print 'Running `' + cmd +'`'
102                 res=subprocess.call(args)
103                 if (0 != res):
104                         print 'Failed to add URI ' + str(symbol.data)
105                 else: 
106                         print 'Added URI ' + str(symbol.data)
107                 exit (res)
108         exit (1)