Template
#!/usr/bin/env python3 # coding: utf-8 import sys import argparse import time import logging import locale LOG_LEVEL = logging.DEBUG def do(): pass def main(**kwargs): logger = _get_logger(name=__file__) start = time.time() logger.info("Script execution STARTED.") try: defined_params = {k: v for k, v in kwargs.items() if v is not None} file_name = defined_params.pop("input_file", "text.txt") do() except Exception as e: logger.error(f"Script execution FAILED.", exc_info=True) return 1 else: end = time.time() logger.info(f"Script execution FINALIZED in {end - start} seconds.") return 0 def _get_logger(name=__name__, handler=logging.StreamHandler(sys.stdout)): logger = logging.getLogger(name) try: locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') except locale.Error as e: logger.debug("Ignoring error %s when setting locale", e) try: logger.setLevel(LOG_LEVEL) except NameError: logger.setLevel(logging.INFO) if not len(logger.handlers): handler = handler formater = logging.Formatter( "%(asctime)s — %(name)s — %(levelname)s — %(message)s") handler.setFormatter(formater) logger.addHandler(handler) return logger def _parse_args(): parser = argparse.ArgumentParser( description="Load data from CSV file or newline delimited JSON file to a running Elasticsearch cluster.", epilog=f"Example: python3 {__file__} my-data.csv my-elastic-index") parser.add_argument( 'input_file', help='Path to the input csv/json file. Extension check on .csv or .json/.log') args = parser.parse_args() return vars(args) if __name__ == "__main__": sys.exit(main(**_parse_args()))
# https://editorconfig.org/ root = true [*] insert_final_newline = true trim_trailing_whitespace = true end_of_line = lf charset = utf-8 # Docstrings and comments use max_line_length = 79 [*.py] indent_style = space indent_size = 4 max_line_length = 119 [*.sh] indent_style = tab indent_size = 4 # The JSON files contain newlines inconsistently [*.json] indent_size = 2 insert_final_newline = ignore [*.{yml,yaml}] indent_style = space indent_size = 2 # Markdown Files [*.md] trim_trailing_whitespace = false