2022-01-22 11:44:09 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
import xml.sax
|
2023-03-07 11:20:57 +08:00
|
|
|
from xml.dom import minidom
|
|
|
|
from xml.dom.minidom import parse, parseString
|
2022-01-22 14:38:04 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2022-01-23 15:57:09 +08:00
|
|
|
import time
|
2022-01-22 14:38:04 +08:00
|
|
|
|
2022-01-22 11:44:09 +08:00
|
|
|
class CDROIDHandler( xml.sax.ContentHandler ):
|
|
|
|
def __init__(self):
|
2022-01-22 14:38:04 +08:00
|
|
|
self.idlist=[]
|
2023-03-07 11:20:57 +08:00
|
|
|
self.strings=[]
|
2022-01-22 11:44:09 +08:00
|
|
|
|
|
|
|
def startElement(self, tag, attributes):
|
2022-01-22 14:38:04 +08:00
|
|
|
for attr in attributes.getNames():
|
|
|
|
if ':id' in attr:
|
|
|
|
self.addID(attributes.get(attr))
|
2023-03-07 11:20:57 +08:00
|
|
|
value = attributes[attr]
|
|
|
|
if 'string/' in value:
|
|
|
|
self.addString(value)
|
2022-01-22 14:38:04 +08:00
|
|
|
|
|
|
|
def addID(self,name):
|
2023-03-07 11:20:57 +08:00
|
|
|
pos=name.find('/')
|
2022-01-22 22:09:59 +08:00
|
|
|
if pos<0:
|
|
|
|
return
|
2022-01-23 15:57:09 +08:00
|
|
|
name=name[pos+1:].strip()
|
|
|
|
if (name[0].isalpha() or (name[0]=='_')) and (name not in self.idlist) :
|
2022-01-22 22:09:59 +08:00
|
|
|
self.idlist.append(name)
|
2022-01-23 15:57:09 +08:00
|
|
|
|
2023-03-07 11:20:57 +08:00
|
|
|
def addString(self,value):
|
|
|
|
pos=value.find('/')
|
|
|
|
if pos<0 :
|
|
|
|
return
|
|
|
|
value=value[pos+1:].strip()
|
|
|
|
self.strings.append(value)
|
|
|
|
|
2022-01-22 22:09:59 +08:00
|
|
|
def groupby(self):
|
|
|
|
new_ids = []
|
|
|
|
for id in self.idlist:
|
|
|
|
if id not in new_ids:
|
|
|
|
new_ids.append(id)
|
|
|
|
self.idlist=new_ids
|
2022-11-29 17:32:27 +08:00
|
|
|
print(new_ids)
|
2022-01-22 22:09:59 +08:00
|
|
|
return new_ids
|
2022-01-22 14:38:04 +08:00
|
|
|
|
|
|
|
class IDGenerater(object):
|
|
|
|
def __init__(self,idstart):
|
|
|
|
self.idstart=idstart
|
2022-01-22 22:09:59 +08:00
|
|
|
self.processDirs=['layout','drawable']
|
2022-01-22 14:38:04 +08:00
|
|
|
self.parser = xml.sax.make_parser()
|
|
|
|
self.parser.setFeature(xml.sax.handler.feature_namespaces, 0)
|
|
|
|
self.Handler = CDROIDHandler()
|
|
|
|
self.parser.setContentHandler( self.Handler )
|
2022-01-22 11:44:09 +08:00
|
|
|
|
2022-01-23 15:57:09 +08:00
|
|
|
def dict2RH(self,filepath,namespace):
|
2022-01-22 11:44:09 +08:00
|
|
|
fr=open(filepath,"w")
|
|
|
|
fr.write("#pragma once\n\n")
|
2023-03-07 11:20:57 +08:00
|
|
|
fr.write("/*Generated by machine ,Do not edit !!!*/\n\n")
|
2022-01-23 15:57:09 +08:00
|
|
|
fr.write("namespace %s{\n\n"%(namespace))
|
2023-03-07 11:20:57 +08:00
|
|
|
fr.write("namespace R{\n")
|
|
|
|
fr.write(" namespace id{\n")
|
2022-01-22 11:44:09 +08:00
|
|
|
i=0
|
2022-11-29 17:32:27 +08:00
|
|
|
print(self.Handler.idlist)
|
2022-01-22 14:38:04 +08:00
|
|
|
for k in self.Handler.idlist:
|
2023-03-07 11:20:57 +08:00
|
|
|
fr.write("%8s static constexpr int %-24s= 0x%08X ;/*%d*/\n"%('',k,self.idstart+i,self.idstart+i))
|
|
|
|
i+=1
|
|
|
|
fr.write("%4s};/*namespace id*/\n\n"%(''))
|
|
|
|
|
|
|
|
fr.write(" namespace strings{\n")
|
|
|
|
i=0
|
|
|
|
for s in self.Handler.strings:
|
|
|
|
fr.write("%8s static constexpr int %-24s= 0x%08X ;/*%d*/\n"%('',k,self.idstart+i,self.idstart+i))
|
2022-01-22 11:44:09 +08:00
|
|
|
i+=1
|
2023-03-07 11:20:57 +08:00
|
|
|
fr.write("%4s};/*namespace strings*/\n\n"%(''))
|
|
|
|
|
|
|
|
fr.write("};//endof namespace R\n\n")
|
2022-01-22 22:09:59 +08:00
|
|
|
fr.write("}//endof namespace\n\n")
|
2022-01-22 11:44:09 +08:00
|
|
|
fr.close()
|
|
|
|
|
2022-01-22 14:38:04 +08:00
|
|
|
def dict2ID(self,filepath):
|
2022-01-22 11:44:09 +08:00
|
|
|
fr=open(filepath,"w")
|
2022-01-22 22:09:59 +08:00
|
|
|
fr.write('<?xml version="1.0" encoding="utf-8"?>')
|
|
|
|
fr.write("<!--Generated by CDdroid's machine,Do not edit !!!-->\n")
|
|
|
|
fr.write("<resources xmlns:android=\"http://schemas.android.com/apk/res/android\">\n")
|
2022-01-22 14:38:04 +08:00
|
|
|
i=0;
|
|
|
|
for k in self.Handler.idlist:
|
2022-01-22 22:09:59 +08:00
|
|
|
fr.write(' <id name="%s">0x%08x</id>\n'%(k,self.idstart+i))
|
2022-01-22 14:38:04 +08:00
|
|
|
i+=1
|
2022-01-22 22:09:59 +08:00
|
|
|
fr.write("</resources>\n\n")
|
2022-01-22 11:44:09 +08:00
|
|
|
fr.close();
|
|
|
|
|
2023-03-07 11:20:57 +08:00
|
|
|
def elementExists(self,elements,tofind):
|
|
|
|
for e in elements:
|
|
|
|
if e.getAttribute('name')==tofind:
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def strings2XML(self,filename):
|
|
|
|
print "===scanned strings===\n"
|
|
|
|
print self.Handler.strings
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
dom=parseString('<?xml version="1.0" encoding="utf-8"?>\n<strings lang="zh_CN"></strings>')
|
|
|
|
else:
|
|
|
|
dom=parse(filename)
|
|
|
|
root = dom.documentElement
|
|
|
|
stringsNodeInFile=root.getElementsByTagName('string')
|
|
|
|
for str in self.Handler.strings:
|
|
|
|
if self.elementExists(stringsNodeInFile,str):
|
|
|
|
continue
|
|
|
|
e=dom.createElement('string')
|
|
|
|
text = dom.createTextNode('')
|
|
|
|
e.setAttribute('name',str)
|
|
|
|
e.appendChild(text)
|
|
|
|
root.appendChild(e)
|
|
|
|
|
|
|
|
fp = open(filename, mode='w')#, encoding='utf-8')
|
|
|
|
dom.writexml(fp, indent='', addindent='\t', newl='\n', encoding='utf-8')
|
|
|
|
fp.close()
|
|
|
|
|
2022-01-22 22:09:59 +08:00
|
|
|
def dirHasId(self,path):
|
|
|
|
for dir in self.processDirs:
|
|
|
|
if path.find(dir)>=0:
|
|
|
|
return 1
|
|
|
|
return -1;
|
|
|
|
|
2022-01-22 14:38:04 +08:00
|
|
|
def scanxml(self,scanPath):
|
2022-01-23 17:48:06 +08:00
|
|
|
lastmodifytime=0
|
2022-01-22 14:38:04 +08:00
|
|
|
for top, dirs, nondirs in os.walk(scanPath):
|
2022-02-11 14:54:04 +08:00
|
|
|
dirs.sort()
|
2022-02-16 18:52:25 +08:00
|
|
|
nondirs.sort()
|
2022-01-22 14:38:04 +08:00
|
|
|
for item in nondirs:
|
|
|
|
fname=os.path.join(top, item)
|
2022-01-26 11:15:58 +08:00
|
|
|
if (not item.endswith('.xml')) or (self.dirHasId(fname)<0):
|
2022-01-22 22:09:59 +08:00
|
|
|
continue
|
2022-01-23 15:57:09 +08:00
|
|
|
newestdate=os.stat(fname);
|
2022-01-23 17:48:06 +08:00
|
|
|
if(lastmodifytime<newestdate.st_mtime):
|
|
|
|
lastmodifytime=newestdate.st_mtime
|
2022-01-22 22:09:59 +08:00
|
|
|
self.parser.parse(fname)
|
2022-01-23 17:48:06 +08:00
|
|
|
return lastmodifytime
|
2022-01-22 14:38:04 +08:00
|
|
|
|
2022-01-22 11:44:09 +08:00
|
|
|
if ( __name__ == "__main__"):
|
2022-01-23 15:57:09 +08:00
|
|
|
idstart=10000
|
2022-01-22 22:09:59 +08:00
|
|
|
if len(sys.argv)<3:
|
2022-11-29 17:32:27 +08:00
|
|
|
print(sys.argv[0])#+'assetspath R.h_path ID.xml'
|
2022-01-23 15:57:09 +08:00
|
|
|
validassetsdir=['assets','res','resources']
|
|
|
|
segments=sys.argv[1].split('/')
|
|
|
|
for i in range(0,len(segments)):
|
|
|
|
if segments[i] in validassetsdir:
|
|
|
|
break
|
|
|
|
namespace=segments[i]
|
|
|
|
if sys.argv[2].find("widget/R.h")>=0 and (namespace=='gui'):
|
|
|
|
namespace='cdroid'
|
|
|
|
idstart=1000
|
2022-11-29 17:32:27 +08:00
|
|
|
print("namespace="+namespace)
|
2022-01-23 17:48:06 +08:00
|
|
|
lastmodifytime=0
|
|
|
|
if os.path.exists(sys.argv[2]):
|
|
|
|
fstat=os.stat(sys.argv[2])
|
|
|
|
lastmodifytime=fstat.st_mtime
|
2022-01-22 22:09:59 +08:00
|
|
|
idgen=IDGenerater(idstart)
|
|
|
|
if not os.path.exists(sys.argv[1]+"/values"):
|
|
|
|
os.makedirs(sys.argv[1]+"/values")
|
2022-01-23 17:48:06 +08:00
|
|
|
if idgen.scanxml(sys.argv[1])>lastmodifytime:
|
|
|
|
idgen.dict2RH(sys.argv[2],namespace)
|
|
|
|
idgen.dict2ID(sys.argv[1]+"/values/ID.xml")
|
2023-03-07 11:20:57 +08:00
|
|
|
idgen.strings2XML(sys.argv[1]+"/values/strings.xml")
|
2022-01-23 17:48:06 +08:00
|
|
|
else:
|
2022-11-29 17:32:27 +08:00
|
|
|
print(sys.argv[2]+" is latest ,skipped.")
|
2022-01-22 11:44:09 +08:00
|
|
|
|