← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~james-w/python-oops-datedir-repo/bson-compat into lp:python-oops-datedir-repo

 

James Westby has proposed merging lp:~james-w/python-oops-datedir-repo/bson-compat into lp:python-oops-datedir-repo.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~james-w/python-oops-datedir-repo/bson-compat/+merge/92560

Hi,

This allows either the bson library or the one provided by pymongo to
be used.

Thanks,

James
-- 
https://code.launchpad.net/~james-w/python-oops-datedir-repo/bson-compat/+merge/92560
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~james-w/python-oops-datedir-repo/bson-compat into lp:python-oops-datedir-repo.
=== modified file 'NEWS'
--- NEWS	2012-02-01 05:18:35 +0000
+++ NEWS	2012-02-10 19:27:34 +0000
@@ -8,6 +8,8 @@
 
 * New helper script bsondump can dump out a bson file for visual inspection.
   (Martin Pool)
+* Now supports either http://pypi.python.org/pypi/bson/ or
+  http://pypi.python.org/pypi/pymongo as the bson library. (James Westby)
 
 0.0.15
 ------

=== added file 'oops_datedir_repo/anybson.py'
--- oops_datedir_repo/anybson.py	1970-01-01 00:00:00 +0000
+++ oops_datedir_repo/anybson.py	2012-02-10 19:27:34 +0000
@@ -0,0 +1,36 @@
+# Copyright (c) 2012, Canonical Ltd
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, version 3 only.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+# GNU Lesser General Public License version 3 (see the file LICENSE).
+
+__all__ = [
+    'dumps',
+    'loads',
+    ]
+
+
+try:
+    from bson import dumps, loads
+
+    # Create the exception that won't be raised by this version of
+    # bson
+    class InvalidBSON(Exception):
+        pass
+except ImportError:
+    from bson import BSON, InvalidBSON
+
+    def dumps(obj):
+        return BSON.encode(obj)
+
+    def loads(data):
+        return BSON(data).decode(tz_aware=True)

=== modified file 'oops_datedir_repo/bsondump.py'
--- oops_datedir_repo/bsondump.py	2012-02-01 05:18:35 +0000
+++ oops_datedir_repo/bsondump.py	2012-02-10 19:27:34 +0000
@@ -22,15 +22,10 @@
 usage: bsondump FILE
 """
 
-import bson
 from pprint import pprint
 import sys
 
-
-def decode_somehow(filename):
-    """There are several bson apis: try to decode it while crying."""
-    fn = getattr(bson, 'loads', None) or getattr(bson, 'decode_all')
-    return fn(file(filename, 'rb').read())
+import anybson as bson
 
 
 def main(argv=None):
@@ -42,4 +37,4 @@
     # I'd like to use json here, but not everything serializable in bson is
     # easily representable in json - even before getting in to the weird parts,
     # oopses commonly have datetime objects. -- mbp 2011-12-20
-    pprint(decode_somehow(argv[1]))
+    pprint(bson.loads(file(argv[1]).read()))

=== modified file 'oops_datedir_repo/repository.py'
--- oops_datedir_repo/repository.py	2011-11-16 23:58:37 +0000
+++ oops_datedir_repo/repository.py	2012-02-10 19:27:34 +0000
@@ -29,20 +29,19 @@
 import os.path
 import stat
 
-import bson
 from pytz import utc
 
+import anybson as bson
 import serializer
 import serializer_bson
-import serializer_rfc822
 from uniquefileallocator import UniqueFileAllocator
 
 
 class DateDirRepo:
     """Publish oopses to a date-dir repository.
-    
+
     A date-dir repository is a directory containing:
-    
+
     * Zero or one directories called 'metadata'. If it exists this directory
       contains any housekeeping material needed (such as a metadata.conf ini
       file).
@@ -97,7 +96,7 @@
         """Write the report to disk.
 
         The report is written to a temporary file, and then renamed to its
-        final location. Programs concurrently reading from a DateDirRepo 
+        final location. Programs concurrently reading from a DateDirRepo
         should ignore files ending in .tmp.
 
         :param now: The datetime to use as the current time.  Will be
@@ -140,7 +139,7 @@
 
     def republish(self, publisher):
         """Republish the contents of the DateDirRepo to another publisher.
