/
abbrev_to_ya.py
91 lines (71 loc) · 2.23 KB
/
abbrev_to_ya.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
# -*- coding: utf-8 -*-
# Filename: abbrev_to_ya.py
import re
import os
info = '''
# -*- mode: snippet -*-
# name: memcpy
# contributor: chenbin
# key: memcpy
# group: m
# --'''
abbrev_dir = '.'
str_in_test = '''accept(<field "int SOCKET">, <field "struct sockaddr *ADDR">, <field "socklen_t *LENGTH_PTR">)<endpoint>'''
field_p = r'(<field "([^>"]+)">)'
field_s = r'${\2}' # replace pattern
endpoint_p = '<endpoint>'
endpoint_s = ';$0' # replace pattern
def replace_abbrev_to_yasnippet(fn, contributor, abbrev_in):
"""replace abbrev to yasnippet
Arguments:
- `fn`: filename
- `contributor`: user who contribute this file
- `abbrev_in`: msf-abbrev string
"""
# first step: replace <field ...>
ya = re.sub(field_p, field_s, abbrev_in);
# second step: replace <endpoint>
ya = re.sub(endpoint_p, endpoint_s, ya);
# third step: strip newline
ya = ya.strip('\n')
name = "# name: %s" % fn
contributor = "contributor: %s" % contributor
key = "# key: %s" % fn
if '_' == fn[0]:
group = "# group: %s" % ("Others")
else:
group = "# group: %s" % fn[0].upper()
result = ["# -*- mode: snippet -*-",
name,
contributor,
key,
group,
"# --",
ya]
return '\n'.join(result)
def read_and_write_file(fin):
"""
Arguments:
- `fin`:
"""
with open(fin, 'r') as fi:
abbrev = fi.read()
# get rid of x append to filename
fout = fin
if 'x' == fin[- 1]:
fout = fin[:-1]
ya = replace_abbrev_to_yasnippet(fout,
'heychenbin@gmail.com',
abbrev)
with open(fout, 'w') as fo:
fo.write(ya)
if __name__ == '__main__':
str = replace_abbrev_to_yasnippet('accept',
'heychenbin@gmail.com',
str_in_test)
files = os.listdir(abbrev_dir)
i = 1
for f in files:
read_and_write_file(f)
print '[%d/%d]' % (i, len(files))
i = i + 1