Skip to content

Styling

momapy.styling

Classes and functions for styling layout elements using style sheets

Classes:

Name Description
ChildSelector

Class for child selectors

ClassSelector

Class for class selectors

DescendantSelector

Class for descendant selectors

IdSelector

Class for id selectors

OrSelector

Class for or selectors

Selector

Abstract class for selectors

StyleCollection

Class for style collections

StyleSheet

Class for style sheets

TypeSelector

Class for type selectors

Functions:

Name Description
apply_style_collection

Apply a style collection to a layout element

apply_style_sheet

Apply a style sheet to a layout element or (layout of) a map

combine_style_sheets

Merge a collection of style sheets into a unique style sheet and return it

ChildSelector dataclass

ChildSelector(parent_selector: Selector, child_selector: Selector)

Bases: Selector

Class for child selectors

Methods:

Name Description
select

Return true if the given layout element satisfies the selector, and false otherwise

select

select(obj: LayoutElement | LayoutElementBuilder, ancestors: Collection[LayoutElement | LayoutElementBuilder])

Return true if the given layout element satisfies the selector, and false otherwise

Source code in src/momapy/styling.py
def select(
    self,
    obj: momapy.core.LayoutElement | momapy.core.LayoutElementBuilder,
    ancestors: collections.abc.Collection[
        momapy.core.LayoutElement | momapy.core.LayoutElementBuilder
    ],
):
    """Return `true` if the given layout element satisfies the selector, and `false` otherwise"""
    if not ancestors:
        return False
    return self.child_selector.select(
        obj, ancestors
    ) and self.parent_selector.select(ancestors[-1], ancestors[:-1])

ClassSelector dataclass

ClassSelector(class_name: str)

Bases: Selector

Class for class selectors

Methods:

Name Description
select

Return true if the given layout element satisfies the selector, and false otherwise

select

select(obj: LayoutElement | LayoutElementBuilder, ancestors: Collection[LayoutElement | LayoutElementBuilder])

Return true if the given layout element satisfies the selector, and false otherwise

Source code in src/momapy/styling.py
def select(
    self,
    obj: momapy.core.LayoutElement | momapy.core.LayoutElementBuilder,
    ancestors: collections.abc.Collection[
        momapy.core.LayoutElement | momapy.core.LayoutElementBuilder
    ],
):
    """Return `true` if the given layout element satisfies the selector, and `false` otherwise"""
    for cls in type(obj).__mro__:
        cls_name = cls.__name__
        # print(cls_name, f"{self.class_name}Builder")
        if (
            cls_name == self.class_name
            or cls_name == f"{self.class_name}Builder"
        ):
            return True
    return False

DescendantSelector dataclass

DescendantSelector(ancestor_selector: Selector, descendant_selector: Selector)

Bases: Selector

Class for descendant selectors

Methods:

Name Description
select

Return true if the given layout element satisfies the selector, and false otherwise

select

select(obj: LayoutElement | LayoutElementBuilder, ancestors: Collection[LayoutElement | LayoutElementBuilder])

Return true if the given layout element satisfies the selector, and false otherwise

Source code in src/momapy/styling.py
def select(
    self,
    obj: momapy.core.LayoutElement | momapy.core.LayoutElementBuilder,
    ancestors: collections.abc.Collection[
        momapy.core.LayoutElement | momapy.core.LayoutElementBuilder
    ],
):
    """Return `true` if the given layout element satisfies the selector, and `false` otherwise"""
    if not ancestors:
        return False
    return self.descendant_selector.select(obj, ancestors) and any(
        [
            self.ancestor_selector.select(ancestor, ancestors[:i])
            for i, ancestor in enumerate(ancestors)
        ]
    )

IdSelector dataclass

IdSelector(id_: str)

Bases: Selector

Class for id selectors

Methods:

Name Description
select

Return true if the given layout element satisfies the selector, and false otherwise

select

select(obj: LayoutElement | LayoutElementBuilder, ancestors: Collection[LayoutElement | LayoutElementBuilder])

Return true if the given layout element satisfies the selector, and false otherwise

