utils.py
branchstable
changeset 3371 762a7bc3b73d
parent 3364 039d1694f36d
child 3372 26b89dfe4170
--- a/utils.py	Tue Sep 22 14:08:21 2009 +0200
+++ b/utils.py	Tue Sep 22 18:52:20 2009 +0200
@@ -19,8 +19,6 @@
 from random import randint, seed
 from calendar import monthrange
 
-import simplejson
-
 # initialize random seed from current time
 seed()
 try:
@@ -270,7 +268,6 @@
         w = self.write
         # 1/ variable declaration if any
         if self.jsvars:
-            from simplejson import dumps
             w(u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n')
             for var, value in self.jsvars:
                 w(u'%s = %s;\n' % (var, dumps(value)))
@@ -363,21 +360,27 @@
     __answer[0] = True
     return True
 
+try:
+    # may not be there if cubicwbeb-web not installed
+    from simplejson import dumps, JSONEncoder
+except ImportError:
+    pass
+else:
 
-class CubicWebJsonEncoder(simplejson.JSONEncoder):
-    """define a simplejson encoder to be able to encode yams std types"""
-    def default(self, obj):
-        if isinstance(obj, pydatetime.datetime):
-            return obj.strftime('%Y/%m/%d %H:%M:%S')
-        elif isinstance(obj, pydatetime.date):
-            return obj.strftime('%Y/%m/%d')
-        elif isinstance(obj, pydatetime.time):
-            return obj.strftime('%H:%M:%S')
-        elif isinstance(obj, decimal.Decimal):
-            return float(obj)
-        try:
-            return simplejson.JSONEncoder.default(self, obj)
-        except TypeError:
-            # we never ever want to fail because of an unknown type,
-            # just return None in those cases.
-            return None
+    class CubicWebJsonEncoder(JSONEncoder):
+        """define a simplejson encoder to be able to encode yams std types"""
+        def default(self, obj):
+            if isinstance(obj, pydatetime.datetime):
+                return obj.strftime('%Y/%m/%d %H:%M:%S')
+            elif isinstance(obj, pydatetime.date):
+                return obj.strftime('%Y/%m/%d')
+            elif isinstance(obj, pydatetime.time):
+                return obj.strftime('%H:%M:%S')
+            elif isinstance(obj, decimal.Decimal):
+                return float(obj)
+            try:
+                return JSONEncoder.default(self, obj)
+            except TypeError:
+                # we never ever want to fail because of an unknown type,
+                # just return None in those cases.
+                return None