#!/usr/bin/env python3
"""
name   : rename_sdf_number_compounds.py
author : nbehrnd@yahoo.com
license: GPL v2
date   : [2024-12-18 Wed]
edit   :
purpose: edit .sdf files' filename to note the number of compounds present

Every compound DataWarrior stores in a .sdf file closes with a line of `$$$$`.
The number of compounds hence equates to the number of these lines.
"""

import argparse
import re
import shutil


def get_args():
    """Get command-line arguments"""
    parser = argparse.ArgumentParser(
        description="""
Edit .sdf filenames to indicate the number of compounds present.  For instance,
file `example.sdf` is copied as `example_5_compounds.sdf`.""",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )

    parser.add_argument(
        "file",
        help="Indicate one, or (Linux:) multiple .sdf files to rename.",
        metavar="FILE",
        type=argparse.FileType("rt"),
        nargs="+",
        default=None,
    )

    parser.add_argument(
        "-v",
        "--verbose",
        help="toggle on a more verbose file processing",
        action="store_true",
    )

    return parser.parse_args()


def analyze_one_sdf_file(input_file, verbosity_level):
    """determine the number of compounds in a dwar file"""
    number_of_compounds = 0
    if verbosity_level:
        print(f"work on: {input_file}")

    try:
        with open(input_file, mode="r", encoding="utf-8") as source:
            for line in source:
                if str("$$$$") in str(line):
                    number_of_compounds += 1
    except OSError as e:
        print(e)

    return number_of_compounds


def main():
    """Join the functionalities"""
    args = get_args()
    file_arg = args.file  # which is a list about one, or multiple files
    verbosity = args.verbose

    for file in file_arg:
        if str(file.name).endswith(".sdf"):
            number_of_compounds = analyze_one_sdf_file(file.name, verbosity)

            new_name = re.sub(
                ".sdf",
                "".join(["_", str(number_of_compounds), "_compounds.sdf"]),
                file.name,
            )
            try:
                shutil.copyfile(file.name, new_name)
            except OSError as e:
                print(e)


if __name__ == "__main__":
    main()
