[PATCH 1/2] test: date_relative in notmuch search json output

2011-07-23 Thread pazz
expect the date_relative field for thread entries
in notmuch search's json output
---
 test/json |2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/test/json b/test/json
index 5a2544c..d54bf25 100755
--- a/test/json
+++ b/test/json
@@ -12,6 +12,7 @@ add_message "[subject]=\"json-search-subject\"" 
"[date]=\"Sat, 01 Jan 2000 12:00
 output=$(notmuch search --format=json "json-search-message" | 
notmuch_search_sanitize)
 test_expect_equal "$output" "[{\"thread\": \"XXX\",
 \"timestamp\": 946728000,
+\"date_relative\": \"2000-01-01\",
 \"matched\": 1,
 \"total\": 1,
 \"authors\": \"Notmuch Test Suite\",
@@ -28,6 +29,7 @@ add_message "[subject]=\"json-search-utf8-body-s?bj?ct\"" 
"[date]=\"Sat, 01 Ja
 output=$(notmuch search --format=json "js?n-search-m?ssage" | 
notmuch_search_sanitize)
 test_expect_equal "$output" "[{\"thread\": \"XXX\",
 \"timestamp\": 946728000,
+\"date_relative\": \"2000-01-01\",
 \"matched\": 1,
 \"total\": 1,
 \"authors\": \"Notmuch Test Suite\",
-- 
1.7.4.1



[PATCH 2/2] json: date_relative for threads

