Skip to content

Commit bf0509f

Browse files
committed
write output using encoding flag, harden regex
1 parent f5523f8 commit bf0509f

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

graphviz2drawio/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _convert_file(
4444
with to_convert.open(encoding=encoding) as contents:
4545
output = convert(contents.read(), program)
4646
except UnicodeDecodeError:
47-
if encoding != UTF8 and isinstance(to_convert, Path):
47+
if encoding.lower() != UTF8 and isinstance(to_convert, Path):
4848
# Attempt to automatically recover for file. Chinese Windows systems in
4949
# particular often use other encodings e.g. gbk, cp950, cp1252, etc. but
5050
# the actual dot files are still UTF-8 encoded
@@ -68,7 +68,7 @@ def _convert_file(
6868

6969
out_path = Path(outfile)
7070
out_path.parent.mkdir(parents=True, exist_ok=True)
71-
out_path.write_text(output)
71+
out_path.write_text(output, encoding=encoding)
7272
stderr.write("Converted file: " + outfile + "\n")
7373
return None
7474

graphviz2drawio/graphviz2drawio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def _load_pygraphviz_graph(
5959
# is mistakenly identified as a filename.
6060
# https://github.com/pygraphviz/pygraphviz/issues/536
6161
pattern = re.compile(
62-
pattern=r"^\s*(strict)?\s*(graph|digraph).*{",
63-
flags=re.DOTALL | re.MULTILINE,
62+
pattern=r"^(?=(\s*))\1(strict)?(?=(\s*))\3(graph|digraph)[^{]*{",
63+
flags=re.MULTILINE,
6464
)
6565
if pattern.search(graph_to_convert):
6666
return AGraph(string=graph_to_convert)

0 commit comments

Comments
 (0)