iOS开发中python脚本的使用

项目目前是12国语言,由于之前iOS和android语言不统一,不久前统一了一版,但是过程中导致项目中有些翻译代码中写了,但是文件中统一过后不存在翻译.

这是项目语言使用的宏定义

#define WYLocalString(key)          [[NSBundle wy_bundle] localizedStringForKey:key value:nil table:nil]

存在问题,统一之前

WYLocalString(@"nvr_max")

统一之后,因为是个弹窗,项目规范话iOS和android统一使用

WYLocalString(@"toast_nvr_max")

但是Localizable.strings文件中并不存在toast_nvr_max的翻译,导致翻译缺失.由于工程大,人工查找效率低下,这是使用脚本的初衷.

最终的脚本

import os, json

meariPath = "/Users/maj/Desktop/cloudedge2.0/Meari"
keyoutPath = "/Users/maj/Desktop/语言/key_out.json"

# 获取keyout值列表
with open(keyoutPath, 'r') as json_file:
    data = json.load(json_file)

# 需要进行遍历的父文件
path1 = []
for i in os.listdir(meariPath):
    tempPath1 = meariPath + "/" + i
    if len(os.path.splitext(i)[1]) == 0 and "." not in os.path.splitext(i)[0] and "SDK" not in os.path.splitext(i)[0]:
        path1.append(tempPath1)

# print(path1)
tempList = []


# 文件做递归, 拿到有用的.h, .m 文件
def findLastFilePathList(file: list):
    for i in file:
        if os.path.isfile(i):

            if ".h" == os.path.splitext(i)[1] or ".m" == os.path.splitext(i)[1]:
                tempList.append(i)
        else:
            tempPath = []
            for j in os.listdir(i):
                tempPath.append(i + "/" + j)
            findLastFilePathList(tempPath)


findLastFilePathList(path1)

errorStrSet = set()
for i in tempList:
    with open(i, "r") as file:
        str1 = file.read()
        strList1 = str1.split("WYLocalString")
        for index in range(len(strList1)):
            obj = strList1[index]
            if index != 0:
                strList = obj.split(")")[0].split("\"")
                if len(strList) != 3:
                    errorFileSet.add(os.path.basename(i))
                    errorStrSet.add(obj.split(")")[0])
                elif strList[1] not in data.keys():
                    errorFileSet.add(os.path.basename(i))
                    errorStrSet.add(strList[1] +"---" + os.path.basename(i))
                else:
                    pass


print(errorStrSet)

解释一下

脚本比较简单,过程中就用了两个库.
  • meariPath 是项目中的文件本地路径
  • keyoutPath是和安卓统一后的语言文件本地路径
    iOS开发中python脚本的使用_第1张图片
    meariPath.png
iOS开发中python脚本的使用_第2张图片
keyoutPath.png
我们要做的是,查找所有的.h文件和.m文件,查看里面的代码语言,是否在统一过后的key值中存在.
  • 获取存储key值的字典
with open(keyoutPath, 'r') as json_file:
    data = json.load(json_file)
  • 获取有效文件
path1 = []
for i in os.listdir(meariPath):
    tempPath1 = meariPath + "/" + i
    if len(os.path.splitext(i)[1]) == 0 and "." not in os.path.splitext(i)[0] and "SDK" not in os.path.splitext(i)[0]:
        path1.append(tempPath1)

查看meariPath.png, 可以看出项目中存放.h和.m的文件夹是 Base,Main,Classes这类文件,所有带. 点的文件都是不需要的. SDK文件我们项目中也不存在,所以这里过滤了.
os.listdir() 是获取文件夹下所有文件
splitext 是分离文件名和扩展名
都是系统os模块的操作

  • 递归这些文件获取所有的.h , .m文件
def findLastFilePathList(file: list):
    for i in file:
        if os.path.isfile(i):

            if ".h" == os.path.splitext(i)[1] or ".m" == os.path.splitext(i)[1]:
                tempList.append(i)
        else:
            tempPath = []
            for j in os.listdir(i):
                tempPath.append(i + "/" + j)
            findLastFilePathList(tempPath)

os.path.isfile() 判断是不是一个文件,如果是,再判断是不是.h.m文件,添加进数组中
如果不是一个文件, 拼接路径继续调用函数.

['/Users/maj/Desktop/cloudedge2.0/Meari/Vendors/Ping/PingFoundation.h', 
'/Users/maj/Desktop/cloudedge2.0/Meari/Vendors/Ping/PingHelper.h', 
'/Users/maj/Desktop/cloudedge2.0/Meari/Vendors/Ping/PingFoundation.m', 
'/Users/maj/Desktop/cloudedge2.0/Meari/Vendors/Ping/PingHelper.m', .............

最终获得这样一个所有由.h,和.m构成的路径的数组

  • 遍历所有的.h和.m文件,查看key值是否存在.
errorStrSet = set()
for i in tempList:
    with open(i, "r") as file:
        str1 = file.read()
        strList1 = str1.split("WYLocalString")
        for index in range(len(strList1)):
            obj = strList1[index]
            if index != 0:
                strList = obj.split(")")[0].split("\"")
                if len(strList) != 3:
                    errorFileSet.add(os.path.basename(i))
                    errorStrSet.add(obj.split(")")[0])
                elif strList[1] not in data.keys():
                    errorFileSet.add(os.path.basename(i))
                    errorStrSet.add(strList[1] +"---" + os.path.basename(i))
                else:
                    pass

print(errorStrSet)

file.read()获取文件内容,以字符串形式输出
split 字符串的分割
os.path.basename 获取文件名称.

最终得到错误的key 和key所在的文件

{'help_config_2m_old_no_ap---NSAttributedString+Extension.m',
 'APP域名---WYMoreDebugHostVC.m',
 'help_config_1m_old_no_ap---NSAttributedString+Extension.m',
 'Scan Code---WYPopoverView.m' ,  ....
完成简单的查找

你可能感兴趣的:(iOS开发中python脚本的使用)