aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-qr.py.in
blob: a5918fdf85f4365765803f5ed55fd219ca79e4f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!@PYTHON@
import sys
import getopt
import subprocess
from sys import argv
try:
    import zbar
except ImportError as e:
    print('Cannot run gnunet-qr, please install zbar-python')
    sys.exit(1)


def help():
    print('gnunet-qr\n\
Scan a QR code using a video device and import\n\
Arguments mandatory for long options are also mandatory for short options.\n\
  -c, --config=FILENAME      use configuration file FILENAME\n\
  -d, --device=DEVICE        use device DEVICE\n\
  -s, --silent               do not show preview windows\n\
  -h, --help                 print this help\n\
  -v, --verbose              be verbose\n\
Report bugs to gnunet-developers@gnu.org.\n\
GNUnet home page: http://www.gnu.org/software/gnunet/\n\
General help using GNU software: http://www.gnu.org/gethelp/')


if __name__ == '__main__':
        configuration = ''
        device = '/dev/video0'
        url = ''
        verbose = False
        silent = False
        # Parse arguments
        try:
                opts, args = getopt.gnu_getopt(sys.argv[1:], "c:hd:sv", ["config", "help", "device", "silent", "verbose"])
        except getopt.GetoptError as e:
                help()
                print(str(e))
                exit(1)
        for o, a in opts:
                if o in ("-h", "--help"):
                        help()
                        sys.exit(0)
                elif o in ("-c", "--config"):
                        configuration = a
                elif o in ("-d", "--device"):
                        device = a
                elif o in ("-s", "--silent"):
                        silent = True
                elif o in ("-v", "--verbose"):
                        verbose = True
        if (True == verbose):
                print('Initializing')
        # create a Processor
        proc = zbar.Processor()

        # configure the Processor
        proc.parse_config('enable')

        # initialize the Processor
        try:
                if (True == verbose):
                        print('Opening video device ' + device)
                proc.init(device)
        except Exception as e:
                print('Failed to open device ' + device)
                exit(1)

        # enable the preview window
        # if (True == silent):
        #       proc.visible = True
        # else:
        #               proc.visible = False

        proc.visible = True
        # read at least one barcode (or until window closed)
        try:
                if (True == verbose):
                        print('Capturing')
                proc.process_one()
        except Exception as e:
                # Window was closed without finding code
                exit(1)

        # hide the preview window
        proc.visible = False

        # extract results
        for symbol in proc.results:
                # do something useful with results
                if (True == verbose):
                        print('Found ', symbol.type, ' symbol ', '"%s"' % symbol.data)
                args = list()
                args.append("gnunet-uri")
                if (configuration != ''):
                        args.append(str("-c " + str(configuration)))
                args.append(str(symbol.data))
                cmd = ''
                for a in args:
                        cmd += " " + str(a)
                if (verbose):
                        print('Running `' + cmd +'`')
                res=subprocess.call(args)
                if (0 != res):
                        print('Failed to add URI ' + str(symbol.data))
                else:
                        print('Added URI ' + str(symbol.data))
                exit(res)
        exit(1)