#!/usr/bin/env python3
# (C) 2026 Cadence Design Systems, Inc. (Cadence)
# All rights reserved.
# TERMS FOR USE OF SAMPLE CODE The software below ("Sample Code") is
# provided to current licensees or subscribers of Cadence products or
# SaaS offerings (each a "Customer").
# Customer is hereby permitted to use, copy, and modify the Sample Code,
# subject to these terms. Cadence claims no rights to Customer's
# modifications. Modification of Sample Code is at Customer's sole and
# exclusive risk. Sample Code may require Customer to have a then
# current license or subscription to the applicable Cadence offering.
# THE SAMPLE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED.  OPENEYE DISCLAIMS ALL WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. In no event shall Cadence be
# liable for any damages or liability in connection with the Sample Code
# or its use.


"""Code snippet for outputting all molecule titles from a database."""

import os
import sys

import rich.console
from openeye import oechem


def main() -> int:
    """Output all molecule titles."""
    if len(sys.argv) != 2:  # noqa: PLR2004
        oechem.OEThrow.Usage(f"Usage: {sys.argv[0]} <mol-file>")

    ifs = oechem.oemolistream()
    if not ifs.open(sys.argv[1]):
        oechem.OEThrow.Fatal(f"Unable to open {sys.argv[1]} for reading")

    console = rich.console.Console()
    output_mol_titles(ifs, console)
    return os.EX_OK


def output_mol_titles(ifs: oechem.oemolistream, console: rich.console.Console) -> None:
    """Output all molecule titles from a molecule database."""
    mol_database = oechem.OEMolDatabase(ifs)
    for idx in range(mol_database.GetMaxMolIdx()):
        title = mol_database.GetTitle(idx)
        if len(title) == 0:
            title = "untitled"
        console.print(f"{title}")


if __name__ == "__main__":
    sys.exit(main())
