author | Edouard Tisserant <edouard@beremiz.fr> |
Thu, 05 Dec 2024 13:56:59 +0100 | |
changeset 4060 | d2f5eb3c7d6e |
parent 4038 | 4b47b4ce0f12 |
permissions | -rw-r--r-- |
4038
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
2 |
# -*- coding: utf-8 -*- |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
3 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
4 |
# See COPYING file for copyrights details. |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
5 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
6 |
from itertools import repeat, islice, chain |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
7 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
8 |
## URI parsing functions |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
9 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
10 |
def split_as_dict(s, sep, labels): |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
11 |
return dict(zip(labels, islice(chain(s.split(sep), repeat("")), len(labels)))) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
12 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
13 |
def parse_tcp(loc): |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
14 |
return split_as_dict(loc, ":", ["host", "port"]) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
15 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
16 |
def parse_sslpsk(loc): |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
17 |
locals().update(**split_as_dict(loc, "#", ["hostport", "ID"])) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
18 |
return dict(**parse_tcp(hostport), ID=ID) # type: ignore |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
19 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
20 |
def parse_serial(loc): |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
21 |
return split_as_dict(loc, "@", ["device", "baudrate"]) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
22 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
23 |
def parse_usb(loc): |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
24 |
return split_as_dict(loc, ":", ["VID", "PID", "serialnumber"]) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
25 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
26 |
## URI building functions |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
27 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
28 |
def build_tcp(fields): |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
29 |
if fields['port']: |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
30 |
return "{host}:{port}".format(**fields) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
31 |
return fields['host'] |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
32 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
33 |
def build_sslpsk(fields): |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
34 |
return "{hostport}#{ID}".format(hostport=build_tcp(fields), **fields) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
35 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
36 |
def build_serial(fields): |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
37 |
if fields['baudrate']: |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
38 |
return "{device}@{baudrate}".format(**fields) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
39 |
return fields['device'] |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
40 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
41 |
def build_usb(fields): |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
42 |
if fields['serialnumber']: |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
43 |
return "{VID}:{PID}:{serialnumber}".format(**fields) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
44 |
if fields['PID']: |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
45 |
return "{VID}:{PID}".format(**fields) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
46 |
return fields['VID'] |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
47 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
48 |
## Dialog fields definition |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
49 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
50 |
model_tcp = [('host', _("Host:")), |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
51 |
('port', _("Port:"))] |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
52 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
53 |
model_serial = [('device', _("Device:")), |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
54 |
('baudrate', _("Baud rate:"))] |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
55 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
56 |
model_usb = [('VID', _("Vendor ID:")), |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
57 |
('PID', _("Product ID:")), |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
58 |
('serialnumber', _("Serial number:"))] |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
59 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
60 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
61 |
## Schemes description |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
62 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
63 |
schemes_desc = [ |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
64 |
# ( scheme name , data model , use ID, parser , builder ) |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
65 |
("LOCAL", [], False, lambda x: {}, lambda x: ""), |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
66 |
("ERPC", model_tcp, False, parse_tcp, build_tcp ), |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
67 |
("ERPCS", model_tcp, True, parse_sslpsk, build_sslpsk), |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
68 |
("ERPC-SERIAL", model_serial, False, parse_serial, build_serial), |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
69 |
("ERPC-USB", model_usb, False, parse_usb, build_usb )] |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
70 |
|
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
71 |
per_scheme_model = {sch: desc for sch, *desc in schemes_desc} |
4b47b4ce0f12
IDE: refactor ERPC URI editor, add serial and USB URI types
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff
changeset
|
72 |