|
1 | 1 | import pathlib |
| 2 | +import sys |
2 | 3 | from sys import stderr |
3 | 4 |
|
4 | 5 | from .graphviz2drawio import convert |
5 | 6 | from .models import Arguments |
6 | 7 | from .version import __version__ |
7 | 8 |
|
| 9 | +DEFAULT_TEXT = "\033[0m" |
| 10 | +RED_TEXT = "\033[91m" |
| 11 | +BOLD = "\033[1m" |
8 | 12 |
|
9 | | -def main() -> None: |
10 | 13 |
|
11 | | - args = Arguments(__version__).parse_args() # pytype: disable=not-callable |
| 14 | +def _gv_filename_to_xml(filename: str) -> str: |
| 15 | + return ".".join(filename.split(".")[:-1]) + ".xml" |
12 | 16 |
|
13 | | - if not args.stdout: |
14 | | - stderr.write("This is beta software, please report issues to:\n") |
15 | | - stderr.write("https://github.com/hbmartin/graphviz2drawio/issues\n") |
16 | 17 |
|
| 18 | +def _convert_file(to_convert: str, program: str, outfile: str | None) -> None: |
17 | 19 | try: |
18 | | - output = convert(args.to_convert, args.program) |
| 20 | + output = convert(to_convert, program) |
19 | 21 | except BaseException: |
20 | | - stderr.write("Something went wrong, please file an issue.\n") |
21 | | - stderr.write("This tool can automatically report using sentry.io.\n") |
22 | | - stderr.write("The report contains no PII or graph data (only the file path).\n") |
23 | | - permission = input("Type 'no' to cancel report, press enter to send: ") |
24 | | - if permission != "no": |
25 | | - from raven import Client |
26 | | - |
27 | | - client = Client( |
28 | | - dsn="https://f1fce21b51864819a26ea116ff4e5b7f:[email protected]/1266157", |
29 | | - release=__version__, |
30 | | - ) |
31 | | - client.captureException() |
| 22 | + stderr.write(f"{RED_TEXT}{BOLD}Error converting {to_convert}\n") |
| 23 | + stderr.write("https://github.com/hbmartin/graphviz2drawio/issues\n") |
| 24 | + stderr.write("Please include an example of your diagram and the following:\n\n") |
| 25 | + stderr.write(DEFAULT_TEXT) |
| 26 | + stderr.write(f"Python: {sys.version}, g2d: {__version__}\n") |
32 | 27 | raise |
33 | 28 |
|
34 | | - if args.stdout: |
| 29 | + if outfile is None: |
35 | 30 | print(output) |
36 | 31 | return |
37 | 32 |
|
38 | | - if args.outfile is not None: |
39 | | - outfile = args.outfile |
40 | | - else: |
41 | | - base = args.to_convert.split(".") |
42 | | - outfile = ".".join(base[:-1]) + ".xml" |
43 | | - |
44 | 33 | pathlib.Path(outfile).write_text(output) |
45 | 34 | stderr.write("Converted file: " + outfile + "\n") |
46 | 35 |
|
47 | 36 |
|
| 37 | +def main() -> None: |
| 38 | + args = Arguments(__version__).parse_args() # pytype: disable=not-callable |
| 39 | + |
| 40 | + in_files: list[str] |
| 41 | + out_files: list[str] | None |
| 42 | + if args.outfile is None or len(args.outfile) == 0: |
| 43 | + in_files = [args.to_convert] |
| 44 | + out_files = None if args.stdout else [_gv_filename_to_xml(args.to_convert)] |
| 45 | + elif len(args.outfile) == 1: |
| 46 | + in_files = [args.to_convert] |
| 47 | + out_files = args.outfile |
| 48 | + else: |
| 49 | + in_files = [args.to_convert, *args.outfile] |
| 50 | + out_files = [ |
| 51 | + _gv_filename_to_xml(args.to_convert), |
| 52 | + *[_gv_filename_to_xml(f) for f in args.outfile], |
| 53 | + ] |
| 54 | + |
| 55 | + if out_files is None: |
| 56 | + _convert_file(in_files[0], args.program, None) |
| 57 | + else: |
| 58 | + for in_file, out_file in zip(in_files, out_files, strict=True): |
| 59 | + _convert_file(in_file, args.program, out_file) |
| 60 | + |
| 61 | + |
48 | 62 | if __name__ == "__main__": |
49 | 63 | main() |
0 commit comments