Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Consolidate simplejson/json imports to a compat module
Browse files Browse the repository at this point in the history
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
jamesls committed Oct 22, 2012
1 parent 238458c commit 5fb324a
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 80 deletions.
6 changes: 1 addition & 5 deletions boto/cloudformation/connection.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.


try:
import simplejson as json
except:
import json

import boto import boto
from boto.cloudformation.stack import Stack, StackSummary, StackEvent from boto.cloudformation.stack import Stack, StackSummary, StackEvent
from boto.cloudformation.stack import StackResource, StackResourceSummary from boto.cloudformation.stack import StackResource, StackResourceSummary
from boto.cloudformation.template import Template from boto.cloudformation.template import Template
from boto.connection import AWSQueryConnection from boto.connection import AWSQueryConnection
from boto.regioninfo import RegionInfo from boto.regioninfo import RegionInfo
from boto.compat import json




class CloudFormationConnection(AWSQueryConnection): class CloudFormationConnection(AWSQueryConnection):
Expand Down
7 changes: 2 additions & 5 deletions boto/cloudfront/distribution.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT # 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, # 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 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.


import uuid import uuid
import base64 import base64
import time import time
try: from boto.compat import json
import simplejson as json
except ImportError:
import json
from boto.cloudfront.identity import OriginAccessIdentity from boto.cloudfront.identity import OriginAccessIdentity
from boto.cloudfront.object import Object, StreamingObject from boto.cloudfront.object import Object, StreamingObject
from boto.cloudfront.signers import ActiveTrustedSigners, TrustedSigners from boto.cloudfront.signers import ActiveTrustedSigners, TrustedSigners
Expand Down
5 changes: 1 addition & 4 deletions boto/cloudsearch/document.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
# #
try:
import simplejson as json
except ImportError:
import json


import boto.exception import boto.exception
from boto.compat import json
import requests import requests
import boto import boto


Expand Down
5 changes: 1 addition & 4 deletions boto/cloudsearch/domain.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
# #


import boto import boto
try: from boto.compat import json
import simplejson as json
except ImportError:
import json
from .optionstatus import OptionStatus from .optionstatus import OptionStatus
from .optionstatus import IndexFieldStatus from .optionstatus import IndexFieldStatus
from .optionstatus import ServicePoliciesStatus from .optionstatus import ServicePoliciesStatus
Expand Down
6 changes: 2 additions & 4 deletions boto/cloudsearch/optionstatus.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
# #


import time import time
try: from boto.compat import json
import simplejson as json
except ImportError:
import json


class OptionStatus(dict): class OptionStatus(dict):
""" """
Expand Down
28 changes: 28 additions & 0 deletions boto/compat.py
Original file line number Original file line Diff line number Diff line change
@@ -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
8 changes: 2 additions & 6 deletions boto/dynamodb/layer1.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
# #
import time


import boto import boto
from boto.connection import AWSAuthConnection from boto.connection import AWSAuthConnection
from boto.exception import DynamoDBResponseError from boto.exception import DynamoDBResponseError
from boto.provider import Provider from boto.provider import Provider
from boto.dynamodb import exceptions as dynamodb_exceptions from boto.dynamodb import exceptions as dynamodb_exceptions

from boto.compat import json
import time
try:
import simplejson as json
except ImportError:
import json


# #
# To get full debug output, uncomment the following line and set the # To get full debug output, uncomment the following line and set the
Expand Down
6 changes: 1 addition & 5 deletions boto/ec2/cloudwatch/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
This module provides an interface to the Elastic Compute Cloud (EC2) This module provides an interface to the Elastic Compute Cloud (EC2)
CloudWatch service from AWS. CloudWatch service from AWS.
""" """
try: from boto.compat import json
import simplejson as json
except ImportError:
import json

from boto.connection import AWSQueryConnection from boto.connection import AWSQueryConnection
from boto.ec2.cloudwatch.metric import Metric from boto.ec2.cloudwatch.metric import Metric
from boto.ec2.cloudwatch.alarm import MetricAlarm, MetricAlarms, AlarmHistoryItem from boto.ec2.cloudwatch.alarm import MetricAlarm, MetricAlarms, AlarmHistoryItem
Expand Down
6 changes: 1 addition & 5 deletions boto/ec2/cloudwatch/alarm.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
from boto.resultset import ResultSet from boto.resultset import ResultSet
from boto.ec2.cloudwatch.listelement import ListElement from boto.ec2.cloudwatch.listelement import ListElement
from boto.ec2.cloudwatch.dimension import Dimension from boto.ec2.cloudwatch.dimension import Dimension

