pipetex.main

Main script for the pipeline application.

This file provides a CLI to the user and executes the pipeline with the specified parameters. It is not responsible for any errorhandling which occur while running the pipeline.

@author: Max Weise created: 11.08.2022

  1"""Main script for the pipeline application.
  2
  3This file provides a CLI to the user and executes the pipeline with the
  4specified parameters. It is not responsible for any errorhandling which occur
  5while running the pipeline.
  6
  7@author: Max Weise
  8created: 11.08.2022
  9"""
 10
 11from pipetex import pipeline
 12
 13
 14import argparse
 15# import coloredlogs
 16import logging
 17
 18
 19def _setup_sysarg_parser() -> argparse.Namespace:
 20    """Creates a namespace which contains the arguments passed by the user.
 21
 22    Define CLI arguments in this function.
 23
 24    Returns:
 25        parser: Namespace holding all CLI arguments.
 26
 27    """
 28    parser = argparse.ArgumentParser()
 29
 30    # === Positional Arguments ===
 31    parser.add_argument(
 32        "filename",
 33        help="Name of file which is processed by the pipeline."
 34    )
 35
 36    # === Optional Arguments and Flags ===
 37    parser.add_argument(
 38        "-v",
 39        help="Turn on console output for the latex engines.",
 40        action="store_true"
 41    )
 42
 43    parser.add_argument(
 44        "-q",
 45        help="Turn the termil log level down to WARNING",
 46        action="store_true"
 47    )
 48
 49    parser.add_argument(
 50        "-b", "--bib",
 51        help="Create a bibliography in the current latex project",
 52        action="store_true"
 53    )
 54
 55    parser.add_argument(
 56        "-g", "--gls",
 57        help="Create a glossary in the current latex project",
 58        action="store_true"
 59    )
 60
 61    return parser.parse_args()
 62
 63
 64def _setup_logger(is_quiet: bool = False,
 65                  log_file_path: str = "log_file.txt") -> logging.Logger:
 66    """Creates the logger instance for the script.
 67
 68    Args:
 69        is_quiet: When specified, the log level of the console handler will
 70            be set to WARNING. Defaults to False.
 71        log_file_path: Path to the logfile where the FileHandler writes the
 72            logs to. If no extra path is given, log_file.txt is used.
 73
 74    Returns:
 75        logger: Logger object which has a Console- and FileHandler
 76            added to it.
 77    """
 78    logger = logging.getLogger("main")
 79    logger.setLevel(logging.DEBUG)
 80
 81    # Create console handler with dynamic log level
 82    stream_level = logging.WARNING if is_quiet else logging.DEBUG
 83    console_handler = logging.StreamHandler()
 84    console_handler.setLevel(stream_level)
 85
 86    # TODO: Create logfiles in seperate folder
 87    #       Each run of the pipeline should create a seperate log file
 88    #       (maybe limit this number to ~10 files in the folder)
 89    #       Include a header for each logfile containing metadata
 90    #       (logfolder needs to be created seperately)
 91    file_handler = logging.FileHandler(log_file_path, encoding="utf-8")
 92    file_handler.setLevel(logging.DEBUG)
 93
 94    # Set format for the logger
 95    frmt = logging.Formatter('[%(name)s - %(levelname)s] - %(message)s')
 96    console_handler.setFormatter(frmt)
 97    file_handler.setFormatter(frmt)
 98
 99    logger.addHandler(console_handler)
100    logger.addHandler(file_handler)
101
102    return logger
103
104
105def main():
106    """Main method of the module."""
107    cli_args = _setup_sysarg_parser()
108    logger = _setup_logger()
109
110    logger.info("Initializing pipeline")
111    p = pipeline.Pipeline(
112        cli_args.filename,
113        create_bib=cli_args.bib,
114        create_glo=cli_args.gls,
115        verbose=cli_args.v,
116        # quiet=cli_args.q
117    )
118
119    logger.info("Starting pipeline")
120    p.execute(p.file_name)
121
122
123if __name__ == "__main__":
124    main()
def main():
106def main():
107    """Main method of the module."""
108    cli_args = _setup_sysarg_parser()
109    logger = _setup_logger()
110
111    logger.info("Initializing pipeline")
112    p = pipeline.Pipeline(
113        cli_args.filename,
114        create_bib=cli_args.bib,
115        create_glo=cli_args.gls,
116        verbose=cli_args.v,
117        # quiet=cli_args.q
118    )
119
120    logger.info("Starting pipeline")
121    p.execute(p.file_name)

Main method of the module.