From: Michal Nazarewicz <[email protected]>

---
 gmail-notmuch.py |   29 +++++++++--------------------
 1 files changed, 9 insertions(+), 20 deletions(-)

On Fri, Dec 07 2012, Jason A. Donenfeld wrote:
> I wrote a script that imports emails and tags from gmail. It's
> resumable and appears to be working reasonably well. I could use some
> experienced eyes looking at it, as my first exposure to notmuch was
> just a few hours ago.

Didn't really use it, but I still have some suggested code changes: ;)

diff --git a/gmail-notmuch.py b/gmail-notmuch.py
index 04a60dc..ef7fa85 100755
--- a/gmail-notmuch.py
+++ b/gmail-notmuch.py
@@ -43,7 +43,7 @@ def main():
        (options, args) = parser.parse_args()
        if options.username == None or options.password == None:
                parser.error("Username and password are required.")
-       if not options.username.lower().endswith("@gmail.com") and not 
options.username.lower().endswith("@googlemail.com"):
+       if options.username.find("@") == -1:
                options.username += "@gmail.com"
        if len(args) == 0:
                parser.error("Maildir location is required.")
@@ -60,7 +60,7 @@ def main():
                print("Nothing to do!")
                logout(imap)
                sys.exit(0)
-       
+
        download_new_messages(imap, new_messages, destination_dir)

        logout(imap)
@@ -84,9 +84,8 @@ def discover_new_messages(imap, old_messages):
        for response in data:
                imap_seq = response[0:response.find(" ")]
                gmail_id = response[response.rfind(" ") + 1:len(response) - 1]
-               if gmail_id in old_messages:
-                       continue
-               new_messages.append((gmail_id, imap_seq))
+               if gmail_id not in old_messages:
+                       new_messages.append((gmail_id, imap_seq))
        return new_messages

 def download_new_messages(imap, messages, destination):
@@ -112,7 +111,7 @@ def download_new_messages(imap, messages, destination):
                typ, data = imap.fetch(str(imap_seq), "(FLAGS X-GM-LABELS)")
                if typ != "OK":
                        sys.exit("Failed to download labels gmail-%d/imap-%d" % 
(gmail_id, imap_seq))
-               
+
                labels = label_parser.search(data[0]).groups()
                labels = filter_labels(shlex.split(labels[0], False, True) + 
labels[1].split(" "))

@@ -149,22 +148,12 @@ def filter_labels(labels):
                        "\\Important":  None, # I realize this is 
controversial, but I hate the priority inbox.
                        "Junk":         "spam",
                        "NonJunk":      None }
-       ret = set()
-       for label in labels:
-               if label in translation:
-                       if translation[label] is None:
-                               continue
-                       ret.add(translation[label])
-               else:
-                       ret.add(label)
-       if "!read!" in ret:
-               ret.remove("!read!")
-       else:
+       ret = set(translation.get(label, label) for label in labels)
+       if not ret.pop("!read!", None):
                ret.add("unread")
-       if "" in ret:
-               ret.remove("")
+       ret.pop(None, None)
+       ret.pop("", None)
        return ret
-                       

 def logout(imap):
        imap.close()
-- 
1.7.7.3

Reply via email to