def _get_time(self): if self.time_string == None: self._cached_time_string = None self._cached_time = None return None if (not hasattr(self, '_cached_time_string') or self.time_string != self._cached_time_string): self._cached_time_string = self.time_string self._cached_time = utility.str_to_time(self.time_string) return self._cached_time
def from_xml(self, xml_string, preserve_uuids=False): u""" Note: If a bug uuid is given, set .alt_id to it's value. >>> bugA = Bug(uuid="0123", summary="Need to test Bug.from_xml()") >>> bugA.date = "Thu, 01 Jan 1970 00:00:00 +0000" >>> bugA.creator = u'Fran\xe7ois' >>> bugA.extra_strings += ['TAG: very helpful'] >>> commA = bugA.comment_root.new_reply(body='comment A') >>> commB = bugA.comment_root.new_reply(body='comment B') >>> commC = commA.new_reply(body='comment C') >>> xml = bugA.xml(show_comments=True) >>> bugB = Bug() >>> bugB.from_xml(xml) >>> bugB.xml(show_comments=True) == xml False >>> bugB.uuid = bugB.alt_id >>> for comm in bugB.comments(): ... comm.uuid = comm.alt_id ... comm.alt_id = None >>> bugB.xml(show_comments=True) == xml True >>> bugB.explicit_attrs # doctest: +NORMALIZE_WHITESPACE ['severity', 'status', 'creator', 'time', 'summary'] >>> len(list(bugB.comments())) 3 >>> bugC = Bug() >>> bugC.from_xml(xml, preserve_uuids=True) >>> bugC.uuid == bugA.uuid True """ if type(xml_string) == types.UnicodeType: xml_string = xml_string.strip().encode('unicode_escape') if hasattr(xml_string, 'getchildren'): # already an ElementTree Element bug = xml_string else: bug = ElementTree.XML(xml_string) if bug.tag != 'bug': raise utility.InvalidXML( \ 'bug', bug, 'root element must be <bug>') tags=['uuid','short-name','severity','status','assigned', 'reporter', 'creator','created','summary','extra-string'] self.explicit_attrs = [] uuid = None estrs = [] comments = [] for child in bug.getchildren(): if child.tag == 'short-name': pass elif child.tag == 'comment': comm = comment.Comment(bug=self) comm.from_xml(child, preserve_uuids=preserve_uuids) comments.append(comm) continue elif child.tag in tags: if child.text == None or len(child.text) == 0: text = settings_object.EMPTY else: text = xml.sax.saxutils.unescape(child.text) if not isinstance(text, unicode): text = text.decode('unicode_escape') text = text.strip() if child.tag == 'uuid' and not preserve_uuids: uuid = text continue # don't set the bug's uuid tag. elif child.tag == 'created': if text is not settings_object.EMPTY: self.time = utility.str_to_time(text) self.explicit_attrs.append('time') continue elif child.tag == 'extra-string': estrs.append(text) continue # don't set the bug's extra_string yet. attr_name = child.tag.replace('-','_') self.explicit_attrs.append(attr_name) setattr(self, attr_name, text) else: libbe.LOG.warning( 'ignoring unknown tag {0} in {1}'.format( child.tag, comment.tag)) if uuid != self.uuid: if not hasattr(self, 'alt_id') or self.alt_id == None: self.alt_id = uuid self.extra_strings = estrs self.add_comments(comments, ignore_missing_references=True)
def _get_time(self): if self.date == None: return None return utility.str_to_time(self.date)
def from_xml(self, xml_string, preserve_uuids=False): u""" Note: If a bug uuid is given, set .alt_id to it's value. >>> bugA = Bug(uuid="0123", summary="Need to test Bug.from_xml()") >>> bugA.date = "Thu, 01 Jan 1970 00:00:00 +0000" >>> bugA.creator = u'Fran\xe7ois' >>> bugA.extra_strings += ['TAG: very helpful'] >>> commA = bugA.comment_root.new_reply(body='comment A') >>> commB = bugA.comment_root.new_reply(body='comment B') >>> commC = commA.new_reply(body='comment C') >>> xml = bugA.xml(show_comments=True) >>> bugB = Bug() >>> bugB.from_xml(xml) >>> bugB.xml(show_comments=True) == xml False >>> bugB.uuid = bugB.alt_id >>> for comm in bugB.comments(): ... comm.uuid = comm.alt_id ... comm.alt_id = None >>> bugB.xml(show_comments=True) == xml True >>> bugB.explicit_attrs # doctest: +NORMALIZE_WHITESPACE ['severity', 'status', 'creator', 'time', 'summary'] >>> len(list(bugB.comments())) 3 >>> bugC = Bug() >>> bugC.from_xml(xml, preserve_uuids=True) >>> bugC.uuid == bugA.uuid True """ if type(xml_string) == types.UnicodeType: xml_string = xml_string.strip().encode('unicode_escape') if hasattr(xml_string, 'getchildren'): # already an ElementTree Element bug = xml_string else: bug = ElementTree.XML(xml_string) if bug.tag != 'bug': raise utility.InvalidXML( \ 'bug', bug, 'root element must be <bug>') tags = [ 'uuid', 'short-name', 'severity', 'status', 'assigned', 'reporter', 'creator', 'created', 'summary', 'extra-string' ] self.explicit_attrs = [] uuid = None estrs = [] comments = [] for child in bug.getchildren(): if child.tag == 'short-name': pass elif child.tag == 'comment': comm = comment.Comment(bug=self) comm.from_xml(child, preserve_uuids=preserve_uuids) comments.append(comm) continue elif child.tag in tags: if child.text == None or len(child.text) == 0: text = settings_object.EMPTY else: text = xml.sax.saxutils.unescape(child.text) if not isinstance(text, unicode): text = text.decode('unicode_escape') text = text.strip() if child.tag == 'uuid' and not preserve_uuids: uuid = text continue # don't set the bug's uuid tag. elif child.tag == 'created': if text is not settings_object.EMPTY: self.time = utility.str_to_time(text) self.explicit_attrs.append('time') continue elif child.tag == 'extra-string': estrs.append(text) continue # don't set the bug's extra_string yet. attr_name = child.tag.replace('-', '_') self.explicit_attrs.append(attr_name) setattr(self, attr_name, text) else: libbe.LOG.warning('ignoring unknown tag {0} in {1}'.format( child.tag, comment.tag)) if uuid != self.uuid: if not hasattr(self, 'alt_id') or self.alt_id == None: self.alt_id = uuid self.extra_strings = estrs self.add_comments(comments, ignore_missing_references=True)