生成json文件

用于egret项目生成资源json文件

#!/usr/bin/env python3
# coding=utf-8

import sys
import json
import os
import copy

def make_json(path=".", groupname="preload"):
    data = dict()
    resources=[]
    groups=[]
    keys = []
    item_dic = dict()
    for file in os.listdir(path):
        if os.path.isdir(os.path.join(path,file)):
            for item in os.listdir(os.path.join(path, file)):
                name, tp = item.split(".")
                item_dic["name"] = name
                if tp in ("jpg","png"):
                    item_dic["type"] = "image"
                    keys.append(name)
                elif tp in ("mp3","ogg"):
                    item_dic["type"] = "sound"
                    keys.append(name)
                elif tp=="json":
                    item_dic["type"] = tp
                item_dic["url"] = os.path.join(file+"/"+item)
                resources.append(copy.copy(item_dic))
    groups.append(dict(name=groupname, keys=",".join(keys)))
    data["resources"] = resources
    data["groups"] = groups
    dump_path = os.path.join(path, "default.res.json")
    f = open(dump_path, "w")
    json.dump(data,f,indent=4)
    f.close()


if __name__ == "__main__":
    args = sys.argv[1:]
    make_json(*args)


你可能感兴趣的:(python)