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中wx将图标显示在右下角的脚本代码
Mar 08 Python
Python的requests网络编程包使用教程
Jul 11 Python
Python利用matplotlib生成图片背景及图例透明的效果
Apr 27 Python
Python表示矩阵的方法分析
May 26 Python
python3+PyQt5实现支持多线程的页面索引器应用程序
Apr 20 Python
python 将md5转为16字节的方法
May 29 Python
对python list 遍历删除的正确方法详解
Jun 29 Python
对python:循环定义多个变量的实例详解
Jan 20 Python
FFrpc python客户端lib使用解析
Aug 24 Python
Python气泡提示与标签的实现
Apr 01 Python
Python是什么 Python的用处
May 26 Python
python爬取豆瓣电影TOP250数据
May 23 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
隐藏你的.php文件的实现方法
2007/03/19 PHP
用php获取本周,上周,本月,上月,本季度日期的代码
2009/08/05 PHP
php数组函数序列之asort() - 对数组的元素值进行升序排序,保持索引关系
2011/11/02 PHP
解析PHP中intval()等int转换时的意外异常情况
2013/06/21 PHP
Codeigniter检测表单post数据的方法
2015/03/21 PHP
Laravel 5框架学习之日期,Mutator 和 Scope
2015/04/08 PHP
php使用Jpgraph绘制柱形图的方法
2015/06/10 PHP
YII Framework框架教程之日志用法详解
2016/03/14 PHP
解决php扩展安装不生效问题
2019/10/25 PHP
JS控制表格隔行变色
2006/06/26 Javascript
线路分流自动跳转代码;希望对大家有用!
2006/12/02 Javascript
jquery animate 动画效果使用说明
2009/11/04 Javascript
用Jquery选择器计算table中的某一列某一行的合计
2014/08/13 Javascript
深入理解javascript严格模式(Strict Mode)
2014/11/28 Javascript
Javascript技术栈中的四种依赖注入详解
2016/02/23 Javascript
详解Angular2中的编程对象Observable
2016/09/17 Javascript
Node.js 进程平滑离场剖析小结
2019/01/24 Javascript
解决vue admin element noCache设置无效的问题
2019/11/12 Javascript
使用js获取身份证年龄的示例代码
2020/12/11 Javascript
[05:31]DOTA2英雄梦之声_第08期_莉娜
2014/06/23 DOTA
[56:42]完美世界DOTA2联赛循环赛 Matador vs Forest 第二场 11.06
2020/11/06 DOTA
Python numpy 提取矩阵的某一行或某一列的实例
2018/04/03 Python
详解python之协程gevent模块
2018/06/14 Python
Python解决线性代数问题之矩阵的初等变换方法
2018/12/12 Python
Python closure闭包解释及其注意点详解
2019/08/28 Python
python使用yield压平嵌套字典的超简单方法
2019/11/02 Python
python对Excel的读取的示例代码
2020/02/14 Python
使用Python打造一款间谍程序的流程分析
2020/02/21 Python
日本即尚网:JSHOPPERS.com(支持中文)
2019/12/03 全球购物
大学生职业生涯规划书范文
2014/01/14 职场文书
网吧消防安全制度
2014/01/28 职场文书
小学新教师培训方案
2014/02/03 职场文书
领导班子四风问题个人对照检查材料
2014/10/04 职场文书
2014年学校德育工作总结
2014/12/05 职场文书
2014年青年教师工作总结
2014/12/17 职场文书
解析Java异步之call future
2021/06/14 Java/Android