from boto.compat import json
try:
import simplejson as json
except ImportError:
import json




class MetricAlarms(list): class MetricAlarms(list):
Expand Down
6 changes: 1 addition & 5 deletions boto/iam/connection.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # 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 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
try:
import json
except ImportError:
import simplejson as json

import boto import boto
import boto.jsonresponse import boto.jsonresponse
from boto.compat import json
from boto.resultset import ResultSet from boto.resultset import ResultSet
from boto.iam.summarymap import SummaryMap from boto.iam.summarymap import SummaryMap
from boto.connection import AWSQueryConnection from boto.connection import AWSQueryConnection
Expand Down
14 changes: 3 additions & 11 deletions boto/pyami/config.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT # 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, # 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 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
Expand Down Expand Up @@ -196,11 +196,7 @@ def dump_safe(self, fp=None):
fp.write('%s = %s\n' % (option, self.get(section, option))) fp.write('%s = %s\n' % (option, self.get(section, option)))


def dump_to_sdb(self, domain_name, item_name): def dump_to_sdb(self, domain_name, item_name):
try: from boto.compat import json
import simplejson as json
except ImportError:
import json

sdb = boto.connect_sdb() sdb = boto.connect_sdb()
domain = sdb.lookup(domain_name) domain = sdb.lookup(domain_name)
if not domain: if not domain:
Expand All @@ -215,11 +211,7 @@ def dump_to_sdb(self, domain_name, item_name):
item.save() item.save()


def load_from_sdb(self, domain_name, item_name): def load_from_sdb(self, domain_name, item_name):
try: from boto.compat import json
import json
except ImportError:
import simplejson as json

sdb = boto.connect_sdb() sdb = boto.connect_sdb()
domain = sdb.lookup(domain_name) domain = sdb.lookup(domain_name)
item = domain.get_item(item_name) item = domain.get_item(item_name)
Expand Down
8 changes: 3 additions & 5 deletions boto/sns/connection.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.


import uuid

from boto.connection import AWSQueryConnection from boto.connection import AWSQueryConnection
from boto.regioninfo import RegionInfo from boto.regioninfo import RegionInfo
from boto.compat import json
import boto import boto
import uuid
try:
import simplejson as json
except ImportError:
import json




class SNSConnection(AWSQueryConnection): class SNSConnection(AWSQueryConnection):
Expand Down
10 changes: 4 additions & 6 deletions boto/sqs/jsonmessage.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT # 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, # 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 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.
import base64


from boto.sqs.message import MHMessage from boto.sqs.message import MHMessage
from boto.exception import SQSDecodeError from boto.exception import SQSDecodeError
import base64 from boto.compat import json
try:
import simplejson as json
except ImportError:
import json


class JSONMessage(MHMessage): class JSONMessage(MHMessage):
""" """
Expand Down
9 changes: 4 additions & 5 deletions boto/sts/credentials.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE. # IN THE SOFTWARE.


import boto.utils
import os import os
import datetime import datetime
try:
import simplejson as json import boto.utils
except ImportError: from boto.compat import json
import json


class Credentials(object): class Credentials(object):
""" """
Expand Down
9 changes: 3 additions & 6 deletions boto/swf/layer1.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@
# IN THE SOFTWARE. # IN THE SOFTWARE.
# #


import time

import boto import boto
from boto.connection import AWSAuthConnection from boto.connection import AWSAuthConnection
from boto.provider import Provider from boto.provider import Provider
from boto.exception import SWFResponseError from boto.exception import SWFResponseError
from boto.swf import exceptions as swf_exceptions from boto.swf import exceptions as swf_exceptions

from boto.compat import json
import time
try:
import simplejson as json
except ImportError:
import json


# #
# To get full debug output, uncomment the following line and set the # To get full debug output, uncomment the following line and set the
Expand Down
5 changes: 1 addition & 4 deletions boto/utils.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@
import md5 import md5
_hashfn = md5.md5 _hashfn = md5.md5


try: from boto.compat import json
import simplejson as json
except:
import json


# List of Query String Arguments of Interest # List of Query String Arguments of Interest
qsa_of_interest = ['acl', 'cors', 'defaultObjectAcl', 'location', 'logging', qsa_of_interest = ['acl', 'cors', 'defaultObjectAcl', 'location', 'logging',
Expand Down

0 comments on commit 5fb324a

Please sign in to comment.