Ejemplo n.º 1
0
def model_to_json_response(model, exclude=("location", "model", "guid", "resource_uri", "id")):
    """
    Converts a single model into a json encodes string that can be used as a
    response body for the G-Node REST API.

    :param model: The model to convert
    :type model: Model
    :param exclude: Excluded field names
    :type exclude: tuple

    :returns: A json encoded string representing the model.
    :rtype: str
    """
    result = {}
    for field_name in model:
        if exclude is not None and field_name not in exclude:
            field = model.get_field(field_name)
            value = model[field_name]

            if field.type_info == "data":
                if value is None:
                    result[field_name] = None
                else:
                    result[field_name] = value["data"]
                    result[field_name + '__unit'] = value["units"]

            elif field.type_info == "datafile":
                if value is not None:  # TODO clean this up
                    #if model.model in (Model.EPOCHARRAY, Model.EPOCHARRAY) and field_name == "labels":
                    #    new_value = helper.id_from_location(value["data"])
                    #else:
                    #    new_value = {"units": value["units"], "data": helper.id_from_location(value["data"])}
                    result[field_name + '__unit'] = value["units"]

            elif field.is_child:
                if model.model == Model.RECORDINGCHANNEL and field_name == "recordingchannelgroups":
                    field_name = field.name_mapping or field.type_info + "_set"
                    new_value = []
                    if value is not None:
                        for i in value:
                            new_value.append(helper.id_from_location(i))
                    result[field_name] = new_value

            elif field.is_parent:
                field_name = field.name_mapping or field_name
                if value is None:
                    result[field_name] = None
                else:
                    result[field_name] = helper.id_from_location(value)

            else:
                field_name = field.name_mapping or field_name
                result[field_name] = value

    json_response = json.dumps(result)

    return json_response
Ejemplo n.º 2
0
 def get_remote_objs(self, model_name):
     """ this assumes that 'select' method already works. in case of any
     bugs in 'select' operation this method will also fail """
     assets = self.remote_assets[model_name]
     filters = {
         'limit': 10,
         'id__in': ",".join([id_from_location(x.location) for x in assets])
     }
     return self.session.select(model_name, filters)
Ejemplo n.º 3
0
    def set_file(self, location, data, temporary=False):
        """
        Write file data to the cache.

        :param location: An url or path that ends with a unique identifier or the identifier itself.
        :type location: str
        :param data: A byte-string that will be stored in a file.
        :type data: str
        """
        ident = helper.id_from_location(location)
        f_name = self.file_cache_path(ident, temporary)

        self._secure_write(f_name, data, False)
Ejemplo n.º 4
0
    def get_array(self, location, temporary=False):
        """
        Read array data from an hdf5 file in the cache.

        :param location: The locations of all entities as path or URL.
        :type location: str

        :returns: The raw file data.
        :rtype: numpy.ndarray|list
        """
        ident = helper.id_from_location(location)
        path = self.__cache.file_cache_path(ident, temporary)

        if os.path.isfile(path):
            data = hdfio.read_array_data(path)
            return data
        else:
            return None
Ejemplo n.º 5
0
    def delete_file(self, location, temporary=False):
        """
        Delete file data form the cache.

        :param location: An url or path that ends with a unique identifier or the identifier itself.
        :type location: str

        :returns: True if the file was deleted, False if not found.
        :rtype: bool
        """
        ident = helper.id_from_location(location)
        f_name = self.file_cache_path(ident, temporary)

        if os.path.isfile(f_name):
            os.remove(f_name)
            return True
        else:
            return False
Ejemplo n.º 6
0
    def get_file(self, location, temporary=False):
        """
        Get file data form the cache.

        :param location: An url or path that ends with a unique identifier or the identifier itself.
        :type location: str

        :returns: The cached file data or None if not found.
        :rtype: str
        """
        ident = helper.id_from_location(location)
        f_name = self.file_cache_path(ident, temporary)

        if os.path.isfile(f_name):
            data = self._secure_read(f_name, serialize=False)
            return data
        else:
            return None
Ejemplo n.º 7
0
    def set(self, location, data, temporary=False):
        """
        Caches some object under a certain identifier. The identifier is extracted from the given
        location.

        Example locations:
            "http://host/foo/identifier?param=val"
            "/foo/identifier"
            "identifier"

        :param location: An url or path that ends with a unique identifier or the identifier itself.
        :type location: str
        :param data: Some object that can be serialized by pickle.
        :type data: object
        """
        ident = helper.id_from_location(location)
        f_name = self.obj_cache_path(ident, temporary)

        all_data = self._secure_read(f_name, {})
        all_data[ident] = data
        self._secure_write(f_name, all_data)
Ejemplo n.º 8
0
    def delete(self, location, temporary=False):
        """
        Delete an object form the cache.

        :param location: An url or path that ends with a unique identifier or the identifier itself.
        :type location: str

        :returns: True if the object was deleted, False if not found.
        :rtype: bool
        """
        ident = helper.id_from_location(location)
        f_name = self.obj_cache_path(ident, temporary)

        all_data = self._secure_read(f_name, {})

        if ident in all_data:
            del all_data[ident]
            self._secure_write(f_name, all_data)
            return True
        else:
            return False
Ejemplo n.º 9
0
    def set_array(self, array_data, location=None, temporary=False):
        """
        Save array data in a cached HDF5 file.

        :param array_data: The raw data to store.
        :type array_data: numpy.ndarray|list
        :param location: The location of the file.
        :type location: str

        :returns: The url to the uploaded file.
        :rtype: str
        """
        if location is None:
            ident = helper.random_base32()
            location = "/api/v1/temp/datafile/" + ident + "/"
        else:
            ident = helper.id_from_location(location)

        path = self.__cache.file_cache_path(ident, temporary)
        hdfio.store_array_data(path, array_data)

        return location
Ejemplo n.º 10
0
    def get(self, location, temporary=False):
        """
        Get an object form the cache.

        :param location: An url or path that ends with a unique identifier or the identifier itself.
        :type location: str

        :returns: The cached object or None if not found.
        :rtype: object
        """
        result = None
        ident = helper.id_from_location(location)

        if len(ident) > 0:
            f_name = self.obj_cache_path(ident, temporary)

            all_data = self._secure_read(f_name, {})

            if ident in all_data:
                result = all_data[ident]

        return result