Represents a Resource in HydroShare
access_permission
property
readonly
Retrieves the access permissions of the resource :return: JSON object
metadata_file
property
readonly
The path to the metadata file
resource_id: str
property
readonly
The resource id (guid) of the HydroShare resource
aggregation_delete(self, aggregation)
Deletes an aggregation from HydroShare. This deletes the files and metadata in the aggregation. :param aggregation: The aggregation object to delete :return: None
Source code in hsclient/hydroshare.py
@refresh
def aggregation_delete(self, aggregation: Aggregation) -> None:
"""
Deletes an aggregation from HydroShare. This deletes the files and metadata in the aggregation.
:param aggregation: The aggregation object to delete
:return: None
"""
path = urljoin(
aggregation._hsapi_path,
"functions",
"delete-file-type",
aggregation.metadata.type.value + "LogicalFile",
aggregation.main_file_path,
)
aggregation._hs_session.delete(path, status_code=200)
aggregation.refresh()
aggregation_download(self, aggregation, save_path='', unzip_to=None)
Download an aggregation from HydroShare :param aggregation: The aggreation to download :param save_path: The local path to save the aggregation to, defaults to the current directory :param unzip_to: If set, the resulting download will be unzipped to the specified path :return: None
Source code in hsclient/hydroshare.py
def aggregation_download(self, aggregation: Aggregation, save_path: str = "", unzip_to: str = None) -> str:
"""
Download an aggregation from HydroShare
:param aggregation: The aggreation to download
:param save_path: The local path to save the aggregation to, defaults to the current directory
:param unzip_to: If set, the resulting download will be unzipped to the specified path
:return: None
"""
return aggregation._download(save_path=save_path, unzip_to=unzip_to)
aggregation_remove(self, aggregation)
Removes an aggregation from HydroShare. This does not remove the files in the aggregation. :param aggregation: The aggregation object to remove :return: None
Source code in hsclient/hydroshare.py
@refresh
def aggregation_remove(self, aggregation: Aggregation) -> None:
"""
Removes an aggregation from HydroShare. This does not remove the files in the aggregation.
:param aggregation: The aggregation object to remove
:return: None
"""
path = urljoin(
aggregation._hsapi_path,
"functions",
"remove-file-type",
aggregation.metadata.type.value + "LogicalFile",
aggregation.main_file_path,
)
aggregation._hs_session.post(path, status_code=200)
aggregation.refresh()
copy(self)
Copies this Resource into a new resource on HydroShare returns: A Resource object of the newly copied resource
Source code in hsclient/hydroshare.py
def copy(self):
"""
Copies this Resource into a new resource on HydroShare
returns: A Resource object of the newly copied resource
"""
path = urljoin(self._hsapi_path, "copy")
response = self._hs_session.post(path, status_code=202)
resource_id = response.text
return Resource("/resource/{}/data/resourcemap.xml".format(resource_id), self._hs_session)
delete(self)
Deletes the resource on HydroShare :return: None
Source code in hsclient/hydroshare.py
@refresh
def delete(self) -> None:
"""
Deletes the resource on HydroShare
:return: None
"""
hsapi_path = self._hsapi_path
self._hs_session.delete(hsapi_path, status_code=204)
download(self, save_path='')
Downloads a zipped bagit archive of the resource from HydroShare param save_path: A local path to save the bag to, defaults to the current working directory returns: The relative pathname of the download
Source code in hsclient/hydroshare.py
def download(self, save_path: str = "") -> str:
"""
Downloads a zipped bagit archive of the resource from HydroShare
param save_path: A local path to save the bag to, defaults to the current working directory
returns: The relative pathname of the download
"""
return self._hs_session.retrieve_bag(self._hsapi_path, save_path=save_path)
file_aggregate(self, path, agg_type, refresh=True)
Aggregate a file to a HydroShare aggregation type. Aggregating files allows you to specify metadata specific to the files associated with the aggregation. To set a FileSet aggregation, include the path to the folder or a file in the folder you would like to create a FileSet aggregation from. :param path: The path to the file to aggregate :param agg_type: The AggregationType to create :param refresh: Defaults True, toggles automatic refreshing of the updated resource in HydroShare :return: The newly created Aggregation object if refresh is True
Source code in hsclient/hydroshare.py
def file_aggregate(self, path: str, agg_type: AggregationType, refresh: bool = True):
"""
Aggregate a file to a HydroShare aggregation type. Aggregating files allows you to specify metadata specific
to the files associated with the aggregation. To set a FileSet aggregation, include the path to the folder or
a file in the folder you would like to create a FileSet aggregation from.
:param path: The path to the file to aggregate
:param agg_type: The AggregationType to create
:param refresh: Defaults True, toggles automatic refreshing of the updated resource in HydroShare
:return: The newly created Aggregation object if refresh is True
"""
type_value = agg_type.value
data = {}
if agg_type == AggregationType.SingleFileAggregation:
type_value = 'SingleFile'
if agg_type == AggregationType.FileSetAggregation:
relative_path = dirname(path)
data = {"folder_path": relative_path}
url = urljoin(self._hsapi_path, "functions", "set-file-type", path, type_value)
self._hs_session.post(url, status_code=201, data=data)
if refresh:
# Only return the newly created aggregation if a refresh is requested
self.refresh()
return self.aggregation(file__path=path)
file_delete(self, path=None)
Delete a file on HydroShare :param path: The path to the file :return: None
Source code in hsclient/hydroshare.py
@refresh
def file_delete(self, path: str = None) -> None:
"""
Delete a file on HydroShare
:param path: The path to the file
:return: None
"""
self._delete_file(path)
file_download(self, path, save_path='', zipped=False)
Downloads a file from HydroShare :param path: The path to the file :param save_path: The local path to save the file to :param zipped: Defaults to False, set to True to download the file zipped :return: The path to the downloaded file
Source code in hsclient/hydroshare.py
def file_download(self, path: str, save_path: str = "", zipped: bool = False):
"""
Downloads a file from HydroShare
:param path: The path to the file
:param save_path: The local path to save the file to
:param zipped: Defaults to False, set to True to download the file zipped
:return: The path to the downloaded file
"""
if zipped:
return self._hs_session.retrieve_zip(
urljoin(self._resource_path, "data", "contents", path), save_path, params={"zipped": "true"}
)
else:
return self._hs_session.retrieve_file(urljoin(self._resource_path, "data", "contents", path), save_path)
file_rename(self, path, new_path)
Rename a file on HydroShare :param path: The path to the file :param new_path: the renamed path to the file :return: None
Source code in hsclient/hydroshare.py
@refresh
def file_rename(self, path: str, new_path: str) -> None:
"""
Rename a file on HydroShare
:param path: The path to the file
:param new_path: the renamed path to the file
:return: None
"""
rename_path = urljoin(self._hsapi_path, "functions", "move-or-rename")
self._hs_session.post(rename_path, status_code=200, data={"source_path": path, "target_path": new_path})
file_unzip(self, path, overwrite=True, ingest_metadata=True)
Unzips a file on HydroShare :param path: The path to the file to unzip :param overwrite: Defaults to True, set to False to unzip the files into a folder with the zip filename :param ingest_metadata: Defaults to True, set to False to not ingest HydroShare RDF metadata xml files :return: None
Source code in hsclient/hydroshare.py
@refresh
def file_unzip(self, path: str, overwrite: bool = True, ingest_metadata=True) -> None:
"""
Unzips a file on HydroShare
:param path: The path to the file to unzip
:param overwrite: Defaults to True, set to False to unzip the files into a folder with the zip filename
:param ingest_metadata: Defaults to True, set to False to not ingest HydroShare RDF metadata xml files
:return: None
"""
if not path.endswith(".zip"):
raise Exception("File {} is not a zip, and cannot be unzipped".format(path))
unzip_path = urljoin(self._hsapi_path, "functions", "unzip", "data", "contents", path)
self._hs_session.post(
unzip_path, status_code=200, data={"overwrite": overwrite, "ingest_metadata": ingest_metadata}
)
file_upload(self, *files, *, destination_path='')
Uploads files to a folder in HydroShare :param *files: The local file paths to upload :param destination_path: The path on HydroShare to upload the files to, defaults to the root contents directory :return: None
Source code in hsclient/hydroshare.py
@refresh
def file_upload(self, *files: str, destination_path: str = "") -> None:
"""
Uploads files to a folder in HydroShare
:param *files: The local file paths to upload
:param destination_path: The path on HydroShare to upload the files to, defaults to the root contents directory
:return: None
"""
if len(files) == 1:
self._upload(files[0], destination_path=destination_path)
else:
with tempfile.TemporaryDirectory() as tmpdir:
zipped_file = os.path.join(tmpdir, 'files.zip')
with ZipFile(zipped_file, 'w') as zipped:
for file in files:
zipped.write(file, os.path.basename(file))
self._upload(zipped_file, destination_path=destination_path)
unzip_path = urljoin(
self._hsapi_path, "functions", "unzip", "data", "contents", destination_path, 'files.zip'
)
self._hs_session.post(
unzip_path, status_code=200, data={"overwrite": "true", "ingest_metadata": "true"}
)
file_zip(self, path, zip_name=None, remove_file=True)
Zip a file on HydroShare :param path: The path to the file :param zip_name: The name of the zipped file :param remove_file: Defaults to True, set to False to not delete the file that was zipped :return: None
Source code in hsclient/hydroshare.py
@refresh
def file_zip(self, path: str, zip_name: str = None, remove_file: bool = True) -> None:
"""
Zip a file on HydroShare
:param path: The path to the file
:param zip_name: The name of the zipped file
:param remove_file: Defaults to True, set to False to not delete the file that was zipped
:return: None
"""
zip_name = basename(path) + ".zip" if not zip_name else zip_name
data = {"input_coll_path": path, "output_zip_file_name": zip_name, "remove_original_after_zip": remove_file}
zip_path = urljoin(self._hsapi_path, "functions", "zip")
self._hs_session.post(zip_path, status_code=200, data=data)
folder_create(self, folder)
Creates a folder on HydroShare :param folder: the folder path to create :return: None
Source code in hsclient/hydroshare.py
@refresh
def folder_create(self, folder: str) -> None:
"""
Creates a folder on HydroShare
:param folder: the folder path to create
:return: None
"""
path = urljoin(self._hsapi_path, "folders", folder)
self._hs_session.put(path, status_code=201)
folder_delete(self, path=None)
Deletes a folder on HydroShare :param path: the path to the folder :return: None
Source code in hsclient/hydroshare.py
@refresh
def folder_delete(self, path: str = None) -> None:
"""
Deletes a folder on HydroShare
:param path: the path to the folder
:return: None
"""
self._delete_file_folder(path)
folder_download(self, path, save_path='')
Downloads a folder from HydroShare :param path: The path to folder :param save_path: The local path to save the download to, defaults to the current directory :return: The path to the download zipped folder
Source code in hsclient/hydroshare.py
def folder_download(self, path: str, save_path: str = ""):
"""
Downloads a folder from HydroShare
:param path: The path to folder
:param save_path: The local path to save the download to, defaults to the current directory
:return: The path to the download zipped folder
"""
return self._hs_session.retrieve_zip(
urljoin(self._resource_path, "data", "contents", path), save_path, params={"zipped": "true"}
)
folder_rename(self, path, new_path)
Renames a folder on HydroShare :param path: the path to the folder to rename :param new_path: the new path folder name :return: None
Source code in hsclient/hydroshare.py
@refresh
def folder_rename(self, path: str, new_path: str) -> None:
"""
Renames a folder on HydroShare
:param path: the path to the folder to rename
:param new_path: the new path folder name
:return: None
"""
self.file_rename(path=path, new_path=new_path)
new_version(self)
Creates a new version of the resource on HydroShare :return: A Resource object of the newly created resource version
Source code in hsclient/hydroshare.py
def new_version(self):
"""
Creates a new version of the resource on HydroShare
:return: A Resource object of the newly created resource version
"""
path = urljoin(self._hsapi_path, "version")
response = self._hs_session.post(path, status_code=202)
resource_id = response.text
return Resource("/resource/{}/data/resourcemap.xml".format(resource_id), self._hs_session)
reference_create(self, file_name, url, path='')
Creates a HydroShare reference object to reference content outside of the resource :param file_name: the file name of the resulting .url file :param url: the url of the referenced content :param path: the path to create the reference in :return: None
Source code in hsclient/hydroshare.py
@refresh
def reference_create(self, file_name: str, url: str, path: str = '') -> None:
"""
Creates a HydroShare reference object to reference content outside of the resource
:param file_name: the file name of the resulting .url file
:param url: the url of the referenced content
:param path: the path to create the reference in
:return: None
"""
request_path = urljoin(self._hsapi_path.replace(self.resource_id, ""), "data-store-add-reference")
self._hs_session.post(
request_path,
data={"res_id": self.resource_id, "curr_path": path, "ref_name": file_name, "ref_url": url},
status_code=200,
)
reference_update(self, file_name, url, path='')
Updates a HydroShare reference object :param file_name: the file name for the .url file :param url: the url of the referenced content :param path: the path to the directory where the reference is located :return: None
Source code in hsclient/hydroshare.py
@refresh
def reference_update(self, file_name: str, url: str, path: str = '') -> None:
"""
Updates a HydroShare reference object
:param file_name: the file name for the .url file
:param url: the url of the referenced content
:param path: the path to the directory where the reference is located
:return: None
"""
request_path = urljoin(self._hsapi_path.replace(self.resource_id, ""), "data_store_edit_reference_url")
self._hs_session.post(
request_path,
data={"res_id": self.resource_id, "curr_path": path, "url_filename": file_name, "new_ref_url": url},
status_code=200,
)
save(self)
Saves the metadata to HydroShare :return: None
Source code in hsclient/hydroshare.py
@refresh
def save(self) -> None:
"""
Saves the metadata to HydroShare
:return: None
"""
metadata_string = rdf_string(self._retrieved_metadata, rdf_format="xml")
path = urljoin(self._hsapi_path, "ingest_metadata")
self._hs_session.upload_file(path, files={'file': ('resourcemetadata.xml', metadata_string)})
set_sharing_status(self, public)
Set the sharing status of the resource to public or private :param public: bool, set to True for public, False for private
Source code in hsclient/hydroshare.py
def set_sharing_status(self, public: bool):
"""
Set the sharing status of the resource to public or private
:param public: bool, set to True for public, False for private
"""
path = urljoin("hsapi", "resource", "accessRules", self.resource_id)
data = {'public': public}
self._hs_session.put(path, status_code=200, data=data)
system_metadata(self)
The system metadata associated with the HydroShare resource returns: JSON object
Source code in hsclient/hydroshare.py
def system_metadata(self):
"""
The system metadata associated with the HydroShare resource
returns: JSON object
"""
hsapi_path = urljoin(self._hsapi_path, 'sysmeta')
return self._hs_session.get(hsapi_path, status_code=200).json()