Skip to content

Commit

Permalink
Merge pull request #39 from SciCatProject/background-ingestor
Browse files Browse the repository at this point in the history
Background ingestor
  • Loading branch information
YooSunYoung authored Jun 27, 2024
2 parents f1ec799 + 8a2da22 commit f228982
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
88 changes: 88 additions & 0 deletions src/background-ingestor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 ScicatProject contributors (https://github.com/ScicatProject)
import json
import logging
from collections.abc import Generator
from contextlib import contextmanager

from scicat_configuration import (
build_background_ingestor_arg_parser,
build_scicat_config,
)
from scicat_logging import build_logger

# import scippnexus as snx


def quit(logger: logging.Logger, unexpected: bool = True) -> None:
"""Log the message and exit the program."""
import sys

logger.info("Exiting ingestor")
sys.exit(1 if unexpected else 0)


@contextmanager
def exit_at_exceptions(logger: logging.Logger) -> Generator[None, None, None]:
"""Exit the program if an exception is raised."""
try:
yield
except KeyboardInterrupt:
logger.info("Received keyboard interrupt.")
quit(logger, unexpected=False)
except Exception as e:
logger.error("An exception occurred: %s", e)
quit(logger, unexpected=True)
else:
logger.error("Loop finished unexpectedly.")
quit(logger, unexpected=True)


def main() -> None:
"""Main entry point of the app."""
arg_parser = build_background_ingestor_arg_parser()
arg_namespace = arg_parser.parse_args()
config = build_scicat_config(arg_namespace)
logger = build_logger(config)

# Log the configuration as dictionary so that it is easier to read from the logs
logger.info(
'Starting the Scicat background Ingestor with the following configuration:'
)
logger.info(config.to_dict())

with exit_at_exceptions(logger):
nexus_file = arg_namespace.nexus_file
logger.info("Nexus file to be ingested : ")
logger.info(nexus_file)

done_writing_message_file = (
arg_namespace.arg_namespace.done_writing_message_file
)
logger.info("Done writing message file linked to nexus file : ")
logger.info(done_writing_message_file)

# open and read done writing message input file
with open(done_writing_message_file, 'r') as f:
done_writing_message = json.load(f)

print(done_writing_message)
# open nexus file
# nxs = snx.File(nexus_file)

# extract instrument

# load instrument metadata configuration

# retrieve information regarding the proposal

# extract and prepare metadata

# create b2blake hash of all the files

# create and populate scicat dataset entry

# create and populate scicat origdatablock entry
# with files and hashes previously computed

pass
33 changes: 33 additions & 0 deletions src/scicat_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@ def build_main_arg_parser() -> argparse.ArgumentParser:
return parser


def build_background_ingestor_arg_parser() -> argparse.ArgumentParser:
parser = build_main_arg_parser()

group = parser.add_argument_group('Scicat Background Ingestor Options')

group.add_argument(
'-f',
'--nf',
'--file',
'--nexus-file',
default='',
dest='nexus_file',
help='Full path of the input nexus file to be ingested',
type=str,
)

group.add_argument(
'-m',
'--dm',
'--wrdm',
'--done-writing-message-file',
default='',
dest='done_writing_message_file',
help="""
Full path of the input done writing message
file that match the nexus file to be ingested
""",
type=str,
)

return parser


@dataclass
class GraylogOptions:
host: str = ""
Expand Down
2 changes: 1 addition & 1 deletion src/scicat_ingestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def main() -> None:
logger = build_logger(config)

# Log the configuration as dictionary so that it is easier to read from the logs
logger.info('Starting the Scicat Ingestor with the following configuration:')
logger.info('Starting the Scicat online Ingestor with the following configuration:')
logger.info(config.to_dict())

with exit_at_exceptions(logger):
Expand Down

0 comments on commit f228982

Please sign in to comment.