Skip to content

Commit

Permalink
Add utility to update scan metadata in local database
Browse files Browse the repository at this point in the history
This commit introduces the `add_metadata_to_scan` function in `fsdb_tools.py`.
It allows adding or updating metadata for a specific scan instance in a local database, with proper validation and logging.
  • Loading branch information
jlegrand62 committed Feb 5, 2025
1 parent c506eca commit 66186f6
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/plantdb/fsdb_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from plantdb.log import get_logger

logger = get_logger(__name__)


def add_metadata_to_scan(db, scan_id, metadata):
"""Add metadata to a scan dataset.
Parameters
----------
db : plantdb.fsdb.FSDB
A local database instance hosting the ``Scan`` instance.
scan_id : str
The identifier of the ``Scan`` instance in the local database.
metadata : dict
The metadata dictionary to assign to the ``Scan`` instance.
Returns
-------
plantdb.fsdb.Scan
The updated ``Scan`` instance with the new metadata.
Examples
--------
>>> from plantdb.fsdb import dummy_db
>>> from plantdb.fsdb_tools import add_metadata_to_scan
>>> db = dummy_db(with_scan=True)
>>> scan = db.get_scan("myscan_001")
>>> print(scan.metadata)
{'test': 1}
>>> scan = add_metadata_to_scan(db, "myscan_001", {"Name": "Example Scan"})
>>> print(scan.metadata)
{'test': 1, 'Name': 'Example Scan'}
>>> db.disconnect() # clean up (delete) the temporary dummy database
"""
if db.scan_exists(scan_id) is False:
logger.warning(
f"Scan '{scan_id}' does not exist in the database. Cannot add metadata."
)
return None
# Get or create the scan instance
scan = db.get_scan(scan_id, create=False)
# Add metadata to the scan
scan.set_metadata(metadata)
# Return the updated scan instance
return scan

0 comments on commit 66186f6

Please sign in to comment.