# Path: dbsync/models.py
# class Node(Base):
#     """
#     A node registry.
# 
#     A node is a client application installed somewhere else.
#     """
# 
#     __tablename__ = "nodes"
# 
#     node_id = Column(Integer, primary_key=True)
#     registered = Column(DateTime)
#     registry_user_id = Column(Integer)
#     secret = Column(String(128))
# 
#     def __init__(self, *args, **kwargs):
#         super(Node, self).__init__(*args, **kwargs)
# 
#     def __repr__(self):
#         return u"<Node node_id: {0}, registered: {1}, "\
#             u"registry_user_id: {2}, secret: {3}>".\
#             format(self.node_id,
#                    self.registered,
#                    self.registry_user_id,
#                    self.secret)
# 
# Path: dbsync/utils.py
# def properties_dict(sa_object):
#     """
#     Returns a dictionary of column-properties for the given SQLAlchemy
#     mapped object.
#     """
#     mapper = object_mapper(sa_object)
#     return dict((prop.key, getattr(sa_object, prop.key))
#                 for prop in mapper.iterate_properties
#                 if isinstance(prop, ColumnProperty))
# 
# def object_from_dict(class_, dict_):
#     "Returns an object from a dictionary of attributes."
#     obj = construct_bare(class_)
#     for k, v in dict_.iteritems():
#         setattr(obj, k, v)
#     return obj
# 
# Path: dbsync/messages/codecs.py
# def encode_dict(class_):
#     """
#     Returns a function that transforms a dictionary, mapping the
#     types to simpler ones, according to the given mapped class.
#     """
#     types = types_dict(class_)
#     encodings = dict((k, encode(t)) for k, t in types.iteritems())
#     return lambda dict_: dict((k, encodings[k](v))
#                               for k, v in dict_.iteritems()
#                               if k in encodings)
# 
# def decode_dict(class_):
#     """
#     Returns a function that transforms a dictionary, mapping the
#     types to richer ones, according to the given mapped class.
#     """
#     types = types_dict(class_)
#     decodings = dict((k, decode(t)) for k, t in types.iteritems())
#     return lambda dict_: dict((k, decodings[k](v))
#                               for k, v in dict_.iteritems()
#                               if k in decodings)


from dbsync.models import Node
from dbsync.utils import properties_dict, object_from_dict
from dbsync.messages.codecs import encode_dict, decode_dict
"""
Register message and related.
"""



class RegisterMessage(object):
    """A register message with node information."""

    #: The node to be registered in the client application
    node = None

    def __init__(self, raw_data=None):
        if raw_data is not None:
            self._build_from_raw(raw_data)

    def _build_from_raw(self, data):
        self.node = object_from_dict(Node, decode_dict(Node)(data['node']))

    def to_json(self):
        encoded = {}
        encoded['node'] = None
        if self.node is not None:

# Path: dbsync/models.py
# class Node(Base):
#     """
#     A node registry.
# 
#     A node is a client application installed somewhere else.
#     """
# 
#     __tablename__ = "nodes"
# 
#     node_id = Column(Integer, primary_key=True)
#     registered = Column(DateTime)
#     registry_user_id = Column(Integer)
#     secret = Column(String(128))
# 
#     def __init__(self, *args, **kwargs):
#         super(Node, self).__init__(*args, **kwargs)
# 
#     def __repr__(self):
#         return u"<Node node_id: {0}, registered: {1}, "\
#             u"registry_user_id: {2}, secret: {3}>".\
#             format(self.node_id,
#                    self.registered,
#                    self.registry_user_id,
#                    self.secret)
# 
# Path: dbsync/utils.py
# def properties_dict(sa_object):
#     """
#     Returns a dictionary of column-properties for the given SQLAlchemy
#     mapped object.
#     """
#     mapper = object_mapper(sa_object)
#     return dict((prop.key, getattr(sa_object, prop.key))
#                 for prop in mapper.iterate_properties
#                 if isinstance(prop, ColumnProperty))
# 
# def object_from_dict(class_, dict_):
#     "Returns an object from a dictionary of attributes."
#     obj = construct_bare(class_)
#     for k, v in dict_.iteritems():
#         setattr(obj, k, v)
#     return obj
# 
# Path: dbsync/messages/codecs.py
# def encode_dict(class_):
#     """
#     Returns a function that transforms a dictionary, mapping the
#     types to simpler ones, according to the given mapped class.
#     """
#     types = types_dict(class_)
#     encodings = dict((k, encode(t)) for k, t in types.iteritems())
#     return lambda dict_: dict((k, encodings[k](v))
#                               for k, v in dict_.iteritems()
#                               if k in encodings)
# 
# def decode_dict(class_):
#     """
#     Returns a function that transforms a dictionary, mapping the
#     types to richer ones, according to the given mapped class.
#     """
#     types = types_dict(class_)
#     decodings = dict((k, decode(t)) for k, t in types.iteritems())
#     return lambda dict_: dict((k, decodings[k](v))
#                               for k, v in dict_.iteritems()
#                               if k in decodings)


# Path: dbsync/messages/register.py
from dbsync.models import Node
from dbsync.utils import properties_dict, object_from_dict
from dbsync.messages.codecs import encode_dict, decode_dict

"""
Register message and related.
"""



class RegisterMessage(object):
    """A register message with node information."""

    #: The node to be registered in the client application
    node = None

    def __init__(self, raw_data=None):
        if raw_data is not None:
            self._build_from_raw(raw_data)

    def _build_from_raw(self, data):
        self.node = object_from_dict(Node, decode_dict(Node)(data['node']))

    def to_json(self):
        encoded = {}
        encoded['node'] = None
        if self.node is not None:

            encoded['node'] = encode_dict(Node)(properties_dict(self.node))