()
| 54 | |
| 55 | |
| 56 | def main(): |
| 57 | parser = argparse.ArgumentParser( |
| 58 | description= |
| 59 | "Replace content in files based on a YAML-defined dictionary.") |
| 60 | parser.add_argument("file_paths", |
| 61 | nargs="+", |
| 62 | help="Paths to the files to be processed.") |
| 63 | parser.add_argument( |
| 64 | "--config", |
| 65 | help= |
| 66 | "Path to the YAML file containing the replacement dictionary and check pattern." |
| 67 | ) |
| 68 | parser.add_argument("-i", |
| 69 | "--inplace", |
| 70 | action="store_true", |
| 71 | help="Write changes back to the file.") |
| 72 | args = parser.parse_args() |
| 73 | |
| 74 | replacement_dict = {} |
| 75 | check_pattern = None |
| 76 | |
| 77 | if args.config: |
| 78 | with open(args.config, 'r', encoding='utf-8') as file: |
| 79 | config = yaml.safe_load(file) or {} |
| 80 | replacement_dict = config.get("mapping", {}) |
| 81 | check_pattern = config.get("check") |
| 82 | |
| 83 | # Compile the regex pattern for bytes if provided |
| 84 | check_regex = re.compile( |
| 85 | check_pattern.encode('utf-8')) if check_pattern else None |
| 86 | |
| 87 | success = True |
| 88 | for file_path in args.file_paths: |
| 89 | file_success = replace_content(file_path=file_path, |
| 90 | replacement_dict=replacement_dict, |
| 91 | inplace=args.inplace, |
| 92 | check_regex=check_regex) |
| 93 | if not file_success: |
| 94 | success = False |
| 95 | |
| 96 | return success |
| 97 | |
| 98 | |
| 99 | if __name__ == "__main__": |
no test coverage detected