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