def generate() -> etree.Element: """Generate the XML tree with first run tasks.""" # Create an empty XML tree first root = etree.Element('project') for task in tasks: tags = extract_tags_from_text(task['content']) task_tag = etree.SubElement(root, 'task') task_tag.attrib['id'] = task['id'] task_tag.attrib['status'] = 'Active' task_tag.attrib['tags'] = ','.join(tags) task_tag.attrib['recurring'] = 'False' title = etree.SubElement(task_tag, 'title') title.text = task['title'] recurring_term = etree.SubElement(task_tag, 'recurring_term') recurring_term.text = 'None' for sub in task['subtasks']: subtask = etree.SubElement(task_tag, 'subtask') subtask.text = sub content = etree.SubElement(task_tag, 'content') content.text = task['content'] return etree.ElementTree(root)
def set_complex_title(self, text, tags=[]): if tags: assert (isinstance(tags[0], str)) due_date = Date.no_date() defer_date = Date.no_date() if text: # Get tags in the title for match in extract_tags_from_text(text): tags.append(match) # Get attributes regexp = r'([\s]*)([\w-]+):\s*([^\s]+)' matches = re.findall(regexp, text, re.UNICODE) for spaces, attribute, args in matches: valid_attribute = True if attribute.lower() in ["tags", _("tags"), "tag", _("tag")]: for tag in args.split(","): if not tag.strip() == "@" and not tag.strip() == "": if not tag.startswith("@"): tag = "@" + tag tags.append(tag) elif attribute.lower() in [ "defer", _("defer"), "start", _("start") ]: try: defer_date = Date.parse(args) except ValueError: valid_attribute = False elif attribute.lower() == "due" or \ attribute.lower() == _("due"): try: due_date = Date.parse(args) except: valid_attribute = False else: # attribute is unknown valid_attribute = False if valid_attribute: # remove valid attribute from the task title text = \ text.replace(f"{spaces}{attribute}:{args}", "") for t in tags: self.add_tag(t) if text != "": self.set_title(text.strip()) self.set_to_keep() self.set_due_date(due_date) self.set_start_date(defer_date)
def addtask(doc, ze_id, title, text, children): """Initialize GTG tasks in order to display them at first run.""" t_xml = doc.createElement("task") t_xml.setAttribute("id", ze_id) t_xml.setAttribute("status", "Active") tags = extract_tags_from_text(text) t_xml.setAttribute("tags", ",".join(tags)) cleanxml.addTextNode(doc, t_xml, "title", title) for child in children: cleanxml.addTextNode(doc, t_xml, "subtask", child) cleanxml.addTextNode(doc, t_xml, "content", text) return t_xml
def _populate_task(self, task, evo_task): """ Updates the attributes of a GTG task copying the ones of an Evolution task """ task.set_title(evo_task.get_summary()) text = evo_task.get_description() if text is None: text = "" task.set_text(text) due_date_timestamp = evo_task.get_due() if isinstance(due_date_timestamp, (int, float)): due_date = self.__date_from_evo_to_gtg(due_date_timestamp) else: due_date = Date.no_date() task.set_due_date(due_date) status = evo_task.get_status() if task.get_status() != _EVOLUTION_TO_GTG_STATUS[status]: task.set_status(_EVOLUTION_TO_GTG_STATUS[status]) task.set_only_these_tags(extract_tags_from_text(text))
def _populate_task(self, task, note): """ Copies the content of a Tomboy note into a task. @param task: a GTG Task @param note: a Tomboy note """ # add tags objects (it's not enough to have @tag in the text to add a # tag with self.TomboyConnection(self, *self.BUS_ADDRESS) as tomboy: with self.DbusWatchdog(self): content = tomboy.GetNoteContents(note) # update the tags list task.set_only_these_tags(extract_tags_from_text(content)) # extract title and text [title, text] = self._tomboy_split_title_and_text(str(content)) # Tomboy speaks unicode, we don't title = unicodedata.normalize('NFKD', title) text = unicodedata.normalize('NFKD', text) task.set_title(title) task.set_text(text) task.add_remote_id(self.get_id(), note)
def generate() -> etree.Element: """Generate the XML tree with first run tasks.""" # Create an empty XML tree first root = skeleton() taskslist = root.find('tasklist') taglist = root.find('taglist') # Fill tasks for task in tasks: task_tag = etree.SubElement(taskslist, 'task') task_tag.set('id', task['id']) task_tag.set('status', 'Active') title = etree.SubElement(task_tag, 'title') title.text = task['title'] # Add tags for this task task_tags = etree.SubElement(task_tag, 'tags') for tag in set(extract_tags_from_text(task['content'])): tag_name = tag.replace('@', '') tags.update({ tag_name: str(uuid4()), }) tag_tag = etree.SubElement(task_tags, 'tag') tag_tag.text = tags.get(tag_name) dates = etree.SubElement(task_tag, 'dates') added = etree.SubElement(dates, 'added') added.text = task['added'] recurring_enabled = etree.SubElement(dates, 'recurring_enabled') recurring_enabled.text = 'false' recurring = etree.SubElement(task_tag, 'recurring') recurring.set('enabled', 'false') recurring_term = etree.SubElement(recurring, 'term') recurring_term.text = 'None' modify = etree.SubElement(dates, 'modified') modify.text = task['modified'] # Add subtasks in this tasks subtask = etree.SubElement(task_tag, 'subtasks') for sub in task['subtasks']: sub_tag = etree.SubElement(subtask, 'sub') sub_tag.text = sub content = etree.SubElement(task_tag, 'content') content.text = etree.CDATA(task['content']) # Fill tags for tag, tid in tags.items(): tag_tag = etree.SubElement(taglist, 'tag') tag_tag.set('id', tid) tag_tag.set('name', tag) return etree.ElementTree(root)
def assertTags(self, text, expected_tags): tag_list = extract_tags_from_text(text) self.assertEqual(expected_tags, tag_list)