Source code in src/momapy/styling.py
def select(
    self,
    obj: momapy.core.LayoutElement | momapy.core.LayoutElementBuilder,
    ancestors: collections.abc.Collection[
        momapy.core.LayoutElement | momapy.core.LayoutElementBuilder
    ],
):
    """Return `true` if the given layout element satisfies the selector, and `false` otherwise"""
    return hasattr(obj, "id_") and obj.id_ == self.id_

OrSelector dataclass

OrSelector(selectors: tuple[Selector])

Bases: Selector

Class for or selectors

Methods:

Name Description
select

Return true if the given layout element satisfies the selector, and false otherwise

select

select(obj: LayoutElement | LayoutElementBuilder, ancestors: Collection[LayoutElement | LayoutElementBuilder])

Return true if the given layout element satisfies the selector, and false otherwise

Source code in src/momapy/styling.py
def select(
    self,
    obj: momapy.core.LayoutElement | momapy.core.LayoutElementBuilder,
    ancestors: collections.abc.Collection[
        momapy.core.LayoutElement | momapy.core.LayoutElementBuilder
    ],
):
    """Return `true` if the given layout element satisfies the selector, and `false` otherwise"""
    return any(
        [selector.select(obj, ancestors) for selector in self.selectors]
    )

Selector dataclass

Selector()

Bases: object

Abstract class for selectors

Methods:

Name Description
select

Return true if the given layout element satisfies the selector, and false otherwise

select abstractmethod

select(obj: LayoutElement | LayoutElementBuilder, ancestors: Collection[LayoutElement | LayoutElementBuilder]) -> bool

Return true if the given layout element satisfies the selector, and false otherwise

Source code in src/momapy/styling.py
@abc.abstractmethod
def select(
    self,
    obj: momapy.core.LayoutElement | momapy.core.LayoutElementBuilder,
    ancestors: collections.abc.Collection[
        momapy.core.LayoutElement | momapy.core.LayoutElementBuilder
    ],
) -> bool:
    """Return `true` if the given layout element satisfies the selector, and `false` otherwise"""
    pass

StyleCollection

Bases: dict

Class for style collections

StyleSheet

Bases: dict

Class for style sheets

Methods:

Name Description
from_file

Read and return a style sheet from a file

from_files

Read and return a style sheet from a collection of files

from_string

Read and return a style sheet from a string

from_file classmethod

from_file(file_path: str) -> StyleSheet

Read and return a style sheet from a file

Source code in src/momapy/styling.py
@classmethod
def from_file(cls, file_path: str) -> "StyleSheet":
    """Read and return a style sheet from a file"""
    style_sheet = _css_document.parse_file(file_path, parse_all=True)[0]
    return style_sheet

from_files classmethod

from_files(file_paths: Collection[str]) -> StyleSheet

Read and return a style sheet from a collection of files

Source code in src/momapy/styling.py
@classmethod
def from_files(
    cls, file_paths: collections.abc.Collection[str]
) -> "StyleSheet":
    """Read and return a style sheet from a collection of files"""
    style_sheets = []
    for file_path in file_paths:
        style_sheet = StyleSheet.from_file(file_path)
        style_sheets.append(style_sheet)
    style_sheet = combine_style_sheets(style_sheets)
    return style_sheet

from_string classmethod

from_string(s: str) -> StyleSheet

Read and return a style sheet from a string

Source code in src/momapy/styling.py
@classmethod
def from_string(cls, s: str) -> "StyleSheet":
    """Read and return a style sheet from a string"""
    style_sheet = _css_document.parse_string(s, parse_all=True)[0]
    return style_sheet

TypeSelector dataclass

TypeSelector(class_name: str)

Bases: Selector

Class for type selectors

Methods:

Name Description
select

Return true if the given layout element satisfies the selector, and false otherwise

select

select(obj: LayoutElement | LayoutElementBuilder, ancestors: Collection[LayoutElement | LayoutElementBuilder])

Return true if the given layout element satisfies the selector, and false otherwise

