Skip to content

Commit

Permalink
Allow multiple style references
Browse files Browse the repository at this point in the history
  • Loading branch information
yorgath committed Dec 16, 2020
1 parent 4e45230 commit e1f08bc
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 40 deletions.
32 changes: 22 additions & 10 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ highest priority among all links in the group.
~~~~~~~~~~

You can add style definitions to the ``styles`` section to create
named styles that can be referred to by the terminals and links. Each
style definition consists of attribute key-value pairs. For example,
the following two terminals are drawn in the same color:
named styles that can be referred to by the terminals, links and
groups. Each style definition consists of attribute key-value pairs.
For example, the following two terminals are drawn in the same color:

.. code:: yaml
Expand All @@ -306,13 +306,19 @@ the following two terminals are drawn in the same color:
stroke_width: 3.0
fill: "#FFDDDD"
Attributes you define in the element itself override the attributes it
inherits from the linked named style.
You add style references to an element using the ``style`` attribute.
The value of this attribute can be either a single style name or a
list of style names. When in a list, later styles override the ones
coming before them. Attributes you define in the element itself
override the attributes it inherits from the referenced named styles.

There are two special style names, ``default_terminal`` and
``default_link``, which you can use to set default values for all the
terminals and links in the diagram respectively.

Styles themselves *cannot* reference other styles, i.e. the program
ignores the ``style`` attribute in style definitions.

``groups``
~~~~~~~~~~

Expand All @@ -335,10 +341,10 @@ attributes. In the example that follows, all links are drawn in blue:
end: d
group: water
A group definition may contain a reference to a named ``style`` if
needed. Note that creating an entry in the ``groups`` section is not
necessary for the grouping of the links; a common ``group`` name in
each link definition is sufficient.
A group definition may contain references to named styles. Note that
creating an entry in the ``groups`` section is not necessary for the
grouping of the links; a common ``group`` name in each link definition
is sufficient.

Attributes
----------
Expand Down Expand Up @@ -611,4 +617,10 @@ Release history
0.2.1 (2020-12-15)
~~~~~~~~~~~~~~~~~~

* Enforce the UTF-8 character encoding for the definition file.
* Enforced the UTF-8 character encoding for the definition file.

0.2.2 (2020-12-16)
~~~~~~~~~~~~~~~~~~

* Enabled multiple style references in definition files.
* Made debug switch compatible with Python 3.8.
41 changes: 23 additions & 18 deletions examples/powerplant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,34 @@ styles:
buffer_fill: lightyellow
stroke_width: 3

shaft:
arrow_forward: false
stroke: dimgrey
stroke_width: 8
stroke_dasharray: 8 8
cold_gas:
stroke: turquoise

combustion:
label: "Combustion\nChamber"

compressor:
label: Compressor

cold_gas:
stroke: turquoise

electrical_generator:
label: "Electrical\nGenerator"

gas_turbine:
label: Gas Turbine

hot_gas:
stroke: orangered
fill: gold

low_priority:
drawing_priority: -1

shaft:
arrow_forward: false
stroke: dimgrey
stroke_width: 8
stroke_dasharray: 8 8

steam:
stroke: grey

Expand All @@ -47,7 +56,7 @@ styles:
groups:

gas_to_steam:
drawing_priority: -1
style: low_priority

terminals:

Expand All @@ -67,12 +76,10 @@ terminals:
style: compressor

cmb1:
label: "Combustion\nChamber"
style: hot_gas
style: [combustion, hot_gas]

cmb2:
label: "Combustion\nChamber"
style: hot_gas
style: [combustion, hot_gas]

cool:
label: "Cooling\nTower"
Expand All @@ -90,12 +97,10 @@ terminals:
style: electrical_generator

gtb1:
label: Gas Turbine
style: hot_gas
style: [gas_turbine, hot_gas]

gtb2:
label: Gas Turbine
style: hot_gas
style: [gas_turbine, hot_gas]

sgn:
label: "Steam\nGenerator"
Expand Down Expand Up @@ -140,7 +145,7 @@ links:

- start: fuel
end: [cmb1, cmb2]
drawing_priority: -1
style: low_priority
end_bias: vertical
stroke: olive

Expand Down
2 changes: 1 addition & 1 deletion orthogram/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.2.1'
__version__ = '0.2.2'

from .build import Builder
from .diagram import Diagram
Expand Down
29 changes: 19 additions & 10 deletions orthogram/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ def add_group(self, name: str, group_def: _Definition) -> None:
"""
attrs: AttributeDict = {}
# Merge attributes inherited from style reference.
style_name = group_def.get('style')
style_attrs = self._get_style(style_name, True)
# Merge attributes inherited from style references.
style_attrs = self._collect_style_attributes(group_def)
self._merge_attributes(attrs, style_attrs)
# Merge attributes defined here.
own_attrs = self._collect_attributes(group_def)
Expand Down Expand Up @@ -175,9 +174,8 @@ def add_terminal(
self._merge_attributes(attrs, def_attrs)
# The terminal definition may be None: an empty definition.
if terminal_def:
# Merge attributes inherited from style reference.
style_name = terminal_def.get('style')
style_attrs = self._get_style(style_name, True)
# Merge attributes inherited from style references.
style_attrs = self._collect_style_attributes(terminal_def)
self._merge_attributes(attrs, style_attrs)
# Merge attributes defined here.
own_attrs = self._collect_attributes(terminal_def)
Expand Down Expand Up @@ -242,16 +240,25 @@ def add_link(self, link_def: _Definition) -> None:
if group and group in self._group_styles:
group_attrs = self._group_styles[group]
self._merge_attributes(attrs, group_attrs)
# Merge attributes inherited from style reference.
style_name = link_def.get('style')
style_attrs = self._get_style(style_name, True)
# Merge attributes inherited from style references.
style_attrs = self._collect_style_attributes(link_def)
self._merge_attributes(attrs, style_attrs)
# Merge attributes defined here.
own_attrs = self._collect_attributes(link_def)
self._merge_attributes(attrs, own_attrs)
# Create the object(s).
self._diagram.add_links(start, end, **attrs)

def _collect_style_attributes(self, any_def: _Definition) -> AttributeMap:
"""Collect the attributes of the named styles in the definition."""
style_value = any_def.get('style')
style_names = self._str_or_list(style_value)
attrs: AttributeDict = {}
for style_name in style_names:
style_attrs = self._get_style(style_name, True)
self._merge_attributes(attrs, style_attrs)
return attrs

def _collect_attributes(self, any_def: _Definition) -> AttributeMap:
"""Collect the attributes from a definition."""
attrs: AttributeDict = {}
Expand Down Expand Up @@ -418,7 +425,9 @@ def _get_style(
@staticmethod
def _str_or_list(text: Any) -> List[str]:
"""Take a string or list of strings and return a list of strings."""
result = []
result: List[str] = []
if not text:
return result
if isinstance(text, list):
result.extend(text)
else:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "orthogram"
version = "0.2.1"
version = "0.2.2"
description = "Draw diagrams of graphs."
license = "GPL-3.0-or-later"
authors = ["Georgios Athanasiou <yorgath@gmail.com>"]
Expand Down

0 comments on commit e1f08bc

Please sign in to comment.