|
| 1 | +# SPDX-License-Identifier: BSD-3-Clause |
| 2 | +# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject) |
| 3 | +import json |
| 4 | +import logging |
| 5 | +from collections.abc import Generator |
| 6 | +from contextlib import contextmanager |
| 7 | + |
| 8 | +from scicat_configuration import ( |
| 9 | + build_background_ingestor_arg_parser, |
| 10 | + build_scicat_config, |
| 11 | +) |
| 12 | +from scicat_logging import build_logger |
| 13 | + |
| 14 | +# import scippnexus as snx |
| 15 | + |
| 16 | + |
| 17 | +def quit(logger: logging.Logger, unexpected: bool = True) -> None: |
| 18 | + """Log the message and exit the program.""" |
| 19 | + import sys |
| 20 | + |
| 21 | + logger.info("Exiting ingestor") |
| 22 | + sys.exit(1 if unexpected else 0) |
| 23 | + |
| 24 | + |
| 25 | +@contextmanager |
| 26 | +def exit_at_exceptions(logger: logging.Logger) -> Generator[None, None, None]: |
| 27 | + """Exit the program if an exception is raised.""" |
| 28 | + try: |
| 29 | + yield |
| 30 | + except KeyboardInterrupt: |
| 31 | + logger.info("Received keyboard interrupt.") |
| 32 | + quit(logger, unexpected=False) |
| 33 | + except Exception as e: |
| 34 | + logger.error("An exception occurred: %s", e) |
| 35 | + quit(logger, unexpected=True) |
| 36 | + else: |
| 37 | + logger.error("Loop finished unexpectedly.") |
| 38 | + quit(logger, unexpected=True) |
| 39 | + |
| 40 | + |
| 41 | +def main() -> None: |
| 42 | + """Main entry point of the app.""" |
| 43 | + arg_parser = build_background_ingestor_arg_parser() |
| 44 | + arg_namespace = arg_parser.parse_args() |
| 45 | + config = build_scicat_config(arg_namespace) |
| 46 | + logger = build_logger(config) |
| 47 | + |
| 48 | + # Log the configuration as dictionary so that it is easier to read from the logs |
| 49 | + logger.info( |
| 50 | + 'Starting the Scicat background Ingestor with the following configuration:' |
| 51 | + ) |
| 52 | + logger.info(config.to_dict()) |
| 53 | + |
| 54 | + with exit_at_exceptions(logger): |
| 55 | + nexus_file = arg_namespace.nexus_file |
| 56 | + logger.info("Nexus file to be ingested : ") |
| 57 | + logger.info(nexus_file) |
| 58 | + |
| 59 | + done_writing_message_file = ( |
| 60 | + arg_namespace.arg_namespace.done_writing_message_file |
| 61 | + ) |
| 62 | + logger.info("Done writing message file linked to nexus file : ") |
| 63 | + logger.info(done_writing_message_file) |
| 64 | + |
| 65 | + # open and read done writing message input file |
| 66 | + with open(done_writing_message_file, 'r') as f: |
| 67 | + done_writing_message = json.load(f) |
| 68 | + |
| 69 | + print(done_writing_message) |
| 70 | + # open nexus file |
| 71 | + # nxs = snx.File(nexus_file) |
| 72 | + |
| 73 | + # extract instrument |
| 74 | + |
| 75 | + # load instrument metadata configuration |
| 76 | + |
| 77 | + # retrieve information regarding the proposal |
| 78 | + |
| 79 | + # extract and prepare metadata |
| 80 | + |
| 81 | + # create b2blake hash of all the files |
| 82 | + |
| 83 | + # create and populate scicat dataset entry |
| 84 | + |
| 85 | + # create and populate scicat origdatablock entry |
| 86 | + # with files and hashes previously computed |
| 87 | + |
| 88 | + pass |
0 commit comments