Skip to content

Commit 5f475f1

Browse files
authored
Remove sentry, support multiple file conversion (#86)
* Remove sentry, support multiple file conversion * typing and formatting
1 parent 61d60a0 commit 5f475f1

5 files changed

Lines changed: 64 additions & 34 deletions

File tree

.github/workflows/lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
cache: pip
2020
cache-dependency-path: requirements.txt
2121
- run: pip install -r requirements.txt
22-
- run: pip install black ruff pytest
22+
- run: pip install black ruff pytest pytest-cov
2323
- run: ruff check .
2424
- name: Run mypy if on 3.12, pytype otherwise
2525
run: |
@@ -35,7 +35,7 @@ jobs:
3535
- uses: psf/black@stable
3636
with:
3737
options: "--check --verbose"
38-
- run: pytest
38+
- run: pytest --cov
3939
- name: Run main script
4040
run: |
4141
python3 -m graphviz2drawio test/directed/hello.gv.txt

graphviz2drawio/__main__.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,63 @@
11
import pathlib
2+
import sys
23
from sys import stderr
34

45
from .graphviz2drawio import convert
56
from .models import Arguments
67
from .version import __version__
78

9+
DEFAULT_TEXT = "\033[0m"
10+
RED_TEXT = "\033[91m"
11+
BOLD = "\033[1m"
812

9-
def main() -> None:
1013

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"
1216

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")
1617

18+
def _convert_file(to_convert: str, program: str, outfile: str | None) -> None:
1719
try:
18-
output = convert(args.to_convert, args.program)
20+
output = convert(to_convert, program)
1921
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")
3227
raise
3328

34-
if args.stdout:
29+
if outfile is None:
3530
print(output)
3631
return
3732

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-
4433
pathlib.Path(outfile).write_text(output)
4534
stderr.write("Converted file: " + outfile + "\n")
4635

4736

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+
4862
if __name__ == "__main__":
4963
main()

graphviz2drawio/models/Arguments.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,28 @@
33

44
class Arguments(ArgumentParser):
55
def __init__(self, version: str) -> None:
6-
super().__init__(prog="graphviz2drawio")
7-
self.add_argument("to_convert", metavar="file.dot", help="graphviz file")
8-
output = self.add_mutually_exclusive_group()
9-
output.add_argument("outfile", help="output file", nargs="?", default=None)
10-
output.add_argument("--stdout", action="store_true", help="print xml to stdout")
6+
super().__init__(
7+
prog="graphviz2drawio",
8+
description="Please report issues at: https://github.com/hbmartin/graphviz2drawio/issues",
9+
)
10+
self.add_argument(
11+
"to_convert",
12+
metavar="file.dot",
13+
help="Path of the graphviz file to convert.",
14+
)
15+
self.add_argument(
16+
"outfile",
17+
metavar="outfile.xml",
18+
help="Optional path to output XML. "
19+
"If more than one file is given, they will be treated as input files.",
20+
nargs="*",
21+
default=None,
22+
)
23+
self.add_argument(
24+
"--stdout",
25+
action="store_true",
26+
help="Print converted XML to stdout instead of writing a file.",
27+
)
1128
self.add_argument(
1229
"--version",
1330
action="version",

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
puremagic==1.25
22
pygraphviz==1.13
3-
raven==6.10.0
43
svg.path==6.3

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
keywords="graphviz graph agraph dot convert conversion draw drawio mxgraph maxgraph",
3939
packages=find_packages(exclude=["doc", "test"]),
4040
python_requires=">=3.10",
41-
install_requires=["puremagic", "pygraphviz", "raven", "svg.path"],
41+
install_requires=["puremagic", "pygraphviz", "svg.path"],
4242
tests_require=["pytest"],
4343
entry_points={"console_scripts": ["graphviz2drawio=graphviz2drawio.__main__:main"]},
4444
project_urls={

0 commit comments

Comments
 (0)