#Some functions that take either #(a) an xml form defintion from the IDE SharpDevelop #or #(b) a C# form definition from the IDE SharpDevelop # #and then produces a python-PythondotNet form defintion # #note that Frederik Lundh's ElementTree is needed #http://effbot.org/downloads/ from elementtree.ElementTree import * def tab(n): if n<=0 : return('') else: s='' for i in range(n): s=s+('\t') return(s) def csharpToPythondotNet (filenameCsharp, filenamePythondotNet): #experimental name='' elc=0 f=open(r'd:\sharp\form1.cs') Ls=f.readlines() f=open(r'd:\sharp\form1cs.py','w') s='import CLR' writeLine(f,s) s='from CLR.System.Drawing import *' writeLine(f,s) s='from CLR.System.Windows.Forms import *' writeLine(f,s) process=False for aL in Ls: #print aL if aL.find('public class')>=0 : k=aL.find('public class') k=k+len('public class') k1=aL.find(':') name=aL[k:k1].strip() print name s='class ' + name + '(CLR.System.Windows.Forms.Form):' writeLine(f,s) process=True tabs=1 continue if process: a=aL.strip() a=a.strip('\t') a=a.strip('\r') a=a.strip('\n') if a.find('private System')==0: continue if a.find('}')==0: continue if a.find(r'//')==0 : aL=aL.replace(r'//',r'#') writeLine(f,aL) continue if a.find('public ' + name)==0: #build constructor s=tab(1) + 'def __init__(self):' writeLine(f,s) tabs=2 continue if a.find('private void Initialize')==0 : s='def InitializeComponent(self):' s= tab(1) + s writeLine(f,s) tabs=2 continue #all other lines a=a.replace('this.','self.') a=a.replace(';','') a=a.replace('{','') a=a.replace('= new','= ') a=a.replace('System.Windows.Forms.','CLR.System.Windows.Forms.') a=a.replace('System.Drawing','CLR.System.Drawing') a=a.replace('true','True') a=a.replace('false','False') if a=='': elc=elc+1 else: elc=0 if a=='': if elc==1: writeLine(f,'') else: s=tab(tabs)+a writeLine(f,s) s='def main():' writeLine(f,s) s=tab(1) + 'f=' + name + '()' writeLine(f,s) s = tab(1) + 'CLR.System.Windows.Forms.Application.Run(f)' writeLine(f,s) s="if __name__== '__main__':" writeLine(f,s) s = tab(1) + 'main()' writeLine(f,s) f.close() def XMLToPythondotNet(filenameXML,filenamePythondotNet): #the recursive function processControls is used because #a form can contain a tab control and a tabpage has its #own controls collection d={} d['ClientSize']='Size' d['Size']='Size' d['Location']='Point' d['Name']='Text' d['TabIndex']='Int' d['Multiline']='Boolean' d['Text']='Text' d['BorderStyle']='Enum' d['SelectedIndex']='Int' d['BackColor']='Color' d['ForeColor']='Color' f=open(filenamePythondotNet,'w') writeLine(f,'import CLR') writeLine(f,'import CLR.System.Windows.Forms as WinForms') writeLine(f,'from CLR.System.Drawing import Size, Point') #get tree from file tree=ElementTree(file=filenameXML) r=tree.getroot() nc=r.getiterator("System.Windows.Forms.Form") n=nc[0] el=n.find("Name") formName=el.get("value") s='class ' + formName + '(WinForms.Form):' writeLine(f,'') writeLine(f,s) writeLine(f,'') s=tab(1) + 'def __init__(self):' writeLine(f,s) el=n.find("ClientSize") s=tab(2) + 'self.' + propertyFormat(el,d) writeLine(f,s) n=n.find('Controls') processControls(n,'self',f,d) s=tab(1) + 'def run(self):' writeLine(f,s) s=tab(2) + 'WinForms.Application.Run(self)' writeLine(f,s) s='def main():' writeLine(f,s) s=tab(1) + formName + '().run()' writeLine(f,s) s="if __name__ == '__main__':" writeLine(f,s) s=tab(1) + 'main()' writeLine(f,s) f.close() def processControls(n,parent,f,d): for c in n._children: #these are the controls elname=c.find('Name') name=elname.get('value') controlType=c.tag controlType=controlType.replace('System.Windows.Forms','WinForms') s=tab(2) + 'self.' + name + ' = ' + controlType + '()' writeLine(f,'') writeLine(f,s) s=tab(2) + parent +'.Controls.Add(self.' + name + ')' writeLine(f,s) for cp in c._children: #these are control properties if not d.has_key(cp.tag): continue ## if cp.tag=='Name': ## continue ## if cp.tag=="Lines": ## continue ## if cp.tag=="TabPages": ## continue ## if cp.tag=="DockPadding": ## continue ## if cp.tag=="Controls": ## continue s=tab(2) + 'self.' + name + '.' + propertyFormat(cp,d) writeLine(f,s) if controlType=='WinForms.TabControl': tcname=name #remember tabcontrol name ntabpages=c.find('TabPages') for ntp in ntabpages._children: controlType=ntp.tag controlType=controlType.replace('System.Windows.Forms','WinForms') if controlType!='WinForms.TabPage': pass elname=ntp.find('Name') name=elname.get('value') writeLine(f,'') s=tab(2) + 'self.' + name + ' = ' + controlType + '()' writeLine(f,s) s=tab(2) + 'self.' + tcname +'.Controls.Add(self.' + name + ')' writeLine(f,s) for cp in ntp._children: if cp.tag=='Name': continue if cp.tag=="Lines": continue if cp.tag=="TabPages": continue if cp.tag=="DockPadding": continue if cp.tag=="Controls": continue s=tab(2) + parent + '.' + name + '.' + propertyFormat(cp,d) writeLine(f,s) ntpc=ntp.find('Controls') processControls(ntpc,(parent + '.' + name),f,d) ## for cp in c._children: ## if cp.tag=='Name': ## continue ## if cp.tag=="Lines": ## continue ## if cp.tag=="TabPages": ## continue ## s=tab(2) + 'self.' + name + '.' + propertyFormat(cp,d) ## writeLine(f,s) #s=tab(2)+parent+'.Controls.Add(self.' + name + ')' #writeLine(f,s) def writeLine(f,s): f.write(s+"\n") def sizeFormat(s): size=s size=size.replace('{','(') size=size.replace('}',')') size=size.replace('Width=','') size=size.replace('Height=','') return ('Size' + size) def pointFormat(s): point=s point=point.replace('{','(') point=point.replace('}',')') point=point.replace('X=','') point=point.replace('Y=','') return ('Point' + point) def colorFormat(s): sa=s.split(',') sn=sa[1][3:] sn+=','+ sa[2][3:] k=sa[3].find(']') sn+=',' + sa[3][3:k] sn="CLR.System.Drawing.Color.FromArgb(" + sn + ")" return sn def propertyFormat(el,d): print el.tag if not d.has_key(el.tag): pass type=d[el.tag] s=el.get('value') if type=='Size' : s=sizeFormat(s) elif type=='Point': s=pointFormat(s) elif type=='Int': pass elif type=='Boolean': pass elif type=='Enum': s="WinForms." + el.tag + '.' + s elif type=='Color': s=colorFormat(s) else: s="'" + s + "'" s=el.tag + '=' + s return(s)