Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor module imports and file structure #163

Merged
merged 2 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions mermaid/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
from typing import Optional, Union

from mermaid.configuration import Config

Expand All @@ -29,20 +29,25 @@ class Graph:
script: str
config: Optional[Config] = None

def save(self, path: Optional[Path] = None) -> None:
def save(self, path: Optional[Union[Path, str]] = None) -> None:
"""Save the diagram to a file.

Args:
path (Optional[Path]): The path to save the diagram. If not
path (Optional[Union[Path,str]]): The path to save the diagram. If not
provided, the diagram will be saved in the current directory
with the title as the filename.
Raises:
ValueError: If the file extension is not '.mmd' or '.mermaid'.
"""
file_path: Path = path if path else Path(f'./{self.title}.mmd')
if file_path.suffix not in ['.mmd', '.mermaid']:

if path is None:
path = Path(f'./{self.title}.mmd')
if isinstance(path, str):
path = Path(path)

if path.suffix not in ['.mmd', '.mermaid']:
raise ValueError("File extension must be '.mmd' or '.mermaid'")
with open(file_path, 'w') as file:
with open(path, 'w') as file:
file.write(self.script)

def _build_script(self) -> None:
Expand Down
265 changes: 1 addition & 264 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ homepage = "https://mermaidpy.vercel.app"
[tool.poetry.dependencies]
python = "^3.9"
requests = "^2.31.0"
ipython = "^8.17.2"


[tool.poetry.group.dev.dependencies]
Expand Down
6 changes: 6 additions & 0 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def test_save_graph_with_path(self) -> None:
self.graph_test.save(Path('./test-graph-file.mmd'))
self.assertTrue(Path.exists(Path('./test-graph-file.mmd')))

def test_save_graph_with_path_str(self) -> None:
self.graph_test.save('./test-graph-str-path.mmd')
self.assertTrue(Path.exists(Path('./test-graph-str-path.mmd')))

def test_save_graph_name_contain_space(self) -> None:
self.graph_test.title = 'test graph file'
self.graph_test.save(Path('./test graph file.mmd'))
Expand All @@ -40,4 +44,6 @@ def tearDown(self) -> None:
os.remove('./test-graph-file.mmd')
if os.path.exists('./test graph file.mmd'):
os.remove('./test graph file.mmd')
if os.path.exists('./test-graph-str-path.mmd'):
os.remove('./test-graph-str-path.mmd')
return super().tearDown()