db_ops
Metadata database read/write helpers for the editor UILink
Small FSDB helpers used by the metadata editor: load the flattened MIAPPE
metadata of every scan, and write updated metadata back to metadata.json
with a .bak backup and schema validation.
A database root must be a formal PlantDB (FSDB, i.e. a directory containing a
romidb marker file). A loose directory that is not a proper ROMI DB is
rejected.
all_scan_metadata
Link
all_scan_metadata(db_path)
Return flattened scan metadata for every scan in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Path to the FSDB to load. |
required |
Returns:
| Type | Description |
|---|---|
dict of dict
|
A mapping of each scan id to its flattened MIAPPE metadata
(see :func: |
Source code in plantdb/client/metadata_app/db_ops.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
apply_bulk
Link
Set the field at path to value on every scan in scan_ids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Path to the FSDB containing the scans. |
required |
|
list of str
|
Scan ids to update. |
required |
|
str
|
Dot-separated field path to set. |
required |
|
Any
|
Value to set at the field. |
required |
|
bool
|
Back up each scan's metadata before overwriting. |
True
|
Returns:
| Type | Description |
|---|---|
list of str
|
The scan ids that were actually modified (i.e. whose value changed). Writes are validated and backed up. |
Source code in plantdb/client/metadata_app/db_ops.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
close_db
Link
close_db(db_path)
Disconnect and drop the cached FSDB for db_path, if any.
Used when the app switches to a different database so only one live connection stays in memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Path of the database whose cached connection to close. |
required |
Source code in plantdb/client/metadata_app/db_ops.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
get_field
Link
Return the value of the field at dot-path in a nested dict.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict of str to Any
|
The nested metadata dict to read. |
required |
|
str
|
Dot-separated path to the field, e.g. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The field value, or |
Source code in plantdb/client/metadata_app/db_ops.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
get_scan_dir
Link
Return the on-disk directory of scan_id in the FSDB at db_path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Path to the FSDB containing the scan. |
required |
|
str
|
Identifier of the scan. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Directory of the scan. |
Source code in plantdb/client/metadata_app/db_ops.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
migratable_scans
Link
migratable_scans(db_path)
Return the ids of scans that still use the legacy (pre-MIAPPE) schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Path to the FSDB to inspect. |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
Scan ids that require migration. |
Source code in plantdb/client/metadata_app/db_ops.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
migrate_scans_progress
Link
migrate_scans_progress(db_path, scan_ids, logger, on_progress=None)
Migrate scan_ids scan by scan, reporting progress.
on_progress(done, total) is called after each scan is processed, so a
caller can surface a live progress bar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Path
|
Path to the FSDB containing the scans. |
required |
|
list of str
|
Scan ids to migrate. |
required |
|
Logger
|
Logger used to report each migrated scan. |
required |
|
Callable[[int, int], None]
|
Callback invoked with |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The number of scans that were actually migrated. |
Source code in plantdb/client/metadata_app/db_ops.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
scan_needs_migration
Link
scan_needs_migration(scan)
Return True if a scan's metadata still holds a legacy object block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Scan
|
Scan to inspect. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/client/metadata_app/db_ops.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
set_field
Link
Return metadata with the field at dot-path set to value.
Intermediate sections are created as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict of str to Any
|
The nested metadata dict to modify (in place). |
required |
|
str
|
Dot-separated path to the field, e.g. |
required |
|
Any
|
Value to set at the field. |
required |
Returns:
| Type | Description |
|---|---|
dict of str to Any
|
The same |
Source code in plantdb/client/metadata_app/db_ops.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
update_biological
Link
Return metadata with the biological sections replaced by tree.
Non-biological keys (owner, created, ...) are preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict of str to Any
|
The full scan metadata to update. |
required |
|
dict of str to Any
|
New MIAPPE biological tree to install. |
required |
Returns:
| Type | Description |
|---|---|
dict of str to Any
|
A copy of |
Source code in plantdb/client/metadata_app/db_ops.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
write_scan_metadata
Link
Validate and write metadata to scan_dir/metadata/metadata.json.
A .bak copy of the previous file is written first when backup is True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Scan
|
Scan instance to use for metadata modification. |
required |
|
dict of str to Any
|
The metadata to persist. |
required |
|
bool
|
Write a |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the MIAPPE biological block of |
Source code in plantdb/client/metadata_app/db_ops.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |