Represents an Aggregation in HydroShare
main_file_path: str
property
readonly
The path to the main file in the aggregation
metadata: BaseMetadata
property
readonly
A metadata object for reading and updating metadata values
metadata_file
property
readonly
The path to the metadata file
metadata_path: str
property
readonly
The path to the metadata file
aggregation(self, **kwargs)
Returns a single Aggregation in the resource that matches the filtering parameters. Uses the same filtering rules described in the aggregations method. :params **kwargs: Search by properties on the metadata object :return: An Aggregation object matching the filter parameters or None if no matching Aggregation was found.
Source code in hsclient/hydroshare.py
def aggregation(self, **kwargs) -> BaseMetadata:
"""
Returns a single Aggregation in the resource that matches the filtering parameters. Uses the same filtering
rules described in the aggregations method.
:params **kwargs: Search by properties on the metadata object
:return: An Aggregation object matching the filter parameters or None if no matching Aggregation was found.
"""
aggregations = self.aggregations(**kwargs)
if aggregations:
return aggregations[0]
return None
aggregations(self, **kwargs)
List the aggregations in the resource. Filter by properties on the metadata object using kwargs. If you need to filter on nested properties, use __ (double underscore) to separate the properties. For example, to filter by the BandInformation name, call this method like aggregations(band_information__name="the name to search"). :params **kwargs: Search by properties on the metadata object :return: a List of Aggregation objects matching the filter parameters
Source code in hsclient/hydroshare.py
def aggregations(self, **kwargs) -> List[BaseMetadata]:
"""
List the aggregations in the resource. Filter by properties on the metadata object using kwargs. If you need
to filter on nested properties, use __ (double underscore) to separate the properties. For example, to filter
by the BandInformation name, call this method like aggregations(band_information__name="the name to search").
:params **kwargs: Search by properties on the metadata object
:return: a List of Aggregation objects matching the filter parameters
"""
aggregations = self._aggregations
for key, value in kwargs.items():
if key.startswith('file__'):
file_args = {key[len('file__') :]: value}
aggregations = [agg for agg in aggregations if agg.files(**file_args)]
elif key.startswith('files__'):
file_args = {key[len('files__') :]: value}
aggregations = [agg for agg in aggregations if agg.files(**file_args)]
else:
aggregations = filter(lambda agg: attribute_filter(agg.metadata, key, value), aggregations)
return list(aggregations)
as_series(self, series_id, agg_path=None)
Creates a pandas Series object out of an aggregation of type TimeSeries. :param series_id: The series_id of the timeseries result to be converted to a Series object. :param agg_path: Not required. Include this parameter to avoid downloading the aggregation if you already have it downloaded locally. :return: A pandas.Series object
Source code in hsclient/hydroshare.py
def as_series(self, series_id: str, agg_path: str = None) -> Dict[int, pandas.Series]:
"""
Creates a pandas Series object out of an aggregation of type TimeSeries.
:param series_id: The series_id of the timeseries result to be converted to a Series object.
:param agg_path: Not required. Include this parameter to avoid downloading the aggregation if you already have
it downloaded locally.
:return: A pandas.Series object
"""
def to_series(timeseries_file: str):
con = sqlite3.connect(timeseries_file)
return pandas.read_sql(
f'SELECT * FROM TimeSeriesResultValues WHERE ResultID IN '
f'(SELECT ResultID FROM Results WHERE ResultUUID = "{series_id}");',
con,
).squeeze()
if agg_path is None:
with tempfile.TemporaryDirectory() as td:
self._download(unzip_to=td)
# zip extracted to folder with main file name
file_name = self.file(extension=".sqlite").name
return to_series(urljoin(td, file_name, file_name))
return to_series(urljoin(agg_path, self.file(extension=".sqlite").name))
file(self, search_aggregations=False, **kwargs)
Returns a single file in the resource that matches the filtering parameters :param search_aggregations: Defaults False, set to true to search aggregations :params **kwargs: Search by properties on the File object (path, name, extension, folder, checksum url) :return: A File object matching the filter parameters or None if no matching File was found
Source code in hsclient/hydroshare.py
def file(self, search_aggregations=False, **kwargs) -> File:
"""
Returns a single file in the resource that matches the filtering parameters
:param search_aggregations: Defaults False, set to true to search aggregations
:params **kwargs: Search by properties on the File object (path, name, extension, folder, checksum url)
:return: A File object matching the filter parameters or None if no matching File was found
"""
files = self.files(search_aggregations=search_aggregations, **kwargs)
if files:
return files[0]
return None
files(self, search_aggregations=False, **kwargs)
List files and filter by properties on the file object using kwargs (i.e. extension='.txt') :param search_aggregations: Defaults False, set to true to search aggregations :params **kwargs: Search by properties on the File object (path, name, extension, folder, checksum url) :return: a List of File objects matching the filter parameters
Source code in hsclient/hydroshare.py
def files(self, search_aggregations: bool = False, **kwargs) -> List[File]:
"""
List files and filter by properties on the file object using kwargs (i.e. extension='.txt')
:param search_aggregations: Defaults False, set to true to search aggregations
:params **kwargs: Search by properties on the File object (path, name, extension, folder, checksum url)
:return: a List of File objects matching the filter parameters
"""
files = self._files
for key, value in kwargs.items():
files = list(filter(lambda file: attribute_filter(file, key, value), files))
if search_aggregations:
for aggregation in self.aggregations():
files = files + list(aggregation.files(search_aggregations=search_aggregations, **kwargs))
return files
refresh(self)
Forces the retrieval of the resource map and metadata files. Currently this is implemented to be lazy and will only retrieve those files again after another call to access them is made. This will be later updated to be eager and retrieve the files asynchronously.
Source code in hsclient/hydroshare.py
def refresh(self) -> None:
"""
Forces the retrieval of the resource map and metadata files. Currently this is implemented to be lazy and will
only retrieve those files again after another call to access them is made. This will be later updated to be
eager and retrieve the files asynchronously.
"""
# TODO, refresh should destroy the aggregation objects and async fetch everything.
self._retrieved_map = None
self._retrieved_metadata = None
self._parsed_files = None
self._parsed_aggregations = None
self._parsed_checksums = None
save(self)
Saves the metadata back to HydroShare :return: None
Source code in hsclient/hydroshare.py
@refresh
def save(self) -> None:
"""
Saves the metadata back to HydroShare
:return: None
"""
metadata_file = self.metadata_file
metadata_string = rdf_string(self._retrieved_metadata, rdf_format="xml")
url = urljoin(self._hsapi_path, "ingest_metadata")
self._hs_session.upload_file(url, files={'file': (metadata_file, metadata_string)})