Skip to content

Commit 5d6b044

Browse files
committed
PR comments
1 parent 509d89e commit 5d6b044

File tree

4 files changed

+39
-46
lines changed

4 files changed

+39
-46
lines changed

.github/workflows/lint.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@ jobs:
2525
- run: pip install -r requirements.txt
2626
- run: pip install black ruff pytest pytest-cov
2727
- run: ruff check .
28-
- name: Run mypy if on 3.12, pytype otherwise
28+
- name: Run pytype if on 3.11, pyrefly if on 3.14, mypy otherwise
2929
run: |
3030
if [[ '${{ steps.cpython3.outputs.python-version }}' == 3.11* ]]; then
3131
pip install pytype
3232
pytype -j auto graphviz2drawio
3333
elif [[ '${{ steps.cpython3.outputs.python-version }}' == 3.14* ]]; then
3434
echo "Using pyrefly for Python 3.14"
3535
pip install pyrefly
36-
pyrefly check
36+
pyrefly check graphviz2drawio
3737
else
38-
echo "Using mypy for Python 3.12"
3938
pip install mypy
4039
mypy graphviz2drawio --ignore-missing-imports
4140
fi

graphviz2drawio/__main__.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env python3
22

3-
import pathlib
43
import sys
54
from argparse import Namespace
65
from io import TextIOWrapper
@@ -15,7 +14,7 @@
1514
RED_TEXT = "\033[91m"
1615
BOLD = "\033[1m"
1716

18-
utf8 = "utf-8"
17+
UTF8 = "utf-8"
1918

2019

2120
def _gv_filename_to_xml(filename: str) -> str:
@@ -45,25 +44,17 @@ def _convert_file(
4544
with to_convert.open(encoding=encoding) as contents:
4645
output = convert(contents.read(), program)
4746
except UnicodeDecodeError:
48-
if encoding != utf8:
49-
# Attempt to automatically recover. Chinese Windows systems in particular
50-
# typically use other encodings e.g. gbk, cp950, cp1252, etc. but the
51-
# actual dot files are still UTF-8 encoded
47+
if encoding != UTF8 and isinstance(to_convert, Path):
48+
# Attempt to automatically recover for file. Chinese Windows systems in
49+
# particular often use other encodings e.g. gbk, cp950, cp1252, etc. but
50+
# the actual dot files are still UTF-8 encoded
5251
# https://github.com/hbmartin/graphviz2drawio/issues/105
53-
return _convert_file(to_convert, program, utf8, outfile)
54-
55-
from locale import getpreferredencoding
56-
57-
error_message = f"Error decoding {to_convert} with {utf8}"
58-
stderr.write(f"{RED_TEXT}{BOLD}{error_message}\n")
59-
stderr.write("Try again by specifying your file encoding with the -e flag.\n")
60-
if (sys_enc := getpreferredencoding(do_setlocale=False).lower()) != utf8:
61-
stderr.write(f"e.g. using your system's default encoding -e {sys_enc})")
62-
stderr.write(f"\n\n{DEFAULT_TEXT}")
63-
else:
64-
_write_stderr_message(str(to_convert))
65-
raise
66-
except BaseException:
52+
return _convert_file(to_convert, program, UTF8, outfile)
53+
54+
_write_stderr_message(str(to_convert))
55+
raise
56+
57+
except Exception:
6758
_write_stderr_message(str(to_convert))
6859
raise
6960

@@ -75,7 +66,7 @@ def _convert_file(
7566
print(output)
7667
return None
7768

78-
out_path = pathlib.Path(outfile)
69+
out_path = Path(outfile)
7970
out_path.parent.mkdir(parents=True, exist_ok=True)
8071
out_path.write_text(output)
8172
stderr.write("Converted file: " + outfile + "\n")

graphviz2drawio/graphviz2drawio.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,7 @@ def convert(
1414
graph_to_convert: AGraph | str | TextIOBase | Path | TextIO,
1515
layout_prog: str = "dot",
1616
) -> str:
17-
if isinstance(graph_to_convert, AGraph):
18-
graph = graph_to_convert
19-
elif isinstance(graph_to_convert, str):
20-
# This fixes a pygraphviz bug where a string beginning with a comment
21-
# is mistakenly identified as a filename.
22-
# https://github.com/pygraphviz/pygraphviz/issues/536
23-
pattern = re.compile(
24-
r"(?:\s*(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/|//[^\r\n]*|\#[^\r\n]*)\s*)*\s*(strict)?\s*(graph|digraph).*?\{.*\}\s*",
25-
re.DOTALL | re.MULTILINE | re.VERBOSE,
26-
)
27-
if pattern.match(graph_to_convert):
28-
graph = AGraph(string=graph_to_convert)
29-
else:
30-
graph = AGraph(filename=graph_to_convert)
31-
elif hasattr(graph_to_convert, "read"):
32-
graph = AGraph(string=graph_to_convert.read())
33-
else:
34-
# Use builtin type detection which includes: hasattr(thing, "open")
35-
graph = AGraph(graph_to_convert)
17+
graph = _load_pygraphviz_graph(graph_to_convert)
3618

3719
graph_edges: dict[str, dict] = {
3820
f"{e[0]}->{e[1]}-"
@@ -65,3 +47,26 @@ def convert(
6547
# Put clusters first, so that nodes are drawn in front
6648
mx_graph = MxGraph(clusters, nodes, edges)
6749
return mx_graph.value()
50+
51+
52+
def _load_pygraphviz_graph(
53+
graph_to_convert: AGraph | str | TextIOBase | Path | TextIO,
54+
) -> AGraph:
55+
if isinstance(graph_to_convert, AGraph):
56+
return graph_to_convert
57+
if isinstance(graph_to_convert, str):
58+
# This fixes a pygraphviz bug where a string beginning with a comment
59+
# is mistakenly identified as a filename.
60+
# https://github.com/pygraphviz/pygraphviz/issues/536
61+
pattern = re.compile(
62+
r"(?:\s*(?:/\*[^*]*\*+(?:[^/*][^*]*\*+)*/|//[^\r\n]*|\#[^\r\n]*)\s*)*\s*(strict)?\s*(graph|digraph).*?\{.*\}\s*",
63+
re.DOTALL | re.MULTILINE | re.VERBOSE,
64+
)
65+
if pattern.match(graph_to_convert):
66+
return AGraph(string=graph_to_convert)
67+
return AGraph(filename=graph_to_convert)
68+
# pyrefly: ignore # missing-attribute
69+
if hasattr(graph_to_convert, "read") and callable(graph_to_convert.read):
70+
return AGraph(string=graph_to_convert.read())
71+
# Use builtin type detection which includes: hasattr(thing, "open")
72+
return AGraph(graph_to_convert)

graphviz2drawio/models/Arguments.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
class NonOpeningFileType:
1010
def __call__(self, string: str) -> TextIO | Path:
1111
# the special argument "-" means sys.std{in,out}
12-
if string == "-":
13-
return sys.stdin
14-
return Path(string)
12+
return sys.stdin if string == "-" else Path(string)
1513

1614

1715
class Arguments(ArgumentParser):

0 commit comments

Comments
 (0)