(self, name, data, serializer=None)
Save data to data directory.

        .. versionadded:: 1.8

        If ``data`` is ``None``, the datastore will be deleted.

        Note that the datastore does NOT support mutliple threads.

        :param name: name of datastore
        :param data: object(s) to store. **Note:** some serializers
            can only handled certain types of data.
        :param serializer: name of serializer to use. If no serializer
            is specified, the default will be used. See
            :class:`SerializerManager` for more information.
        :returns: data in datastore or ``None``
Save data to data directory.
Save data to data directory .
def store_data(self, name, data, serializer=None):
        """Save data to data directory.

        .. versionadded:: 1.8

        If ``data`` is ``None``, the datastore will be deleted.

        Note that the datastore does NOT support mutliple threads.

        :param name: name of datastore
        :param data: object(s) to store. **Note:** some serializers
            can only handled certain types of data.
        :param serializer: name of serializer to use. If no serializer
            is specified, the default will be used. See
            :class:`SerializerManager` for more information.
        :returns: data in datastore or ``None``

        """
        # Ensure deletion is not interrupted by SIGTERM
        @uninterruptible
        def delete_paths(paths):
            """Clear one or more data stores"""
            for path in paths:
                if os.path.exists(path):
                    os.unlink(path)
                    self.logger.debug('deleted data file: %s', path)

        serializer_name = serializer or self.data_serializer

        # In order for `stored_data()` to be able to load data stored with
        # an arbitrary serializer, yet still have meaningful file extensions,
        # the format (i.e. extension) is saved to an accompanying file
        metadata_path = self.datafile('.{0}.alfred-workflow'.format(name))
        filename = '{0}.{1}'.format(name, serializer_name)
        data_path = self.datafile(filename)

        if data_path == self.settings_path:
            raise ValueError(
                'Cannot save data to' +
                '`{0}` with format `{1}`. '.format(name, serializer_name) +
                "This would overwrite Alfred-Workflow's settings file.")

        serializer = manager.serializer(serializer_name)

        if serializer is None:
            raise ValueError(
                'Invalid serializer `{0}`. Register your serializer with '
                '`manager.register()` first.'.format(serializer_name))

        if data is None:  # Delete cached data
            delete_paths((metadata_path, data_path))
            return

        # Ensure write is not interrupted by SIGTERM
        @uninterruptible
        def _store():
            # Save file extension
            with atomic_writer(metadata_path, 'wb') as file_obj:
                file_obj.write(serializer_name)

            with atomic_writer(data_path, 'wb') as file_obj:
                serializer.dump(data, file_obj)

        _store()

        self.logger.debug('saved data: %s', data_path)
def store_data ( self , name , data , serializer = None ) : # Ensure deletion is not interrupted by SIGTERM @ uninterruptible def delete_paths ( paths ) : """Clear one or more data stores""" for path in paths : if os . path . exists ( path ) : os . unlink ( path ) self . logger . debug ( 'deleted data file: %s' , path ) serializer_name = serializer or self . data_serializer # In order for `stored_data()` to be able to load data stored with # an arbitrary serializer, yet still have meaningful file extensions, # the format (i.e. extension) is saved to an accompanying file metadata_path = self . datafile ( '.{0}.alfred-workflow' . format ( name ) ) filename = '{0}.{1}' . format ( name , serializer_name ) data_path = self . datafile ( filename ) if data_path == self . settings_path : raise ValueError ( 'Cannot save data to' + '`{0}` with format `{1}`. ' . format ( name , serializer_name ) + "This would overwrite Alfred-Workflow's settings file." ) serializer = manager . serializer ( serializer_name ) if serializer is None : raise ValueError ( 'Invalid serializer `{0}`. Register your serializer with ' '`manager.register()` first.' . format ( serializer_name ) ) if data is None : # Delete cached data delete_paths ( ( metadata_path , data_path ) ) return # Ensure write is not interrupted by SIGTERM @ uninterruptible def _store ( ) : # Save file extension with atomic_writer ( metadata_path , 'wb' ) as file_obj : file_obj . write ( serializer_name ) with atomic_writer ( data_path , 'wb' ) as file_obj : serializer . dump ( data , file_obj ) _store ( ) self . logger . debug ( 'saved data: %s' , data_path )