# Path: pylm/parts/messages_pb2.py
# DESCRIPTOR = _descriptor.FileDescriptor(
#   name='messages.proto',
#   package='',
#   syntax='proto3',
#   serialized_pb=_b('\n\x0emessages.proto\"p\n\x0bPalmMessage\x12\x10\n\x08pipeline\x18\x01 \x01(\t\x12\x0e\n\x06\x63lient\x18\x02 \x01(\t\x12\r\n\x05stage\x18\x03 \x01(\x03\x12\x10\n\x08\x66unction\x18\x04 \x01(\t\x12\r\n\x05\x63\x61\x63he\x18\x05 \x01(\t\x12\x0f\n\x07payload\x18\x06 \x01(\x0c\x62\x06proto3')
# )
# _PALMMESSAGE = _descriptor.Descriptor(
#   name='PalmMessage',
#   full_name='PalmMessage',
#   filename=None,
#   file=DESCRIPTOR,
#   containing_type=None,
#   fields=[
#     _descriptor.FieldDescriptor(
#       name='pipeline', full_name='PalmMessage.pipeline', index=0,
#       number=1, type=9, cpp_type=9, label=1,
#       has_default_value=False, default_value=_b("").decode('utf-8'),
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#     _descriptor.FieldDescriptor(
#       name='client', full_name='PalmMessage.client', index=1,
#       number=2, type=9, cpp_type=9, label=1,
#       has_default_value=False, default_value=_b("").decode('utf-8'),
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#     _descriptor.FieldDescriptor(
#       name='stage', full_name='PalmMessage.stage', index=2,
#       number=3, type=3, cpp_type=2, label=1,
#       has_default_value=False, default_value=0,
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#     _descriptor.FieldDescriptor(
#       name='function', full_name='PalmMessage.function', index=3,
#       number=4, type=9, cpp_type=9, label=1,
#       has_default_value=False, default_value=_b("").decode('utf-8'),
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#     _descriptor.FieldDescriptor(
#       name='cache', full_name='PalmMessage.cache', index=4,
#       number=5, type=9, cpp_type=9, label=1,
#       has_default_value=False, default_value=_b("").decode('utf-8'),
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#     _descriptor.FieldDescriptor(
#       name='payload', full_name='PalmMessage.payload', index=5,
#       number=6, type=12, cpp_type=9, label=1,
#       has_default_value=False, default_value=_b(""),
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#   ],
#   extensions=[
#   ],
#   nested_types=[],
#   enum_types=[
#   ],
#   options=None,
#   is_extendable=False,
#   syntax='proto3',
#   extension_ranges=[],
#   oneofs=[
#   ],
#   serialized_start=18,
#   serialized_end=130,
# )


from wsgiref.simple_server import make_server
from collections import namedtuple
from pylm.parts.messages_pb2 import PalmMessage

class RequestHandler(object):
    def __init__(self, environ):
        self.environ = environ
        length = int(environ.get('CONTENT_LENGTH'), 0)
        self.request = Request(method=environ.get('REQUEST_METHOD'),
                               data=environ.get('wsgi.input').read(length))
        self.message = None

    def handle(self):
        if self.request.method == 'POST':
            try:
                message = PalmMessage()
                message.ParseFromString(self.request.data)

                # This exports the message information
                self.message = message
                instruction = message.function.split('.')[1]
                result = getattr(self, instruction)(message.payload)
                message.payload = result
                response_body = message.SerializeToString()
                status = '200 OK'

            except Exception as exc:
                status = '500 Internal Server Error'
                response_body = b''
        else:
            status = '405 Method not allowed'
            response_body = b''


# Path: pylm/parts/messages_pb2.py
# DESCRIPTOR = _descriptor.FileDescriptor(
#   name='messages.proto',
#   package='',
#   syntax='proto3',
#   serialized_pb=_b('\n\x0emessages.proto\"p\n\x0bPalmMessage\x12\x10\n\x08pipeline\x18\x01 \x01(\t\x12\x0e\n\x06\x63lient\x18\x02 \x01(\t\x12\r\n\x05stage\x18\x03 \x01(\x03\x12\x10\n\x08\x66unction\x18\x04 \x01(\t\x12\r\n\x05\x63\x61\x63he\x18\x05 \x01(\t\x12\x0f\n\x07payload\x18\x06 \x01(\x0c\x62\x06proto3')
# )
# _PALMMESSAGE = _descriptor.Descriptor(
#   name='PalmMessage',
#   full_name='PalmMessage',
#   filename=None,
#   file=DESCRIPTOR,
#   containing_type=None,
#   fields=[
#     _descriptor.FieldDescriptor(
#       name='pipeline', full_name='PalmMessage.pipeline', index=0,
#       number=1, type=9, cpp_type=9, label=1,
#       has_default_value=False, default_value=_b("").decode('utf-8'),
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#     _descriptor.FieldDescriptor(
#       name='client', full_name='PalmMessage.client', index=1,
#       number=2, type=9, cpp_type=9, label=1,
#       has_default_value=False, default_value=_b("").decode('utf-8'),
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#     _descriptor.FieldDescriptor(
#       name='stage', full_name='PalmMessage.stage', index=2,
#       number=3, type=3, cpp_type=2, label=1,
#       has_default_value=False, default_value=0,
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#     _descriptor.FieldDescriptor(
#       name='function', full_name='PalmMessage.function', index=3,
#       number=4, type=9, cpp_type=9, label=1,
#       has_default_value=False, default_value=_b("").decode('utf-8'),
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#     _descriptor.FieldDescriptor(
#       name='cache', full_name='PalmMessage.cache', index=4,
#       number=5, type=9, cpp_type=9, label=1,
#       has_default_value=False, default_value=_b("").decode('utf-8'),
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#     _descriptor.FieldDescriptor(
#       name='payload', full_name='PalmMessage.payload', index=5,
#       number=6, type=12, cpp_type=9, label=1,
#       has_default_value=False, default_value=_b(""),
#       message_type=None, enum_type=None, containing_type=None,
#       is_extension=False, extension_scope=None,
#       options=None),
#   ],
#   extensions=[
#   ],
#   nested_types=[],
#   enum_types=[
#   ],
#   options=None,
#   is_extendable=False,
#   syntax='proto3',
#   extension_ranges=[],
#   oneofs=[
#   ],
#   serialized_start=18,
#   serialized_end=130,
# )


# Path: pylm/remote/server.py
from wsgiref.simple_server import make_server
from collections import namedtuple
from pylm.parts.messages_pb2 import PalmMessage


class RequestHandler(object):
    def __init__(self, environ):
        self.environ = environ
        length = int(environ.get('CONTENT_LENGTH'), 0)
        self.request = Request(method=environ.get('REQUEST_METHOD'),
                               data=environ.get('wsgi.input').read(length))
        self.message = None

    def handle(self):
        if self.request.method == 'POST':
            try:
                message = PalmMessage()
                message.ParseFromString(self.request.data)

                # This exports the message information
                self.message = message
                instruction = message.function.split('.')[1]
                result = getattr(self, instruction)(message.payload)
                message.payload = result
                response_body = message.SerializeToString()
                status = '200 OK'

            except Exception as exc:
                status = '500 Internal Server Error'
                response_body = b''
        else:
            status = '405 Method not allowed'
            response_body = b''


        return status, response_body