[Python] 纯文本查看 复制代码# -*- coding: utf-8 -*-
import sys
import os
import xmltodict
import collections
def change_interface(src):
with open(src,'r+') as file:
xml = file.read()
doc = xmltodict.parse(xml)
old_name = ''
old_note = collections.OrderedDict()
if isinstance(doc['manifest']['application']['activity'],dict):
note = doc['manifest']['application']['activity']
if isinstance(note['intent-filter'],list):
isinterface = is_interface_from_list(note['intent-filter'])
if isinterface:
old_name = note['@android:name']
else:
isinterface = is_interface_from_dict(note['intent-filter'])
if isinterface:
old_name = note['@android:name']
else:
for note in doc['manifest']['application']['activity']:
if note.has_key('intent-filter'):
if isinstance(note['intent-filter'],list):
isinterface = is_interface_from_list(note['intent-filter'])
if isinterface:
old_name = note['@android:name']
elif isinstance(note['intent-filter'],dict):
isinterface = is_interface_from_dict(note['intent-filter'])
if isinterface:
old_name = note['@android:name']
activity = collections.OrderedDict()
category = collections.OrderedDict()
action = collections.OrderedDict()
intent = collections.OrderedDict()
activity['@android:name'] = 'com.cutils.CActivity'
activity['@android:theme'] = '@style/CShareAppTheme'
category['@android:name'] = 'android.intent.category.LAUNCHER'
action['@android:name'] = 'android.intent.action.MAIN'
intent['action'] = action
intent['category'] = category
activity['intent-filter'] = intent
if isinstance(doc['manifest']['application']['activity'],list):
doc['manifest']['application']['activity'].insert(0,activity)
else:
signal = doc['manifest']['application']['activity']
activityList = [signal,activity];
doc['manifest']['application']['activity'] = activityList
# doc['manifest']['application']['activity'].insert(0,activity)
with open('./manifest_tmp.xml','w') as f:
f.writelines(xmltodict.unparse(doc))
return old_name
def is_interface_from_list(notes):
for note in notes:
if note.has_key('category') and isinstance(note['category'],list):
category = note['category']
for item in category:
if item['@android:name'].find('category.LAUNCHER') > 0:
category.remove(item)
return True
elif note.has_key('category') and isinstance(note['category'],dict):
if note['category']['@android:name'].find('category.LAUNCHER') > 0:
del note['category']
return True
return False
def is_interface_from_dict(note):
if note.has_key('category') and isinstance(note['category'],list):
category = note['category']
for item in category:
if item['@android:name'].find('category.LAUNCHER') > 0:
category.remove(item)
return True
elif note.has_key('category') and isinstance(note['category'],dict):
if note['category']['@android:name'].find('category.LAUNCHER') > 0:
del note['category']
return True
else:
return False
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf-8')
src = sys.argv[1]
old_interface = change_interface(src)
cmd = 'xmllint --format ./manifest_tmp.xml' + ' > ' +src
# cmd = 'xmllint --format ./manifest_tmp.xml > test.xml'
os.system(cmd)
print(old_interface.replace('.','\/'))