-
Notifications
You must be signed in to change notification settings - Fork 48
/
create_lircrc.py
executable file
·82 lines (61 loc) · 2.04 KB
/
create_lircrc.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
import sys
import re
PROGNAME = "cec-ir-mqtt"
TEMPLATE = """
begin
remote = {remote}
button = {key}
prog = {prog}
config = {remote},{key}
end
"""
class Remote(object):
def __init__(self, name):
self.name = name
self.keys = []
def add_key(self, keyname):
self.keys.append(keyname)
class Parser(object):
def __init__(self):
self.remotes = []
def _parse_toplevel(self, line: str):
if re.match("^[ \t]*begin[ ]+remote.*", line):
return self._parse_remote
return self._parse_toplevel
def _parse_remote(self, line: str):
if re.match("^[ \t]*begin[ ]+codes.*", line):
return self._parse_keys
if re.match("^[ \t]*end[ ]+remote.*", line):
return self._parse_toplevel
match = re.match("^[ \t]*name[ \t]+(?P<name>[^ \t]+)([ \t].*)?$", line)
if match is not None:
self.remotes.append(Remote(match.group("name")))
return self._parse_remote
def _parse_keys(self, line: str):
if re.match("^[ \t]*end[ ]+codes.*", line):
return self._parse_remote
match = re.match("^[ \t]*(?P<KEY>[^ \t]+)[ \t]+(?P<CODE>0x[^ \t]+)([ \t].*)?$", line)
if match is not None:
self.remotes[-1].add_key(match.group("KEY"))
return self._parse_keys
def parse_file(self, filename):
parser = self._parse_toplevel
with open(filename, "r") as fn:
for line in fn.readlines():
line = line.strip()
if line.startswith("#"):
continue
parser = parser(line)
def print(self):
for remote in self.remotes:
for key in remote.keys:
block = TEMPLATE.format(prog=PROGNAME, remote=remote.name, key=key)
print(block)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: ./create_lircrc.py <lircd-cfg>")
sys.exit(1)
parser = Parser()
parser.parse_file(sys.argv[1])
parser.print()