File: defs2magic.py

package info (click to toggle)
ckb-next 0.6.2%2Bdfsg-0.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,732 kB
  • sloc: cpp: 20,009; ansic: 13,562; xml: 434; sh: 221; python: 74; makefile: 10
file content (28 lines) | stat: -rwxr-xr-x 688 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python3
import sys
import re
import os

defs = "../src/daemon/protocol.h"
defspath = os.path.dirname(__file__) + "/" + defs
defs_file = open(defspath, "r")
defs_list = defs_file.readlines()

cmds = []
for cmd in defs_list:
    match = re.match("#define[\s]+([\S]+)[\s]+(0x[\da-f]+|\d+)", cmd)
    if match:
        cmds.append((match.group(1), match.group(2)))
#print(str(cmds))
defs_file.close()

# Sort the list by string len desc
cmds.sort(key = lambda i: len(i[0]), reverse = True)

stdin = sys.stdin.readlines()
print()
for stdline in stdin:
    finalstr = stdline
    for cmd in cmds:
        finalstr = finalstr.replace(cmd[0], cmd[1])
    print(finalstr, end = "")