edouard@4038: #!/usr/bin/env python edouard@4038: # -*- coding: utf-8 -*- edouard@4038: edouard@4038: # See COPYING file for copyrights details. edouard@4038: edouard@4038: from itertools import repeat, islice, chain edouard@4038: edouard@4038: ## URI parsing functions edouard@4038: edouard@4038: def split_as_dict(s, sep, labels): edouard@4038: return dict(zip(labels, islice(chain(s.split(sep), repeat("")), len(labels)))) edouard@4038: edouard@4038: def parse_tcp(loc): edouard@4038: return split_as_dict(loc, ":", ["host", "port"]) edouard@4038: edouard@4038: def parse_sslpsk(loc): edouard@4038: locals().update(**split_as_dict(loc, "#", ["hostport", "ID"])) edouard@4038: return dict(**parse_tcp(hostport), ID=ID) # type: ignore edouard@4038: edouard@4038: def parse_serial(loc): edouard@4038: return split_as_dict(loc, "@", ["device", "baudrate"]) edouard@4038: edouard@4038: def parse_usb(loc): edouard@4038: return split_as_dict(loc, ":", ["VID", "PID", "serialnumber"]) edouard@4038: edouard@4038: ## URI building functions edouard@4038: edouard@4038: def build_tcp(fields): edouard@4038: if fields['port']: edouard@4038: return "{host}:{port}".format(**fields) edouard@4038: return fields['host'] edouard@4038: edouard@4038: def build_sslpsk(fields): edouard@4038: return "{hostport}#{ID}".format(hostport=build_tcp(fields), **fields) edouard@4038: edouard@4038: def build_serial(fields): edouard@4038: if fields['baudrate']: edouard@4038: return "{device}@{baudrate}".format(**fields) edouard@4038: return fields['device'] edouard@4038: edouard@4038: def build_usb(fields): edouard@4038: if fields['serialnumber']: edouard@4038: return "{VID}:{PID}:{serialnumber}".format(**fields) edouard@4038: if fields['PID']: edouard@4038: return "{VID}:{PID}".format(**fields) edouard@4038: return fields['VID'] edouard@4038: edouard@4038: ## Dialog fields definition edouard@4038: edouard@4038: model_tcp = [('host', _("Host:")), edouard@4038: ('port', _("Port:"))] edouard@4038: edouard@4038: model_serial = [('device', _("Device:")), edouard@4038: ('baudrate', _("Baud rate:"))] edouard@4038: edouard@4038: model_usb = [('VID', _("Vendor ID:")), edouard@4038: ('PID', _("Product ID:")), edouard@4038: ('serialnumber', _("Serial number:"))] edouard@4038: edouard@4038: edouard@4038: ## Schemes description edouard@4038: edouard@4038: schemes_desc = [ edouard@4038: # ( scheme name , data model , use ID, parser , builder ) edouard@4038: ("LOCAL", [], False, lambda x: {}, lambda x: ""), edouard@4038: ("ERPC", model_tcp, False, parse_tcp, build_tcp ), edouard@4038: ("ERPCS", model_tcp, True, parse_sslpsk, build_sslpsk), edouard@4038: ("ERPC-SERIAL", model_serial, False, parse_serial, build_serial), edouard@4038: ("ERPC-USB", model_usb, False, parse_usb, build_usb )] edouard@4038: edouard@4038: per_scheme_model = {sch: desc for sch, *desc in schemes_desc} edouard@4038: