Python实现删除Android工程中的冗余字符串


Posted in Python onJanuary 19, 2015

Android提供了一套很方便的进行资源(语言)国际化机制,为了更好地支持多语言,很多工程的翻译往往会放到类似crowdin这样的平台上。资源是全了,但是还是会有一些问题。

哪些问题

以下使用一些语言进行举例。其中values为工程默认的资源。

1.某语言的资源和某语言限定区域的资源之间。如values-fr-rCA存在于values-fr相同的字符串,这种表现最为严重。
2.某语言的资源和默认的资源之间。values-fr存在与values相同的字符串,可能原因是由于values-fr存在未翻译字符串导致

为什么要去重

洁癖,容不下半点冗余。

解决思路

1.如果values-fr-rCA存在于values-fr相同的字符串,去除values-fr-rCA中的重复字符串,保留values-fr。这样可以保证在values-fr-rCA下也可以正确读取到资源。

2.如果values-fr存在与values相同的字符串。如去除values-fr中得重复字符串,保留values的条目。

Py脚本

#!/usr/bin/env python

# coding=utf-8

from os import listdir,path, system

from sys import argv

try:

    import xml.etree.cElementTree as ET

except ImportError:

    import xml.etree.ElementTree as ET


def genRegionLangPair(filePath):

    basicLanguage = None

    if ('values' in filePath) :

        hasRegionLimit = ('r' == filePath[-3:-2])

        if (hasRegionLimit):

            basicLanguage = filePath[0:-4]

            if (not path.exists(basicLanguage)) :

                return None

            belongsToEnglish =  ("values-en" in basicLanguage)

            if (belongsToEnglish):

                #Compare with the res/values/strings.xml

                return (path.dirname(basicLanguage) + '/values/strings.xml', filePath + "/strings.xml")

            else:

                return (basicLanguage + '/strings.xml', filePath + "/strings.xml")

    return None
def genLangPair(filePath):

    def shouldGenLanPair(filePath):

        if (not 'values' in filePath ):

            return False

        if('dpi' in filePath):

            return False

        if ('dimes' in filePath):

            return False

        if ('large' in filePath):

            return False

        return True
    if(shouldGenLanPair(filePath)):

        basicLanguage = path.dirname(filePath) + '/values/strings.xml'

        targetLanguage = filePath + '/strings.xml'

        if (not path.exists(targetLanguage)):

           return None
        if (not path.samefile(basicLanguage,targetLanguage)) :

            return (basicLanguage, targetLanguage)

    return None
def genCompareList(filePath):

    compareLists = []

    for file in listdir(filePath):

        regionPair = genRegionLangPair(filePath + '/' + file)

        if (None != regionPair):

            compareLists.append(regionPair)
        languagePair = genLangPair(filePath + '/' + file)

        if (None != languagePair) :

            compareLists.append(languagePair)
    return compareLists
def getXmlEntries(filePath):

    root = ET.ElementTree(file=filePath).getroot()

    entries = {}

    for child in root:

        attrib = child.attrib

        if (None != attrib) :

            entries[attrib.get('name')] = child.text

    print 'xmlEntriesCount',len(entries)

    return entries
def rewriteRegionFile(sourceEntries, filePath):

    if (not path.exists(filePath)):

        return

    ET.register_namespace('xliff',"urn:oasis:names:tc:xliff:document:1.2")

    tree = ET.ElementTree(file=filePath)

    root = tree.getroot()

    print root

    totalCount = 0

    removeCount = 0

    unRemoveCount = 0

    print len(root)

    toRemoveList = []

    for child in root:

        totalCount = totalCount + 1

        attrib = child.attrib

        if (None == attrib):

            continue
        childName = attrib.get('name')
        if (sourceEntries.get(childName) == child.text):

            removeCount = removeCount + 1

            toRemoveList.append(child)

        else:

            unRemoveCount = unRemoveCount + 1

            print childName, sourceEntries.get(childName), child.text

    print filePath,totalCount, removeCount,unRemoveCount
    for aItem in toRemoveList:

        root.remove(aItem)
    if (len(root) != 0 ):

        tree.write(filePath, encoding="UTF-8")

    else:

        command = 'rm -rf %s'%(path.dirname(filePath))

        print command

        system(command)

