Exemplo n.º 1
0
Arquivo: api.py Projeto: ktibi/rally
def serialize_data(data):
    if data is None:
        return None
    if isinstance(data, (
            six.integer_types,
            six.string_types,
            six.text_type,
            dt.date,
            dt.time,
            float,
    )):
        return data
    if isinstance(data, dict):
        return collections.OrderedDict(
            (k, serialize_data(v)) for k, v in data.items())
    if isinstance(data, (list, tuple)):
        return [serialize_data(i) for i in data]
    if hasattr(data, "_as_dict"):
        # NOTE(andreykurilin): it is an instance of the Model. It support a
        #   method `_as_dict`, which should transform an object into dict
        #   (quite logical as from the method name), BUT it does some extra
        #   work - tries to load properties which were marked to not be loaded
        #   in particular request and fails since the session object is not
        #   present. That is why the code bellow makes a custom transformation.
        result = {}
        for key in data.__dict__:
            if not key.startswith("_"):
                result[key] = serialize_data(getattr(data, key))
        return result

    raise exceptions.DBException("Can not serialize %s" % data)
Exemplo n.º 2
0
def serialize_data(data):
    if data is None:
        return None
    if isinstance(data, (
            six.integer_types,
            six.string_types,
            six.text_type,
            dt.date,
            dt.time,
            float,
    )):
        return data
    if isinstance(data, dict):
        return collections.OrderedDict(
            (k, serialize_data(v)) for k, v in data.items())
    if isinstance(data, (list, tuple)):
        return [serialize_data(i) for i in data]

    if isinstance(data, models.RallyBase):
        result = {}
        for key in data.__dict__:
            if not key.startswith("_"):
                result[key] = serialize_data(getattr(data, key))
        return result

    raise exceptions.DBException("Can not serialize %s" % data)
Exemplo n.º 3
0
Arquivo: api.py Projeto: ktibi/rally
    def model_query(self, model, session=None):
        """The helper method to create query.

        :param model: The instance of
                      :class:`rally.common.db.sqlalchemy.models.RallyBase` to
                      request it.
        :param session: Reuse the session object or get new one if it is
                        None.
        :returns: The query object.
        :raises Exception: when the model is not a sublcass of
                 :class:`rally.common.db.sqlalchemy.models.RallyBase`.
        """
        def issubclassof_rally_base(obj):
            return isinstance(obj, type) and issubclass(obj, models.RallyBase)

        if not issubclassof_rally_base(model):
            raise exceptions.DBException(
                "The model %s should be a subclass of RallyBase" % model)

        session = session or get_session()
        return session.query(model)