diff options
author | W. Trevor King <wking@drexel.edu> | 2009-07-12 13:56:05 -0400 |
---|---|---|
committer | W. Trevor King <wking@drexel.edu> | 2009-07-12 13:56:05 -0400 |
commit | 37c4e7f0f0d012e8df88b94022bc9a9d75373831 (patch) | |
tree | 009ffafad4ade518ee0cf14ab67c2268ba37602e /xml/be-xml-to-mbox | |
parent | d49efee62f70c9f49a5236719fd8c52dabd37dae (diff) | |
download | bugseverywhere-37c4e7f0f0d012e8df88b94022bc9a9d75373831.tar.gz |
be-mbox-to-xml passes attributes on to each part of multipart messages.
Previously "message[<some-attr>]" just returned None if it wasn't set
for that message part, which overwrote anything passed in through
fields.
"from" and "date" added to list of attributes passed along.
For be-xml-to-mbox, "alt-id" was added to Comment._attrs,
and Comment.print_to_mbox was adjusted to handle the case where
we have no information about the parent bug.
With all of this, I can complete the loop
be-mbox-to-xml example.mbox | be-xml-to-mbox > example2.mbox
without errors :p.
Finally, be-xml-to-mbox has been adjusted to also work on files (it
had previously only handled data via stdin). We can't add stdin
handling to be-mbox-to-xml though, because the mailbox package needs
an actual file to work on, and I haven't setup a tmpfile workaround
yet...
Diffstat (limited to 'xml/be-xml-to-mbox')
-rwxr-xr-x | xml/be-xml-to-mbox | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/xml/be-xml-to-mbox b/xml/be-xml-to-mbox index 5e59e8c..b74a33d 100755 --- a/xml/be-xml-to-mbox +++ b/xml/be-xml-to-mbox @@ -26,6 +26,7 @@ followed by a blank line. """ #from mailbox import mbox, Message # the mailbox people really want an on-disk copy +import codecs import email.utils from libbe.encoding import get_encoding, set_IO_stream_encodings from libbe.utility import str_to_time as rfc2822_to_gmtime_integer @@ -125,19 +126,34 @@ class Bug (LimitedAttrDict): class Comment (LimitedAttrDict): _attrs = [u"uuid", + u"alt-id", u"short-name", u"in-reply-to", u"from", u"date", u"content-type", u"body"] - def print_to_mbox(self, bug): + def print_to_mbox(self, bug=None): + if bug == None: + bug = Bug() + bug[u"uuid"] = u"no-uuid" name,addr = email.utils.parseaddr(self["from"]) print "From %s %s" % (addr, rfc2822_to_asctime(self["date"])) - print "Message-ID: <%s@%s>" % (self["uuid"], DEFAULT_DOMAIN) + if "uuid" in self: id = self["uuid"] + elif "alt-id" in self: id = self["alt-id"] + else: id = None + if id != None: + print "Message-ID: <%s@%s>" % (id, DEFAULT_DOMAIN) print "Date: %s" % self["date"] print "From: %s" % self["from"] - print "Subject: %s: %s" % (self["short-name"], bug["summary"]) + subject = "" + if "short-name" in self: + subject += self["short-name"]+u": " + if "summary" in bug: + subject += bug["summary"] + else: + subject += u"no-subject" + print "Subject: %s" % subject if "in-reply-to" not in self.keys(): self["in-reply-to"] = bug["uuid"] print "In-Reply-To: <%s@%s>" % (self["in-reply-to"], DEFAULT_DOMAIN) @@ -179,8 +195,11 @@ def print_to_mbox(element): if __name__ == "__main__": import sys - - xml_unicode = sys.stdin.read() + + if len(sys.argv) == 1: # no filename given, use stdin + xml_unicode = sys.stdin.read() + else: + xml_unicode = codecs.open(sys.argv[1], "r", DEFAULT_ENCODING).read() xml_str = xml_unicode.encode("unicode_escape").replace(r"\n", "\n") elist = ElementTree.XML(xml_str) print_to_mbox(elist) |