Skip to content

Commit

Permalink
chore: Refactor module imports and file structure (#162)
Browse files Browse the repository at this point in the history
* chore: Refactor module imports and file structure

* chore: Remove unused import statement
  • Loading branch information
ouhammmourachid authored Jul 20, 2024
1 parent 3c7fdd9 commit 8d03b8e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 60 deletions.
4 changes: 2 additions & 2 deletions mermaid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
"""
from enum import Enum

from ._main import Mermaid, Position
from ._utils import load, text_to_snake_case
from .__main__ import Mermaid, Position
from .configuration import Config
from .graph import Graph
from .icon import Icon
from .style import Style
from .utils import load, text_to_snake_case

__version__: str = '0.5.1'

Expand Down
File renamed without changes.
Empty file removed mermaid/classdiagram/__init__.py
Empty file.
113 changes: 56 additions & 57 deletions mermaid/graph/__init__.py → mermaid/graph.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
"""Graph module.
This module provides the Graph base class for creating and manipulating
different types of diagrams such as flowcharts, ER diagrams, etc.
Classes:
Graph: Represents a base class for different types of diagrams.
"""
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Optional

from mermaid.configuration import Config


@dataclass
class Graph:
"""Graph base class.
This class serves as a base for other classes representing different
types of diagrams like `Flowchart`, `ERDiagram`, etc.
Attributes:
title (str): The title of the diagram.
script (str): The main script to create the diagram.
config (Optional[Config]): The configuration for the diagram.
"""
title: str
script: str
config: Optional[Config] = None

def save(self, path: Optional[Path] = None) -> None:
"""Save the diagram to a file.
Args:
path (Optional[Path]): 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']:
raise ValueError("File extension must be '.mmd' or '.mermaid'")
with open(file_path, 'w') as file:
file.write(self.script)

def _build_script(self) -> None:
script: str = f'---\ntitle: {self.title}\n---'
if self.config:
script += '\n' + str(self.config)
script += self.script
self.script = script


__all__ = ['Graph']
"""Graph module.
This module provides the Graph base class for creating and manipulating
different types of diagrams such as flowcharts, ER diagrams, etc.
Classes:
Graph: Represents a base class for different types of diagrams.
"""
from dataclasses import dataclass
from pathlib import Path
from typing import Optional

from mermaid.configuration import Config


@dataclass
class Graph:
"""Graph base class.
This class serves as a base for other classes representing different
types of diagrams like `Flowchart`, `ERDiagram`, etc.
Attributes:
title (str): The title of the diagram.
script (str): The main script to create the diagram.
config (Optional[Config]): The configuration for the diagram.
"""
title: str
script: str
config: Optional[Config] = None

def save(self, path: Optional[Path] = None) -> None:
"""Save the diagram to a file.
Args:
path (Optional[Path]): 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']:
raise ValueError("File extension must be '.mmd' or '.mermaid'")
with open(file_path, 'w') as file:
file.write(self.script)

def _build_script(self) -> None:
script: str = f'---\ntitle: {self.title}\n---'
if self.config:
script += '\n' + str(self.config)
script += self.script
self.script = script


__all__ = ['Graph']
2 changes: 1 addition & 1 deletion mermaid/sequence/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from typing import Union

from mermaid._utils import text_to_snake_case
from mermaid.utils import text_to_snake_case


class Actor:
Expand Down
File renamed without changes.

0 comments on commit 8d03b8e

Please sign in to comment.