Skip to content

fsdb_import_folder

FSDB Folder Import CLILink

A command‑line utility that imports a folder's contents as a Fileset in a known Scan dataset, optionally attaching metadata and providing configurable logging. It streamlines adding new data to a PlantDB database directly from the terminal.

Key FeaturesLink

  • Simple CLI interface using click with positional arguments for the target scan and source folder.
  • Optional metadata: load a JSON file and associate its contents with the imported fileset.
  • Configurable logging: choose from predefined log levels to control output verbosity.
  • Automatic database handling: connects to the FSDB, creates the necessary scan/fileset hierarchy, imports the files, applies metadata, and cleanly disconnects.
  • Login support: authenticate with username/password or use no-auth for testing.

Usage ExamplesLink

Basic import of a folder into an existing scanLink

fsdb_import_folder /romi_db/scan001 /path/to/folder_with_files

Import with metadata and verbose loggingLink

fsdb_import_folder /romi_db/scan001 /path/to/folder_with_files     --metadata /path/to/folder_meta.json     --log-level INFO

main Link

main(scan_path, folder, metadata, user, password, no_auth, log_level)

FSDB Folder Import CLI

A command‑line utility that imports a folder's contents as a Fileset in a known Scan dataset, optionally attaching metadata and providing configurable logging.

SCAN is the path to the scan that will receive the imported fileset. FOLDER is the path to the source folder containing files to import.

Source code in plantdb/commons/cli/fsdb_import_folder.py
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
@click.command(context_settings=dict(help_option_names=["-h", "--help"]))
@click.argument('scan_path', type=click.Path(exists=True))
@click.argument('folder', type=click.Path(exists=True))
@click.option(
    '--metadata',
    type=click.Path(exists=True),
    default=None,
    help='Path to a JSON file with fileset related metadata.'
)
@optgroup.group("Login", cls=OptionGroup)
@optgroup.option('-u', '--user', type=str, default=None,
                 help="Username for FSDB login.")
@optgroup.option('-p', '--password', type=str, default=None,
                 help="Password for FSDB login.")
@optgroup.option('--no-auth', is_flag=True, default=False,
                 help="Use a database with automatic 'admin' user log in, for testing purposes only.")
@optgroup.group("Logging", cls=OptionGroup)
@optgroup.option(
    "--log-level",
    type=click.Choice(LOG_LEVELS, case_sensitive=False),
    default=DEFAULT_LOG_LEVEL,
    show_default=True,
    help="Logging level.",
)
def main(scan_path, folder, metadata, user, password, no_auth, log_level):
    """FSDB Folder Import CLI

    A command‑line utility that imports a folder's contents as a ``Fileset`` in a known ``Scan`` dataset, optionally
    attaching metadata and providing configurable logging.

    SCAN is the path to the scan that will receive the imported fileset.
    FOLDER is the path to the source folder containing files to import.
    """
    # Get the logger and change the level if needed:
    logger = get_logger(os.environ.get('ROMI_APP_LOGGER', __name__))
    logger.setLevel(log_level)

    if not (no_auth or (user and password)):
        raise click.UsageError("Requires using either the --no-auth flag or using both --user and --password")

    # Load metadata if a path is provided
    if metadata is not None:
        with open(metadata, "r", encoding="utf-8") as f:
            metadata = json.load(f)

    scan_path = Path(scan_path)
    scan_id = scan_path.name
    db_path = scan_path.parent

    # Initialize the database
    db = FSDB(db_path, no_auth=no_auth)
    db.connect()

    # Authenticate unless explicitly disabled
    if not no_auth and (user and password):
        db.login(user, password)

    folder_path = Path(folder).resolve()
    fileset_id = folder_path.name

    fileset = scan_path.create_fileset(fileset_id)
    try:
        for f in os.listdir(folder_path):
            if os.path.isfile(os.path.join(folder_path, f)):
                fi = fileset.create_file(os.path.splitext(f)[0])
                fi.import_file(os.path.join(folder_path, f))
    except Exception as e:
        scan_path.delete_fileset(fileset_id)
        raise e

    if metadata is not None:
        fileset.set_metadata(metadata)

    db.disconnect()