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