def main(): parsed_args = parse_args() settings = collect_settings(parsed_args) workspace = settings["workspace"] link_file = settings["link_file"] # setup logger logging_config = settings["logging_config"] logging.basicConfig(level=logging_config["level"], format=logging_config["format"]) logger = logging.getLogger() with tempfile.TemporaryDirectory() as tmpdirname: temp_workspace = Path(tmpdirname) / workspace.stem for input_file in settings["input_files"]: try: convert_one_file(input_file, temp_workspace, link_file) except Exception: logger.exception("Error while converting %s file", input_file) if settings["output_format"] == RESULT_TYPE_FOLDER: shutil.rmtree(str(workspace), ignore_errors=True) shutil.copytree(str(temp_workspace), str(workspace)) if settings["output_format"] == RESULT_TYPE_ZIP: shutil.make_archive(str(workspace), "zip", str(temp_workspace)) logger.info("Conversion completed") return 0
def main(): parsed_args = parse_args() settings = collect_settings(parsed_args) workspace = settings["workspace"] # setup logger logging_config = settings["logging_config"] logging.basicConfig(level=logging_config["level"], format=logging_config["format"]) with tempfile.TemporaryDirectory() as tmpdirname: temp_workspace = Path(tmpdirname) / workspace.stem for input_file in settings["input_files"]: try: convert_one_file(input_file, temp_workspace) except Exception: traceback.print_exc() if settings["output_format"] == RESULT_TYPE_FOLDER: shutil.rmtree(str(workspace), ignore_errors=True) shutil.copytree(str(temp_workspace), str(workspace)) if settings["output_format"] == RESULT_TYPE_ZIP: shutil.make_archive(str(workspace), "zip", str(temp_workspace)) return 0
def test_parse_args_csv_file(imscc_file, link_map_csv): """ Basic cli test with csv file parsed. """ parsed_args = parse_args(["-i", str(imscc_file), "-f", link_map_csv]) assert parsed_args == Namespace(inputs=[imscc_file], loglevel="INFO", result="folder", link_file=link_map_csv)
def settings(imscc_file): """ Basic settings fixture. """ parsed_args = parse_args(["-i", str(imscc_file)]) _settings = collect_settings(parsed_args) yield _settings shutil.rmtree(_settings["workspace"], ignore_errors=True)
def test_parse_args_passport_file(imscc_file, passports_csv): """ Input test for lti passport csv """ parsed_args = parse_args(["-i", str(imscc_file), "-p", passports_csv]) assert parsed_args == Namespace( inputs=[imscc_file], loglevel="INFO", result="folder", link_file=None, passport_file=passports_csv, output="output", )
def test_parse_args(imscc_file): """ Basic cli test. """ parsed_args = parse_args([ "-i", str(imscc_file), ]) assert parsed_args == Namespace(inputs=[imscc_file], loglevel="INFO", result="folder", link_file=None)
def test_collect_settings(imscc_file): parsed_args = parse_args(["-i", str(imscc_file)]) settings = collect_settings(parsed_args) assert settings == { "input_files": {imscc_file}, "output_format": parsed_args.result, "workspace": Path.cwd() / "output", "link_file": None, "logging_config": { "level": parsed_args.loglevel, "format": "{%(filename)s:%(lineno)d} - %(message)s", }, }