-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·124 lines (105 loc) · 2.5 KB
/
generate.py
File metadata and controls
executable file
·124 lines (105 loc) · 2.5 KB
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
123
124
#!/usr/bin/python
# coding: utf-8
import argparse
import time
import os
# 生成新文件,自动生成一些注释和必要的内容
# 生成cpp文件,生成python文件
file_type_enum = {
'py': 'python',
'cpp': 'c++',
'h': 'c++_header',
'go': 'golang'
}
file_type = None
def check_invalid(args):
file = args.filename.split('.')[-1]
if file not in ('py', 'cpp', 'h', 'go'):
raise "filename illegal"
global file_type
file_type = file_type_enum[file]
def write_file(func):
def wrapper(filename):
content = func(filename)
with open(filename, 'a+') as f:
f.write(content)
return wrapper
@write_file
def generate_cpp_file(filename):
return f'''/*
@filename {filename}
@author caonan
@date {time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}
@reference
@url
@brief
*/
#include <cassert>
#include <climits>
#include <cstdio>
#include <algorithm>
#include <bitset>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int main(){{
return 0;
}}
'''
@write_file
def generate_cpp_h_file(filename):
return f'''/*
@filename {filename}
@author caonan
@date {time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}
@reference
@url
@brief
*/
#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <vector>'''
@write_file
def generate_python_file(filename):
return f'''#!/usr/bin/python
# coding: utf-8
#filename {filename}
#author caonan
#date {time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}
#reference
#url
#brief
if __name__ == "__main__":
pass
'''
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='code generater for cpp python...')
parser.add_argument('filename',
type=str,
help='file name eg: a.cpp,b.py,c.go')
args = parser.parse_args()
check_invalid(args)
if file_type == 'c++':
generate_cpp_file(args.filename)
elif file_type == 'python':
generate_python_file(args.filename)
elif file_type == 'c++_header':
generate_cpp_h_file(args.filename)
path = f'{os.getcwd()}/{args.filename}'
print(f'generate {args.filename} success in {path}')