-        
+
         This makes it easy to treat a DateDirRepo as a backing store in message
         queue environments: if the message queue is down, flush to the
         DateDirRepo, then later pick the OOPSes up and send them to the message
@@ -255,7 +254,7 @@
         """Delete OOPS reports filed between start_time and stop_time.
 
         A report is deleted if all of the following are true:
-        
+
         * it is in a datedir covered by [start_time, stop_time] inclusive of
           the end points.
 
@@ -298,7 +297,7 @@
                         report_time = datetime.datetime.combine(
                             dirdate, midnight)
                     if (report_time >= start_time and
-                        report_time <= stop_time and 
+                        report_time <= stop_time and
                         report['id'] not in references):
                         # Unreferenced and prunable
                         os.unlink(candidate)

=== modified file 'oops_datedir_repo/serializer.py'
--- oops_datedir_repo/serializer.py	2011-11-27 19:22:20 +0000
+++ oops_datedir_repo/serializer.py	2012-02-10 19:27:34 +0000
@@ -36,6 +36,7 @@
 from StringIO import StringIO
 
 from oops_datedir_repo import (
+    anybson as bson,
     serializer_bson,
     serializer_rfc822,
     )
@@ -43,7 +44,7 @@
 
 def read(fp):
     """Deserialize an OOPS from a bson or rfc822 message.
-    
+
     The whole file is read regardless of the OOPS format.
 
     :raises IOError: If the file has no content.
@@ -55,5 +56,5 @@
         raise IOError("Empty OOPS Report")
     try:
         return serializer_bson.read(StringIO(content))
-    except KeyError:
+    except (KeyError, bson.InvalidBSON):
         return serializer_rfc822.read(StringIO(content))

=== modified file 'oops_datedir_repo/serializer_bson.py'
--- oops_datedir_repo/serializer_bson.py	2011-11-11 04:48:09 +0000
+++ oops_datedir_repo/serializer_bson.py	2012-02-10 19:27:34 +0000
@@ -49,14 +49,7 @@
 
 __metaclass__ = type
 
-import datetime
-import logging
-import rfc822
-import re
-import urllib
-
-import bson
-import iso8601
+import anybson as bson
 
 
 def read(fp):

=== modified file 'oops_datedir_repo/tests/test_repository.py'
--- oops_datedir_repo/tests/test_repository.py	2011-11-16 23:58:37 +0000
+++ oops_datedir_repo/tests/test_repository.py	2012-02-10 19:27:34 +0000
@@ -26,7 +26,6 @@
     Fixture,
     TempDir,
     )
-import bson
 from pytz import utc
 import testtools
 from testtools.matchers import (
@@ -35,6 +34,7 @@
     )
 
 from oops_datedir_repo import (
+    anybson as bson,
     DateDirRepo,
     serializer_bson,
     )

=== modified file 'oops_datedir_repo/tests/test_serializer.py'
--- oops_datedir_repo/tests/test_serializer.py	2011-11-27 19:22:20 +0000
+++ oops_datedir_repo/tests/test_serializer.py	2012-02-10 19:27:34 +0000
@@ -20,7 +20,6 @@
 import datetime
 import StringIO
 
-import bson
 from pytz import utc
 import testtools
 

=== modified file 'oops_datedir_repo/tests/test_serializer_bson.py'
--- oops_datedir_repo/tests/test_serializer_bson.py	2011-11-11 04:21:05 +0000
+++ oops_datedir_repo/tests/test_serializer_bson.py	2012-02-10 19:27:34 +0000
@@ -21,10 +21,10 @@
 import datetime
 import StringIO
 
-import bson
 from pytz import utc
 import testtools
 
+from oops_datedir_repo import anybson as bson
 from oops_datedir_repo.serializer_bson import (
     dumps,
     read,