pyrbd 0.2.5__py3-none-any.whl → 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyrbd/block.py CHANGED
@@ -40,7 +40,7 @@ class Block:
40
40
  "anchor=west",
41
41
  "align=center",
42
42
  "fill={fill_color}",
43
- "draw=black",
43
+ "draw=black!70!gray",
44
44
  "minimum height=1cm",
45
45
  "rounded corners=0.3mm",
46
46
  "inner sep=4pt",
@@ -48,6 +48,8 @@ class Block:
48
48
  ]
49
49
  )
50
50
 
51
+ arrow_options: str = "arrowcolor, thick"
52
+
51
53
  def __init__(
52
54
  self,
53
55
  text: str,
@@ -90,7 +92,7 @@ class Block:
90
92
 
91
93
  return "".join(
92
94
  [
93
- f"\\draw[thick, rectangle connector={connector_position}cm]",
95
+ f"\\draw[{self.arrow_options}, rectangle connector={connector_position}cm]",
94
96
  f"({self.parent.id}.east) to ({self.id}.west);\n\n",
95
97
  ]
96
98
  )
@@ -402,7 +404,7 @@ class Group(Block):
402
404
  if self.parent is None:
403
405
  return ""
404
406
 
405
- return f"\\draw[thick] ({self.parent.id}.east) to ({self.id}.west);\n"
407
+ return f"\\draw[{self.arrow_options}] ({self.parent.id}.east) to ({self.id}.west);\n"
406
408
 
407
409
  @property
408
410
  def arrows(self) -> str:
@@ -412,7 +414,7 @@ class Group(Block):
412
414
  [
413
415
  " ".join(
414
416
  [
415
- "\\draw[thick, rectangle line]",
417
+ f"\\draw[{self.arrow_options}, rectangle line]",
416
418
  f"({block1.id}.east) to ({block2.id}.east);\n",
417
419
  ]
418
420
  )
pyrbd/diagram.py CHANGED
@@ -1,5 +1,6 @@
1
1
  """Module containing Diagram class definition."""
2
2
 
3
+ from typing import Optional
3
4
  import subprocess
4
5
 
5
6
  import pymupdf
@@ -18,12 +19,24 @@ class Diagram:
18
19
  list of `Block` instances
19
20
  hazard : str, optional
20
21
  string defining the `hazard` block text
22
+ colors : dict[str, str], optional
23
+ dictionary with custom color definitions in HEX format:
24
+ `{'color name': '6 digit hex code'}`
21
25
  """
22
26
 
23
- def __init__(self, name: str, blocks: list[Block], hazard: str = "") -> None:
27
+ # Default color definitions
28
+ colors: dict[str, str] = {"arrowcolor": "4c4d4c", "hazardcolor": "ff6666"}
29
+
30
+ def __init__(
31
+ self,
32
+ name: str,
33
+ blocks: list[Block],
34
+ hazard: str = "",
35
+ colors: Optional[dict[str, str]] = None,
36
+ ) -> None:
24
37
  self.filename = name
25
38
  if hazard:
26
- self.head = Block(hazard, "red!60")
39
+ self.head = Block(hazard, "hazardcolor")
27
40
  else:
28
41
  self.head = blocks.pop(0)
29
42
 
@@ -31,11 +44,14 @@ class Diagram:
31
44
  self.blocks = blocks
32
45
  self.blocks[0].parent = self.head
33
46
 
47
+ if colors is not None:
48
+ self.colors = self.colors | colors
49
+
34
50
  def write(self) -> None:
35
51
  """Write diagram to .tex file."""
36
52
 
37
53
  with open(f"{self.filename}.tex", mode="w", encoding="utf-8") as file:
38
- file.write(TEX_PREAMBLE)
54
+ file.write(tex_preamble(self.colors))
39
55
  for block in [self.head, *self.blocks]:
40
56
  file.write(block.get_node())
41
57
  file.write(TEX_END)
@@ -53,7 +69,19 @@ class Diagram:
53
69
  page = pdf_document[0]
54
70
 
55
71
  # Get and convert page to svg image
56
- svg_content = page.get_svg_image()
72
+ svg_content = page.get_svg_image().splitlines()
73
+ svg_content.insert(
74
+ 1,
75
+ "\n".join(
76
+ [
77
+ "<style>",
78
+ " @media (prefers-color-scheme: light) { :root { --color: #000000; } }",
79
+ " @media (prefers-color-scheme: dark) { :root { --color: #DDDDDD; } }",
80
+ "</style>",
81
+ ]
82
+ ),
83
+ )
84
+ svg_content = "\n".join(svg_content).replace(r"#4c4d4c", "var(--color)")
57
85
 
58
86
  # Save to file
59
87
  with open(output_file := f"{self.filename}.svg", "w", encoding="utf-8") as file:
@@ -145,38 +173,54 @@ class Diagram:
145
173
  return output_files
146
174
 
147
175
 
148
- TEX_PREAMBLE = "\n".join(
149
- [
150
- r"\documentclass{standalone}",
151
- r"\usepackage{tikz}",
152
- r"\usetikzlibrary{shapes,arrows,positioning,calc}",
153
- r"\pgfdeclarelayer{background}",
154
- r"\pgfsetlayers{background, main}",
155
- r"\tikzset{",
156
- r"connector/.style={",
157
- r"-latex,",
158
- r"font=\scriptsize},",
159
- r"line/.style={",
160
- r"font=\scriptsize},",
161
- r"rectangle connector/.style={",
162
- r"connector,"
163
- r"to path={(\tikztostart) -- ++(#1,0pt) \tikztonodes |- (\tikztotarget) },",
164
- r"pos=0.5},"
165
- r"rectangle connector/.default=0.5cm,",
166
- r"rectangle line/.style={",
167
- r"line,"
168
- r"to path={(\tikztostart) -- ++(#1,0pt) \tikztonodes |- (\tikztotarget) },",
169
- r"pos=0.5},"
170
- r"rectangle line/.default=0.5cm,",
171
- r"straight connector/.style={",
172
- r"connector,",
173
- r"to path=--(\tikztotarget) \tikztonodes}",
174
- r"}",
175
- r"\begin{document}",
176
- r"\begin{tikzpicture}",
177
- "",
178
- ]
179
- )
176
+ def tex_preamble(custom_colors: dict[str, str] | None = None) -> str:
177
+ """LaTeX file preamble file with definition of custom colors given in dictionary."""
178
+
179
+ color_defs = []
180
+ if custom_colors is not None:
181
+ color_defs = [
182
+ f"\\definecolor{{{color_name}}}{{HTML}}{{{hex_code}}}"
183
+ for (color_name, hex_code) in custom_colors.items()
184
+ ]
185
+
186
+ return "\n".join(
187
+ [
188
+ r"\documentclass{standalone}",
189
+ r"\usepackage[T1]{fontenc}"
190
+ r"\usepackage{helvet}",
191
+ r"\renewcommand{\familydefault}{\sfdefault}",
192
+ r"\usepackage[dvipsnames,svgnames,x11names]{xcolor}",
193
+ r"\usepackage{tikz}",
194
+ r"\usetikzlibrary{shapes,arrows,positioning,calc}",
195
+ r"\pgfdeclarelayer{background}",
196
+ r"\pgfsetlayers{background, main}",
197
+ r"\tikzset{",
198
+ r"connector/.style={",
199
+ r"-latex,",
200
+ r"font=\scriptsize},",
201
+ r"line/.style={",
202
+ r"font=\scriptsize},",
203
+ r"rectangle connector/.style={",
204
+ r"connector,"
205
+ r"to path={(\tikztostart) -- ++(#1,0pt) \tikztonodes |- (\tikztotarget) },",
206
+ r"pos=0.5},"
207
+ r"rectangle connector/.default=0.5cm,",
208
+ r"rectangle line/.style={",
209
+ r"line,"
210
+ r"to path={(\tikztostart) -- ++(#1,0pt) \tikztonodes |- (\tikztotarget) },",
211
+ r"pos=0.5},"
212
+ r"rectangle line/.default=0.5cm,",
213
+ r"straight connector/.style={",
214
+ r"connector,",
215
+ r"to path=--(\tikztotarget) \tikztonodes}",
216
+ r"}",
217
+ *color_defs,
218
+ r"\begin{document}",
219
+ r"\begin{tikzpicture}",
220
+ "",
221
+ ]
222
+ )
223
+
180
224
 
181
225
  TEX_END = "\n".join(
182
226
  [
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyrbd
3
- Version: 0.2.5
3
+ Version: 0.3.0
4
4
  Summary: Package for creating simple reliability block diagrams (RBDs) using LaTeX and TikZ.
5
5
  Project-URL: Repository, https://github.com/hghugdal/pyrbd
6
6
  Project-URL: Issues, https://github.com/hghugdal/pyrbd/issues
@@ -14,9 +14,13 @@ Requires-Python: >=3.10
14
14
  Requires-Dist: pymupdf>=1.26.3
15
15
  Description-Content-Type: text/markdown
16
16
 
17
- # <img alt="pyRBDlogo" src=docs/images/logo.svg width=40 align=top> pyRBD
17
+ # <img alt="pyRBDlogo" src="https://raw.githubusercontent.com/hghugdal/pyrbd/23d7f00ca74e8465df3760821488b4bb78df803c/docs/images/logo.svg" width=40 align=top> pyRBD
18
18
 
19
- <img alt="Python" src="https://img.shields.io/badge/Python->= 3.10-blue?logo=python&link=None"> ![PyPI - Version](https://img.shields.io/pypi/v/pyrbd?link=https%3A%2F%2Fpypi.org%2Fproject%2Fpyrbd) <img alt="Tests" src="https://img.shields.io/badge/Tests-Passing-darkgreen?logo=pytest&link=None"> <img alt="Coverage" src="https://img.shields.io/badge/Coverage-100%25-darkgreen?link=None"> <img alt="Pylint" src="https://img.shields.io/badge/Pylint-10%2F10-darkgreen?link=None">
19
+ [![PyPI - Version](https://img.shields.io/pypi/v/pyrbd)](https://pypi.org/project/pyrbd/)
20
+ <img alt="Python" src="https://img.shields.io/badge/Python->= 3.10-blue?logo=python&link=None">
21
+ <img alt="Tests" src="https://img.shields.io/badge/Tests-Passing-darkgreen?logo=pytest&link=None">
22
+ <img alt="Coverage" src="https://img.shields.io/badge/Coverage-100%25-darkgreen?link=None">
23
+ <img alt="Pylint" src="https://img.shields.io/badge/Pylint-10%2F10-darkgreen?link=None">
20
24
 
21
25
  A Python package for creating simple reliability block diagrams (RBDs) using `LaTeX` and [`TikZ`](https://en.wikipedia.org/wiki/PGF/TikZ).
22
26
 
@@ -25,6 +29,7 @@ A Python package for creating simple reliability block diagrams (RBDs) using `La
25
29
 
26
30
  ## Simple example diagram
27
31
  The blocks of the RBD are defined using `Block`, `Series` and `Group`, and the diagram itself is handled by the `Diagram` class. A simple example is given by the code
32
+
28
33
  ```python linenums="1"
29
34
  from pyrbd import Block, Diagram
30
35
 
@@ -39,7 +44,8 @@ diagram = Diagram(
39
44
  diagram.write()
40
45
  diagram.compile()
41
46
  ```
47
+
42
48
  producing the following diagram
43
- <div><img src="docs/examples/simple_RBD.svg" width=500/></div>
49
+ <div><img src="https://raw.githubusercontent.com/hghugdal/pyrbd/23d7f00ca74e8465df3760821488b4bb78df803c/docs/examples/simple_RBD.svg" width=500/></div>
44
50
 
45
51
  For more examples, visit the [documentation](https://hghugdal.github.io/pyrbd/).
@@ -0,0 +1,7 @@
1
+ pyrbd/__init__.py,sha256=mpsb022BX9eDGSBhuYIr2gOr4sg_M9sYbPGHLPIdvl0,219
2
+ pyrbd/block.py,sha256=qQJx2Sn_SvAnm3Chz1f078nTzbS61LSFdxaEveu2RcY,12319
3
+ pyrbd/diagram.py,sha256=llZ0ncDmyJRg7zyQfTXAzbEM1W66wyHs9gfiovy-ujI,6762
4
+ pyrbd-0.3.0.dist-info/METADATA,sha256=KEMFXHPv7ykDCYbh2fOOjcKPyqhOchXl0Ck2pc3ODwQ,2369
5
+ pyrbd-0.3.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
+ pyrbd-0.3.0.dist-info/licenses/LICENSE,sha256=mxXMgWpFgk71JpEEElFrb9FJxeoaDgvrU8gjUUU9LzA,1069
7
+ pyrbd-0.3.0.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- pyrbd/__init__.py,sha256=mpsb022BX9eDGSBhuYIr2gOr4sg_M9sYbPGHLPIdvl0,219
2
- pyrbd/block.py,sha256=ETRZJeEUU4OPk0qGo6zZIsaR1oLkyDPf1kelfuKWNsc,12219
3
- pyrbd/diagram.py,sha256=NVAsdfmqAu6-6J2mT5Gp2W_8a9WsI2iMbKjcEI6QFlE,5092
4
- pyrbd-0.2.5.dist-info/METADATA,sha256=WN1raYOu5KJtH5wj-7b1P8wH8-1GGHmnnqvwrt5hmZs,2196
5
- pyrbd-0.2.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
6
- pyrbd-0.2.5.dist-info/licenses/LICENSE,sha256=mxXMgWpFgk71JpEEElFrb9FJxeoaDgvrU8gjUUU9LzA,1069
7
- pyrbd-0.2.5.dist-info/RECORD,,
File without changes