diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2024-03-30 22:16:37 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2024-03-30 22:16:37 +0100 |
commit | 6669d427f87ec62a86a680a542d2f87f2d65cc80 (patch) | |
tree | 80e08e1830eb6283fffa2d3db600ad00090d1970 /misc | |
parent | bc53c496220b283773f65762d4283c8f1e480131 (diff) | |
download | bugseverywhere-6669d427f87ec62a86a680a542d2f87f2d65cc80.tar.gz |
Used PyCharms inspectors.
Diffstat (limited to 'misc')
-rwxr-xr-x | misc/xml/be-mail-to-xml | 99 | ||||
-rwxr-xr-x | misc/xml/be-xml-to-mbox | 123 |
2 files changed, 121 insertions, 101 deletions
diff --git a/misc/xml/be-mail-to-xml b/misc/xml/be-mail-to-xml index 6155baa..6071c74 100755 --- a/misc/xml/be-mail-to-xml +++ b/misc/xml/be-mail-to-xml @@ -27,19 +27,21 @@ followed by a blank line. import base64 import email.utils -from libbe.util.encoding import get_output_encoding -from libbe.util.utility import time_to_str -import mailbox # the mailbox people really want an on-disk copy +import mailbox # the mailbox people really want an on-disk copy import optparse -from time import asctime, gmtime, mktime import types +from time import mktime from xml.sax.saxutils import escape -BREAK = u'--' # signature separator +from libbe.util.encoding import get_output_encoding +from libbe.util.utility import time_to_str + +BREAK = u'--' # signature separator DEFAULT_ENCODING = get_output_encoding() KNOWN_IDS = [] + def normalize_email_address(address): """ Standardize whitespace, etc. @@ -49,6 +51,7 @@ def normalize_email_address(address): return None return addr + def normalize_RFC_2822_date(date): """ Some email clients write non-RFC 2822-compliant date tags like: @@ -57,60 +60,59 @@ def normalize_RFC_2822_date(date): to deal with such inconsistencies. """ time_tuple = email.utils.parsedate(date) - assert time_tuple != None, \ + assert time_tuple is not None, \ 'unparsable date: "%s"' % date return time_to_str(mktime(time_tuple)) + def strip_footer(body): body_lines = body.splitlines() - for i,line in enumerate(body_lines): + for i, line in enumerate(body_lines): if line.startswith(BREAK): break - i += 1 # increment past the current valid line. + i += 1 # increment past the current valid line. return u'\n'.join(body_lines[:i]).strip() + def comment_message_to_xml(message, fields=None): - if fields == None: + if fields is None: fields = {} - new_fields = {} - new_fields[u'alt-id'] = message[u'message-id'] - new_fields[u'in-reply-to'] = message[u'in-reply-to'] - new_fields[u'author'] = normalize_email_address(message[u'from']) - new_fields[u'date'] = message[u'date'] - if new_fields[u'date'] != None: + new_fields = {u'alt-id': message[u'message-id'], u'in-reply-to': message[u'in-reply-to'], + u'author': normalize_email_address(message[u'from']), u'date': message[u'date']} + if new_fields[u'date'] is not None: new_fields[u'date'] = normalize_RFC_2822_date(new_fields[u'date']) new_fields[u'content-type'] = message.get_content_type() - for k,v in new_fields.items(): - if v != None and type(v) != types.UnicodeType: - fields[k] = unicode(v, encoding=DEFAULT_ENCODING) - elif v == None and k in fields: + for k, v in new_fields.items(): + if v is not None and type(v) != types.UnicodeType: + fields[k] = str(v, encoding=DEFAULT_ENCODING) + elif v is None and k in fields: new_fields[k] = fields[k] - for k,v in fields.items(): + for k, v in fields.items(): if k not in new_fields: new_fields.k = fields[k] fields = new_fields - if fields[u'in-reply-to'] == None: - if message[u'references'] != None: + if fields[u'in-reply-to'] is None: + if message[u'references'] is not None: refs = message[u'references'].split() - for ref in refs: # search for a known reference id. + for ref in refs: # search for a known reference id. if ref in KNOWN_IDS: fields[u'in-reply-to'] = ref break - if fields[u'in-reply-to'] == None and len(refs) > 0: - fields[u'in-reply-to'] = refs[0] # default to the first - else: # check for mutliple in-reply-to references. + if fields[u'in-reply-to'] is None and len(refs) > 0: + fields[u'in-reply-to'] = refs[0] # default to the first + else: # check for mutliple in-reply-to references. refs = fields[u'in-reply-to'].split() found_ref = False - for ref in refs: # search for a known reference id. + for ref in refs: # search for a known reference id. if ref in KNOWN_IDS: fields[u'in-reply-to'] = ref found_ref = True break if found_ref == False and len(refs) > 0: - fields[u'in-reply-to'] = refs[0] # default to the first + fields[u'in-reply-to'] = refs[0] # default to the first - if fields[u'alt-id'] != None: + if fields[u'alt-id'] is not None: KNOWN_IDS.append(fields[u'alt-id']) if message.is_multipart(): @@ -123,51 +125,56 @@ def comment_message_to_xml(message, fields=None): continue fields[u'author'] = from_str fields[u'date'] = date - if len(ret) > 0: # we've added one part already - fields.pop(u'alt-id') # don't pass alt-id to other parts - fields[u'in-reply-to'] = alt_id # others respond to first + if len(ret) > 0: # we've added one part already + fields.pop(u'alt-id') # don't pass alt-id to other parts + fields[u'in-reply-to'] = alt_id # others respond to first ret.append(comment_message_to_xml(m, fields)) return u'\n'.join(ret) charset = message.get_content_charset(DEFAULT_ENCODING).lower() - #assert charset == DEFAULT_ENCODING.lower(), \ + # assert charset == DEFAULT_ENCODING.lower(), \ # u"Unknown charset: %s" % charset - if message[u'content-transfer-encoding'] == None: + if message[u'content-transfer-encoding'] is None: encoding = DEFAULT_ENCODING else: encoding = message[u'content-transfer-encoding'].lower() - body = message.get_payload(decode=True) # attempt to decode - assert body != None, "Unable to decode?" + body = message.get_payload(decode=True) # attempt to decode + assert body is not None, "Unable to decode?" if fields[u'content-type'].startswith(u"text/"): - body = strip_footer(unicode(body, encoding=charset)) + body = strip_footer(str(body, encoding=charset)) else: - body = base64.encode(body) + body = base64.encodebytes(body) fields[u'body'] = body lines = [u"<comment>"] - for tag,body in fields.items(): - if body != None: + for tag, body in fields.items(): + if body is not None: ebody = escape(body) lines.append(u" <%s>%s</%s>" % (tag, ebody, tag)) lines.append(u"</comment>") return u'\n'.join(lines) + def main(argv): parser = optparse.OptionParser(usage='%prog [options] mailbox') formats = ['mbox', 'Maildir', 'MH', 'Babyl', 'MMDF'] parser.add_option('-f', '--format', type='choice', dest='format', help="Select the mailbox format from %s. See the mailbox module's documention for descriptions of these formats." \ - % ', '.join(formats), + % ', '.join(formats), default='mbox', choices=formats) - options,args = parser.parse_args(argv) + options, args = parser.parse_args(argv) mailbox_file = args[1] reader = getattr(mailbox, options.format) mb = reader(mailbox_file, factory=None) - print u'<?xml version="1.0" encoding="%s" ?>' % DEFAULT_ENCODING - print u"<be-xml>" + print + u'<?xml version="1.0" encoding="%s" ?>' % DEFAULT_ENCODING + print + u"<be-xml>" for message in mb: - print comment_message_to_xml(message) - print u"</be-xml>" + print + comment_message_to_xml(message) + print + u"</be-xml>" if __name__ == "__main__": diff --git a/misc/xml/be-xml-to-mbox b/misc/xml/be-xml-to-mbox index 0151792..48454f9 100755 --- a/misc/xml/be-xml-to-mbox +++ b/misc/xml/be-xml-to-mbox @@ -27,23 +27,20 @@ Messages begin with a a From_ line, followed by RFC 822 email, followed by a blank line. """ -#from mailbox import mbox, Message # the mailbox people really want an on-disk copy +# from mailbox import mbox, Message # the mailbox people really want an on-disk copy import email.utils from libbe.util.encoding import get_output_encoding from libbe.util.utility import str_to_time as rfc2822_to_gmtime_integer from time import asctime, gmtime import types -try: # import core module, Python >= 2.5 - from xml.etree import ElementTree -except ImportError: # look for non-core module - from elementtree import ElementTree +from xml.etree import ElementTree from xml.sax.saxutils import unescape - DEFAULT_DOMAIN = "invalid.com" DEFAULT_EMAIL = "dummy@" + DEFAULT_DOMAIN DEFAULT_ENCODING = get_output_encoding() + def rfc2822_to_asctime(rfc2822_string): """Convert an RFC 2822-fomatted string into a asctime string. >>> rfc2822_to_asctime("Thu, 01 Jan 1970 00:00:00 +0000") @@ -53,27 +50,32 @@ def rfc2822_to_asctime(rfc2822_string): return asctime(gmtime(0)) return asctime(gmtime(rfc2822_to_gmtime_integer(rfc2822_string))) -class LimitedAttrDict (dict): + +class LimitedAttrDict(dict): """ Dict with error checking, to avoid invalid bug/comment fields. """ - _attrs = [] # override with list of valid attribute names + _attrs = [] # override with list of valid attribute names + def __init__(self, **kwargs): dict.__init__(self) - for key,value in kwargs.items(): + for key, value in kwargs.items(): self[key] = value + def __setitem__(self, key, item): self._validate_key(key) dict.__setitem__(self, key, item) + def _validate_key(self, key): if key in self._attrs: return elif type(key) not in types.StringTypes: - raise TypeError, "Invalid attribute type %s for '%s'" % (type(key), key) + raise TypeError("Invalid attribute type %s for '%s'" % (type(key), key)) else: - raise ValueError, "Invalid attribute name '%s'" % key + raise ValueError("Invalid attribute name '%s'" % key) + -class Bug (LimitedAttrDict): +class Bug(LimitedAttrDict): _attrs = [u"uuid", u"short-name", u"severity", @@ -85,32 +87,34 @@ class Bug (LimitedAttrDict): u"summary", u"comments", u"extra-strings"] + def print_to_mbox(self): if "creator" in self: # otherwise, probably a `be show` uuid-only bug to avoid # root comments. - name,addr = email.utils.parseaddr(self["creator"]) - print "From %s %s" % (addr, rfc2822_to_asctime(self["created"])) - print "Message-id: <%s@%s>" % (self["uuid"], DEFAULT_DOMAIN) - print "Date: %s" % self["created"] - print "From: %s" % self["creator"] - print "Content-Type: %s; charset=%s" \ - % ("text/plain", DEFAULT_ENCODING) - print "Content-Transfer-Encoding: 8bit" - print "Subject: %s: %s" % (self["short-name"], self["summary"]) + name, addr = email.utils.parseaddr(self["creator"]) + print("From %s %s" % (addr, rfc2822_to_asctime(self["created"]))) + print("Message-id: <%s@%s>" % (self["uuid"], DEFAULT_DOMAIN)) + print("Date: %s" % self["created"]) + print("From: %s" % self["creator"]) + print("Content-Type: %s; charset=%s" + % ("text/plain", DEFAULT_ENCODING)) + print("Content-Transfer-Encoding: 8bit") + print("Subject: %s: %s" % (self["short-name"], self["summary"])) if "extra-strings" in self: for estr in self["extra-strings"]: - print "X-Extra-String: %s" % estr - print "" - print self["summary"] - print "" + print("X-Extra-String: %s" % estr) + print() + print(self["summary"]) + print() if "comments" in self: for comment in self["comments"]: - comment.print_to_mbox(self) + comment.print_to_mbox(self) + def init_from_etree(self, element): assert element.tag == "bug", element.tag for field in element.getchildren(): - text = unescape(unicode(field.text).decode("unicode_escape").strip()) + text = unescape(bytes(field.text).decode("unicode_escape").strip()) if field.tag == "comment": comm = Comment() comm.init_from_etree(field) @@ -126,12 +130,14 @@ class Bug (LimitedAttrDict): else: self[field.tag] = text + def wrap_id(id): if "@" not in id: return "<%s@%s>" % (id, DEFAULT_DOMAIN) return id -class Comment (LimitedAttrDict): + +class Comment(LimitedAttrDict): _attrs = [u"uuid", u"alt-id", u"short-name", @@ -141,49 +147,54 @@ class Comment (LimitedAttrDict): u"content-type", u"body", u"extra-strings"] + def print_to_mbox(self, bug=None): - if bug == None: + if bug is None: bug = Bug() bug[u"uuid"] = u"no-uuid" - name,addr = email.utils.parseaddr(self["author"]) - print "From %s %s" % (addr, rfc2822_to_asctime(self["date"])) - 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" % wrap_id(id) + name, addr = email.utils.parseaddr(self["author"]) + print("From %s %s" % (addr, rfc2822_to_asctime(self["date"]))) + if "uuid" in self: + id = self["uuid"] + elif "alt-id" in self: + id = self["alt-id"] + else: + id = None + if id is not None: + print("Message-id: %s" % wrap_id(id)) if "alt-id" in self: - print "Alt-id: %s" % wrap_id(self["alt-id"]) - print "Date: %s" % self["date"] - print "From: %s" % self["author"] + print("Alt-id: %s" % wrap_id(self["alt-id"])) + print("Date: %s" % self["date"]) + print("From: %s" % self["author"]) subject = "" if "short-name" in self: - subject += self["short-name"]+u": " + subject += self["short-name"] + u": " if "summary" in bug: subject += bug["summary"] else: subject += u"no-subject" - print "Subject: %s" % subject + print("Subject: %s" % subject) if "in-reply-to" not in self.keys(): self["in-reply-to"] = bug["uuid"] - print "In-Reply-To: %s" % wrap_id(self["in-reply-to"]) + print("In-Reply-To: %s" % wrap_id(self["in-reply-to"])) if "extra-strings" in self: for estr in self["extra-strings"]: - print "X-Extra-String: %s" % estr + print("X-Extra-String: %s" % estr) if self["content-type"].startswith("text/"): - print "Content-Transfer-Encoding: 8bit" - print "Content-Type: %s; charset=%s" \ - % (self["content-type"], DEFAULT_ENCODING) + print("Content-Transfer-Encoding: 8bit") + print("Content-Type: %s; charset=%s" + % (self["content-type"], DEFAULT_ENCODING)) else: - print "Content-Transfer-Encoding: base64" - print "Content-Type: %s;" % (self["content-type"]) - print "" - print self["body"] - print "" + print("Content-Transfer-Encoding: base64") + print("Content-Type: %s;" % (self["content-type"])) + print() + print(self["body"]) + print() + def init_from_etree(self, element): assert element.tag == "comment", element.tag for field in element.getchildren(): - text = unescape(unicode(field.text).decode("unicode_escape").strip()) + text = unescape(bytes(field.text).decode("unicode_escape").strip()) if field.tag == "extra-string": if "extra-strings" in self: self["extra-strings"].append(text) @@ -191,9 +202,10 @@ class Comment (LimitedAttrDict): self["extra-strings"] = [text] else: if field.tag == "body": - text+="\n" + text += "\n" self[field.tag] = text + def print_to_mbox(element): if element.tag == "bug": b = Bug() @@ -207,14 +219,15 @@ def print_to_mbox(element): for elt in element.getchildren(): print_to_mbox(elt) + if __name__ == "__main__": import codecs import sys - + sys.stdin = codecs.getreader(DEFAULT_ENCODING)(sys.stdin) sys.stdout = codecs.getwriter(DEFAULT_ENCODING)(sys.stdout) - if len(sys.argv) == 1: # no filename given, use stdin + 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() |