| 49 | |
| 50 | |
| 51 | def main(): |
| 52 | parser = _build_parser() |
| 53 | args = parser.parse_args() |
| 54 | |
| 55 | if args.input and args.output: |
| 56 | try: |
| 57 | if os.path.samefile(args.input, args.output): |
| 58 | print('opencc: error: input and output refer to the same file', file=sys.stderr) |
| 59 | sys.exit(1) |
| 60 | except FileNotFoundError: |
| 61 | pass |
| 62 | |
| 63 | converter = opencc.OpenCC( |
| 64 | args.config, |
| 65 | include_tofu_risk_dictionaries=args.include_tofu_risk_dictionaries, |
| 66 | resource_zip=args.resource_zip, |
| 67 | ) |
| 68 | |
| 69 | if args.input: |
| 70 | with open(args.input, 'rb') as f: |
| 71 | text = f.read().decode('utf-8') |
| 72 | else: |
| 73 | text = sys.stdin.buffer.read().decode('utf-8') |
| 74 | |
| 75 | result = converter.convert(text) |
| 76 | |
| 77 | if args.output: |
| 78 | with open(args.output, 'wb') as f: |
| 79 | f.write(result.encode('utf-8')) |
| 80 | else: |
| 81 | sys.stdout.buffer.write(result.encode('utf-8')) |
| 82 | |
| 83 | |
| 84 | if __name__ == '__main__': |