Created
April 15, 2018 06:13
-
-
Save Mossuru777/4080c383f3ad7f54f7f6194dae9e36f1 to your computer and use it in GitHub Desktop.
Convert mode2 output to LIRC configuration file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
import re | |
import sys | |
def main(): | |
parser = argparse.ArgumentParser(description="mode2の出力をパースし、LIRCのRaw Code形式に変換して出力します") | |
parser.add_argument("fh", nargs="?", type=argparse.FileType("r"), default=sys.stdin) | |
args = parser.parse_args() | |
contents = args.fh.read() | |
try: | |
codes = parse(contents) | |
conf = create_conf(codes) | |
print(conf) | |
exit(0) | |
except ValueError as e: | |
print(e) | |
exit(1) | |
def create_conf(lines): | |
codes = " " + "\n ".join(lines) | |
return """\ | |
begin remote | |
name AirCon | |
flags RAW_CODES | |
eps 30 | |
aeps 100 | |
gap 0 | |
begin raw_codes | |
name Control | |
{} | |
end raw_codes | |
end remote | |
""".format(codes) | |
def parse(contents): | |
# 想定していない文字列が来たら例外送出 | |
if re.match("^(?!.*(?:Using driver|(?:space|pulse) \d+)).*$", contents, flags=re.IGNORECASE|re.MULTILINE) is not None: | |
raise ValueError("パースエラー: 「space 数値」か「pulse 数値」じゃない行があるよ") | |
# 各行の数値のみ抜き出して配列に | |
matches = re.findall("^(?:space|pulse) (\d+)$", contents, flags=re.IGNORECASE|re.MULTILINE) | |
# 数値配列をスペース区切りで結合して返す (1行は80字以下に抑える) | |
lines = [] | |
line_result = "" | |
for match in matches[1:]: # 最初の1つ(超長いspace)は捨ててループ | |
strs = ("" if line_result == "" else " ") + str(match) | |
if len(line_result + strs) > 80: | |
lines.append(line_result) | |
line_result = "" | |
strs = str(match) | |
line_result += strs | |
if line_result != "": | |
lines.append(line_result) | |
return lines | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment