最简Flask框架

目前写了一个很简单根据配置文件动态生成flask框架模版的代码,简单示例如下:

创建代码如下

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
@click.command('create_project')
@click.option('--name', default='py_summer_demo', type=click.STRING, help='项目名称', prompt='项目名称', required=True)
@click.option('--path', default='./', type=click.STRING, help='项目路径', prompt='项目路径', required=True)
def create_project(name, path):
"""
创建新项目
:param name: 项目路径
:param path: 项目名称
:return:
"""
project_full_path = os.path.join(path, name)
if os.path.exists(project_full_path):
shutil.rmtree(project_full_path)
os.makedirs(project_full_path)
with open('./tpl/config.json', "r") as f:
config = json.load(f)
structure = config.get('structure')
_create_file_folder(structure, project_full_path)


def _create_file_folder(structure, project_full_path):
"""
根据配置文件夹创建文件和文件夹
:param structure: 配置文件
:param project_full_path: 项目路径
:return:
"""
for item in structure:
name = item.get('name')
is_folder = item.get('is_folder')
child = item.get('child')
current_path = os.path.join(project_full_path, name)

if is_folder:
_create_folder(current_path)
_create_file_folder(child, current_path)
else:
tpl = item.get('content')
_create_file(current_path, tpl)


def _create_folder(folder_path):
"""
创建文件夹
:param folder_path:
:return:
"""
if os.path.exists(folder_path):
shutil.rmtree(folder_path)
else:
os.makedirs(folder_path)


def _create_file(file_path, tpl_name):
"""
生成文件,根据模版
:param file_path:
:param tpl_name:
:return:
"""
tpl_file_path = os.path.join('.', 'tpl', tpl_name)
_buffer = open(tpl_file_path, 'rb')
with open(file_path, 'wb') as f:
f.write(_buffer.read())
_buffer.close()

配置文件如下

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
125
126
{
"structure": [
{
"name": "application",
"is_folder": true,
"child": [
{
"name": "controller",
"is_folder": true,
"child": [
{
"name": "helloController.py",
"is_folder": false,
"content": "helloController.tpl"
}
]
},
{
"name": "model",
"is_folder": true,
"child": [
{
"name": "test.py",
"is_folder": false,
"content": "test.tpl"
}
]
},
{
"name": "view",
"is_folder": true,
"child": [
{
"name": "helloHandler.py",
"is_folder": false,
"content": "helloHandler.tpl"
}
]
},
{
"name": "router.py",
"is_folder": false,
"content": "router.tpl"
},
{
"name": "__init__.py",
"is_folder": false,
"content": "__init__.tpl"
}
]
},
{
"name": "config",
"is_folder": true,
"child": [
{
"name": "config.py",
"is_folder": false,
"content": "config.tpl"
}
]
},
{
"name": "docker",
"is_folder": true,
"child": [
{
"name": "docker-compose.yml",
"is_folder": false,
"content": "docker-compose.tpl"
},
{
"name": "start.sh",
"is_folder": false,
"content": "start.tpl"
}
]
},
{
"name": "log",
"is_folder": true,
"child": []
},
{
"name": "test",
"is_folder": true,
"child": []
},
{
"name": "tools",
"is_folder": true,
"child": [
{
"name": "const.py",
"is_folder": false,
"content": "const.tpl"
},
{
"name": "error.py",
"is_folder": false,
"content": "error.tpl"
}
]
},
{
"name": ".gitignore",
"is_folder": false,
"content": ".gitignore.tpl"
},
{
"name": "gunicorn.conf",
"is_folder": false,
"content": "gunicorn.tpl"
},
{
"name": "requirements.txt",
"is_folder": false,
"content": "requirements.tpl"
},
{
"name": "server.py",
"is_folder": false,
"content": "server.tpl"
}
]
}

不足和拓展

  1. 目前是很简单的依靠模版来生成的
  2. 模版的内容就是生成结果的内容

生成的结果

https://github.com/rexyan/mature_flask/tree/auto_create_v1

精简的Flask框架