# -*- coding: utf-8 -*-

# Copyright (c) 2013-2015 CoNWeT Lab., Universidad Politécnica de Madrid

# This file is part of Wirecloud.

# Wirecloud is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# Wirecloud is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with Wirecloud.  If not, see <http://www.gnu.org/licenses/>.

import sys

from django.utils.encoding import force_text
from django.utils.functional import Promise
from json import JSONEncoder


class LazyEncoder(JSONEncoder):
    def default(self, o):
        if isinstance(o, Promise):
            return force_text(o)
        else:
            return super(LazyEncoder, self).default(o)


if sys.version_info >= (2, 7):

    class LazyEncoderXHTML(LazyEncoder):

        def encode(self, o):
            chunks = self.iterencode(o, True)
            return ''.join(chunks)

        def iterencode(self, o, _one_shot=False):
            chunks = super(LazyEncoderXHTML, self).iterencode(o, _one_shot)
            for chunk in chunks:
                chunk = chunk.replace('&', '\\u0026')
                chunk = chunk.replace('<', '\\u003c')
                chunk = chunk.replace('>', '\\u003e')
                yield chunk
else:

    class LazyEncoderXHTML(LazyEncoder):

        def encode(self, o):
            chunks = self.iterencode(o)
            return ''.join(chunks)

        def iterencode(self, o):
            chunks = super(LazyEncoderXHTML, self).iterencode(o)
            for chunk in chunks:
                chunk = chunk.replace('&', '\\u0026')
                chunk = chunk.replace('<', '\\u003c')
                chunk = chunk.replace('>', '\\u003e')
                yield chunk

@@ -1105,44 +1105,8 @@
 )%0A%0A%0A
-if sys.version_info %3E= (2, 7):%0A%0A    
 clas
@@ -1131,36 +1131,32 @@
 (LazyEncoder):%0A%0A
-    
     def encode(s
@@ -1156,36 +1156,32 @@
 ncode(self, o):%0A
-    
         chunks =
@@ -1206,36 +1206,32 @@
 , True)%0A        
-    
 return ''.join(c
@@ -1230,36 +1230,32 @@
 '.join(chunks)%0A%0A
-    
     def iterenco
@@ -1284,36 +1284,32 @@
 False):%0A        
-    
 chunks = super(L
@@ -1356,36 +1356,32 @@
 e_shot)%0A        
-    
 for chunk in chu
@@ -1389,36 +1389,32 @@
 ks:%0A            
- 
-   
 chunk = chunk.re
@@ -1427,36 +1427,32 @@
 '&', '%5C%5Cu0026')%0A
-    
             chun
@@ -1489,36 +1489,32 @@
 c')%0A            
- 
-   
 chunk = chunk.re
@@ -1539,487 +1539,8 @@
 e')%0A
-                yield chunk%0Aelse:%0A%0A    class LazyEncoderXHTML(LazyEncoder):%0A%0A        def encode(self, o):%0A            chunks = self.iterencode(o)%0A            return ''.join(chunks)%0A%0A        def iterencode(self, o):%0A            chunks = super(LazyEncoderXHTML, self).iterencode(o)%0A            for chunk in chunks:%0A                chunk = chunk.replace('&', '%5C%5Cu0026')%0A                chunk = chunk.replace('%3C', '%5C%5Cu003c')%0A                chunk = chunk.replace('%3E', '%5C%5Cu003e')%0A    
     

Remove python 2.6 code as we don't support it anymore