ability to specify a single code from the command line

This commit is contained in:
Davide Alberani 2017-04-15 16:20:26 +02:00
parent f75e6f6302
commit d44217542c

View file

@ -25,6 +25,7 @@ import time
import serial import serial
import urllib import urllib
import logging import logging
import argparse
import requests import requests
import configparser import configparser
from requests.packages.urllib3.exceptions import InsecureRequestWarning from requests.packages.urllib3.exceptions import InsecureRequestWarning
@ -37,6 +38,31 @@ logging.getLogger('urllib3').setLevel(logging.WARNING)
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) 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(): class Connector():
def __init__(self, cfg): def __init__(self, cfg):
self.cfg = cfg self.cfg = cfg
@ -75,7 +101,7 @@ class Connector():
code = code[:limit_field] code = code[:limit_field]
params = {cfg['event']['field']: code, '_errorMessage': 'code: %s' % code} params = {cfg['event']['field']: code, '_errorMessage': 'code: %s' % code}
checkin_url = self.checkin_url + '?' + urllib.parse.urlencode(params) 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) req = self.session.put(checkin_url, json=json)
error = False error = False
try: try:
@ -114,11 +140,20 @@ def scan(port):
if __name__ == '__main__': if __name__ == '__main__':
cfg = configparser.ConfigParser() parser = argparse.ArgumentParser()
cfg.read('qrcode_reader.ini') 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'): if cfg['qrcode_reader'].getboolean('debug'):
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
connector = Connector(cfg) connector = Connector(cfg)
if args.code:
connector.checkin(args.code)
else:
try: try:
for code in scan(port=cfg['connection']['port']): for code in scan(port=cfg['connection']['port']):
connector.checkin(code) connector.checkin(code)