-
Notifications
You must be signed in to change notification settings - Fork 14
/
makeformats.py
executable file
·122 lines (89 loc) · 2.3 KB
/
makeformats.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/python
#
# Uli Meis <a.sporto+bee@gmail.com>
#
# Just a short script to generate our FORMAT_REC
#
import sys,os,re
lines = map(lambda x: x.strip(),open(sys.argv[1],"r").readlines())
hdr = open("otr-formats.h","w")
src = open("otr-formats.c","w")
srcx = open("xchat-formats.c","w")
src.write('#include "otr.h"\n');
srcx.write('#include "otr.h"\n');
src.write("""char *otr_help = "%s";\n""" % "\\n".join(
["%9- OTR help -%9"]+
[re.sub('^(/otr.*)$','%_\\1%_',
re.sub('^(otr_[a-z_]*)$','%_\\1%_',
re.sub('"([^"]*)"','\\"%_\\1%_\\"',
x.replace('\n','').replace("\t"," ")
)))
for x in open(sys.argv[2],"r").readlines()]
+["%9- End of OTR help -%9"]
))
src.write('FORMAT_REC formats[] = {\n')
srcx.write('FORMAT_REC formats[] = {\n')
src.write('{ MODULE_NAME, "otr", 0}\n')
srcx.write('{ MODULE_NAME, "otr", 0}\n')
hdr.write("extern char *otr_help;\n\n");
hdr.write("enum {\n")
hdr.write("TXT_OTR_MODULE_NAME")
fills = 0
section = None
for line in lines:
src.write(",\n")
srcx.write(",\n")
e = line.split("\t")
if len(e)==1:
# Section name
section = e[0]
src.write("""{ NULL, "%s", 0 }\n""" % (e[0]))
srcx.write("""{ NULL, "%s", 0 }\n""" % (e[0]))
hdr.write(",\nTXT_OTR_FILL_%d" % fills)
fills += 1
continue
params = []
fo = e[1]
new = ""
last=0
i=0
srcx.write("""{ "%s", "%s", 0""" % (e[0],fo.replace("%%9","").replace("%9","").replace("%g","").replace("%n","")))
for m in re.finditer("(^|[^%])%([0-9]*)[ds]",fo):
if m.group()[-1]=='d':
params += ['1']
else:
params += ['0']
new += fo[last:m.start()+len(m.group(1))].replace('%%','%')+"$"
if m.group(2): new+= "[%s]" % m.group(2)
new += "%d" % i
last = m.end()
i += 1
new += fo[last:].replace('%%','%')
e[1] = new
e += [len(params)] + params
#print "Handling line %s with elen %d" % (line,len(e))
premsg = ""
if e[1][0] != "{" and section!="Nickignore" and section!="Contexts":
premsg = "%9OTR%9: "
src.write("""{ "%s", "%s%s", %s""" % (e[0],premsg,e[1],e[2]))
if len(params)>0:
src.write(", { %s }" % ", ".join(params))
src.write("}")
srcx.write("}")
hdr.write(",\n")
hdr.write("TXT_%s" % e[0].upper())
hdr.write("""
};
extern FORMAT_REC formats[];
""")
src.write(""",
{ NULL, NULL, 0 }
};
""")
srcx.write(""",
{ NULL, NULL, 0 }
};
""")
hdr.close()
src.close()
srcx.close()