|
hg@2
|
1 |
#!/usr/bin/env python
|
|
hg@2
|
2 |
|
|
hg@0
|
3 |
import re
|
|
hg@0
|
4 |
|
|
hg@0
|
5 |
FUNC_SKEL = """#contributor : Andrew Gwozdziewycz <web@apgwoz.com>
|
|
hg@0
|
6 |
#name : %(func_name)s(%(func_sig)s)
|
|
hg@0
|
7 |
# --
|
|
hg@0
|
8 |
%(func_name)s(%(func_args)s)$0"""
|
|
hg@0
|
9 |
|
|
hg@0
|
10 |
func_args_re = re.compile('([a-zA-Z0-9_]+) \((.*?)\)')
|
|
hg@0
|
11 |
|
|
hg@0
|
12 |
def split_func_args(file):
|
|
hg@0
|
13 |
fi = open(file)
|
|
hg@0
|
14 |
lines = [x for x in fi.xreadlines() if not x.startswith(' ')]
|
|
hg@0
|
15 |
fargs = []
|
|
hg@0
|
16 |
for n in lines:
|
|
hg@0
|
17 |
it = func_args_re.search(n)
|
|
hg@0
|
18 |
if it:
|
|
hg@0
|
19 |
print it.groups()
|
|
hg@0
|
20 |
fargs.append(it.groups())
|
|
hg@0
|
21 |
return fargs
|
|
hg@0
|
22 |
|
|
hg@0
|
23 |
def parse_args(args):
|
|
hg@2
|
24 |
args = args.split('[')
|
|
hg@2
|
25 |
argsl = args[0].split(',')
|
|
hg@0
|
26 |
argsout = []
|
|
hg@0
|
27 |
for arg in argsl:
|
|
hg@0
|
28 |
arg = arg.strip()
|
|
hg@0
|
29 |
pieces = arg.split()
|
|
hg@0
|
30 |
if len(pieces) == 2:
|
|
hg@0
|
31 |
argsout.append(pieces[1])
|
|
hg@2
|
32 |
if len(args) > 1 and len(args[1]):
|
|
hg@2
|
33 |
newa = '[' + args[1].rstrip()
|
|
hg@2
|
34 |
if not newa.endswith(']'):
|
|
hg@2
|
35 |
newa += ']'
|
|
hg@2
|
36 |
if len(argsout):
|
|
hg@2
|
37 |
argsout[-1] += newa
|
|
hg@2
|
38 |
else:
|
|
hg@2
|
39 |
argsout.append(newa)
|
|
hg@0
|
40 |
return argsout
|
|
hg@0
|
41 |
|
|
hg@0
|
42 |
def parse_definitions(file):
|
|
hg@0
|
43 |
functions = split_func_args(file)
|
|
hg@0
|
44 |
out = []
|
|
hg@0
|
45 |
for f in functions:
|
|
hg@0
|
46 |
args = parse_args(f[1])
|
|
hg@0
|
47 |
out.append((f[0], args))
|
|
hg@0
|
48 |
return out
|
|
hg@0
|
49 |
|
|
hg@0
|
50 |
def generate_snippets(outpath, defs):
|
|
hg@0
|
51 |
for d in defs:
|
|
hg@0
|
52 |
ellipsis = ', '.join(['...'] * len(d[1]))
|
|
hg@0
|
53 |
args = ', '.join(['${%s}' % x for x in d[1]])
|
|
hg@6
|
54 |
filename = d[0].replace('_', '.', 1).replace('_', '-')
|
|
hg@0
|
55 |
f = open(outpath + '/' + filename, 'w')
|
|
hg@0
|
56 |
f.write(FUNC_SKEL % {'func_sig': ellipsis,
|
|
hg@0
|
57 |
'func_name': d[0],
|
|
hg@0
|
58 |
'func_args': args})
|
|
hg@0
|
59 |
|
|
hg@0
|
60 |
if __name__ == "__main__":
|
|
hg@2
|
61 |
defs = parse_definitions("php-functions-with-args.txt")
|
|
hg@2
|
62 |
generate_snippets('php-mode', defs)
|