gnunet-qr.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import sys
  2. import getopt
  3. import subprocess
  4. from sys import argv
  5. try:
  6. import zbar
  7. except ImportError as e:
  8. print('Cannot run gnunet-qr, please install the zbar module.')
  9. print('For Debian, you can obtain it as "python-zbar".')
  10. print('Upstream: http://zbar.sourceforge.net/')
  11. sys.exit(1)
  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: https://gnunet.org/\n\
  23. General help using GNU software: https://www.gnu.org/gethelp/')
  24. if __name__ == '__main__':
  25. configuration = ''
  26. device = '/dev/video0'
  27. url = ''
  28. verbose = False
  29. silent = False
  30. # Parse arguments
  31. try:
  32. opts, args = getopt.gnu_getopt(sys.argv[1:], "c:hd:sv", ["config", "help", "device", "silent", "verbose"])
  33. except getopt.GetoptError as e:
  34. help()
  35. print(str(e))
  36. exit(1)
  37. for o, a in opts:
  38. if o in ("-h", "--help"):
  39. help()
  40. sys.exit(0)
  41. elif o in ("-c", "--config"):
  42. configuration = a
  43. elif o in ("-d", "--device"):
  44. device = a
  45. elif o in ("-s", "--silent"):
  46. silent = True
  47. elif o in ("-v", "--verbose"):
  48. verbose = True
  49. if (True == verbose):
  50. print('Initializing')
  51. # create a Processor
  52. proc = zbar.Processor()
  53. # configure the Processor
  54. proc.parse_config('enable')
  55. # initialize the Processor
  56. try:
  57. if (True == verbose):
  58. print('Opening video device ' + device)
  59. proc.init(device)
  60. except Exception as e:
  61. print('Failed to open device ' + device)
  62. exit(1)
  63. # enable the preview window
  64. # if (True == silent):
  65. # proc.visible = True
  66. # else:
  67. # proc.visible = False
  68. proc.visible = True
  69. # read at least one barcode (or until window closed)
  70. try:
  71. if (True == verbose):
  72. print('Capturing')
  73. proc.process_one()
  74. except Exception as e:
  75. # Window was closed without finding code
  76. exit(1)
  77. # hide the preview window
  78. proc.visible = False
  79. # extract results
  80. for symbol in proc.results:
  81. # do something useful with results
  82. if (True == verbose):
  83. print('Found ', symbol.type, ' symbol ', '"%s"' % symbol.data)
  84. args = list()
  85. args.append("gnunet-uri")
  86. if (configuration != ''):
  87. args.append(str("-c " + str(configuration)))
  88. args.append(str(symbol.data))
  89. cmd = ''
  90. for a in args:
  91. cmd += " " + str(a)
  92. if (verbose):
  93. print('Running `' + cmd +'`')
  94. res = subprocess.call(args)
  95. if (0 != res):
  96. print('Failed to add URI ' + str(symbol.data))
  97. else:
  98. print('Added URI ' + str(symbol.data))
  99. exit(res)
  100. exit(1)