Fork自 CSDN 技术社区 / CSDN-技能森林
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)
if pkg.get('type') == 'job':
self.visit_package(file_full_name, pkg) def visit_package(self, file_full_name, pkg):
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 for level in levels: if level.get('level_id') is None or level.get('level_id') == '': level['level_id'] = f'pkg-level-{pacakge}'+uuid.uuid4().hex
dump_json(file_full_name, pkg, exist_ok=True, override=True)