core
plantdb.commons.fsdb.core Link
This module implement a database as a local file structure.
Assuming that the FSDB
root database directory is dbroot/
, there is a Scan
with 'myscan_001'
as Scan.id
and there are some metadata (see below), you should have the following file structure:
dbroot/ # base directory of the database
├── myscan_001/ # scan dataset directory, id=`myscan_001`
│ ├── files.json # JSON file referencing the all files for the dataset
│ ├── images/ # `Fileset` gathering the 'images'
│ │ ├── scan_img_01.jpg # 'image' `File` 01
│ │ ├── scan_img_02.jpg # 'image' `File` 02
│ │ ├── [...] #
│ │ └── scan_img_99.jpg # 'image' `File` 99
│ ├── metadata/ # metadata directory
│ │ ├── images # 'images' metadata directory
│ │ │ ├── scan_img_01.json # JSON metadata attached to this file
│ │ │ ├── scan_img_02.json #
│ │ │ [...] #
│ │ │ └── scan_img_99.json #
│ │ ├── Task_A/ # optional, only present if metadata attached to one of the outputs from `Task_A`
│ │ │ └── outfile.json # optional metadata attached to the output file from `Task_A`
│ │ └── (metadata.json) # optional metadata attached to the dataset
│ ├── task_A/ # `Fileset` gathering the outputs of `Task_A`
│ │ └── outfile.ext # output file from `Task_A`
│ └── (measures.json) # optional manual measurements file
├── myscan_002/ # scan dataset directory, id=`myscan_002`
:
├── users.json # user registry
├── (LOCK_FILE_NAME) # "lock file", present if DB is connected
└── MARKER_FILE_NAME # ROMI DB marker file
The myscan_001/files.json
file then contains the following structure:
{
"filesets": [
{
"id": "images",
"files": [
{
"id": "scan_img_01",
"file": "scan_img_01.jpg"
},
{
"id": "scan_img_02",
"file": "scan_img_02.jpg"
},
[...]
{
"id": "scan_img_99",
"file": "scan_img_99.jpg"
}
]
}
]
}
The metadata of the scan (metadata.json
), of the set of 'images' files (<Fileset.id>.json
) and of each 'image' files (<File.id>.json
) are all stored as JSON files in a separate directory:
myscan_001/metadata/
myscan_001/metadata/metadata.json
myscan_001/metadata/images.json
myscan_001/metadata/images/scan_img_01.json
myscan_001/metadata/images/scan_img_02.json
[...]
myscan_001/metadata/images/scan_img_99.json
FSDB Link
FSDB(basedir, required_filesets=['metadata'], required_files_json=True, dummy=False)
Bases: DB
Implement a local File System DataBase version of abstract class db.DB
.
Implement as a simple local file structure with following directory structure and marker files:
* directory ${FSDB.basedir}
as database root directory;
* marker file MARKER_FILE_NAME
at database root directory;
* (OPTIONAL) lock file LOCK_FILE_NAME
at database root directory when connected;
Attributes:
Name | Type | Description |
---|---|---|
basedir |
Path
|
The absolute path to the base directory hosting the database. |
lock_path |
Path
|
The absolute path to the lock file. |
scans |
dict[str, Scan]
|
The dictionary of |
is_connected |
bool
|
|
Notes
Requires the marker file MARKER_FILE_NAME
at the given basedir
.
Lock file LOCK_FILE_NAME
is found only when connecting an FSBD instance to the given basedir
.
See Also
plantdb.commons.db.DB plantdb.commons.fsdb.core.MARKER_FILE_NAME plantdb.commons.fsdb.core.LOCK_FILE_NAME
Examples:
>>> # EXAMPLE 1: Use a temporary dummy local database:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db()
>>> print(type(db))
<class 'plantdb.commons.fsdb.FSDB'>
>>> print(db.path())
/tmp/romidb_********
>>> # Create a new `Scan`:
>>> new_scan = db.create_scan("007")
>>> print(type(new_scan))
<class 'plantdb.commons.fsdb.Scan'>
>>> db.disconnect() # clean up (delete) the temporary dummy database
>>> # EXAMPLE 2: Use a local database:
>>> import os
>>> from plantdb.commons.fsdb import FSDB
>>> db = FSDB(os.environ.get('ROMI_DB', "/data/ROMI/DB/"))
>>> db.connect()
>>> [scan.id for scan in db.get_scans()] # list scan ids found in database
>>> scan = db.get_scans()[1]
>>> [fs.id for fs in scan.get_filesets()] # list fileset ids found in scan
>>> db.disconnect() # clean up (delete) the temporary dummy database
Database constructor.
Check given basedir
directory exists and load accessible Scan
objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
basedir
|
str or Path
|
The path to the root directory of the database. |
required |
required_filesets
|
list of str
|
A list of required filesets to consider a scan valid.
Set it to |
['metadata']
|
required_files_json
|
bool
|
Require the |
True
|
dummy
|
bool
|
If |
False
|
Raises:
Type | Description |
---|---|
NotADirectoryError
|
If the given |
NotAnFSDBError
|
If the |
See Also
plantdb.commons.fsdb.core.MARKER_FILE_NAME
Source code in plantdb/commons/fsdb/core.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 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 358 359 360 361 362 363 364 365 366 367 |
|
connect Link
connect(login=None, password='', unsafe=False)
Connect to the local database.
Handle DB "locking" system by adding a LOCK_FILE_NAME
file in the DB.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
login
|
str
|
The user login, if not defined, use the |
None
|
unsafe
|
bool
|
If |
False
|
Raises:
Type | Description |
---|---|
DBBusyError
|
If the database is already used by another process.
This is achieved by searching for a |
See Also
plantdb.commons.fsdb.LOCK_FILE_NAME
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db()
>>> print(db.is_connected)
True
>>> db.create_user("batman", "Bruce Wayne")
>>> db.connect('batman', 'joker')
>>> print(db.is_connected)
True
>>> db.disconnect() # clean up (delete) the temporary dummy database
>>> print(db.is_connected)
False
Source code in plantdb/commons/fsdb/core.py
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
|
create_scan Link
create_scan(scan_id, metadata=None)
Create a new Scan
instance in the local database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scan_id
|
str
|
The identifier of the scan to create. It should contain only alphanumeric characters, underscores, dashes and dots. It should be non-empty and not longer than 255 characters It should not exist in the local database |
required |
metadata
|
dict
|
A dictionary of metadata to append to the new |
None
|
Returns:
Type | Description |
---|---|
Scan
|
The |
Raises:
Type | Description |
---|---|
OSError
|
If the |
See Also
plantdb.commons.fsdb._is_valid_id plantdb.commons.fsdb._make_scan
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db()
>>> new_scan = db.create_scan('007', metadata={'project': 'GoldenEye'}) # create a new scan dataset
>>> print(new_scan.get_metadata('owner')) # default user 'anonymous' for dummy database
anonymous
>>> print(new_scan.get_metadata('project'))
GoldenEye
>>> scan = db.create_scan('007') # attempt to create an existing scan dataset
OSError: Given scan identifier '007' already exists!
>>> scan = db.create_scan('0/07') # attempt to create a scan dataset using invalid characters
OSError: Invalid scan identifier '0/07'!
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 |
|
create_user Link
create_user(username, fullname, password)
Create a new user and store the user information in a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username
|
str
|
The username of the user to be created. This will be converted to lowercase. |
required |
fullname
|
str
|
The full name of the user to be created. |
required |
password
|
str
|
The password of the user to be created. |
required |
Examples:
>>> from plantdb.commons.fsdb import FSDB
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db()
>>> db.create_user('batman', "Bruce Wayne", "joker")
>>> db.connect('batman', 'joker')
>>> print(db.user)
batman
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
|
delete_scan Link
delete_scan(scan_id)
Delete an existing Scan
from the local database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scan_id
|
str
|
The name of the scan to delete from the local database. |
required |
Raises:
Type | Description |
---|---|
IOError
|
If the |
See Also
plantdb.commons.fsdb._delete_scan
Examples:
>>> from plantdb.commons.fsdb import FSDB
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db()
>>> new_scan = db.create_scan('007')
>>> print(new_scan)
<plantdb.commons.fsdb.Scan object at 0x7f0730b1e390>
>>> db.delete_scan('007')
>>> scan = db.get_scan('007')
>>> print(scan)
None
>>> db.delete_scan('008')
OSError: Invalid id
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 |
|
disconnect Link
disconnect()
Disconnect from the local database.
Handle DB "locking" system by removing the LOCK_FILE_NAME
file from the DB.
Raises:
Type | Description |
---|---|
IOError
|
If the |
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db()
>>> print(db.is_connected)
True
>>> db.disconnect() # clean up (delete) the temporary dummy database
>>> print(db.is_connected)
False
Source code in plantdb/commons/fsdb/core.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 |
|
get_scan Link
get_scan(scan_id, create=False)
Get or create a Scan
instance in the local database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scan_id
|
str
|
The name of the scan dataset to get/create.
It should exist if |
required |
create
|
bool
|
If |
False
|
Raises:
Type | Description |
---|---|
ScanNotFoundError
|
If the |
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db()
>>> db.list_scans()
[]
>>> new_scan = db.get_scan('007', create=True)
>>> print(new_scan)
<plantdb.commons.fsdb.Scan object at **************>
>>> db.list_scans()
['007']
>>> unknown_scan = db.get_scan('unknown')
plantdb.commons.fsdb.ScanNotFoundError: Unknown scan id 'unknown'!
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 |
|
get_scans Link
get_scans(query=None, fuzzy=False, owner_only=True)
Get the list of Scan
instances defined in the local database, possibly filtered using a query
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
dict
|
A query to use to filter the returned list of scans.
The metadata must match given |
None
|
fuzzy
|
bool
|
Whether to use fuzzy matching or not, that is the use of regular expressions. |
False
|
owner_only
|
bool
|
Whether to filter the returned list of scans to only include scans owned by the current user.
Default is |
True
|
Returns:
Type | Description |
---|---|
list of plantdb.commons.fsdb.Scan
|
List of |
See Also
plantdb.commons.fsdb._filter_query
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> db.get_scans()
[<plantdb.commons.fsdb.Scan at *x************>]
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 |
|
list_scans Link
list_scans(query=None, fuzzy=False, owner_only=True)
Get the list of scans in identifiers the local database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
dict
|
A query to use to filter the returned list of scans.
The metadata must match given |
None
|
fuzzy
|
bool
|
Whether to use fuzzy matching or not, that is the use of regular expressions. |
False
|
Returns:
Type | Description |
---|---|
list[str]
|
The list of scan identifiers in the local database. |
See Also
plantdb.commons.fsdb._filter_query
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_scan=True)
>>> db.list_scans()
['myscan_001']
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 |
|
path Link
path()
Get the path to the local database root directory.
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db()
>>> print(db.path())
/tmp/romidb_********
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
865 866 867 868 869 870 871 872 873 874 875 876 |
|
reload Link
reload(scan_id=None)
Reload the database by scanning datasets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scan_id
|
str or list of str
|
The name of the scan(s) to reload. |
None
|
Source code in plantdb/commons/fsdb/core.py
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
|
scan_exists Link
scan_exists(scan_id)
Check if a given scan ID exists in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scan_id
|
str
|
The ID of the scan to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_scan=True)
>>> db.scan_exists("myscan_001")
True
>>> db.scan_exists("nonexistent_id")
False
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
|
user_exists Link
user_exists(username)
Check if the user exists in the local database.
Source code in plantdb/commons/fsdb/core.py
551 552 553 |
|
validate_user Link
validate_user(username, password)
Validate the user login.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username
|
str
|
The username provided by the user attempting to log in. |
required |
password
|
str
|
The password provided by the user attempting to log in. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Raises:
Type | Description |
---|---|
KeyError
|
If there is an issue accessing necessary user data. |
Source code in plantdb/commons/fsdb/core.py
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
|
File Link
File(fileset, f_id, **kwargs)
Bases: File
Implement File
for the local File System DataBase from abstract class db.File
.
Attributes:
Name | Type | Description |
---|---|---|
db |
FSDB
|
Database where to find the fileset. |
fileset |
Fileset
|
Set of files containing the file. |
id |
str
|
Name of the file in the |
filename |
str
|
File name. |
metadata |
dict
|
Dictionary of metadata attached to the file. |
See Also
plantdb.commons.db.File
Notes
File
must be writen using write_raw
or write
methods to exist on disk.
Else they are just referenced in the database!
Contrary to other classes (Scan
& Fileset
) the uniqueness is not checked!
Source code in plantdb/commons/fsdb/core.py
1732 1733 1734 |
|
get_db Link
get_db()
Get parent database instance.
Returns:
Type | Description |
---|---|
DB
|
The parent database instance. |
Source code in plantdb/commons/db.py
466 467 468 469 470 471 472 473 474 |
|
get_fileset Link
get_fileset()
Get parent fileset.
Returns:
Type | Description |
---|---|
Fileset
|
The parent fileset instance. |
Source code in plantdb/commons/db.py
486 487 488 489 490 491 492 493 494 |
|
get_id Link
get_id()
Get file id.
Returns:
Type | Description |
---|---|
str
|
The id of the file instance. |
Source code in plantdb/commons/db.py
456 457 458 459 460 461 462 463 464 |
|
get_metadata Link
get_metadata(key=None, default={})
Get the metadata associated to a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
A key that should exist in the file's metadata. |
None
|
default
|
Any
|
The default value to return if the key do not exist in the metadata.
Default is an empty dictionary |
{}
|
Returns:
Type | Description |
---|---|
any
|
If |
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> from plantdb.commons.fsdb.path_helpers import _file_metadata_path
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset('fileset_001')
>>> f = fs.get_file("test_json")
>>> print(f.get_metadata())
{'random json': True}
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 |
|
get_scan Link
get_scan()
Get parent scan instance.
Returns:
Type | Description |
---|---|
Scan
|
The parent scan instance. |
Source code in plantdb/commons/db.py
476 477 478 479 480 481 482 483 484 |
|
import_file Link
import_file(path)
Import the file from its local path to the current fileset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str or Path
|
The path to the file to import. |
required |
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> from plantdb.commons.fsdb.path_helpers import _file_metadata_path
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset('fileset_001')
>>> file = fs.get_file("test_json")
>>> new_file = fs.create_file('test_json2')
>>> new_file.import_file(file.path())
>>> print(new_file.path().exists())
True
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 |
|
path Link
path()
Get the path to the local file.
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_scan=True, with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> fs.list_files()
['dummy_image', 'test_image', 'test_json']
>>> f = fs.get_file('dummy_image')
>>> f.path()
/tmp/romidb_********/myscan_001/fileset_001/dummy_image.png
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 |
|
read Link
read()
Read the file and return its contents.
Returns:
Type | Description |
---|---|
str
|
The contents of the file. |
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.get_file("test_json")
>>> js = f.read()
>>> print(js) # print the content of the file
{
"Who you gonna call?": "Ghostbuster"
}
>>> # Convert this raw json into a dictionary with dedicated method from `json` library:
>>> import json
>>> js_dict = json.loads(js)
>>> print(js_dict)
{'Who you gonna call?': 'Ghostbuster'}
>>> print(type(js_dict))
<class 'dict'>
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 |
|
read_raw Link
read_raw()
Read the file and return its contents.
Returns:
Type | Description |
---|---|
bytes
|
The contents of the file. |
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.get_file("test_json")
>>> js = f.read_raw()
>>> print(js) # print the raw bytes content
>>> # Convert this raw json into a dictionary with dedicated method from `json` library:
>>> import json
>>> js_dict = json.loads(js)
>>> print(js_dict)
{'Who you gonna call?': 'Ghostbuster'}
>>> print(type(js_dict))
<class 'dict'>
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 |
|
set_metadata Link
set_metadata(data, value=None)
Add a new metadata to the file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
str or dict
|
If a string, a key to address the |
required |
value
|
any
|
The value to assign to |
None
|
Examples:
>>> import json
>>> from plantdb.commons.fsdb import dummy_db
>>> from plantdb.commons.fsdb.path_helpers import _file_metadata_path
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset('fileset_001')
>>> file = fs.get_file("test_json")
>>> file.set_metadata("test", "value")
>>> p = _file_metadata_path(file)
>>> print(p.exists())
True
>>> print(json.load(p.open(mode='r')))
{'random json': True, 'test': 'value'}
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 |
|
store Link
store()
Save changes to the scan main JSON FILE (files.json
).
Source code in plantdb/commons/fsdb/core.py
1837 1838 1839 1840 |
|
write Link
write(data, ext='')
Write a file from data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
str
|
A string representation of the content to write. |
required |
ext
|
str
|
The extension to use to save the file. |
''
|
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> new_f = fs.create_file('file_007')
>>> md = {"Name": "Bond, James Bond"} # Create an example dictionary to save as JSON
>>> import json
>>> data = json.dumps(md)
>>> print(data)
{"Name": "Bond, James Bond"}
>>> print(type(data))
<class 'str'>
>>> new_f.write(data, 'json')
>>> print([f.name for f in fs.path().iterdir()])
['dummy_image.png', 'test_json.json', 'test_image.png', 'file_007.json']
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 |
|
write_raw Link
write_raw(data, ext='')
Write a file from raw byte data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
bytes
|
The raw byte content to write. |
required |
ext
|
str
|
The extension to use to save the file. |
''
|
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> new_f = fs.create_file('file_007')
>>> md = {"Name": "Bond, James Bond"} # Create an example dictionary to save as JSON
>>> import json
>>> data = json.dumps(md).encode()
>>> print(data)
b'{"Name": "Bond, James Bond"}'
>>> new_f.write_raw(data, 'json')
>>> print([f.name for f in fs.path().iterdir()])
['dummy_image.png', 'test_json.json', 'test_image.png', 'file_007.json']
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 |
|
Fileset Link
Fileset(scan, fs_id)
Bases: Fileset
Implement Fileset
for the local File System DataBase from abstract class db.Fileset
.
Implementation of a fileset as a simple files structure with:
* directory ${FSDB.basedir}/${FSDB.scan.id}/${Fileset.id}
containing set of files;
* directory ${FSDB.basedir}/${FSDB.scan.id}/metadata
containing JSON metadata associated to files;
* JSON file files.json
containing the list of files from fileset;
Attributes:
Name | Type | Description |
---|---|---|
db |
FSDB
|
A local database instance hosting the |
scan |
Scan
|
A scan instance hosting this |
id |
str
|
The identifier of this |
metadata |
dict
|
A metadata dictionary. |
files |
dict[str, File]
|
A dictionary of |
See Also
plantdb.commons.db.Fileset
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scan
|
Scan
|
A scan instance containing the fileset. |
required |
fs_id
|
str
|
The identifier of the fileset instance. |
required |
Source code in plantdb/commons/fsdb/core.py
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 |
|
create_file Link
create_file(f_id)
Create a new File
instance in the local database attached to the current Fileset
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f_id
|
str
|
The name of the file to create. |
required |
Returns:
Type | Description |
---|---|
File
|
The |
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan('myscan_001')
>>> fs = scan.get_fileset('fileset_001')
>>> fs.list_files()
['dummy_image', 'test_image', 'test_json']
>>> new_f = fs.create_file('file_007')
>>> fs.list_files()
['dummy_image', 'test_image', 'test_json', 'file_007']
>>> print([f.name for f in fs.path().iterdir()]) # the file only exist in the database, not on drive!
['dummy_image.png', 'test_json.json', 'test_image.png']
>>> md = {"Name": "Bond, James Bond"} # Create an example dictionary to save as JSON
>>> from plantdb.commons import io
>>> io.write_json(new_f, md, "json") # write the file on drive
>>> print([f.name for f in fs.path().iterdir()])
['file_007.json', 'test_image.png', 'test_json.json', 'dummy_image.png']
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 |
|
delete_file Link
delete_file(f_id)
Delete a given file from the current fileset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fileset_id
|
str
|
Name of the file to delete. |
required |
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan('myscan_001')
>>> fs = scan.get_fileset('fileset_001')
>>> fs.list_files()
['dummy_image', 'test_image', 'test_json']
>>> fs.delete_file('dummy_image')
INFO [plantdb.commons.fsdb] Deleted JSON metadata file for file 'dummy_image' from 'myscan_001/fileset_001'.
INFO [plantdb.commons.fsdb] Deleted file 'dummy_image' from 'myscan_001/fileset_001'.
>>> fs.list_files()
['test_image', 'test_json']
>>> print([f.name for f in fs.path().iterdir()]) # the file has been removed from the drive and the database
['test_json.json', 'test_image.png']
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 |
|
file_exists Link
file_exists(file_id)
Check if a given file ID exists in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_id
|
str
|
The ID of the file to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_scan=True)
>>> scan = db.get_scan('myscan_001')
>>> scan.file_exists("myfile_001")
False
>>> scan.create_file("myfile_001")
>>> scan.file_exists("myfile_001")
True
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 |
|
get_db Link
get_db()
Get parent database instance.
Returns:
Type | Description |
---|---|
DB
|
The parent database instance. |
Source code in plantdb/commons/db.py
307 308 309 310 311 312 313 314 315 |
|
get_file Link
get_file(f_id, create=False)
Get or create a File
instance, of given f_id
, in the current fileset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f_id
|
str
|
Name of the file to get/create. |
required |
create
|
bool
|
If the file exists, this parameter is ignored and the file is returned.
If |
False
|
Returns:
Type | Description |
---|---|
File
|
The retrieved or created file. |
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> f = fs.get_file("test_image")
>>> # To read the file you need to load the right reader from plantdb.commons.io
>>> from plantdb.commons.io import read_image
>>> img = read_image(f)
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 |
|
get_files Link
get_files(query=None, fuzzy=False)
Get the list of File
instances defined in the current fileset, possibly filtered using a query
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
dict
|
Query to use to get a list of files. |
None
|
fuzzy
|
bool
|
Whether to use fuzzy matching or not, that is the use of regular expressions. |
False
|
Returns:
Type | Description |
---|---|
list of plantdb.commons.fsdb.File
|
List of |
See Also
plantdb.commons.fsdb._filter_query
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan('myscan_001')
>>> fs = scan.get_fileset('fileset_001')
>>> fs.get_files()
[<plantdb.commons.fsdb.File at *x************>,
<plantdb.commons.fsdb.File at *x************>,
<plantdb.commons.fsdb.File at *x************>]
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 |
|
get_id Link
get_id()
Get the fileset instance id.
Returns:
Type | Description |
---|---|
str
|
The id of the fileset instance. |
Source code in plantdb/commons/db.py
297 298 299 300 301 302 303 304 305 |
|
get_metadata Link
get_metadata(key=None, default={})
Get the metadata associated to a fileset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
A key that should exist in the fileset's metadata. |
None
|
default
|
Any
|
The default value to return if the key do not exist in the metadata.
Default is an empty dictionary |
{}
|
Returns:
Type | Description |
---|---|
any
|
If |
Examples:
>>> import json
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset('fileset_001')
>>> fs.set_metadata("test", "value")
>>> print(fs.get_metadata("test"))
'value'
>>> db.dummy=False # to avoid cleaning up the temporary dummy database
>>> db.disconnect()
>>> db.connect('anonymous')
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset('fileset_001')
>>> print(fs.get_metadata("test"))
'value'
>>> db.dummy=True
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 |
|
get_scan Link
get_scan()
Get parent scan instance.
Returns:
Type | Description |
---|---|
Scan
|
The parent scan instance. |
Source code in plantdb/commons/db.py
317 318 319 320 321 322 323 324 325 |
|
list_files Link
list_files(query=None, fuzzy=False)
Get the list of files identifiers in the fileset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
dict
|
A query to use to filter the returned list of files.
The metadata must match given |
None
|
fuzzy
|
bool
|
Whether to use fuzzy matching or not, that is the use of regular expressions. |
False
|
Returns:
Type | Description |
---|---|
list[str]
|
The list of file identifiers in the fileset. |
See Also
plantdb.commons.fsdb._filter_query
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_scan=True, with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset("fileset_001")
>>> fs.list_files()
['dummy_image', 'test_image', 'test_json']
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 |
|
path Link
path()
Get the path to the local fileset.
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_scan=True, with_file=True)
>>> [scan.id for scan in db.get_scans()] # list scan ids found in database
['myscan_001']
>>> scan = db.get_scan("myscan_001")
>>> print(scan.path())
/tmp/romidb_********/myscan_001
>>> [fs.id for fs in scan.get_filesets()] # list fileset ids found in scan
['fileset_001']
>>> fs = scan.get_fileset("fileset_001")
>>> print(fs.path())
/tmp/romidb_********/myscan_001/fileset_001
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 |
|
set_metadata Link
set_metadata(data, value=None)
Add a new metadata to the fileset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
str or dict
|
If a string, a key to address the |
required |
value
|
any
|
The value to assign to |
None
|
Examples:
>>> import json
>>> from plantdb.commons.fsdb import dummy_db
>>> from plantdb.commons.fsdb.path_helpers import _fileset_metadata_json_path
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> fs = scan.get_fileset('fileset_001')
>>> fs.set_metadata("test", "value")
>>> p = _fileset_metadata_json_path(fs)
>>> print(p.exists())
True
>>> print(json.load(p.open(mode='r')))
{'test': 'value'}
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 |
|
store Link
store()
Save changes to the scan main JSON FILE (files.json
).
Source code in plantdb/commons/fsdb/core.py
1642 1643 1644 1645 |
|
Scan Link
Scan(db, scan_id)
Bases: Scan
Implement Scan
for the local File System DataBase from abstract class db.Scan
.
Implementation of a scan as a simple file structure with:
* directory ${Scan.db.basedir}/${Scan.db.id}
as scan root directory;
* (OPTIONAL) directory ${Scan.db.basedir}/${Scan.db.id}/metadata
containing JSON metadata file
* (OPTIONAL) JSON file metadata.json
with Scan metadata
Attributes:
Name | Type | Description |
---|---|---|
db |
FSDB
|
A local database instance hosting this |
id |
str
|
The identifier of this |
metadata |
dict
|
A metadata dictionary. |
filesets |
dict[str, Fileset]
|
A dictionary of |
Notes
Optional directory metadata
& JSON file metadata.json
are found when using method set_metadata()
.
See Also
plantdb.commons.db.Scan
Examples:
>>> import os
>>> from plantdb.commons.fsdb import Scan
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db()
>>> # Example #1: Initialize a `Scan` object using an `FSBD` object:
>>> scan = Scan(db, '007')
>>> print(type(scan))
<class 'plantdb.commons.fsdb.Scan'>
>>> print(scan.path()) # the obtained path should be different as the path to the created `dummy_db` change...
/tmp/romidb_j0pbkoo0/007
>>> print(db.get_scan('007')) # Note that it did NOT create this `Scan` in the database!
None
>>> print(os.listdir(db.path())) # And it is NOT found under the `basedir` directory
['romidb']
>>> # HOWEVER if you add metadata to the `Scan` object:
>>> scan.set_metadata({'Name': "Bond... James Bond!"})
>>> print(scan.metadata)
{'Name': 'Bond... James Bond!'}
>>> print(db.get_scan('007')) # The `Scan` is still not found in the database!
None
>>> print(os.listdir(db.path())) # BUT it is now found under the `basedir` directory
['007', 'romidb']
>>> print(os.listdir(os.path.join(db.path(), scan.id))) # Same goes for the metadata
['metadata']
>>> print(os.listdir(os.path.join(db.path(), scan.id, "metadata"))) # Same goes for the metadata
>>> db.disconnect() # clean up (delete) the temporary dummy database
>>> # Example #2: Get it from an `FSDB` object:
>>> db = dummy_db()
>>> scan = db.get_scan('007', create=True)
>>> print(type(scan))
<class 'plantdb.commons.fsdb.Scan'>
>>> print(db.get_scan('007')) # This time the `Scan` object is found in the `FSBD`
<plantdb.commons.fsdb.Scan object at 0x7f34fc860fd0>
>>> print(os.listdir(db.path())) # And it is found under the `basedir` directory
['007', 'romidb']
>>> print(os.listdir(os.path.join(db.path(), scan.id))) # Same goes for the metadata
['metadata']
>>> db.dummy = False # to avoid cleaning up the
>>> db.disconnect()
>>> # When reconnecting to db, if created scan is EMPTY (no Fileset & File) it is not found!
>>> db.connect()
>>> print(db.get_scan('007'))
None
>>> db.dummy = True # to clean up the temporary dummy database
>>> db.disconnect() # clean up (delete) the temporary dummy database
>>> # Example #3: Use an existing database:
>>> from os import environ
>>> from plantdb.commons.fsdb import FSDB
>>> db = FSDB(environ.get('ROMI_DB', "/data/ROMI/DB/"))
>>> db.connect(unsafe=True)
>>> scan = db.get_scan('sango_90_300_36')
>>> scan.get_metadata()
Scan dataset constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db
|
FSDB
|
The database to put/find the scan dataset. |
required |
scan_id
|
str
|
The scan dataset name, should be unique in the |
required |
Source code in plantdb/commons/fsdb/core.py
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 |
|
create_fileset Link
create_fileset(fs_id)
Create a new Fileset
instance in the local database attached to the current Scan
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fs_id
|
str
|
The name of the fileset to create. It should not exist in the current |
required |
Returns:
Type | Description |
---|---|
Fileset
|
The |
Raises:
Type | Description |
---|---|
IOError
|
If the |
See Also
plantdb.commons.fsdb._is_valid_id plantdb.commons.fsdb._make_fileset
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> scan = db.get_scan('myscan_001')
>>> scan.list_filesets()
['fileset_001']
>>> new_fs = scan.create_fileset('fs_007')
>>> scan.list_filesets()
['fileset_001', 'fs_007']
>>> wrong_fs = scan.create_fileset('fileset_001')
OSError: Given fileset identifier 'fileset_001' already exists!
>>> wrong_fs = scan.create_fileset('fileset/001')
OSError: Invalid fileset identifier 'fileset/001'!
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 |
|
delete_fileset Link
delete_fileset(fs_id)
Delete a given fileset from the scan dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fs_id
|
str
|
Name of the fileset to delete. |
required |
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan('myscan_001')
>>> scan.list_filesets()
['fileset_001']
>>> scan.delete_fileset('fileset_001')
>>> scan.list_filesets()
[]
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 |
|
fileset_exists Link
fileset_exists(fileset_id)
Check if a given fileset ID exists in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fileset_id
|
str
|
The ID of the fileset to check. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_scan=True)
>>> scan = db.get_scan('myscan_001')
>>> scan.fileset_exists("myfileset_001")
False
>>> scan.create_fileset("myfileset_001")
>>> scan.fileset_exists("myfileset_001")
True
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 |
|
get_db Link
get_db()
Get parent database instance.
Returns:
Type | Description |
---|---|
DB
|
Database instance where to find the scan. |
Source code in plantdb/commons/db.py
163 164 165 166 167 168 169 170 171 |
|
get_fileset Link
get_fileset(fs_id, create=False)
Get or create a Fileset
instance, of given id
, in the current scan dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fs_id
|
str
|
The name of the fileset to get/create.
It should exist if |
required |
create
|
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
Fileset
|
The retrieved or created fileset. |
Notes
If the id
do not exist in the local database and create
is False
, None
is returned.
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> scan = db.get_scan('myscan_001')
>>> scan.list_filesets()
['fileset_001']
>>> new_fileset = scan.get_fileset('007', create=True)
>>> print(new_fileset)
<plantdb.commons.fsdb.Fileset object at **************>
>>> scan.list_filesets()
['fileset_001', '007']
>>> unknown_fs = scan.get_fileset('unknown')
plantdb.commons.fsdb.FilesetNotFoundError: Unknown fileset id 'unknown'!
>>> print(unknown_fs)
None
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 |
|
get_filesets Link
get_filesets(query=None, fuzzy=False)
Get the list of Fileset
instances defined in the current scan dataset, possibly filtered using a query
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
dict
|
A query to use to filter the returned list of files.
The metadata must match given |
None
|
fuzzy
|
bool
|
Whether to use fuzzy matching or not, that is the use of regular expressions. |
False
|
Returns:
Type | Description |
---|---|
list of plantdb.commons.fsdb.Fileset
|
List of |
See Also
plantdb.commons.fsdb._filter_query
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_fileset=True)
>>> scan = db.get_scan('myscan_001')
>>> scan.get_filesets()
[<plantdb.commons.fsdb.Fileset at *x************>]
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 |
|
get_id Link
get_id()
Get the scan instance id.
Returns:
Type | Description |
---|---|
str
|
Id of the scan instance. |
Source code in plantdb/commons/db.py
153 154 155 156 157 158 159 160 161 |
|
get_measures Link
get_measures(key=None)
Get the manual measurements associated to a scan.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
A key that should exist in the scan's manual measurements. |
None
|
Returns:
Type | Description |
---|---|
any
|
If |
Notes
These manual measurements should be a JSON file named measures.json
.
It is located at the root folder of the scan dataset.
Source code in plantdb/commons/fsdb/core.py
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 |
|
get_metadata Link
get_metadata(key=None, default={})
Get the metadata associated to a scan.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
A key that should exist in the scan's metadata. |
None
|
default
|
Any
|
The default value to return if the key do not exist in the metadata.
Default is an empty dictionary |
{}
|
Returns:
Type | Description |
---|---|
any
|
If |
Source code in plantdb/commons/fsdb/core.py
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 |
|
list_filesets Link
list_filesets(query=None, fuzzy=False)
Get the list of filesets identifiers in the scan dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query
|
dict
|
A query to use to filter the returned list of filesets.
The metadata must match given |
None
|
fuzzy
|
bool
|
Whether to use fuzzy matching or not, that is the use of regular expressions. |
False
|
Returns:
Type | Description |
---|---|
list[str]
|
The list of filesets identifiers in the scan dataset. |
See Also
plantdb.commons.fsdb._filter_query
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> scan.list_filesets()
['fileset_001']
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 |
|
path Link
path()
Get the path to the local scan dataset.
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> scan.path() # should be '/tmp/romidb_********/myscan_001'
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 |
|
set_metadata Link
set_metadata(data, value=None)
Add a new metadata to the scan.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
str or dict
|
If a string, a key to address the |
required |
value
|
any
|
The value to assign to |
None
|
Examples:
>>> import json
>>> from plantdb.commons.fsdb import dummy_db
>>> from plantdb.commons.fsdb.path_helpers import _scan_metadata_path
>>> db = dummy_db(with_file=True)
>>> scan = db.get_scan("myscan_001")
>>> scan.set_metadata("test", "value")
>>> p = _scan_metadata_path(scan)
>>> print(p.exists())
True
>>> print(json.load(p.open(mode='r')))
{'test': 'value'}
>>> db.disconnect() # clean up (delete) the temporary dummy database
Source code in plantdb/commons/fsdb/core.py
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 |
|
store Link
store()
Save changes to the scan main JSON FILE (files.json
).
Source code in plantdb/commons/fsdb/core.py
1259 1260 1261 1262 |
|
dummy_db Link
dummy_db(with_scan=False, with_fileset=False, with_file=False)
Create a dummy temporary database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
with_scan
|
bool
|
If |
False
|
with_fileset
|
bool
|
If |
False
|
with_file
|
bool
|
If
|
False
|
Returns:
Type | Description |
---|---|
FSDB
|
The dummy database. |
Notes
- Returns a 'connected' database, no need to call the
connect()
method. - Uses the 'anonymous' user to login.
Examples:
>>> from plantdb.commons.fsdb import dummy_db
>>> db = dummy_db(with_file=True)
>>> db.connect()
INFO [plantdb.commons.fsdb] Already connected as 'anonymous' to the database '/tmp/romidb_********'!
>>> print(db.path()) # the database directory
/tmp/romidb_********
>>> print(db.list_scans())
['myscan_001']
>>> scan = db.get_scan("myscan_001") # get the existing scan
>>> print(scan.list_filesets())
['fileset_001']
>>> fs = scan.get_fileset("fileset_001")
>>> print(list(fs.list_files()))
['dummy_image', 'test_image', 'test_json']
>>> f = fs.get_file("test_image")
>>> print(f.path())
/tmp/romidb_********/myscan_001/fileset_001/test_image.png
>>> db.disconnect() # clean up (delete) the temporary dummy database
>>> print(db.path().exists())
False
Source code in plantdb/commons/fsdb/core.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|