Skip to content
Browse files

Consolidate simplejson/json imports to a compat module

This remove duplication in modules that are checking for
simplejson, falling back to json when it doesn't exist.

I've updated the various modules that were importing
simplejson to now use this compat module.

The md5 module is another good candidate for moving
to the compat module
  • Loading branch information...
1 parent 238458c commit 5fb324a20bb9c7ac991dfb85c8ef002684acce09 @jamesls jamesls committed Oct 22, 2012
View
6 boto/cloudformation/connection.py
@@ -19,17 +19,13 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
-try:
- import simplejson as json
-except:
- import json
-
import boto
from boto.cloudformation.stack import Stack, StackSummary, StackEvent
from boto.cloudformation.stack import StackResource, StackResourceSummary
from boto.cloudformation.template import Template
from boto.connection import AWSQueryConnection
from boto.regioninfo import RegionInfo
+from boto.compat import json
class CloudFormationConnection(AWSQueryConnection):
View
7 boto/cloudfront/distribution.py
@@ -14,18 +14,15 @@
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
-# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
import uuid
import base64
import time
-try:
- import simplejson as json
-except ImportError:
- import json
+from boto.compat import json
from boto.cloudfront.identity import OriginAccessIdentity
from boto.cloudfront.object import Object, StreamingObject
from boto.cloudfront.signers import ActiveTrustedSigners, TrustedSigners
View
5 boto/cloudsearch/document.py
@@ -21,12 +21,9 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
-try:
- import simplejson as json
-except ImportError:
- import json
import boto.exception
+from boto.compat import json
import requests
import boto
View
5 boto/cloudsearch/domain.py
@@ -23,10 +23,7 @@
#
import boto
-try:
- import simplejson as json
-except ImportError:
- import json
+from boto.compat import json
from .optionstatus import OptionStatus
from .optionstatus import IndexFieldStatus
from .optionstatus import ServicePoliciesStatus
View
6 boto/cloudsearch/optionstatus.py
@@ -23,10 +23,8 @@
#
import time
-try:
- import simplejson as json
-except ImportError:
- import json
+from boto.compat import json
+
class OptionStatus(dict):
"""
View
28 boto/compat.py
@@ -0,0 +1,28 @@
+# Copyright (c) 2012 Amazon.com, Inc. or its affiliates. All Rights Reserved
+#
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish, dis-
+# tribute, sublicense, and/or sell copies of the Software, and to permit
+# persons to whom the Software is furnished to do so, subject to the fol-
+# lowing conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
+# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+# IN THE SOFTWARE.
+#
+
+# This allows boto modules to say "from boto.compat import json". This is
+# preferred so that all modules don't have to repeat this idiom.
+try:
+ import simplejson as json
+except ImportError:
+ import json
View
8 boto/dynamodb/layer1.py
@@ -20,18 +20,14 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
+import time
import boto
from boto.connection import AWSAuthConnection
from boto.exception import DynamoDBResponseError
from boto.provider import Provider
from boto.dynamodb import exceptions as dynamodb_exceptions
-
-import time
-try:
- import simplejson as json
-except ImportError:
- import json
+from boto.compat import json
#
# To get full debug output, uncomment the following line and set the
View
6 boto/ec2/cloudwatch/__init__.py
@@ -23,11 +23,7 @@
This module provides an interface to the Elastic Compute Cloud (EC2)
CloudWatch service from AWS.
"""
-try:
- import simplejson as json
-except ImportError:
- import json
-
+from boto.compat import json
from boto.connection import AWSQueryConnection
from boto.ec2.cloudwatch.metric import Metric
from boto.ec2.cloudwatch.alarm import MetricAlarm, MetricAlarms, AlarmHistoryItem
View
6 boto/ec2/cloudwatch/alarm.py
@@ -24,11 +24,7 @@
from boto.resultset import ResultSet
from boto.ec2.cloudwatch.listelement import ListElement
from boto.ec2.cloudwatch.dimension import Dimension
-
-try:
- import simplejson as json
-except ImportError:
- import json
+from boto.compat import json
class MetricAlarms(list):
View
6 boto/iam/connection.py
@@ -19,13 +19,9 @@
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
-try:
- import json
-except ImportError:
- import simplejson as json
-
import boto
import boto.jsonresponse
+from boto.compat import json
from boto.resultset import ResultSet
from boto.iam.summarymap import SummaryMap
from boto.connection import AWSQueryConnection
View
14 boto/pyami/config.py
@@ -15,7 +15,7 @@
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
-# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
@@ -196,11 +196,7 @@ def dump_safe(self, fp=None):
fp.write('%s = %s\n' % (option, self.get(section, option)))
def dump_to_sdb(self, domain_name, item_name):
- try:
- import simplejson as json
- except ImportError:
- import json
-
+ from boto.compat import json
sdb = boto.connect_sdb()
domain = sdb.lookup(domain_name)
if not domain:
@@ -215,11 +211,7 @@ def dump_to_sdb(self, domain_name, item_name):
item.save()
def load_from_sdb(self, domain_name, item_name):
- try:
- import json
- except ImportError:
- import simplejson as json
-
+ from boto.compat import json
sdb = boto.connect_sdb()
domain = sdb.lookup(domain_name)
item = domain.get_item(item_name)
View
8 boto/sns/connection.py
@@ -20,14 +20,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
+import uuid
+
from boto.connection import AWSQueryConnection
from boto.regioninfo import RegionInfo
+from boto.compat import json
import boto
-import uuid
-try:
- import simplejson as json
-except ImportError:
- import json
class SNSConnection(AWSQueryConnection):
View
10 boto/sqs/jsonmessage.py
@@ -14,18 +14,16 @@
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
-# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
+import base64
from boto.sqs.message import MHMessage
from boto.exception import SQSDecodeError
-import base64
-try:
- import simplejson as json
-except ImportError:
- import json
+from boto.compat import json
+
class JSONMessage(MHMessage):
"""
View
9 boto/sts/credentials.py
@@ -20,13 +20,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
-import boto.utils
import os
import datetime
-try:
- import simplejson as json
-except ImportError:
- import json
+
+import boto.utils
+from boto.compat import json
+
class Credentials(object):
"""
View
9 boto/swf/layer1.py
@@ -22,17 +22,14 @@
# IN THE SOFTWARE.
#
+import time
+
import boto
from boto.connection import AWSAuthConnection
from boto.provider import Provider
from boto.exception import SWFResponseError
from boto.swf import exceptions as swf_exceptions
-
-import time
-try:
- import simplejson as json
-except ImportError:
- import json
+from boto.compat import json
#
# To get full debug output, uncomment the following line and set the
View
5 boto/utils.py
@@ -73,10 +73,7 @@
import md5
_hashfn = md5.md5
-try:
- import simplejson as json
-except:
- import json
+from boto.compat import json
# List of Query String Arguments of Interest
qsa_of_interest = ['acl', 'cors', 'defaultObjectAcl', 'location', 'logging',

0 comments on commit 5fb324a

Please sign in to comment.
Something went wrong with that request. Please try again.