main.py 2.4 KB
Newer Older
M
Mars Liu 已提交
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
# -*- coding: UTF-8 -*-
from common.logger import init_log

from config.config import load_config
from options import parse_options, show_help
from tree import gen_tree
from os import walk
import os.path


def test(config, options, actions):
    import test as test
    test.dispatch(config, options, actions)


def readall(path):
    with open(path) as f:
        return f.read()


def write(path, data):
    with open(path, "w") as f:
        f.write(data)


def makeup():
    for dir, sub_dirs, files in walk("../data"):
        for fname in files:
            _, ext = os.path.splitext(fname)
            if ext == ".md":
                full_path = os.path.join(dir, fname)
                content = readall(full_path)
                lines = content.split("\n")
                new_lines = []
                flag = False
                in_block = False
                for line in lines:
                    new_line = line
                    if line.startswith("```"):
                        if in_block:
                            in_block = False
                        else:
                            in_block = True
                            if line[3:] != "":
                                continue
                            else:
                                new_line = line + 'c'
                                flag = True
                                
                    new_lines.append(new_line)

                if flag:
                    content = "\n".join(new_lines)
                    write(full_path, content)


def tree(config, options, actions):
    import test as test
    gen_tree("../data")


def run(options):
    # 操作入口
    if options.action is not None:
        actions = options.action.split('.')
        if len(actions) == 0:
            return

        print('@init config...')
        config = load_config(options, args)
        print('')

        print('@init log...')
        init_log(config, options)
        print('')

        print('@dispatch action:{}...'.format(options.action))
        root_action = actions[0]
        next = actions[1:]
        dispatch = {
            'test': lambda: test(config, options, next),
            'tree': lambda: tree(config, options, next),
            'makeup': lambda: makeup()
        }
        dispatch[root_action]()
    else:
        show_help()


if __name__ == "__main__":
    [options, args] = parse_options()
    run(options)