def __init__(self, parameters): """ Instantiates a new backend. @param parameters: A dictionary of parameters, generated from _static_parameters. A few parameters are added to those, the list of these is in the "DefaultBackend" class, look for the KEY_* constants. The backend should take care if one expected value is None or does not exist in the dictionary. """ super(Backend, self).__init__(parameters) # RETROCOMPATIBILIY # NOTE: retrocompatibility from the 0.2 series to 0.3. # We convert "filename" to "path and we forget about "filename " if "need_conversion" in parameters: parameters["path"] = parameters.pop("need_conversion") if self.KEY_DEFAULT_BACKEND not in parameters: parameters[self.KEY_DEFAULT_BACKEND] = True self.doc, self.xmlproj = cleanxml.openxmlfile(self.get_path(), "project") # status if backup was used while trying to open xml file self._used_backup = cleanxml.used_backup() self._backup_file_info = cleanxml.backup_file_info() # Make safety daily backup after loading cleanxml.savexml(self.get_path(), self.doc, backup=True)
def set_task(self, task): """ This function is called from GTG core whenever a task should be saved, either because it's a new one or it has been modified. This function will look into the loaded XML object if the task is present, and if it's not, it will create it. Then, it will save the task data in the XML object. @param task: the task object to save """ tid = task.get_id() # We create an XML representation of the task t_xml = taskxml.task_to_xml(self.doc, task) # we find if the task exists in the XML treenode. existing = None for node in self.xmlproj.childNodes: if node.nodeName == TASK_NODE and node.getAttribute("id") == tid: existing = node modified = False # We then replace the existing node if existing and t_xml: # We will write only if the task has changed if t_xml.toxml() != existing.toxml(): self.xmlproj.replaceChild(t_xml, existing) modified = True # If the node doesn't exist, we create it else: self.xmlproj.appendChild(t_xml) modified = True # if the XML object has changed, we save it to file if modified and self._parameters["path"] and self.doc: cleanxml.savexml(self.get_path(), self.doc)
def save_tagtree(self): """ Saves the tag tree to an XML file """ if not self.tagfile_loaded: return doc, xmlroot = cleanxml.emptydoc(TAG_XMLROOT) tags = self._tagstore.get_main_view().get_all_nodes() already_saved = [] for tagname in tags: if tagname in already_saved: continue tag = self._tagstore.get_node(tagname) attributes = tag.get_all_attributes(butname=True, withparent=True) if "special" in attributes or len(attributes) == 0: continue t_xml = doc.createElement("tag") t_xml.setAttribute("name", tagname) for attr in attributes: # skip labels for search tags if tag.is_search_tag() and attr == 'label': continue value = tag.get_attribute(attr) if value: t_xml.setAttribute(attr, value) xmlroot.appendChild(t_xml) already_saved.append(tagname) cleanxml.savexml(TAGS_XMLFILE, doc, backup=True)
def save_tagtree(self): """ Saves the tag tree to an XML file """ if not self.tagfile_loaded: return doc, xmlroot = cleanxml.emptydoc(TAG_XMLROOT) tags = self._tagstore.get_main_view().get_all_nodes() already_saved = [] for tagname in tags: if tagname in already_saved: continue tag = self._tagstore.get_node(tagname) attributes = tag.get_all_attributes(butname=True, withparent=True) if "special" in attributes or len(attributes) == 0: continue t_xml = doc.createElement("tag") t_xml.setAttribute("name", tagname) for attr in attributes: # skip labels for search tags if tag.is_search_tag() and attr == "label": continue value = tag.get_attribute(attr) if value: t_xml.setAttribute(attr, value) xmlroot.appendChild(t_xml) already_saved.append(tagname) cleanxml.savexml(TAGS_XMLFILE, doc, backup=True)
def __init__(self, parameters): """ Instantiates a new backend. @param parameters: A dictionary of parameters, generated from _static_parameters. A few parameters are added to those, the list of these is in the "DefaultBackend" class, look for the KEY_* constants. The backend should take care if one expected value is None or does not exist in the dictionary. """ super(Backend, self).__init__(parameters) # RETROCOMPATIBILIY # NOTE: retrocompatibility from the 0.2 series to 0.3. # We convert "filename" to "path and we forget about "filename " if "need_conversion" in parameters: parameters["path"] = parameters.pop("need_conversion") if self.KEY_DEFAULT_BACKEND not in parameters: parameters[self.KEY_DEFAULT_BACKEND] = True self.doc, self.xmlproj = cleanxml.openxmlfile( self.get_path(), "project") # status if backup was used while trying to open xml file self._used_backup = cleanxml.used_backup() self._backup_file_info = cleanxml.backup_file_info() # Make safety daily backup after loading cleanxml.savexml(self.get_path(), self.doc, backup=True)
def this_is_the_first_run(self, xml): """ Called upon the very first GTG startup. This function is needed only in this backend, because it can be used as default one. The xml parameter is an object containing GTG default tasks. It will be saved to a file, and the backend will be set as default. @param xml: an xml object containing the default tasks. """ self._parameters[self.KEY_DEFAULT_BACKEND] = True cleanxml.savexml(self.get_path(), xml) self.doc, self.xmlproj = cleanxml.openxmlfile( self.get_path(), "project")
def remove_task(self, tid): """ This function is called from GTG core whenever a task must be removed from the backend. Note that the task could be not present here. @param tid: the id of the task to delete """ modified = False for node in self.xmlproj.childNodes: if node.nodeName == TASK_NODE and node.getAttribute("id") == tid: modified = True self.xmlproj.removeChild(node) # We save the XML file only if it's necessary if modified: cleanxml.savexml(self.get_path(), self.doc, backup=True)
def remove_task(self, tid): ''' This function is called from GTG core whenever a task must be removed from the backend. Note that the task could be not present here. @param tid: the id of the task to delete ''' modified = False for node in self.xmlproj.childNodes: if node.getAttribute("id") == tid: modified = True self.xmlproj.removeChild(node) if tid in self.tids: self.tids.remove(tid) #We save the XML file only if it's necessary if modified: cleanxml.savexml(self._parameters["path"], self.doc)
def save(self, quit=False): """ Saves the backends parameters. @param quit: If quit is true, backends are shut down """ try: self.start_get_tasks_thread.join() except Exception: pass doc, xmlconfig = cleanxml.emptydoc("config") # we ask all the backends to quit first. if quit: # we quit backends in parallel threads_dic = {} for b in self.get_all_backends(): thread = threading.Thread(target=b.quit) threads_dic[b.get_id()] = thread thread.start() for backend_id, thread in threads_dic.iteritems(): # after 20 seconds, we give up thread.join(20) if thread.isAlive(): Log.error("The %s backend stalled while quitting", backend_id) # we save the parameters for b in self.get_all_backends(disabled=True): t_xml = doc.createElement("backend") for key, value in b.get_parameters().iteritems(): if key in ["backend", "xmlobject"]: # We don't want parameters, backend, xmlobject: # we'll create them at next startup continue param_type = b.get_parameter_type(key) value = b.cast_param_type_to_string(param_type, value) t_xml.setAttribute(str(key), value) # Saving all the projects at close xmlconfig.appendChild(t_xml) datadir = CoreConfig().get_data_dir() datafile = os.path.join(datadir, CoreConfig.DATA_FILE) cleanxml.savexml(datafile, doc, backup=True) # Saving the tagstore self.save_tagtree()
def save_tagtree(self): if self.tagfile: doc, xmlroot = cleanxml.emptydoc(TAG_XMLROOT) tags = self.__tagstore.get_main_view().get_all_nodes() already_saved = [] #We avoid saving the same tag twice #we don't save tags with no attributes #It saves space and allow the saved list growth to be controlled for tname in tags: t = self.__tagstore.get_node(tname) attr = t.get_all_attributes(butname = True, withparent = True) if "special" not in attr and len(attr) > 0: tagname = t.get_name() if not tagname in already_saved: t_xml = doc.createElement("tag") t_xml.setAttribute("name", tagname) already_saved.append(tagname) for a in attr: value = t.get_attribute(a) if value: t_xml.setAttribute(a, value) xmlroot.appendChild(t_xml) cleanxml.savexml(self.tagfile, doc)