Re: [PATCH] emacs: move async json parser to its own function
authorMark Walters <markwalters1009@gmail.com>
Mon, 30 Jul 2012 20:35:30 +0000 (21:35 +0100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:48:40 +0000 (09:48 -0800)
78/b072319319a789db2a448867d30c0cfad62918 [new file with mode: 0644]

diff --git a/78/b072319319a789db2a448867d30c0cfad62918 b/78/b072319319a789db2a448867d30c0cfad62918
new file mode 100644 (file)
index 0000000..6233a77
--- /dev/null
@@ -0,0 +1,234 @@
+Return-Path: <m.walters@qmul.ac.uk>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id DFCF5431FAF\r
+       for <notmuch@notmuchmail.org>; Mon, 30 Jul 2012 13:35:41 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.098\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
+       tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+       NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id eu4rvh0TM589 for <notmuch@notmuchmail.org>;\r
+       Mon, 30 Jul 2012 13:35:37 -0700 (PDT)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 83CC2431FAE\r
+       for <notmuch@notmuchmail.org>; Mon, 30 Jul 2012 13:35:37 -0700 (PDT)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+       by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1Svwge-0001si-9F; Mon, 30 Jul 2012 21:35:32 +0100\r
+Received: from 94-192-233-223.zone6.bethere.co.uk ([94.192.233.223]\r
+       helo=localhost)\r
+       by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1Svwgd-00022y-R5; Mon, 30 Jul 2012 21:35:32 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Austin Clements <amdragon@MIT.EDU>\r
+Subject: Re: [PATCH] emacs: move async json parser to its own function\r
+In-Reply-To: <20120730013506.GF8502@mit.edu>\r
+References: <87k3xo85tv.fsf@qmul.ac.uk> <20120730013506.GF8502@mit.edu>\r
+User-Agent: Notmuch/0.13.2+96~g634443c (http://notmuchmail.org) Emacs/23.4.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Mon, 30 Jul 2012 21:35:30 +0100\r
+Message-ID: <87obmxyokt.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Sender-Host-Address: 94.192.233.223\r
+X-QM-SPAM-Info: Sender has good ham record.  :)\r
+X-QM-Body-MD5: 2a8679cc1c65562ecb10fffbf0b0b313 (of first 20000 bytes)\r
+X-SpamAssassin-Score: -1.8\r
+X-SpamAssassin-SpamBar: -\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+       determine if it is\r
+       spam. We require at least 5.0 points to mark a message as spam.\r
+       This message scored -1.8 points.\r
+       Summary of the scoring: \r
+       * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,\r
+       *      medium trust\r
+       *      [138.37.6.40 listed in list.dnswl.org]\r
+       * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+       provider *      (markwalters1009[at]gmail.com)\r
+       * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay\r
+       *      domain\r
+       *  0.5 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\r
+Cc: notmuch@notmuchmail.org\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 30 Jul 2012 20:35:42 -0000\r
+\r
+On Mon, 30 Jul 2012, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> This seems like a good idea, but as a generic interface, this seems\r
+> suboptimal.  In particular, it seems odd that a parsing function would\r
+> have to know about a process and require the caller to set up various\r
+> process properties and buffer-local variables.  What about something\r
+> dedicated to incrementally parsing lists, like the async parser but\r
+> more specialized?  Something along the lines of,\r
+>\r
+> (defun notmuch-json-parse-partial-list (result-function error-function \r
+>                                      &optional buffer)\r
+>   "Parse a partial JSON list from BUFFER (or the current buffer).\r
+>\r
+> This function consumes a JSON list from BUFFER, applying\r
+> RESULT-FUNCTION to each complete value in the list.  It operates\r
+> incrementally and should be called whenever the buffer has been\r
+> extended with additional data.\r
+>\r
+> If there is a syntax error, this will attempt to resynchronize with\r
+> the input and will apply ERROR-FUNCTION to any input that was\r
+> skipped.\r
+>\r
+> This calls RESULT-FUNCTION and ERROR-FUNCTION with the same current\r
+> buffer as this function is called with (that is, this function does\r
+> not visibly switch to BUFFER)."\r
+>   ...)\r
+>\r
+> This could use buffer-local variables for tracking the async parser\r
+> state as well its own state.  It could even set these up automatically\r
+> when called for the first time in a buffer without them set, making it\r
+> very DWIM.  It would clearly require a little more help from the\r
+> caller for process management than your patch does (and a little less\r
+> for parser setup), but I think the genericity would be worth it.\r
+\r
+This all seems good: I will send a draft patch in a reply to this\r
+email. The one change I have made is to make the function called with\r
+current-buffer the parse-buffer and give the results-buffer as an\r
+argument. This seems more natural to me as the caller has probably just\r
+added data to the parse-buffer, and it slightly simplifies the function.\r
+\r
+The other change from the current state is that the parser and state\r
+variable are buffer local to the parse-buffer rather than the\r
+results-buffer.\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+\r
+>\r
+> Quoth Mark Walters on Jul 28 at 12:48 pm:\r
+>> \r
+>> We separate out the json parser into its own function. \r
+>> ---\r
+>> \r
+>> Hi\r
+>> \r
+>> Notmuch pick uses the new asynchronous json parser and the code to do so\r
+>> is almost identical to that for the search mode. Thus separate out the\r
+>> parsing in search mode into a more general function that can easily be\r
+>> used by both pick and search.\r
+>> \r
+>> This saves nearly 50 lines of duplicated code in notmuch-pick.el.\r
+>> \r
+>> The function notmuch-json-async-parse should probably be move in\r
+>> notmuch-lib but that can be a follow on patch.\r
+>> \r
+>> Best wishes\r
+>> \r
+>> Mark\r
+>> \r
+>>  emacs/notmuch.el |   46 ++++++++++++++++++++++++++++++++++++----------\r
+>>  1 files changed, 36 insertions(+), 10 deletions(-)\r
+>> \r
+>> diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+>> index fd1836f..ee01028 100644\r
+>> --- a/emacs/notmuch.el\r
+>> +++ b/emacs/notmuch.el\r
+>> @@ -816,7 +816,32 @@ non-authors is found, assume that all of the authors match."\r
+>>    "Incremental JSON parser for the search process filter.")\r
+>>  \r
+>>  (defun notmuch-search-process-filter (proc string)\r
+>> -  "Process and filter the output of \"notmuch search\""\r
+>> +  "Process and filter the output of  \"notmuch search\" using the asynchronous parser."\r
+>> +  (setq notmuch-search-process-state\r
+>> +   (notmuch-json-async-parse proc\r
+>> +                             string\r
+>> +                             notmuch-search-process-state\r
+>> +                             notmuch-search-json-parser\r
+>> +                             'notmuch-search-show-result\r
+>> +                             'notmuch-search-show-error)))\r
+>> +\r
+>> +(defun notmuch-json-async-parse (proc string process-state parser result-function error-function)\r
+>> +  "Process and filter the output using the asynchronous parser.\r
+>> +\r
+>> +This function steps into the first level of JSON nesting and then\r
+>> +applies RESULT-FUNCTION to each complete JSON object as it comes\r
+>> +in.\r
+>> +\r
+>> +PROC is the process: it should have a results buffer as\r
+>> +process-buffer and a 'parse-buf for the incoming json.\r
+>> +PROCESS-STATE the current state of filter process\r
+>> +STRING the incoming data\r
+>> +PARSER the parser\r
+>> +RESULT-FUNCTION a function to call on complete pieces of json\r
+>> +ERROR-FUNCTION the function to call on errors\r
+>> +\r
+>> +The function returns the new PROCESS-STATE"\r
+>> +\r
+>>    (let ((results-buf (process-buffer proc))\r
+>>     (parse-buf (process-get proc 'parse-buf))\r
+>>     (inhibit-read-only t)\r
+>> @@ -831,28 +856,28 @@ non-authors is found, assume that all of the authors match."\r
+>>        (with-current-buffer results-buf\r
+>>     (while (not done)\r
+>>       (condition-case nil\r
+>> -         (case notmuch-search-process-state\r
+>> +         (case process-state\r
+>>             ((begin)\r
+>>              ;; Enter the results list\r
+>>              (if (eq (notmuch-json-begin-compound\r
+>> -                     notmuch-search-json-parser) 'retry)\r
+>> +                     parser) 'retry)\r
+>>                  (setq done t)\r
+>> -              (setq notmuch-search-process-state 'result)))\r
+>> +              (setq process-state 'result)))\r
+>>             ((result)\r
+>>              ;; Parse a result\r
+>> -            (let ((result (notmuch-json-read notmuch-search-json-parser)))\r
+>> +            (let ((result (notmuch-json-read parser)))\r
+>>                (case result\r
+>>                  ((retry) (setq done t))\r
+>> -                ((end) (setq notmuch-search-process-state 'end))\r
+>> -                (otherwise (notmuch-search-show-result result)))))\r
+>> +                ((end) (setq process-state 'end))\r
+>> +                (otherwise (funcall result-function result)))))\r
+>>             ((end)\r
+>>              ;; Any trailing data is unexpected\r
+>> -            (notmuch-json-eof notmuch-search-json-parser)\r
+>> +            (notmuch-json-eof parser)\r
+>>              (setq done t)))\r
+>>         (json-error\r
+>>          ;; Do our best to resynchronize and ensure forward\r
+>>          ;; progress\r
+>> -        (notmuch-search-show-error\r
+>> +        (funcall error-function\r
+>>           "%s"\r
+>>           (with-current-buffer parse-buf\r
+>>             (let ((bad (buffer-substring (line-beginning-position)\r
+>> @@ -861,7 +886,8 @@ non-authors is found, assume that all of the authors match."\r
+>>               bad))))))\r
+>>     ;; Clear out what we've parsed\r
+>>     (with-current-buffer parse-buf\r
+>> -     (delete-region (point-min) (point)))))))\r
+>> +     (delete-region (point-min) (point))))\r
+>> +      process-state)))\r
+>>  \r
+>>  (defun notmuch-search-tag-all (&optional tag-changes)\r
+>>    "Add/remove tags from all messages in current search buffer.\r