modify idgen,add dir scan support

This commit is contained in:
houzh 2022-01-22 06:38:04 +00:00
parent 5f9abe3e70
commit 3d3080d929

View File

@ -1,30 +1,33 @@
#!/usr/bin/python
import xml.sax
import re
import os
import sys
class CDROIDHandler( xml.sax.ContentHandler ):
def __init__(self):
self.CurrentData = ""
self.iddict={}
self.autoid=1000
self.idlist=[]
def startElement(self, tag, attributes):
for attr in attributes.getNames():
if ':id' in attr:
idname = attributes.get(attr)
#print attr+':'+idname
self.id2Dict(idname)
self.addID(attributes.get(attr))
def id2Dict(self,name):
def addID(self,name):
pos=name.find('/');
if pos>0:
name=name[pos+1:]
if name[0].isalpha() or (name[0]=='_'):
self.iddict[name]=self.autoid
self.autoid=self.autoid+1
self.idlist.append(name)
class IDGenerater(object):
def __init__(self,idstart):
self.idstart=idstart
self.parser = xml.sax.make_parser()
self.parser.setFeature(xml.sax.handler.feature_namespaces, 0)
self.Handler = CDROIDHandler()
self.parser.setContentHandler( self.Handler )
def dict2RH(self,filepath):
print self.iddict
fr=open(filepath,"w")
fr.write("#pragma once\n\n")
fr.write("/*Generated by machine ,Do not edit!!!*/\n\n")
@ -32,13 +35,13 @@ class CDROIDHandler( xml.sax.ContentHandler ):
fr.write("class R{\n")
fr.write("public:\n\n")
fr.write(" enum id {\n")
dsize =len(self.iddict)
dsize =len(self.Handler.idlist)
i=0
for k in self.iddict.keys():
fr.write(" ");
fr.write(k+"="+str(self.iddict[k]))
for k in self.Handler.idlist:
fr.write(" "+k+"=")
fr.write(str(self.idstart+i))
if(i<dsize-1):
fwrite(",")
fr.write(",")
fr.write("\n")
i+=1
fr.write(" };//endof enum id\n\n")
@ -46,24 +49,28 @@ class CDROIDHandler( xml.sax.ContentHandler ):
fr.write("};//endof namespace\n\n")
fr.close()
def dict2IDResource(self,filepath):
def dict2ID(self,filepath):
fr=open(filepath,"w")
fr.write("<resource xmlns:android=\"http://schemas.android.com/apk/res/android\">\n")
for k,v in self.iddict.items():
fr.write(" <id name=\""+k+"\">"+str(v)+"</id>\n")
i=0;
for k in self.Handler.idlist:
fr.write(" <id name=\""+k+"\">"+str(self.idstart+i)+"</id>\n")
i+=1
fr.write("</resource>\n\n")
fr.close();
def scanxml(self,scanPath):
for top, dirs, nondirs in os.walk(scanPath):
for item in nondirs:
fname=os.path.join(top, item)
if fname.find('.xml')>0:
print "parse file:"+fname
self.parser.parse(fname)
print self.Handler.idlist
if ( __name__ == "__main__"):
parser = xml.sax.make_parser()
# turn off namepsaces
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
Handler = CDROIDHandler()
parser.setContentHandler( Handler )
parser.parse("/home/houzh/Miniwin/apps/uidemo/assets/layout/main.xml")
Handler.dict2RH("./R.h")
Handler.dict2IDResource("./ID.xml")
idgen=IDGenerater(1000)
idgen.scanxml(sys.argv[1])
idgen.dict2RH("./R.h")
idgen.dict2ID("./ID.xml")