# Path: isomorphic/client/template_client.py
# class TemplateClient():
#     def render_template(self, file_path, context=None, request_data=None):
#         data = {
#             'template': file_path,
#             'request': request_data,
#             'context': clean_context(context)
#         }
#         message = json.dumps(data, default=_make_none).encode()
# 
#         sock = self.connect()
#         sock.send(message)
#         response = self.receive(sock)
# 
#         if not response:
#             raise JsTemplateException('Empty template response')
# 
#         status = response[0]
# 
#         if status != '0':
#             raise JsTemplateException(response[1:])
# 
#         return response[1:]
# 
#     def connect(self):
#         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
#         sock.connect(server_address)
#         return sock
# 
#     def receive(self, sock):
#         message = []
#         while True:
#             data = sock.recv(BUFFER_SIZE)
#             if not data:
#                 break
#             message.append(data.decode())
#         return ''.join(message)
# 
# Path: isomorphic/template/engine.py
# class JsEngine(Engine):
#     def __init__(self, dirs=None, app_dirs=False,
#                  allowed_include_roots=None, context_processors=None,
#                  debug=False, loaders=None, string_if_invalid='',
#                  file_charset='utf-8'):
# 
#         # Set the default loader to the JS loader
#         if loaders is None:
#             loaders = ['isomorphic.template.loaders.JsLoader']
#             if app_dirs:
#                 loaders += ['django.template.loaders.app_directories.Loader']
#                 app_dirs = False
# 
#         super(JsEngine, self).__init__(
#             dirs=dirs,
#             app_dirs=app_dirs,
#             allowed_include_roots=allowed_include_roots, context_processors=context_processors,
#             debug=debug, loaders=loaders, string_if_invalid=string_if_invalid,
#             file_charset=file_charset
#         )
# 
#     def get_template(self, template_name, dirs=_dirs_undefined):
#         if dirs is _dirs_undefined:
#             dirs = None
# 
#         template_path, origin = self.find_template(template_name, dirs)
#         return template_path


from django.middleware.csrf import get_token
from django.template.backends.base import BaseEngine
from django.template.engine import _dirs_undefined
from ..client.template_client import TemplateClient
from ..template.engine import JsEngine



class JsTemplates(BaseEngine):
    def __init__(self, params):
        params = params.copy()
        options = params.pop('OPTIONS').copy()
        super(JsTemplates, self).__init__(params)
        self.engine = JsEngine(self.dirs, self.app_dirs, **options)

    def get_template(self, template_name, dirs=_dirs_undefined):
        return JsTemplate(self.engine.get_template(template_name))


class JsTemplate(object):
    def __init__(self, template_path):
        self.template_path = template_path

    def render(self, context=None, request=None):
        context = context.copy()
        request_data = {}

        if request is not None:
            context['csrf_token'] = get_token(request)


# Path: isomorphic/client/template_client.py
# class TemplateClient():
#     def render_template(self, file_path, context=None, request_data=None):
#         data = {
#             'template': file_path,
#             'request': request_data,
#             'context': clean_context(context)
#         }
#         message = json.dumps(data, default=_make_none).encode()
# 
#         sock = self.connect()
#         sock.send(message)
#         response = self.receive(sock)
# 
#         if not response:
#             raise JsTemplateException('Empty template response')
# 
#         status = response[0]
# 
#         if status != '0':
#             raise JsTemplateException(response[1:])
# 
#         return response[1:]
# 
#     def connect(self):
#         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
#         sock.connect(server_address)
#         return sock
# 
#     def receive(self, sock):
#         message = []
#         while True:
#             data = sock.recv(BUFFER_SIZE)
#             if not data:
#                 break
#             message.append(data.decode())
#         return ''.join(message)
# 
# Path: isomorphic/template/engine.py
# class JsEngine(Engine):
#     def __init__(self, dirs=None, app_dirs=False,
#                  allowed_include_roots=None, context_processors=None,
#                  debug=False, loaders=None, string_if_invalid='',
#                  file_charset='utf-8'):
# 
#         # Set the default loader to the JS loader
#         if loaders is None:
#             loaders = ['isomorphic.template.loaders.JsLoader']
#             if app_dirs:
#                 loaders += ['django.template.loaders.app_directories.Loader']
#                 app_dirs = False
# 
#         super(JsEngine, self).__init__(
#             dirs=dirs,
#             app_dirs=app_dirs,
#             allowed_include_roots=allowed_include_roots, context_processors=context_processors,
#             debug=debug, loaders=loaders, string_if_invalid=string_if_invalid,
#             file_charset=file_charset
#         )
# 
#     def get_template(self, template_name, dirs=_dirs_undefined):
#         if dirs is _dirs_undefined:
#             dirs = None
# 
#         template_path, origin = self.find_template(template_name, dirs)
#         return template_path


# Path: isomorphic/template/backend.py
from django.middleware.csrf import get_token
from django.template.backends.base import BaseEngine
from django.template.engine import _dirs_undefined
from ..client.template_client import TemplateClient
from ..template.engine import JsEngine




class JsTemplates(BaseEngine):
    def __init__(self, params):
        params = params.copy()
        options = params.pop('OPTIONS').copy()
        super(JsTemplates, self).__init__(params)
        self.engine = JsEngine(self.dirs, self.app_dirs, **options)

    def get_template(self, template_name, dirs=_dirs_undefined):
        return JsTemplate(self.engine.get_template(template_name))


class JsTemplate(object):
    def __init__(self, template_path):
        self.template_path = template_path

    def render(self, context=None, request=None):
        context = context.copy()
        request_data = {}

        if request is not None:
            context['csrf_token'] = get_token(request)


            request_data['path'] = request.path