package.py 2.4 KB
Newer Older
F
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import os
import uuid

from src.main.utils import load_json, dump_json


class Package:
    def __init__(self, root) -> None:
        self.root = root

    def walk(self):
        for base, dirs, files in os.walk(self.root):
            for file_name in files:
                file_full_name = os.path.join(base, file_name)
                _, ext = os.path.splitext(file_name)
                if ext == '.json':
                    pkg = load_json(file_full_name)
F
feilong 已提交
18
                    if pkg.get('type') == 'job':
F
feilong 已提交
19 20 21
                        self.visit_package(file_full_name, pkg)

    def visit_package(self, file_full_name, pkg):
F
feilong 已提交
22 23 24 25
        pacakge = pkg['package']
        levels = pkg['levels']
        if pkg.get('package_id') is None or pkg.get('package_id').strip() == '':
            pkg['package_id'] = f'pkg-{pacakge}-'+uuid.uuid4().hex
F
feilong 已提交
26

F
feilong 已提交
27 28
        for level in levels:
            if level.get('level_id') is None or level.get('level_id') == '':
F
feilong 已提交
29
                level['level_id'] = f'pkg-{pacakge}-level-'+uuid.uuid4().hex
F
feilong 已提交
30
        dump_json(file_full_name, pkg, exist_ok=True, override=True)
F
feilong 已提交
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

    def list(self):
        pkg_list = []
        tree_list = []
        for base, dirs, files in os.walk(self.root):
            for file_name in files:
                file_full_name = os.path.join(base, file_name)
                _, ext = os.path.splitext(file_name)
                if ext == '.json':
                    print(file_full_name)
                    pkg = load_json(file_full_name)
                    if pkg.get('type') == 'job':
                        self.list_package(file_full_name, pkg,
                                          pkg_list, tree_list)
        out = {
            'jobs': pkg_list,
            'trees': sorted(list(set(tree_list)))
        }
        dump_json('data/index.json', out, exist_ok=True, override=True)

    def list_package(self, file_full_name, pkg, pkg_list, tree_list):
        package = pkg['package']
        package_name = pkg['package_name']
        levels = pkg['levels']
        for level in levels:
            level_en = level['level']
            level_name = level['level_name']
            skill_trees = level['skill_trees']

            package_level = f'{package}_{level_en}'
            pkg_list.append({
                'name': package_name,
                'level_name': level_name,
                'router': package_level
            })
            for skill_tree in skill_trees:
                tree_list.append(skill_tree)