Lazy import for ipaddress package

This commit is contained in:
Konsta Vesterinen
2013-07-23 10:28:51 +03:00
parent e663bb9cd5
commit 165d9f4cfd
2 changed files with 14 additions and 2 deletions

View File

@@ -24,7 +24,8 @@ class ColorType(types.TypeDecorator):
# Bail if colour is not found. # Bail if colour is not found.
if colour is None: if colour is None:
raise ImproperlyConfigured( raise ImproperlyConfigured(
"'colour' is required to use 'ColorType'") "'colour' package is required to use 'ColorType'"
)
super(ColorType, self).__init__(*args, **kwargs) super(ColorType, self).__init__(*args, **kwargs)
self.impl = types.Unicode(max_length) self.impl = types.Unicode(max_length)

View File

@@ -1,6 +1,12 @@
import six import six
import ipaddress
ipaddress = None
try:
import ipaddress
except:
pass
from sqlalchemy import types from sqlalchemy import types
from sqlalchemy_utils import ImproperlyConfigured
class IPAddressType(types.TypeDecorator): class IPAddressType(types.TypeDecorator):
@@ -11,6 +17,11 @@ class IPAddressType(types.TypeDecorator):
impl = types.Unicode(50) impl = types.Unicode(50)
def __init__(self, max_length=50, *args, **kwargs): def __init__(self, max_length=50, *args, **kwargs):
if not ipaddress:
raise ImproperlyConfigured(
"'ipaddress' package is required to use 'IPAddressType'"
)
super(IPAddressType, self).__init__(*args, **kwargs) super(IPAddressType, self).__init__(*args, **kwargs)
self.impl = types.Unicode(max_length) self.impl = types.Unicode(max_length)