hbp_nrp_backend.storage_client_api.storage_client module

Wrapper around the storage API

class hbp_nrp_backend.storage_client_api.storage_client.StorageClient[source]

Bases: object

Wrapper around the storage server API. Users of this class should first call the authentication function to retrieve a token, before making the requests to the storage server

can_access_experiment(token: str, context_id, experiment_id)[source]

Verifies if an authenticated user can access an experiment

Parameters
  • token – The authentication token

  • context_id – Optional context idenfifier

  • experiment_id – The experiment id

Returns

Whether the user can access the experiment

static check_file_extension(filename: str, extensions: List[str]) → bool[source]

checks if a file is of a certain extension

Parameters
  • filename – the file name

  • extensions – the extensions list to check

clone_all_experiment_files(token: str, experiment: str, destination_dir: Optional[str] = None, exclude: Optional[List[str]] = None)[source]

Clones all the experiment files to a simulation folder. The caller has then the responsibility of managing this folder.

Parameters
  • token – The token of the request

  • experiment – The experiment to clone

  • destination_dir – the directory in which to clone the files, if None or an empty string is provided, clones into a temporary folder

  • exclude – a list of folders of files not to clone (folder names ends with ‘/’)

Returns

A dictionary containing the paths to the experiment files

clone_file(token: str, filename: str, experiment: str) → Optional[str][source]

Clones a file according to a given filename to a simulation folder. The caller then has the responsibility of managing this folder.

Parameters
  • filename – The filename of the file to clone

  • token – The token of the request

  • experiment – The experiment which contains the file

Returns

The local path of the cloned file,

copy_file_content(token: str, src_folder, dest_folder, filename)[source]

copy the content of file located in the Storage into the proper tmp folder

Parameters
  • token – The token of the request

  • src_folder – folder location where it will be copy from

  • dest_folder – folder location where it will be copy to

  • filename – name of the file to be copied.

copy_folder_content_to_tmp(token: str, folder)[source]

Copy the content of the folder located in storage/experiment into sim_dir folder

Parameters
  • token – The token of the request

  • folder – the folder in the storage folder to copy in tmp folder, it has included the uuid of the experiment

create_and_extract_zip(token: str, experiment: str, name: str, content)[source]

Creates and extracts a zip under an experiment. .. TODO:: add a REST endpoint in the storage to allow the transfer of multiple files. For instance we might add a create_files(files_list) in StorageClient. Internally, the files might be zipped on the client and unzipped on the server.

Parameters
  • token – a valid token to be used for the request

  • experiment – the name of the experiment

  • name – the name of the zip

  • content – the content of the file

create_folder(token: str, experiment: str, name: str)[source]

Creates a folder under an experiment. If the folder exists we reuse it

Parameters
  • token – a valid token to be used for the request

  • experiment – the name of the experiment

  • name – the name of the folder to create

create_or_update(token: str, experiment: str, filename, content, content_type: str, append: bool = False)[source]

Creates or updates a file under an experiment

Parameters
  • token – a valid token to be used for the request

  • experiment – the name of the experiment

  • filename – the name of the file to update/create

  • content – the content of the file

  • content_type – the content type of the file i.e. text/plain or application/octet-stream

  • append – append to file or create new file

delete_file(token: str, experiment: str, filename) → str[source]

Deletes a file under an experiment based on the experiment name and the filename. Needs the user token

Parameters
  • token – a valid token to be used for the request

  • experiment – the name of the experiment

  • filename – the name of the file to delete

Returns

if successful, the name of the deleted file

get_file(token: str, experiment: str, filename: str, by_name: bool = False) → bytes[source]

Gets the content of a file under an experiment based on the filename and on the filetype

Parameters
  • token – a valid token to be used for the request

  • experiment – the name of the experiment

  • filename – the name of the file to return

Returns

if successful, the content of the file as a binary string

get_files_list(token: str, experiment: str, folder: bool = False)[source]

Lists all the files under an experiment based on the experiment name and the user token

Parameters
  • token – a valid token to be used for the request

  • experiment – the name of the experiment

  • folder – A boolean variable indicating whether folders should included in the result.

Returns

if successful, the files under the experiment

get_folder_uuid_by_name(token, context_id, folder_name)[source]

Returns the uuid of a folder provided its name

Parameters
  • token – a valid token to be used for the request

  • context_id – the context_id of the collab

  • folder_name – the name of the folder

Returns

if found, the uuid of the named folder

get_user(token)[source]

Retrieves the user id for the specified authentication token

Parameters

token – the authentication token

Returns

the user id

Raises
  • ValueError – Could not verify auth token

  • ConnectionError

list_experiments(token: str, context_id, get_all=False, name=None)[source]

Lists the experiments the user has access to depending on his token.

Parameters
  • token – a valid token to be used for the request

  • context_id – the context_id if we are using collab storage

  • get_all – a parameter to return all the available experiments, not only the ones available to a specific user

  • name – if we want to get a specific folder i.e. the robots

Returns

an array of all the available to the user experiments

set_sim_dir(sim_dir)[source]

Sets the sim_dir for this client

Parameters

sim_dir – Simulation directory