Make dependency on netaddr optional

https://review.openstack.org/#/c/38023/ pointed out that
the dependency on netaddr is unnecessary and can be avoided.
Use try_import to avoid a hard dependency on netaddr.

Change-Id: I8b246f20f9001379fcb5ca3a84aabee3847cb945
This commit is contained in:
Dirk Mueller
2013-08-04 23:32:21 +02:00
parent 5f4b35cdc1
commit ded9bd6d7c

View File

@@ -41,11 +41,12 @@ import json
import types
import xmlrpclib
import netaddr
import six
from openstack.common import importutils
from openstack.common import timeutils
netaddr = importutils.try_import("netaddr")
_nasty_type_tests = [inspect.ismodule, inspect.isclass, inspect.ismethod,
inspect.isfunction, inspect.isgeneratorfunction,
@@ -138,7 +139,7 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
# Likely an instance of something. Watch for cycles.
# Ignore class member vars.
return recursive(value.__dict__, level=level + 1)
elif isinstance(value, netaddr.IPAddress):
elif netaddr and isinstance(value, netaddr.IPAddress):
return six.text_type(value)
else:
if any(test(value) for test in _nasty_type_tests):