# Path: dockit/schema/common.py
# class DotPathList(list):
#     def traverse_dot_path(self, traverser):
#         if traverser.remaining_paths:
#             new_value = None
#             name = traverser.next_part
#             try:
#                 new_value = self[int(name)]
#             except ValueError:
#                 raise DotPathNotFound("Invalid index given, must be an integer")
#             except IndexError:
#                 pass
#             traverser.next(value=new_value)
#         else:
#             traverser.end(value=self)
#     
#     def set_value(self, attr, value):
#         index = int(attr)
#         if value is UnSet:
#             self.pop(index)
#         elif index == len(self):
#             self.append(value)
#         else:
#             self[index] = value
# 
# class DotPathDict(dict):
#     def traverse_dot_path(self, traverser):
#         if traverser.remaining_paths:
#             new_value = None
#             name = traverser.next_part
#             try:
#                 new_value = self[name]
#             except KeyError:
#                 pass
#             traverser.next(value=new_value)
#         else:
#             traverser.end(value=self)
#     
#     def set_value(self, attr, value):
#         if value is UnSet:
#             del self[attr]
#         else:
#             self[attr] = value


from django.contrib.contenttypes.models import ContentType
from django.core.serializers.json import DjangoJSONEncoder
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.query import QuerySet
from django.db import models
from django.utils import simplejson
from decimal import Decimal
from dockit.schema.common import DotPathList, DotPathDict
    #CONSIDER it might be a good idea to allow registering more serializers
    return {'encoder': JSONEncoder(handlers=handlers),
            'decoder': JSONDecoder(handlers=handlers),}

class PrimitiveProcessor(object):
    def __init__(self, handlers):
        self.handlers = handlers
        self.handlers_by_key = dict([(handler.key, handler) for handler in self.handlers])
    
    def to_primitive(self, obj):
        if isinstance(obj, (QuerySet, list)):
            return map(self.to_primitive, obj)
        for handler in self.handlers:
            if isinstance(obj, handler.instancetype):
                return handler.encode(obj)
        if isinstance(obj, dict):
            for key, value in obj.items():
                obj[self.to_primitive(key)] = self.to_primitive(value)
        elif isinstance(obj, list):
            return map(self.to_primtive, obj)
        return obj
    
    def to_python(self, obj):
        if isinstance(obj, dict):
            if '__type__' in obj:
                return self.handlers_by_key[obj['__type__']].decode(obj)
            for key, value in obj.items():
                obj[self.to_python(key)] = self.to_python(value)
            obj = DotPathDict(obj)
        elif isinstance(obj, list):

# Path: dockit/schema/common.py
# class DotPathList(list):
#     def traverse_dot_path(self, traverser):
#         if traverser.remaining_paths:
#             new_value = None
#             name = traverser.next_part
#             try:
#                 new_value = self[int(name)]
#             except ValueError:
#                 raise DotPathNotFound("Invalid index given, must be an integer")
#             except IndexError:
#                 pass
#             traverser.next(value=new_value)
#         else:
#             traverser.end(value=self)
#     
#     def set_value(self, attr, value):
#         index = int(attr)
#         if value is UnSet:
#             self.pop(index)
#         elif index == len(self):
#             self.append(value)
#         else:
#             self[index] = value
# 
# class DotPathDict(dict):
#     def traverse_dot_path(self, traverser):
#         if traverser.remaining_paths:
#             new_value = None
#             name = traverser.next_part
#             try:
#                 new_value = self[name]
#             except KeyError:
#                 pass
#             traverser.next(value=new_value)
#         else:
#             traverser.end(value=self)
#     
#     def set_value(self, attr, value):
#         if value is UnSet:
#             del self[attr]
#         else:
#             self[attr] = value


# Path: dockit/schema/serializer.py
from django.contrib.contenttypes.models import ContentType
from django.core.serializers.json import DjangoJSONEncoder
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.query import QuerySet
from django.db import models
from django.utils import simplejson
from decimal import Decimal
from dockit.schema.common import DotPathList, DotPathDict

    #CONSIDER it might be a good idea to allow registering more serializers
    return {'encoder': JSONEncoder(handlers=handlers),
            'decoder': JSONDecoder(handlers=handlers),}

class PrimitiveProcessor(object):
    def __init__(self, handlers):
        self.handlers = handlers
        self.handlers_by_key = dict([(handler.key, handler) for handler in self.handlers])
    
    def to_primitive(self, obj):
        if isinstance(obj, (QuerySet, list)):
            return map(self.to_primitive, obj)
        for handler in self.handlers:
            if isinstance(obj, handler.instancetype):
                return handler.encode(obj)
        if isinstance(obj, dict):
            for key, value in obj.items():
                obj[self.to_primitive(key)] = self.to_primitive(value)
        elif isinstance(obj, list):
            return map(self.to_primtive, obj)
        return obj
    
    def to_python(self, obj):
        if isinstance(obj, dict):
            if '__type__' in obj:
                return self.handlers_by_key[obj['__type__']].decode(obj)
            for key, value in obj.items():
                obj[self.to_python(key)] = self.to_python(value)
            obj = DotPathDict(obj)
        elif isinstance(obj, list):

            return DotPathList(map(self.to_python, obj))