Skip to content

Commit

Permalink
fix: update code export and naming (#15)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: file imports have been changed to lowercase words
  • Loading branch information
MarkusSagen authored May 27, 2023
1 parent b2a79d8 commit a4ee1f5
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ connections = [

diagram = D2Diagram(shapes=shapes, connections=connections)

with open("graph.d2", "w") as f:
with open("graph.d2", "w", encoding="utf-8") as f:
f.write(str(diagram))

```
Expand All @@ -47,7 +47,7 @@ shape_name1 -> shape_name2
```

This can be rendered using `d2 render graph.d2` or [https://play.d2lang.com/](https://play.d2lang.com/) to produce
This can be rendered using `d2 graph.d2 graph.svg && open graph.svg` or [https://play.d2lang.com/](https://play.d2lang.com/) to produce

![example graph](/docs/images/d2.svg)

Expand Down
2 changes: 1 addition & 1 deletion graph.d2
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ shape_name2: {
fill: blue
}
}
shape_name1 -> shape_name2
shape_name1 -> shape_name2
16 changes: 16 additions & 0 deletions src/py_d2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .connection import D2Connection
from .connection import Direction
from .diagram import D2Diagram
from .shape import D2Shape
from .shape import D2Text
from .style import D2Style


__all__ = [
"Direction",
"D2Connection",
"D2Diagram",
"D2Shape",
"D2Text",
"D2Style",
]
File renamed without changes.
4 changes: 2 additions & 2 deletions src/py_d2/D2Diagram.py → src/py_d2/diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from typing import List
from typing import Optional

from py_d2.D2Connection import D2Connection
from py_d2.D2Shape import D2Shape
from py_d2.connection import D2Connection
from py_d2.shape import D2Shape


class D2Diagram:
Expand Down
10 changes: 5 additions & 5 deletions src/py_d2/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
from py_d2.D2Connection import D2Connection
from py_d2.D2Diagram import D2Diagram
from py_d2.D2Shape import D2Shape
from py_d2.D2Style import D2Style
from py_d2.connection import D2Connection
from py_d2.diagram import D2Diagram
from py_d2.shape import D2Shape
from py_d2.style import D2Style


def example():
Expand All @@ -16,7 +16,7 @@ def example():
diagram = D2Diagram(shapes=shapes, connections=connections)

print("Writing graph to file...")
with open("graph.d2", "w") as f:
with open("graph.d2", "w", encoding="utf-8") as f:
f.write(str(diagram))
print("Done! (graph.d2)")

Expand Down
10 changes: 5 additions & 5 deletions src/py_d2/D2Shape.py → src/py_d2/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from typing import List
from typing import Optional

from py_d2.D2Connection import D2Connection
from py_d2.D2Style import D2Style
from py_d2.connection import D2Connection
from py_d2.helpers import add_label_and_properties
from py_d2.helpers import flatten
from py_d2.helpers import indent
from py_d2.style import D2Style


class Shape(Enum):
Expand Down Expand Up @@ -44,17 +44,17 @@ def __init__(
# The actual text body (multiline is fine)
text: str,
# The format, eg) md, tex, html, css etc
format: str,
formatting: str,
# The number of pipes to use
pipes: int = 1,
):
self.text = text
self.format = format
self.formatting = formatting
self.pipes = pipes

def lines(self) -> List[str]:
sep = "|" * self.pipes
return [f"{sep}{self.format}", *self.text.split("\n"), sep]
return [f"{sep}{self.formatting}", *self.text.split("\n"), sep]

def __repr__(self) -> str:
return "\n".join(self.lines())
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions tests/test_py_d2/test_d2_connection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from py_d2.D2Connection import D2Connection
from py_d2.D2Connection import Direction
from py_d2.connection import D2Connection
from py_d2.connection import Direction


def test_d2_connection_label():
Expand Down
8 changes: 4 additions & 4 deletions tests/test_py_d2/test_d2_diagram.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
from py_d2.D2Connection import D2Connection
from py_d2.D2Diagram import D2Diagram
from py_d2.D2Shape import D2Shape
from py_d2.D2Style import D2Style
from py_d2.connection import D2Connection
from py_d2.diagram import D2Diagram
from py_d2.shape import D2Shape
from py_d2.style import D2Style


def test_d2_diagram():
Expand Down
22 changes: 11 additions & 11 deletions tests/test_py_d2/test_d2_shape.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
from py_d2.D2Connection import D2Connection
from py_d2.D2Shape import D2Shape
from py_d2.D2Shape import D2Text
from py_d2.D2Shape import Shape
from py_d2.D2Style import D2Style
from py_d2.connection import D2Connection
from py_d2.shape import D2Shape
from py_d2.shape import D2Text
from py_d2.shape import Shape
from py_d2.style import D2Style


def test_d2_shape():
Expand Down Expand Up @@ -32,7 +32,6 @@ def test_d2_shape_container():


def test_d2_shape_container_style():

shape = D2Shape(name="shape_name", label="container_label")
shape.add_shape(D2Shape(name="shape_1", style=D2Style(fill="red")))
shape.add_shape(D2Shape(name="shape_2"))
Expand All @@ -54,7 +53,6 @@ def test_d2_shape_container_style():


def test_d2_shape_container_in_container_with_shapes():

shape = D2Shape(name="shape_name", label="container_label")
shape.add_shape(
D2Shape(
Expand Down Expand Up @@ -146,13 +144,13 @@ def test_d2_shape_near():

def test_d2_shape_other_properties():
text = "Some text"
shape = D2Shape(name="shape_name", thing=D2Text(text=text, format="md"))
shape = D2Shape(name="shape_name", thing=D2Text(text=text, formatting="md"))
assert str(shape) == "\n".join(["shape_name: {", " thing: |md", " Some text", " |", "}"])


def test_d2_shape_other_properties_multi_line():
text = "\n".join(["multiline text", "like this", "works too"])
shape = D2Shape(name="shape_name", thing=D2Text(text=text, format="md"))
shape = D2Shape(name="shape_name", thing=D2Text(text=text, formatting="md"))
assert str(shape) == "\n".join(
["shape_name: {", " thing: |md", " multiline text", " like this", " works too", " |", "}"]
)
Expand All @@ -161,7 +159,9 @@ def test_d2_shape_other_properties_multi_line():
def test_d2_shape_other_properties_can_be_anything():
text = "\n".join(["multiline text", "like this", "works too"])
shape = D2Shape(
name="shape_name", description=D2Text(text=text, format="md"), other_thing=D2Text(text=text, format="md")
name="shape_name",
description=D2Text(text=text, formatting="md"),
other_thing=D2Text(text=text, formatting="md"),
)
assert str(shape) == "\n".join(
[
Expand All @@ -183,7 +183,7 @@ def test_d2_shape_other_properties_can_be_anything():

def test_d2_shape_text_can_specify_pipe_count():
text = "const iLoveTypescript = 1 || true;\nconst really = () => iLoveTypescript"
shape = D2Shape(name="shape_name", code=D2Text(text=text, format="ts", pipes=3))
shape = D2Shape(name="shape_name", code=D2Text(text=text, formatting="ts", pipes=3))
assert str(shape) == "\n".join(
[
"shape_name: {",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_py_d2/test_d2_style.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from py_d2.D2Style import D2Style
from py_d2.style import D2Style


def test_d2_style():
Expand Down

0 comments on commit a4ee1f5

Please sign in to comment.