Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
_小猫_
code-generator
提交
5299c033
C
code-generator
项目概览
_小猫_
/
code-generator
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
2
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
code-generator
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
2
合并请求
2
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5299c033
编写于
6月 26, 2020
作者:
W
wjmcat
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
complete simple version of code-generator
上级
864e03e6
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
148 addition
and
19 deletion
+148
-19
examples/config/base_model.json
examples/config/base_model.json
+10
-0
examples/plugins/base.plugin.jinja2
examples/plugins/base.plugin.jinja2
+10
-0
examples/templates/index.html.jinja2
examples/templates/index.html.jinja2
+15
-0
plugins/base.plugin.jinja2
plugins/base.plugin.jinja2
+0
-0
setup.py
setup.py
+53
-0
src/code_generator/config.py
src/code_generator/config.py
+3
-1
src/code_generator/loader.py
src/code_generator/loader.py
+22
-10
src/code_generator/main.py
src/code_generator/main.py
+35
-8
未找到文件。
examples/config/base_model.json
0 → 100644
浏览文件 @
5299c033
{
"model"
:
"基础模型"
,
"columns"
:
[{
"name"
:
"1"
},{
"name"
:
"22"
},{
"name"
:
"33"
}]
}
\ No newline at end of file
examples/plugins/base.plugin.jinja2
0 → 100644
浏览文件 @
5299c033
{%- macro number_input(column) -%}
<h1>number_input</h1>
<h2>{{ column.name }}</h2>
{% endmacro -%}
{%- macro date_input(column) -%}
<h1>date_input</h1>
<h3>{{ column.name }}</h3>
{% endmacro -%}
examples/templates/index.html.jinja2
0 → 100644
浏览文件 @
5299c033
<h1>{{ model }}</h1>
<ul>
{%- for column in columns %}
<li>
{%- if column.name == "1" %}
{{ number_input(column) }}
{%- elif column.name == "22" %}
{{ date_input(column) }}
{%- else %}
<h1>ssss</h1>
{%- endif %}
</li>
{%- endfor %}
</ul>
\ No newline at end of file
plugins/base.plugin.jinja2
已删除
100644 → 0
浏览文件 @
864e03e6
setup.py
0 → 100644
浏览文件 @
5299c033
"""
Code Generator - https://github.com/wj-Mcat/code-generator
Authors: Jingjing WU (吴京京) <https://github.com/wj-Mcat>
2020-now @ Copyright wj-Mcat
Licensed under the Apache License, Version 2.0 (the 'License');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an 'AS IS' BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from
setuptools
import
setup
,
find_packages
with
open
(
"./VERSION"
,
'r+'
)
as
f
:
VERSION
=
f
.
read
()
with
open
(
'./requirements.txt'
,
'r+'
)
as
f
:
requirements
=
f
.
readlines
()
setup
(
name
=
'code-generator'
,
version
=
VERSION
,
description
=
"a simple code generator for "
,
long_description
=
'a templated code generator for all language, especially '
'for boring code'
,
keywords
=
'code-gen,code-generator,python,code-snippet'
,
author
=
'wj-Mcat'
,
author_email
=
'1435130236@qq.com'
,
license
=
'Apache 2'
,
packages
=
find_packages
(
'src'
),
package_dir
=
{
""
:
"src"
},
include_package_data
=
True
,
package_data
=
{
'code_generator'
:
[
'vue/*'
,
'flask/*'
,
'element.css'
,
'index.js'
,
'jquery.min.js'
,
'template.html'
,
'vue.js'
]
},
install_requires
=
requirements
,
entry_points
=
{
'console_scripts'
:
[
'code-gen = code_generator.main:main'
]
}
)
\ No newline at end of file
src/code_generator/config.py
浏览文件 @
5299c033
...
...
@@ -21,11 +21,13 @@ from __future__ import annotations
import
logging
from
jinja2
import
Template
def
get_logger
():
"""get the logger"""
log_formatter
=
logging
.
Formatter
(
fmt
=
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
fmt
=
'%(asctime)s - %(name)s - %(levelname)s - %(
filename)s - %(funcName)s - %(
message)s'
)
logger
=
logging
.
getLogger
(
"CodeGenerator"
)
logger
.
handlers
=
[]
...
...
src/code_generator/loader.py
浏览文件 @
5299c033
...
...
@@ -19,7 +19,7 @@ limitations under the License.
"""
from
__future__
import
annotations
import
os
from
typing
import
Dict
from
typing
import
Dict
,
Optional
from
abc
import
ABCMeta
from
jinja2
import
Template
...
...
@@ -39,7 +39,7 @@ class Loader(metaclass=ABCMeta):
lazy load plugins
"""
self
.
files
:
Dict
[
str
,
str
]
=
{}
log
.
info
(
'load the
plugin
s from : %s'
,
file_or_dir
)
log
.
info
(
'load the
file
s from : %s'
,
file_or_dir
)
if
not
os
.
path
.
exists
(
file_or_dir
):
log
.
warning
(
'plugin file/dir doesn"t exist, there are '
'no plugins to load'
)
...
...
@@ -82,11 +82,17 @@ class TemplateLoader(Loader):
def
__init__
(
self
,
file_or_dir
:
str
=
'templates'
):
super
(
TemplateLoader
,
self
).
__init__
(
file_or_dir
)
def
load_templates
(
self
)
->
Dict
[
str
,
Template
]:
def
load_templates
(
self
,
plugins
:
Optional
[
str
]
=
None
)
->
Dict
[
str
,
Template
]:
"""load template"""
self
.
_load_files
()
templates
:
Dict
[
str
,
Template
]
=
{}
for
key
,
template
in
self
.
files
.
items
():
if
plugins
is
not
None
:
template
=
"{plugin}
\n
{template}"
.
format
(
plugin
=
plugins
,
template
=
template
)
log
.
info
(
template
)
templates
[
key
]
=
Template
(
template
)
return
templates
...
...
@@ -102,7 +108,7 @@ class PluginLoader(Loader):
def
load_to_file
(
self
)
->
str
:
files
=
self
.
_load_plugins
()
return
'
\n
'
.
join
(
files
)
return
'
\n
'
.
join
(
files
.
values
()
)
def
load_plugin_descriptions
(
self
)
->
Dict
[
str
,
str
]:
descriptions
:
Dict
[
str
,
str
]
=
{}
...
...
@@ -113,12 +119,18 @@ class PluginLoader(Loader):
attrs
=
dir
(
module
)
macros
=
[(
attr
,
getattr
(
module
,
attr
))
for
attr
in
attrs
if
isinstance
(
getattr
(
module
,
attr
),
Macro
)]
# find macros description from html node description using bs4
# first we only load the name of plugin
for
macro_name
,
macro
in
macros
:
if
macro_name
not
in
descriptions
:
descriptions
[
macro_name
]
=
macro_name
return
descriptions
def
load_to_module
(
self
):
"""
load plugins to the instance of module type
:return:
"""
plugin_content
=
self
.
load_to_file
()
module
=
Template
(
plugin_content
).
module
return
module
src/code_generator/main.py
浏览文件 @
5299c033
...
...
@@ -18,7 +18,12 @@ See the License for the specific language governing permissions and
limitations under the License.
"""
from
__future__
import
annotations
import
json
from
argparse
import
ArgumentParser
import
os
from
jinja2
import
Template
from
.config
import
get_logger
from
.loader
import
TemplateLoader
,
PluginLoader
...
...
@@ -32,12 +37,33 @@ def run_server():
pass
def
render_by_config
():
file
=
args
[
'file'
]
templates
=
TemplateLoader
(
file
).
load_templates
()
plugin_file
=
PluginLoader
(
file
).
load_to_file
()
def
_save_to_file
(
template_dir
:
str
,
file_name
:
str
,
content
:
str
):
if
not
os
.
path
.
exists
(
template_dir
):
os
.
mkdir
(
template_dir
)
path
=
os
.
path
.
join
(
template_dir
,
file_name
)
with
open
(
path
,
'w'
,
encoding
=
'utf-8'
)
as
f
:
f
.
write
(
content
)
def
_read_to_json
(
file
:
str
)
->
dict
:
"""read json file to json data"""
file_path
=
os
.
path
.
join
(
os
.
getcwd
(),
file
)
print
(
file_path
)
if
not
os
.
path
.
exists
(
file_path
):
raise
FileNotFoundError
(
f
'file <
{
file
}
> not found'
)
with
open
(
file_path
,
'r+'
,
encoding
=
'utf-8'
)
as
f
:
return
json
.
load
(
f
)
def
render_by_config
():
config
=
_read_to_json
(
args
[
'config'
])
plugins
=
PluginLoader
(
args
[
'plugins'
]).
load_to_file
()
log
.
info
(
plugins
)
templates
=
TemplateLoader
(
args
[
'templates'
]).
load_templates
(
plugins
)
for
name
,
template
in
templates
.
items
():
result
=
template
.
render
(
config
)
base_name
=
os
.
path
.
basename
(
args
[
'templates'
])
+
'_result'
_save_to_file
(
base_name
,
name
,
result
)
def
main
():
...
...
@@ -49,15 +75,16 @@ def main():
serve_parser
.
add_argument
(
'--file'
,
default
=
'config.json'
,
type
=
str
)
serve_parser
.
set_defaults
(
func
=
run_server
)
config_parser
=
subparsers
.
add_parser
(
name
=
'config'
)
config_parser
.
add_argument
(
'--file'
,
default
=
'config.json'
,
type
=
str
)
config_parser
.
add_argument
(
'--template'
,
default
=
'flask,vue'
,
type
=
str
)
config_parser
=
subparsers
.
add_parser
(
name
=
'render'
)
config_parser
.
add_argument
(
'--config'
,
type
=
str
,
default
=
'./config.json'
)
config_parser
.
add_argument
(
'--templates'
,
type
=
str
,
default
=
'./templates'
)
config_parser
.
add_argument
(
'--plugins'
,
type
=
str
,
default
=
'./plugins'
)
config_parser
.
set_defaults
(
func
=
render_by_config
)
local_args
=
parser
.
parse_args
()
log
.
info
(
args
)
args
.
update
(
local_args
.
__dict__
)
local_args
.
func
(
args
)
local_args
.
func
()
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录