""" nwcPy2nwcXml.py Translates FROM: nwcPythonListDictFormat (kahman's nwctxt to python converter pyNWC-a output) TO: jdorocak's homegrown xml (So I can use data folding in a text editor.) :) Usage: nwcPy2nwcXml.py infileName outfileName Example: nwcPy2nwcXml.py mySong.py mySong.xml Author: Joe Dorocak License: Use this as you please. :) Use with caution - I have done minimal testing. No warranty is implied. :) """ class ConvertToXML(): """ Converts FROM: nwcPythonListDictFormat (NWC Virtuoso kahman's nwctxt to python converter pyNWC-a output) TO: jdorocak's homegrown xml (So I can use data folding in a text editor.) :) rs == resultString; il == indentationLevel; aprs(il,str) == appendToResultString(indentationLevel,strToAppend) data == string output of kahman's nwctxt to python converter ix == index of current pythonListDict item; joe's xml syntax for nwctxtPy files inferred from analysis of data (i added elements named j*): """ def __init__(self, indata): """ data == data to be converted. FROM: nwcPythonListDictFormat (kahman's nwctxt to python converter pyNWC-a output) rs == resultString; converted data jdorocak's homegrown xml (So I can use data folding in a text editor.) :) """ self.data = eval(indata) self.data.append(['End', {'Joe': 'End', 'Pos': 'Whatever'}]) self.rs = '' def toxml(self): self.aprs(0,'') #SongInfo self.aprs(1,'') ix = 0 print len(self.data) for k,v in self.data[ix][1].iteritems(): self.aprs (2,'<' + k + '>' + str(v) + '') self.aprs (1,'') #PgSetup self.aprs (1,'') ix = 1 for k,v in self.data[ix][1].iteritems(): self.aprs (2,'<' + k + '>' + str(v) + '' ) self.aprs (1,'') #Fonts self.aprs (1,'') for ix in range(2,14): self.aprs (2,'') for k,v in self.data[ix][1].iteritems(): self.aprs (3,'<' + k + '>' + str(v) + '' ) self.aprs (2,'') self.aprs (1,'') #PgMargins self.aprs (1,'') ix = 15 for k,v in self.data[ix][1].iteritems(): self.aprs (2,'<' + k + '>' + str(v) + '' ) self.aprs (1,'') self.aprs (1,'') self.aprs (2,'') #AddStaff ix = 16 for ix in range(ix,len(self.data)-1): #if self.data[ix+1][0] == 'AddStaff': self.aprs (2,'') #ix+1 needed to add ['End',{'Joe':'whatever'}] self.aprs (3,'<'+ self.data[ix][0] +'>') for k,v in self.data[ix][1].iteritems(): self.aprs (4,'<' + k + '>' + str(v) + '' ) self.aprs (3,'') if self.data[ix+1][0] == 'AddStaff': #ix+1 needed to add ['End',{'Joe':'whatever'}] self.aprs (2,'') self.aprs (2,'') self.aprs (2,'') self.aprs (1,'') self.aprs(0,'') return self.rs def aprs(self, il, strToAppend): """aprs(il,str) == appendToResultString(indentationLevel,strToAppend)""" self.rs = ''.join([self.rs, il*2*' '+strToAppend+'\n']) #print il*2*' '+strToAppend import sys N_ARGS = 2 def main(argv=None): if argv == None: argv=sys.argv args = argv[1:] #NOTE: args[0] != argv[0] == if len(args) != N_ARGS or "-h" in args or "--help" in args: print __doc__ sys.exit(2) f = open(args[0], 'r'); s = f.read(); f.close() # get the input file into s cx = ConvertToXML(s) # make a new converter for s sXml = cx.toxml() # translate s into sXml f = open(args[1], 'w'); f.write(sXml); f.close() # write str(s2) into the output file print 'Translated ' + str(args[0]) + ' yielding ' + str(args[1]) if __name__ == '__main__': sys.exit(main())