import os import re import lxml.etree import net.Lliurex.JClic.Library import net.Lliurex.Base import locale import gettext locale.textdomain("lliurex-recursos") gettext.textdomain("lliurex-recursos") _=gettext.gettext class Settings(net.Lliurex.Base.Base): conf_file=".edu.xtec.properties" lib_conf_file=".lliurex-recursos.conf" jclics = [] def __init__(self): self.info("Settings.__init__") self.is_configured = False self.is_lib_configured = False self.path = "" self.libraries=[] self.lib_path=None self.must_save = False def look_up_settings(self): user_home = os.environ["HOME"] if os.path.exists(user_home + os.sep + Settings.conf_file): self.is_configured = True file = open(user_home + os.sep + Settings.conf_file) for line in file: match=re.match("JClic=.*$",line) if not match==None: tmp=line.split("=") self.path=tmp[1].rstrip('\n') self.info(("JClic path:") + tmp[1]) break def read_settings(self): if not self.is_configured: return if not os.path.exists(self.path + os.sep + "jclic.cfg"): self.is_configured = False return document = lxml.etree.parse(self.path + os.sep + "jclic.cfg") root = document.getroot() for node in root: if node.tag=="libraryManager": for pnode in node: if pnode.tag=="library": name = pnode.get("name") path = pnode.get("path") self.info(("Library" ) +" " + name+ "->"+path) library = net.Lliurex.JClic.Library.Library(name,path) library.load() self.libraries.append(library) def read_lib_config(self): user_home = os.environ["HOME"] if os.path.exists(user_home + os.sep + Settings.lib_conf_file): f = open(user_home + os.sep + Settings.lib_conf_file) for line in f.readlines(): tmp = line.split("=") #this may ensure a tuple 'variable'='value' has been found if len(tmp)==2: if tmp[0]=="lib_path": self.lib_path = tmp[1] self.is_lib_configured=True self.info("lib_path"+tmp[1]) f.close() def get_current_installed(self): tmp = self.lib_path.split(".") for lib in self.libraries: if lib.name==tmp[0]: for current in lib.get_current_installed(tmp[1]): self.jclics.append(current) def sync_dir(self): tmp = self.lib_path.split(".") for lib in self.libraries: if lib.name==tmp[0]: lib.sync_dir(tmp[1]) def get_is_configured(self): return self.is_configured def get_is_lib_configured(self): return self.is_lib_configured def get_path(self): return self.path def get_libraries(self): return self.libraries def set_lib_path(self,path): self.must_save = True self.lib_path=path self.is_lib_configured = True def save_lib_settings(self): if self.must_save: user_home = os.environ["HOME"] f = open(user_home + os.sep + Settings.lib_conf_file,"w") f.writelines("lib_path="+self.lib_path) f.close()