ProtoBuff文件编译成py文件

#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Users: LiMu
# @Files:BuffCompile.py
# @Times: 2024/4/29 
# @Software:PyCharm

import os
import json
import time
import random
import warnings

#pkg_resources方法在python3.8后已弃用,使用warnings提供新的语法指向pkg_resources
warnings.filterwarnings('ignore', category=DeprecationWarning, module='pkg_resources')

class Compileprotobuff:
    '''
    #强制依赖以下三方库,版本必须一致。
    pip install protobuf==5.26.1
    pip install grpcio-tools==1.62.2
    pip install setuptools==67.6.1
    '''
    #protobuff文件存放的位置
    paths = "E:\DqsjTwoExample\proto"
    @staticmethod
    def list_directory_contents(dir_path):
        filepath,filename = [],[]
        for entry in os.scandir(dir_path):
            if entry.is_file():
                filename.append(entry.name)
            elif entry.is_dir():
                filepath.append(entry.name)
            else:
                pass
        return filepath,filename

    @staticmethod
    def StartCompile(ModelName=None):
        #遍历接受BUFF文件对应的模块名称
        filepath = Compileprotobuff.list_directory_contents(Compileprotobuff.paths)[0]

        for i in filepath:
            #遍历接受BUFF文件对名称
            filename = Compileprotobuff.list_directory_contents("{}\\{}".format(Compileprotobuff.paths, i))[1]
            if ModelName == i:
                #指定编译某个模块的BUFF文件
                for j in filename:
                    runpath  ="./{}".format(i)  + "\\" + j
                    os.system(f"python -m grpc_tools.protoc -I. --python_out=../core/  {runpath}")
                    print(f"成功编译文件:{runpath}")
                break
            else:
                #遍历编译文件夹下所有功能模块的buff文件
                for j in filename:
                    runpath = "./{}".format(i) + "\\" + j
                    os.system(f"python -m grpc_tools.protoc -I. --python_out=../core/  {runpath}")
                    print(f"成功编译文件:{runpath}")

if __name__ == '__main__':
    Compileprotobuff.StartCompile("Adventure")
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容