import lxml.etree import os import re import zipfile import net.Lliurex.JClic.Activity import net.Lliurex.JClic.Settings import locale import gettext import net.Lliurex.Base locale.textdomain("lliurex-recursos") gettext.textdomain("lliurex-recursos") _=gettext.gettext class Library(net.Lliurex.Base.Base): def __init__(self,name,path): self.info("init") self.path = path self.name=name self.root= None self.document = None self.activities = [] def load(self): print "Loading library " + self.name self.document = lxml.etree.parse(self.path) self.root = self.document.getroot() for node in self.root: if node.tag=="activities": for pnode in node: if pnode.tag=="activity": caption="noname" value = pnode.get("name") children = [] for snode in pnode: if snode.tag=="description": for qnode in snode: if qnode.tag=="p": caption = qnode.text if snode.tag=="menuElement": sequence=snode.get("sequence") if not sequence==None: #print "child ",sequence self.info("child "+sequence) children.append(sequence) #print "Activity ",value, " ",caption self.info("Activity "+value+" "+caption) activity = net.Lliurex.JClic.Activity.Activity(caption,value) activity.children = children self.activities.append(activity) #if #for #if #for def get_current_installed(self,id): activity_node = None installed = [] #todo: improve speed #looking activity node for node in self.root: if node.tag=="activities": for pnode in node: if pnode.tag=="activity": value = pnode.get("name") if value==id: activity_node = pnode break for pnode in activity_node: if pnode.tag=="menuElement": if pnode.get("sequence")==None: #print "JClic Found: " + pnode.get("caption") + "=>" + pnode.get("path") installed.append((pnode.get("caption"),pnode.get("path"))) return installed def sync_dir(self,id): activity_node = None #todo: improve speed #looking activity node for node in self.root: if node.tag=="activities": for pnode in node: if pnode.tag=="activity": value = pnode.get("name") if value==id: activity_node = pnode break for pnode in activity_node: if pnode.tag=="menuElement": if pnode.get("sequence")==None: activity_node.remove(pnode) for caption,path in net.Lliurex.JClic.Settings.Settings.jclics: #print "Found: ",caption,"->",path self.info("Found: "+caption+"->"+path) child=lxml.etree.SubElement(activity_node,"menuElement") child.set("caption",unicode(caption)) child.set("path",path) f = open(self.path,"w") f.writelines(lxml.etree.tostring(self.root, encoding="UTF-8",xml_declaration=True,pretty_print=True)) f.close()