-
Notifications
You must be signed in to change notification settings - Fork 19
/
devspec.py
51 lines (37 loc) · 940 Bytes
/
devspec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from typing import NamedTuple
class NetDevSpec(NamedTuple):
method: str
target: str
port: int
unit: int = 0
def __str__(self):
return tostring(self)
class SerialDevSpec(NamedTuple):
method: str
target: str
rate: int
unit: int = 0
def __str__(self):
return tostring(self)
def tostring(d):
return ':'.join(map(str, d))
def create(*args, **kwargs):
method = args[0] if len(args) else kwargs.get('method', None)
if method in ['tcp', 'udp']:
return NetDevSpec(*args, **kwargs)
if method in ['ascii', 'rtu']:
return SerialDevSpec(*args, **kwargs)
raise Exception('Bad or missing method')
def fromstring(s):
d = s.split(':')
d[2] = int(d[2])
d[3] = int(d[3])
return create(*d)
def fromstrings(ss):
d = set()
for s in ss:
try:
d.add(fromstring(s))
except:
continue
return d