Source code in src/momapy/styling.py
def select(
    self,
    obj: momapy.core.LayoutElement | momapy.core.LayoutElementBuilder,
    ancestors: collections.abc.Collection[
        momapy.core.LayoutElement | momapy.core.LayoutElementBuilder
    ],
):
    """Return `true` if the given layout element satisfies the selector, and `false` otherwise"""
    obj_cls_name = type(obj).__name__
    return (
        obj_cls_name == self.class_name
        or obj_cls_name == f"{self.class_name}Builder"
    )

apply_style_collection

apply_style_collection(layout_element: LayoutElement | LayoutElementBuilder, style_collection: StyleCollection, strict: bool = True) -> LayoutElement | LayoutElementBuilder

Apply a style collection to a layout element

Source code in src/momapy/styling.py
def apply_style_collection(
    layout_element: (
        momapy.core.LayoutElement | momapy.core.LayoutElementBuilder
    ),
    style_collection: StyleCollection,
    strict: bool = True,
) -> momapy.core.LayoutElement | momapy.core.LayoutElementBuilder:
    """Apply a style collection to a layout element"""
    if not isinstance(layout_element, momapy.builder.Builder):
        layout_element = momapy.builder.builder_from_object(layout_element)
        is_builder = False
    else:
        is_builder = True
    for attribute, value in style_collection.items():
        if hasattr(layout_element, attribute):
            setattr(layout_element, attribute, value)
        else:
            if strict:
                raise AttributeError(
                    f"{type(layout_element)} object has no "
                    f"attribute '{attribute}'"
                )
    if is_builder:
        return layout_element
    return momapy.builder.object_from_builder(layout_element)

apply_style_sheet

apply_style_sheet(map_or_layout_element: Map | LayoutElement | MapBuilder | LayoutElementBuilder, style_sheet: StyleSheet, strict: bool = True, ancestors: Collection[LayoutElement | LayoutElementBuilder] = None) -> Map | LayoutElement | LayoutElementBuilder | MapBuilder

Apply a style sheet to a layout element or (layout of) a map

Source code in src/momapy/styling.py
def apply_style_sheet(
    map_or_layout_element: (
        momapy.core.Map
        | momapy.core.LayoutElement
        | momapy.core.MapBuilder
        | momapy.core.LayoutElementBuilder
    ),
    style_sheet: StyleSheet,
    strict: bool = True,
    ancestors: collections.abc.Collection[
        momapy.core.LayoutElement | momapy.core.LayoutElementBuilder
    ] = None,
) -> (
    momapy.core.Map
    | momapy.core.LayoutElement
    | momapy.core.LayoutElementBuilder
    | momapy.core.MapBuilder
):
    """Apply a style sheet to a layout element or (layout of) a map"""
    if not isinstance(map_or_layout_element, momapy.builder.Builder):
        map_or_layout_element = momapy.builder.builder_from_object(
            map_or_layout_element
        )
        is_builder = False
    else:
        is_builder = True
    if isinstance(map_or_layout_element, momapy.core.MapBuilder):
        layout_element = map_or_layout_element.layout
    else:
        layout_element = map_or_layout_element
    if style_sheet is not None:
        if ancestors is None:
            ancestors = []
        for selector, style_collection in style_sheet.items():
            if selector.select(layout_element, ancestors):
                apply_style_collection(
                    layout_element=layout_element,
                    style_collection=style_collection,
                    strict=strict,
                )
        ancestors = ancestors + [layout_element]
        for child in layout_element.children():
            apply_style_sheet(
                map_or_layout_element=child,
                style_sheet=style_sheet,
                strict=strict,
                ancestors=ancestors,
            )
    if is_builder:
        return map_or_layout_element
    return momapy.builder.object_from_builder(map_or_layout_element)

combine_style_sheets

combine_style_sheets(style_sheets: Collection[StyleSheet]) -> StyleSheet

Merge a collection of style sheets into a unique style sheet and return it

Source code in src/momapy/styling.py
def combine_style_sheets(
    style_sheets: collections.abc.Collection[StyleSheet],
) -> StyleSheet:
    """Merge a collection of style sheets into a unique style sheet and return it"""
    if not style_sheets:
        return None
    output_style_sheet = style_sheets[0]
    for style_sheet in style_sheets[1:]:
        output_style_sheet |= style_sheet
    return output_style_sheet