2011-07-23 Thread pazz
include the date_relative field in the
json formated output of notmuch search
---
 notmuch-search.c |2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index faccaf7..b1adc03 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -176,12 +176,14 @@ format_thread_json (const void *ctx,

 printf ("\"thread\": %s,\n"
"\"timestamp\": %ld,\n"
+   "\"date_relative\": \"%s\",\n"
"\"matched\": %d,\n"
"\"total\": %d,\n"
"\"authors\": %s,\n"
"\"subject\": %s,\n",
json_quote_str (ctx_quote, thread_id),
date,
+   notmuch_time_relative_date (ctx, date),
matched,
total,
json_quote_str (ctx_quote, authors),
-- 
1.7.4.1



[PATCH] interpret Xapian errors from sdterr as exceptions

2011-07-23 Thread pazz
This introduces globals.RaiseStderrErrors, a ContextManager
that raises error messages printed by libnotmuch to stderr
as NotmuchError(STATUS.XAPIAN_EXCEPTION, message=err).
---
 bindings/python/notmuch/database.py |5 +
 bindings/python/notmuch/globals.py  |   24 
 2 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/bindings/python/notmuch/database.py 
b/bindings/python/notmuch/database.py
index 874087e..443980b 100644
--- a/bindings/python/notmuch/database.py
+++ b/bindings/python/notmuch/database.py
@@ -18,8 +18,10 @@ Copyright 2010 Sebastian Spaeth '
 """

 import os
+
 from ctypes import c_int, c_char_p, c_void_p, c_uint, c_long, byref
 from notmuch.globals import nmlib, STATUS, NotmuchError, Enum
+from notmuch.globals import RaiseStderrErrors
 from notmuch.thread import Threads
 from notmuch.message import Messages, Message
 from notmuch.tag import Tags
@@ -540,6 +542,9 @@ class Query(object):
 if query_p is None:
 NotmuchError(STATUS.NULL_POINTER)
 self._query = query_p
+# ensure Xapian errors from stderr get raised if query syntax is bad
+with RaiseStderrErrors():
+Query._count_messages(self._query)

 def set_sort(self, sort):
 """Set the sort order future results will be delivered in
diff --git a/bindings/python/notmuch/globals.py 
b/bindings/python/notmuch/globals.py
index 77f2905..5e527ca 100644
--- a/bindings/python/notmuch/globals.py
+++ b/bindings/python/notmuch/globals.py
@@ -17,6 +17,10 @@ along with notmuch.  If not, see 
.
 Copyright 2010 Sebastian Spaeth '
 """

+import tempfile
+import sys
+import os
+
 from ctypes import CDLL, c_char_p, c_int
 from ctypes.util import find_library

@@ -98,3 +102,23 @@ class NotmuchError(Exception):
 return self.args[0]
 else:
 return STATUS.status2str(self.args[1])
+
+
+class RaiseStderrErrors:
+def __enter__(self):
+sys.stderr.flush()
+(self.errfd, fn) = tempfile.mkstemp()
+self.ferr = os.fdopen(self.errfd, 'r')
+os.unlink(fn)
+self.oldstderr = os.dup(sys.stderr.fileno())
+os.dup2(self.errfd, sys.stderr.fileno())
+
+def __exit__(self, *args):
+sys.stderr.flush()
+os.dup2(self.oldstderr, sys.stderr.fileno())
+os.close(self.oldstderr)
+os.lseek(self.errfd, 0, 0)
+err = self.ferr.read()
+if err:
+raise NotmuchError(STATUS.XAPIAN_EXCEPTION, message=err)
+self.ferr.close()
-- 
1.7.4.1



[PATCH 1/2] test: date_relative in notmuch search json output

2011-07-23 Thread pazz
expect the date_relative field for thread entries
in notmuch search's json output
---
 test/json |2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/test/json b/test/json
index 5a2544c..d54bf25 100755
--- a/test/json
+++ b/test/json
@@ -12,6 +12,7 @@ add_message "[subject]=\"json-search-subject\"" 
"[date]=\"Sat, 01 Jan 2000 12:00
 output=$(notmuch search --format=json "json-search-message" | 
notmuch_search_sanitize)
 test_expect_equal "$output" "[{\"thread\": \"XXX\",
 \"timestamp\": 946728000,
+\"date_relative\": \"2000-01-01\",
 \"matched\": 1,
 \"total\": 1,
 \"authors\": \"Notmuch Test Suite\",
@@ -28,6 +29,7 @@ add_message "[subject]=\"json-search-utf8-body-sübjéct\"" 
"[date]=\"Sat, 01 Ja
 output=$(notmuch search --format=json "jsön-search-méssage" | 
notmuch_search_sanitize)
 test_expect_equal "$output" "[{\"thread\": \"XXX\",
 \"timestamp\": 946728000,
+\"date_relative\": \"2000-01-01\",
 \"matched\": 1,
 \"total\": 1,
 \"authors\": \"Notmuch Test Suite\",
-- 
1.7.4.1

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


[PATCH 2/2] json: date_relative for threads

2011-07-23 Thread pazz
include the date_relative field in the
json formated output of notmuch search
---
 notmuch-search.c |2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/notmuch-search.c b/notmuch-search.c
index faccaf7..b1adc03 100644
--- a/notmuch-search.c
+++ b/notmuch-search.c
@@ -176,12 +176,14 @@ format_thread_json (const void *ctx,
 
 printf ("\"thread\": %s,\n"
"\"timestamp\": %ld,\n"
+   "\"date_relative\": \"%s\",\n"
"\"matched\": %d,\n"
"\"total\": %d,\n"
"\"authors\": %s,\n"
"\"subject\": %s,\n",
json_quote_str (ctx_quote, thread_id),
date,
+   notmuch_time_relative_date (ctx, date),
matched,
total,
json_quote_str (ctx_quote, authors),
-- 
1.7.4.1

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


[PATCH] interpret Xapian errors from sdterr as exceptions

2011-07-23 Thread pazz
This introduces globals.RaiseStderrErrors, a ContextManager
that raises error messages printed by libnotmuch to stderr
as NotmuchError(STATUS.XAPIAN_EXCEPTION, message=err).
---
 bindings/python/notmuch/database.py |5 +
 bindings/python/notmuch/globals.py  |   24 
 2 files changed, 29 insertions(+), 0 deletions(-)

diff --git a/bindings/python/notmuch/database.py 
b/bindings/python/notmuch/database.py
index 874087e..443980b 100644
--- a/bindings/python/notmuch/database.py
+++ b/bindings/python/notmuch/database.py
@@ -18,8 +18,10 @@ Copyright 2010 Sebastian Spaeth '
 """
 
 import os
+
 from ctypes import c_int, c_char_p, c_void_p, c_uint, c_long, byref
 from notmuch.globals import nmlib, STATUS, NotmuchError, Enum
+from notmuch.globals import RaiseStderrErrors
 from notmuch.thread import Threads
 from notmuch.message import Messages, Message
 from notmuch.tag import Tags
@@ -540,6 +542,9 @@ class Query(object):
 if query_p is None:
 NotmuchError(STATUS.NULL_POINTER)
 self._query = query_p
+# ensure Xapian errors from stderr get raised if query syntax is bad
+with RaiseStderrErrors():
+Query._count_messages(self._query)
 
 def set_sort(self, sort):
 """Set the sort order future results will be delivered in
diff --git a/bindings/python/notmuch/globals.py 
b/bindings/python/notmuch/globals.py
index 77f2905..5e527ca 100644
--- a/bindings/python/notmuch/globals.py
+++ b/bindings/python/notmuch/globals.py
@@ -17,6 +17,10 @@ along with notmuch.  If not, see 
.
 Copyright 2010 Sebastian Spaeth '
 """
 
+import tempfile
+import sys
+import os
+
 from ctypes import CDLL, c_char_p, c_int
 from ctypes.util import find_library
 
@@ -98,3 +102,23 @@ class NotmuchError(Exception):
 return self.args[0]
 else:
 return STATUS.status2str(self.args[1])
+
+
+class RaiseStderrErrors:
+def __enter__(self):
+sys.stderr.flush()
+(self.errfd, fn) = tempfile.mkstemp()
+self.ferr = os.fdopen(self.errfd, 'r')
+os.unlink(fn)
+self.oldstderr = os.dup(sys.stderr.fileno())
+os.dup2(self.errfd, sys.stderr.fileno())
+
+def __exit__(self, *args):
+sys.stderr.flush()
+os.dup2(self.oldstderr, sys.stderr.fileno())
+os.close(self.oldstderr)
+os.lseek(self.errfd, 0, 0)
+err = self.ferr.read()
+if err:
+raise NotmuchError(STATUS.XAPIAN_EXCEPTION, message=err)
+self.ferr.close()
-- 
1.7.4.1

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch