def write(self, path, conversations): if len(conversations) != 1: raise ParseError( ("'%s' only supports one conversation " "per file:\n '%s' has %i") % (self.type, path, len(conversations)) ) conversation = conversations[0] file_object = codecs.open(path, 'wb', 'utf-8') util.write_comment(file_object, const.HEADER_COMMENT % conversation.original_parser_name) self._write_title(file_object, conversation) for entry in conversation.entries: timefmt = self.TIME_FMT_CONVERSATION_WITH_DATE if entry.delayed \ else self.TIME_FMT_CONVERSATION self._write_entry(file_object, entry, conversation, timefmt) file_object.write('\n') # newline at end file_object.write('</body></html>\n') file_object.close() self.copy_images(path, conversation)
def write(self, path, conversations): if len(conversations) != 1: raise ParseError(("'%s' only supports one conversation " "per file:\n '%s' has %i") % (self.type, path, len(conversations))) conversation = conversations[0] file_object = codecs.open(path, 'wb', 'utf-8') util.write_comment( file_object, const.HEADER_COMMENT % conversation.original_parser_name) self._write_title(file_object, conversation) for entry in conversation.entries: timefmt = self.TIME_FMT_CONVERSATION_WITH_DATE if entry.delayed \ else self.TIME_FMT_CONVERSATION self._write_entry(file_object, entry, conversation, timefmt) file_object.write('\n') # newline at end file_object.write('</body></html>\n') file_object.close() self.copy_images(path, conversation)
def _write_entry(self, file_object, entry, conversation, timefmt): timestr = entry.time.strftime(timefmt) if isinstance(entry, Message): if entry.alternate: color = self.ALTERNATE_COLOR elif entry.isuser: color = self.SOURCE_COLOR else: color = self.DESTINATION_COLOR name = entry.alias if entry.alias else entry.sender autoreply = self.AUTOREPLY_HTML if entry.auto else '' timestr = entry.time.strftime(timefmt) # write real sender in groupchat Message # if sender is ambiguous (not source) if conversation.isgroup and entry.alias \ and entry.alias != entry.sender and \ entry.sender != conversation.source: util.write_comment(file_object, entry.sender) file_object.write(self.MESSAGE_LINE_FMT % (color, timestr, name, autoreply)) self._write_entry_html(file_object, entry) file_object.write(self.MESSAGE_LINE_END) elif isinstance(entry, Status): # append comment indicating this Status was not parsed by us if conversation.original_parser_name != self.type: text = "%s|%s|%s" % (entry.type, '1' if entry.system else '', entry.sender) util.write_comment(file_object, text) if entry.type == Status.ERROR: fmt = self.ERROR_LINE_FMT end = self.ERROR_LINE_END else: fmt = self.STATUS_LINE_FMT end = self.STATUS_LINE_END file_object.write(fmt % timestr) self._write_entry_html(file_object, entry) file_object.write(end) else: util.write_comment(file_object, entry.dump())
def write(self, path, conversations): if len(conversations) != 1: raise ParseError( ("'%s' only supports one conversation per file:" "\n %s has %i") % (self.type, path, len(conversations)) ) conversation = conversations[0] file_object = codecs.open(path, 'wb', 'utf-8') file_object.write(self.XML_HEADER+'\n') untransformed_source = self.UNTRANSFORMS['source'](conversation.source, conversation) attrs = dict(xmlns=self.XMLNS, account=untransformed_source, service=self.PAM_ECIVRES[conversation.service], resource=conversation.resource) # this attribute will only be useful if we're not the original parser if conversation.isgroup and \ conversation.original_parser_name != self.type: attrs['groupchat'] = "true" util.write_comment(file_object, const.HEADER_COMMENT % conversation.original_parser_name) self._write_xml(file_object, 'chat', attrs, conversation,close=False) file_object.write('\n') for i, entry in enumerate(conversation.entries): attrs = {'alias': entry.alias, 'sender': (untransformed_source if entry.sender == conversation.source else entry.sender) } if isinstance(entry, Message): name = 'message' if entry.auto: attrs['auto'] = "true" elif isinstance(entry, Status): name = 'status' attrs['type'] = self.PAMEPYT_SUTATS[entry.type] elif isinstance(entry, Event): name = 'event' attrs['type'] = self.PAMEPYT_TNEVE[entry.type] # no alias for event attrs['alias'] = '' if entry.system: # no alias or sender for these del attrs['alias'] del attrs['sender'] elif not attrs['alias']: del attrs['alias'] f1 = self.TIME_FMT_CONVERSATION[:-2] f2 = self.TIME_FMT_CONVERSATION[-2:] v1 = entry.time.strftime(f1) v2 = entry.time.strftime(f2) v = v1+v2[:3]+':'+v2[3:] attrs['time'] = v # comments should look like 1|status text comment = ['1', ''] if entry.alternate else ['', ''] if isinstance(entry, Status) and entry.type in Status.USER_TYPES: htmlattr = 'msg_html' if entry.has_other_html: comment[1] = ''.join([x.string for x in entry.html]) else: htmlattr = 'html' if [x for x in comment if x]: util.write_comment(file_object, '|'.join(comment)) self._write_xml(file_object, name, attrs, conversation, contents=getattr(entry, htmlattr)) if i != len(conversation.entries)-1: file_object.write('\n') file_object.write('</chat>') file_object.close() self.copy_images(path, conversation)
def write(self, path, conversations): if len(conversations) != 1: raise ParseError( ("'%s' only supports one conversation per file:" "\n %s has %i") % (self.type, path, len(conversations))) conversation = conversations[0] file_object = codecs.open(path, 'wb', 'utf-8') file_object.write(self.XML_HEADER + '\n') untransformed_source = self.UNTRANSFORMS['source'](conversation.source, conversation) attrs = dict(xmlns=self.XMLNS, account=untransformed_source, service=self.PAM_ECIVRES[conversation.service], resource=conversation.resource) # this attribute will only be useful if we're not the original parser if conversation.isgroup and \ conversation.original_parser_name != self.type: attrs['groupchat'] = "true" util.write_comment( file_object, const.HEADER_COMMENT % conversation.original_parser_name) self._write_xml(file_object, 'chat', attrs, conversation, close=False) file_object.write('\n') for i, entry in enumerate(conversation.entries): attrs = { 'alias': entry.alias, 'sender': (untransformed_source if entry.sender == conversation.source else entry.sender) } if isinstance(entry, Message): name = 'message' if entry.auto: attrs['auto'] = "true" elif isinstance(entry, Status): name = 'status' attrs['type'] = self.PAMEPYT_SUTATS[entry.type] elif isinstance(entry, Event): name = 'event' attrs['type'] = self.PAMEPYT_TNEVE[entry.type] # no alias for event attrs['alias'] = '' if entry.system: # no alias or sender for these del attrs['alias'] del attrs['sender'] elif not attrs['alias']: del attrs['alias'] f1 = self.TIME_FMT_CONVERSATION[:-2] f2 = self.TIME_FMT_CONVERSATION[-2:] v1 = entry.time.strftime(f1) v2 = entry.time.strftime(f2) v = v1 + v2[:3] + ':' + v2[3:] attrs['time'] = v # comments should look like 1|status text comment = ['1', ''] if entry.alternate else ['', ''] if isinstance(entry, Status) and entry.type in Status.USER_TYPES: htmlattr = 'msg_html' if entry.has_other_html: comment[1] = ''.join([x.string for x in entry.html]) else: htmlattr = 'html' if [x for x in comment if x]: util.write_comment(file_object, '|'.join(comment)) self._write_xml(file_object, name, attrs, conversation, contents=getattr(entry, htmlattr)) if i != len(conversation.entries) - 1: file_object.write('\n') file_object.write('</chat>') file_object.close() self.copy_images(path, conversation)