main.py 3.0 KB
Newer Older
W
wjmcat 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
"""
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 __future__ import annotations
21 22

import json
W
wjmcat 已提交
23
from argparse import ArgumentParser
24 25
import os

W
wjmcat 已提交
26 27 28
from .config import get_logger
from .loader import TemplateLoader, PluginLoader

W
wjmcat 已提交
29
# pylint: disable=invalid-name
W
wjmcat 已提交
30
log = get_logger()
W
wjmcat 已提交
31 32
# pylint: disable=invalid-name
args: dict = {}
W
wjmcat 已提交
33 34 35


def run_server():
W
wjmcat 已提交
36
    """we can run a simple http server to display the code-generator"""
W
wjmcat 已提交
37 38


39
def _save_to_file(template_dir: str, file_name: str, content: str):
W
wjmcat 已提交
40
    """save template result to the file"""
41 42 43 44 45
    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)
W
wjmcat 已提交
46 47


48 49 50 51 52 53 54 55 56 57 58
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():
W
wjmcat 已提交
59
    """render the template with config file mode"""
60 61 62 63 64
    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():
W
wjmcat 已提交
65
        result = template.render(**config)
66 67
        base_name = os.path.basename(args['templates']) + '_result'
        _save_to_file(base_name, name, result)
W
wjmcat 已提交
68 69 70


def main():
W
wjmcat 已提交
71
    """main entry of the code-generator"""
W
wjmcat 已提交
72 73 74 75 76 77 78 79
    parser = ArgumentParser()
    subparsers = parser.add_subparsers(help='sub command')
    serve_parser = subparsers.add_parser(name='serve')
    serve_parser.add_argument('--model', default='models', type=str)
    serve_parser.add_argument('--port', default=5002, type=int)
    serve_parser.add_argument('--file', default='config.json', type=str)
    serve_parser.set_defaults(func=run_server)

80 81 82 83
    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')
W
wjmcat 已提交
84 85 86 87 88
    config_parser.set_defaults(func=render_by_config)

    local_args = parser.parse_args()
    log.info(args)
    args.update(local_args.__dict__)
89
    local_args.func()
W
wjmcat 已提交
90 91 92 93


if __name__ == '__main__':
    main()