import os
import re


class GsdParser:
    @classmethod
    def parseGsdFiles(cls, folderPath):
        gsdData = []
        for filename in os.listdir(folderPath):
            if filename.lower().endswith('.gsd'):
                filePath = os.path.join(folderPath, filename)
                identNumber, modelName = cls.parseGsdFile(filePath)
                if identNumber and modelName:
                    gsdData.append({
                        'filename': filename,
                        'identNumber': identNumber,
                        'modelName': modelName
                    })
        return gsdData

    @classmethod
    def parseGsdFile(cls, filePath):
        identNumber = None
        modelName = None
        with open(filePath, 'r', encoding='utf-8', errors='ignore') as file:
            content = file.read().replace(' ', '')
            identNumberMatch = re.search(r'Ident_Number=(\w+)', content)
            modelNameMatch = re.search(r'Model_Name="(.+?)"', content)
            
            if identNumberMatch:
                identNumber = identNumberMatch.group(1)[2:]
            if modelNameMatch:
                modelName = modelNameMatch.group(1)
                
        return identNumber, modelName


# 示例用法
if __name__ == '__main__':
    folderPath = './'  # 替换为实际的文件夹路径
    parsedData = GsdParser.parseGsdFiles(folderPath)

    for gsdInfo in parsedData:
        print(f"File: {gsdInfo['filename']}")
        print(f"Ident_Number: {gsdInfo['identNumber']}")
        print(f"Model_Name: {gsdInfo['modelName']}")
        print("-" * 40)