def main(projectDir):

    lists = genCompareList(projectDir + "/res/")
    for item in lists:

        print item

        src = item[0]

        dest = item[1]

        rewriteRegionFile(getXmlEntries(src),dest)
if __name__ == "__main__":

    if (len(argv) == 2) :

        main(argv[1])

如何使用

python removeRepeatedStrings.py your_android_project_root_dir
Python 相关文章推荐
如何使用python爬取csdn博客访问量
Feb 14 Python
python 查找文件名包含指定字符串的方法
Jun 05 Python
python爬虫之urllib3的使用示例
Jul 09 Python
python 反向输出字符串的方法
Jul 16 Python
Python模块的加载讲解
Jan 15 Python
Python函数式编程指南:对生成器全面讲解
Nov 19 Python
利用pandas将非数值数据转换成数值的方式
Dec 18 Python
Django import export实现数据库导入导出方式
Apr 03 Python
设置jupyter中DataFrame的显示限制方式
Apr 12 Python
python filecmp.dircmp实现递归比对两个目录的方法
May 22 Python
python如何使用腾讯云发送短信
Sep 17 Python
python实现控制台输出颜色
Mar 02 Python
Python中字典和JSON互转操作实例
Jan 19 #Python
Python中的字典遍历备忘
Jan 17 #Python
Python中处理unchecked未捕获异常实例
Jan 17 #Python
Python实现过滤单个Android程序日志脚本分享
Jan 16 #Python
Python中的对象,方法,类,实例,函数用法分析
Jan 15 #Python
Python转换HTML到Text纯文本的方法
Jan 15 #Python
python中os操作文件及文件路径实例汇总
Jan 15 #Python
You might like
关于页面优化和伪静态
2009/10/11 PHP
FirePHP 推荐一款PHP调试工具
2011/04/23 PHP
php file_put_contents()功能函数(集成了fopen、fwrite、fclose)
2011/05/24 PHP
php实现插入排序
2015/03/29 PHP
php静态成员方法和静态的成员属性的使用方法
2017/10/26 PHP
详解PHP发送邮件知识点
2018/05/06 PHP
javascript实现tabs选项卡切换效果(扩展版)
2013/03/19 Javascript
JS 添加千分位与去掉千分位的示例
2013/07/11 Javascript
JavaScript学习笔记之定时器
2015/01/22 Javascript
jquery实现弹出层效果实例
2015/05/19 Javascript
JavaScript实现函数返回多个值的方法
2015/06/09 Javascript
js代码验证手机号码和电话号码是否合法
2015/07/30 Javascript
JQuery实现网页右侧随动广告特效
2016/01/17 Javascript
JS添加删除DIV的简单实例
2016/07/08 Javascript
微信小程序加载更多 点击查看更多
2016/11/29 Javascript
jQuery中的on与bind绑定事件区别实例详解
2017/02/28 Javascript
layui实现文件或图片上传记录
2018/08/28 Javascript
JavaScript函数重载操作实例浅析
2020/05/02 Javascript
[01:49]一目了然!DOTA2DotA快捷操作对比第二弹
2014/05/16 DOTA
[06:20]2015国际邀请赛第三日top10
2015/08/08 DOTA
[02:10]2018DOTA2亚洲邀请赛赛前采访-Liquid
2018/04/03 DOTA
Python sys.path详细介绍
2013/10/17 Python
浅析Python多线程下的变量问题
2015/04/28 Python
Python中的单继承与多继承实例分析
2018/05/10 Python
基于python OpenCV实现动态人脸检测
2018/05/25 Python
virtualenv 指定 python 解释器的版本方法
2018/10/25 Python
Python给图像添加噪声具体操作
2019/03/03 Python
Python 控制终端输出文字的实例
2019/07/12 Python
python解析yaml文件过程详解
2019/08/30 Python
对python中各个response的使用说明
2020/03/28 Python
python操作redis数据库的三种方法
2020/09/10 Python
Aeropostale官网:美国著名校园品牌及青少年服饰品牌
2019/03/21 全球购物
大学应届生求职简历的自我评价
2013/10/08 职场文书
法律专业实习鉴定
2013/12/22 职场文书
自查自纠整改报告
2014/11/06 职场文书
Flask response响应的具体使用
2021/07/15 Python