From d44217542cd1916cd7d85dfd5466b1977ae8da12 Mon Sep 17 00:00:00 2001 From: Davide Alberani Date: Sat, 15 Apr 2017 16:20:26 +0200 Subject: [PATCH] ability to specify a single code from the command line --- tools/qrcode_reader.py | 51 +++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/tools/qrcode_reader.py b/tools/qrcode_reader.py index 1ee3c57..b52287c 100755 --- a/tools/qrcode_reader.py +++ b/tools/qrcode_reader.py @@ -25,6 +25,7 @@ import time import serial import urllib import logging +import argparse import requests import configparser from requests.packages.urllib3.exceptions import InsecureRequestWarning @@ -37,6 +38,31 @@ logging.getLogger('urllib3').setLevel(logging.WARNING) requests.packages.urllib3.disable_warnings(InsecureRequestWarning) +def convert_obj(obj): + try: + return int(obj) + except: + pass + if isinstance(obj, str): + obj_l = obj.lower() + if obj_l in ['true', 'on', 'yes']: + return True + elif obj_l in ['false', 'off', 'no']: + return False + return obj + + +def convert(seq): + if isinstance(seq, dict): + d = {} + for key, item in seq.items(): + d[key] = convert(item) + return d + if isinstance(seq, (list, tuple)): + return [convert(x) for x in seq] + return convert_obj(seq) + + class Connector(): def __init__(self, cfg): self.cfg = cfg @@ -75,7 +101,7 @@ class Connector(): code = code[:limit_field] params = {cfg['event']['field']: code, '_errorMessage': 'code: %s' % code} checkin_url = self.checkin_url + '?' + urllib.parse.urlencode(params) - json = dict(self.cfg['actions']) + json = convert(dict(self.cfg['actions'])) req = self.session.put(checkin_url, json=json) error = False try: @@ -114,13 +140,22 @@ def scan(port): if __name__ == '__main__': - cfg = configparser.ConfigParser() - cfg.read('qrcode_reader.ini') + parser = argparse.ArgumentParser() + parser.add_argument('-c', '--code', help='specify a single code', action='store') + parser.add_argument('--config', help='user a different configuration file (default: qrcode_reader.ini)', + action='store', default='qrcode_reader.ini') + args = parser.parse_args() + + cfg = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation()) + cfg.read(args.config) if cfg['qrcode_reader'].getboolean('debug'): logging.basicConfig(level=logging.DEBUG) connector = Connector(cfg) - try: - for code in scan(port=cfg['connection']['port']): - connector.checkin(code) - except KeyboardInterrupt: - logger.info('exiting...') + if args.code: + connector.checkin(args.code) + else: + try: + for code in scan(port=cfg['connection']['port']): + connector.checkin(code) + except KeyboardInterrupt: + logger.info('exiting...')