diff options
| author | Russell Bryant <rbryant@redhat.com> | 2012-05-15 12:54:16 -0400 |
|---|---|---|
| committer | Russell Bryant <rbryant@redhat.com> | 2012-05-15 15:44:14 -0400 |
| commit | ba76b954e69de56f76f9db5cade1780bc351be67 (patch) | |
| tree | 981706789636899cb13739a9f8942d882232b72c /nova/utils.py | |
| parent | c8cafc6e9df3a389330da08cbcbf60fb6ca1b238 (diff) | |
Make use of openstack.common.jsonutils.
This patch imports jsonutils from openstack-common. It removes the
equivalent code from nova.utils and then converts the code base to use
jsonutils. The primary motivator for this change was to remove the rest
of the dependencies from nova.rpc on nova.utils.
Change-Id: If43658b9b098ed56cba018c81be268b8c3e2916a
Diffstat (limited to 'nova/utils.py')
| -rw-r--r-- | nova/utils.py | 100 |
1 files changed, 0 insertions, 100 deletions
diff --git a/nova/utils.py b/nova/utils.py index 2b495d1ff..9577ff269 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -25,8 +25,6 @@ import errno import functools import hashlib import inspect -import itertools -import json import os import pyclbr import random @@ -677,104 +675,6 @@ def utf8(value): return value -def to_primitive(value, convert_instances=False, level=0): - """Convert a complex object into primitives. - - Handy for JSON serialization. We can optionally handle instances, - but since this is a recursive function, we could have cyclical - data structures. - - To handle cyclical data structures we could track the actual objects - visited in a set, but not all objects are hashable. Instead we just - track the depth of the object inspections and don't go too deep. - - Therefore, convert_instances=True is lossy ... be aware. - - """ - nasty = [inspect.ismodule, inspect.isclass, inspect.ismethod, - inspect.isfunction, inspect.isgeneratorfunction, - inspect.isgenerator, inspect.istraceback, inspect.isframe, - inspect.iscode, inspect.isbuiltin, inspect.isroutine, - inspect.isabstract] - for test in nasty: - if test(value): - return unicode(value) - - # value of itertools.count doesn't get caught by inspects - # above and results in infinite loop when list(value) is called. - if type(value) == itertools.count: - return unicode(value) - - # FIXME(vish): Workaround for LP bug 852095. Without this workaround, - # tests that raise an exception in a mocked method that - # has a @wrap_exception with a notifier will fail. If - # we up the dependency to 0.5.4 (when it is released) we - # can remove this workaround. - if getattr(value, '__module__', None) == 'mox': - return 'mock' - - if level > 3: - return '?' - - # The try block may not be necessary after the class check above, - # but just in case ... - try: - if isinstance(value, (list, tuple)): - o = [] - for v in value: - o.append(to_primitive(v, convert_instances=convert_instances, - level=level)) - return o - elif isinstance(value, dict): - o = {} - for k, v in value.iteritems(): - o[k] = to_primitive(v, convert_instances=convert_instances, - level=level) - return o - elif isinstance(value, datetime.datetime): - return str(value) - elif hasattr(value, 'iteritems'): - return to_primitive(dict(value.iteritems()), - convert_instances=convert_instances, - level=level) - elif hasattr(value, '__iter__'): - return to_primitive(list(value), level) - elif convert_instances and hasattr(value, '__dict__'): - # Likely an instance of something. Watch for cycles. - # Ignore class member vars. - return to_primitive(value.__dict__, - convert_instances=convert_instances, - level=level + 1) - else: - return value - except TypeError, e: - # Class objects are tricky since they may define something like - # __iter__ defined but it isn't callable as list(). - return unicode(value) - - -def dumps(value): - try: - return json.dumps(value) - except TypeError: - pass - return json.dumps(to_primitive(value)) - - -def loads(s): - return json.loads(s) - - -try: - import anyjson -except ImportError: - pass -else: - anyjson._modules.append(("nova.utils", "dumps", TypeError, - "loads", ValueError)) - anyjson.force_implementation("nova.utils") - - class GreenLockFile(lockfile.FileLock): """Implementation of lockfile that allows for a lock per greenthread. |
