forked from LuchoBazz/cpp-algorithm-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_vscode_snippets.py
73 lines (59 loc) · 2.02 KB
/
generate_vscode_snippets.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
import sys
import os
import json
import platform
HOME = os.path.expanduser("~")
OS = platform.system()
if OS in ['Windows', 'Win32']: # for windows
PATH = HOME + '/AppData/Roaming/Code/User/snippets/cpp.json'
elif OS == 'Linux': # for linux
PATH = HOME + '/.config/Code/User/snippets/cpp.json'
elif OS == 'Darwin': # for mac os
PATH = HOME + '/Library/Application Support/Code/User/snippets/cpp.json'
else:
print(OS)
sys.exit('unsupported operating system')
def add_snippets_to_vscode(root_path, filename, ext):
path = root_path + filename + '.' + ext
json_str = ''
with open(PATH, 'r') as json_data:
json_str += json_data.read()
snippets = json.loads(json_str)
code = []
with open(path, 'r') as reader:
code = reader.read().split('\n')
if 'template_' in filename:
for i in range(len(code)):
line = code[i]
line = line.replace(
'`!v strftime("%B %d, %Y")`',
'${CURRENT_MONTH_NAME} ${CURRENT_DATE}, ${CURRENT_YEAR}'
)
code[i] = line
data = {
'prefix': filename,
'body': code
}
snippets[filename] = data
snippets = json.dumps(snippets)
snippets = json.loads(snippets)
print('Snippet', filename, 'was generated')
with open(PATH, 'w') as outfile:
json.dump(snippets, outfile, indent=4)
def main():
basepath = './'
for directory in os.listdir(basepath):
if os.path.isdir(os.path.join(basepath, directory)):
dir_path = basepath + directory + '/'
for filename in os.listdir(dir_path):
fragments = filename.split('.')
ext = fragments.pop()
filename = ''
if len(fragments) > 0:
filename = fragments.pop()
else:
continue
if ext == 'cpp' and filename != '':
add_snippets_to_vscode(dir_path, filename, ext)
if __name__ == '__main__':
main()