import lxml.etree import os import Pictogram import gtk import glob import os.path def scan_grids(path,lookup): for file in glob.glob(path+"/*.xml"): print "[Grid] Found grid: " + file document = lxml.etree.parse(file) root = document.getroot() for node in root: if node.tag=="gridname": if node.text==lookup: return file return None class Grid: def __init__(self): self.name="noname" self.classname="noclass" self.width=1 self.height=1 self.pictograms=[[]] self.tabsize=0 self.parent=None #by default, grids have no parent, we wait until being instanced to set a parent def create(self,name,classname,width,height): self.name=name self.classname=classname self.width=width self.height=height def load(self,path,requested_theme): print "[Grid] Load: " + path document = lxml.etree.parse(path) root = document.getroot() self.filename=os.path.basename(path) self.filename=path print "[Grid] Filename:" + self.filename for node in root: if node.tag=="gridname": self.name=node.text if node.tag=="class": self.classname=node.text if node.tag=="width": self.width=int(node.text) if node.tag=="height": self.height=int(node.text) #This code need to be checked, as we cannot grant pictogram table is built before width and heigh values are already known self.pictograms=[[0 for col in range(self.height)] for row in range(self.width)] if node.tag=="pictograms": for pictogram in node: if pictogram.tag=="pictogram": pict=Pictogram.Pictogram() pict.position=(int(pictogram.get("col")),int(pictogram.get("row"))) for mnode in pictogram: if mnode.tag=="pictname": pict.name=mnode.text if mnode.tag=="background": #pict.background=Pictogram.Pictogram.lut[mnode.text] if mnode.text==None or mnode.text=="": pict.bakcground=gtk.gdk.color_parse("#ffffff") else: pict.background=gtk.gdk.color_parse(mnode.text) if mnode.tag=="images": #pict.image=mnode.text for imgsnode in mnode: if imgsnode.tag=="image": theme=None source=None for imgnode in imgsnode: if imgnode.tag=="theme": theme=imgnode.text if imgnode.tag=="source": source=imgnode.text if not theme==None and not source==None: if theme==requested_theme: pict.image=source print "[Grid] source:"+pict.image if mnode.tag=="caption": pict.caption=mnode.text if pict.caption==None: pict.caption="NONAME" fn=mnode.get("fontface") ft=mnode.get("typeface") fs=mnode.get("size") pict.font=fn+" "+fs fc=mnode.get("color") if fc=="": pict.fontcolor=gtk.gdk.color_parse("#000000") else: pict.fontcolor=gtk.gdk.color_parse(fc) if mnode.tag=="action": for action in mnode: if action.tag=="speech": pict.action=("speech",[action.text]) if action.tag=="sound": pict.action=("sound",[action.text]) if action.tag=="child": pict.action=("child",[action.text]) if action.tag=="parent": pict.action=("parent",None) if action.tag=="menuaction": pict.action=("menuaction",[action.text]) #if x,y=pict.position print pict.position self.pictograms[x-1][y-1]=pict