def __write_org_subitem(self, timestamp, output, note="", properties=OrgProperties(), tags=[]): """ internally called by write_org_subitem and __append_org_subitem """ output_tags = "" if tags != []: output_tags = u"\t:" + ":".join(map(str, tags)) + ":" output = output.lstrip() timestamp = timestamp.strip() self.writeln(u"** " + timestamp + u" " + output + output_tags) if note != "": for n in note.splitlines(): self.writeln(" " + n) self.writeln(unicode(properties)) if self.__test: self.write(properties.get_multiline_properties()) else: self.writeln(properties.get_multiline_properties())
def write_org_subitem(self, timestamp, output, note="", properties=OrgProperties(), tags=None): """ Writes an org item line. i.e:** <timestamp> <output> :<tags>:\n :PROPERTIES: <properties> :ID: -generated id- :END: if an argument -a or --append is given, then a desicion regarding the :ID: is made if the item has to be written to file @param timestamp: str/unicode @param output: st tar/unicode @param note: str/unicode @param tags: list of tags @param properties: OrgProperties object """ assert (timestamp.__class__ == str or timestamp.__class__ == unicode) assert tags.__class__ == list or tags == None assert properties.__class__ == OrgProperties assert (output.__class__ == str or output.__class__ == unicode) assert (note.__class__ == str or note.__class__ == unicode) # count the entries we have written, if above our limit do not write if self.__number_entries and \ self.__entries_count == self.__number_entries: return else: self.__entries_count += 1 if tags == None: tags = [] if self.__autotag_dict != {}: self.__get_autotags(tags, output) if self.__append: self.__append_org_subitem(timestamp, output, note, properties, tags) else: self.__write_org_subitem(timestamp, output, note, properties, tags)
def __append_org_subitem(self, timestamp, output, note="", properties=OrgProperties(), tags=[]): """ Checks if subitem exists in orgfile (:ID: <id> is same), if not, it will be appended """ identifier = properties.get_id() if id == None: raise Exception("id :ID: Property not set!") if self.__id_exists(identifier): # do nothing, id exists ... logging.debug("NOT appending") else: # id does not exist so we can append logging.debug("appending") self.__write_org_subitem(timestamp, output, note, properties, tags)
def write_org_subitem(self, timestamp, output, note="", properties=OrgProperties(), tags=None): """ Writes an org item line. i.e:** <timestamp> <output> :<tags>:\n :PROPERTIES: <properties> :ID: -generated id- :END: if an argument -a or --append is given, then a desicion regarding the :ID: is made if the item has to be written to file @param timestamp: str/unicode @param output: st tar/unicode @param note: str/unicode @param tags: list of tags @param properties: OrgProperties object """ assert (timestamp.__class__ == str or timestamp.__class__ == unicode) assert tags.__class__ == list or tags == None assert properties.__class__ == OrgProperties assert (output.__class__ == str or output.__class__ == unicode) assert (note.__class__ == str or note.__class__ == unicode) # count the entries we have written, if above our limit do not write if self.__number_entries and \ self.__entries_count == self.__number_entries: return else: self.__entries_count += 1 if tags == None: tags = [] if self.__autotag_dict != {}: self.__get_autotags(tags, output) ## fix time-stamps (if user wants to) if self.__timestamp_delta: timestamp = OrgFormat.apply_timedelta_to_Orgmode_timestamp( timestamp, float(self.__timestamp_delta)) ## a bit of a hack to get inactive time-stamps: ## FIXXME: use OrgFormat method to generate inactive time-stamps in the first place and remove asserts if self.__inactive_timestamps: assert ( timestamp[0] == '<' ) ## at least try to find cases where this replace method fails assert ( timestamp[-1] == '>' ) ## at least try to find cases where this replace method fails timestamp = '[' + timestamp[1:-1] + ']' if self.__append: self.__append_org_subitem(timestamp, output, note, properties, tags) else: self.__write_org_subitem(timestamp, output, note, properties, tags)
def parse_message(message, add_body=False): """ parses whole mail from string @param message: mail message @param add_body: if specified, body is added @return values for OrgWriter.write_org_subitem """ msg = message_from_string(message) # Read only these fields use_headers = ["To", "Date", "From", "Subject", "Reply-To", "Newsgroups", "Cc", ] # These fields are added, if found to :PROPERTIES: drawer not_properties = ["Date", "Subject", "From" ] properties = OrgProperties() headers = {} logging.debug("Message items:") logging.debug(msg.items()) msg_id = None # fill headers and properties for key, value in msg.items(): value = value.replace("\r", "").decode('utf-8') if key in use_headers: headers[key] = value if key not in not_properties: properties.add(key, value.replace("\n", "")) if key.upper() == "MESSAGE-ID": msg_id = value notes = "" # look for payload # if more than one payload, use text/plain payload if add_body: payload = msg.get_payload() if payload.__class__ == list: # default use payload[0] payload_msg = payload[0].get_payload() for payload_id in len(payload): for param in payload[payload_id].get_params(): if param[0] == 'text/plain': payload_msg = payload[payload_id].get_payload() break if payload_msg != payload[0].get_payload(): break notes = payload_msg else: notes = payload notes = notes.replace("\r", "").decode('utf-8') output_from = MailParser.get_value_or_empty_str(headers, "From") if output_from != "": output_from = OrgFormat.contact_mail_mailto_link(output_from) subject = MailParser.get_value_or_empty_str(headers, "Subject", True) dt = MailParser.get_value_or_empty_str(headers, "Date", False) timestamp = "" if dt != "": try: time_tupel = time.localtime(time.mktime(parsedate(dt))) timestamp = OrgFormat.datetime(time_tupel) except TypeError: logging.error("could not parse dateime from msg %s", dt) properties.add_data_for_hashing(timestamp + "_" + msg_id) if "Newsgroups" in headers: ng_list = [] for ng in headers["Newsgroups"].split(","): ng_list.append(OrgFormat.newsgroup_link(ng)) output_ng = ", ".join(map(str, ng_list)) output = output_from + u"@" + output_ng + ": " + subject else: output = output_from + u": " + subject return timestamp, output, notes, properties
def parse_message(message, add_body=False): """ parses whole mail from string @param message: mail message @param add_body: if specified, body is added @return values for OrgWriter.write_org_subitem """ msg = message_from_string(message) # Read only these fields use_headers = [ "To", "Date", "From", "Subject", "Reply-To", "Newsgroups", "Cc", ] # These fields are added, if found to :PROPERTIES: drawer not_properties = ["Date", "Subject", "From"] properties = OrgProperties() headers = {} logging.debug("Message items:") logging.debug(msg.items()) msg_id = None # fill headers and properties for key, value in msg.items(): value = value.replace("\r", "").decode('utf-8') if key in use_headers: headers[key] = value if key not in not_properties: properties.add( key, MailParser.get_value_or_empty_str(headers, key, True)) if key.upper() == "MESSAGE-ID": msg_id = value notes = "" # look for payload # if more than one payload, use text/plain payload if add_body: payload = msg.get_payload() if payload.__class__ == list: # default use payload[0] payload_msg = payload[0].get_payload() for payload_id in len(payload): for param in payload[payload_id].get_params(): if param[0] == 'text/plain': payload_msg = payload[payload_id].get_payload() break if payload_msg != payload[0].get_payload(): break notes = payload_msg else: notes = payload notes = notes.replace("\r", "").decode('utf-8') output_from = MailParser.get_value_or_empty_str(headers, "From") if output_from != "": output_from = OrgFormat.contact_mail_mailto_link(output_from) subject = MailParser.get_value_or_empty_str(headers, "Subject", True) dt = MailParser.get_value_or_empty_str(headers, "Date", False) timestamp = "" if dt != "": try: time_tupel = time.localtime(time.mktime(parsedate(dt))) timestamp = OrgFormat.datetime(time_tupel) except TypeError: logging.error("could not parse dateime from msg %s", dt) properties.add_data_for_hashing(timestamp + "_" + msg_id) if "Newsgroups" in headers: ng_list = [] for ng in headers["Newsgroups"].split(","): ng_list.append(OrgFormat.newsgroup_link(ng)) output_ng = ", ".join(map(str, ng_list)) output = output_from + u"@" + output_ng + ": " + subject else: output = output_from + u": " + subject return timestamp, output, notes, properties