Python API reference

These interfaces operate on loaded CWL process objects. For a complete calling example, see Render with Python.

Converter APIs

Convert a CWL, given its document model, to a PlantUML diagram.

Parameters:
  • cwl_document (Mapping[str, Process]) –

    The Processes object model representing the CWL document.

  • diagram_type (DiagramType) –

    The PlantUML diagram type to render.

  • output_stream (TextIO) –

    The output stream for the PlantUML diagram.

  • workflow_id (str, default: 'main' ) –

    The identifier of the workflow to render.

Source code in src/cwl2puml/__init__.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
def to_puml(
    cwl_document: Mapping[str, Process],
    diagram_type: DiagramType,
    output_stream: TextIO,
    workflow_id: str = "main",
) -> None:
    """Convert a CWL, given its document model, to a PlantUML diagram.

    Args:
        cwl_document: The Processes object model representing the CWL document.
        diagram_type: The PlantUML diagram type to render.
        output_stream: The output stream for the PlantUML diagram.
        workflow_id: The identifier of the workflow to render.
    """
    template = _jinja_environment.get_template(f"{diagram_type.name.lower()}.puml")

    output_stream.write(
        template.render(
            version=_get_version(),
            timestamp=datetime.fromtimestamp(time.time()).isoformat(timespec="milliseconds"),
            workflows=cwl_document.values(),
            workflow_id=workflow_id,
            index=cwl_document,
        )
    )

Bases: Enum

The supported PlantUML diagram types

Source code in src/cwl2puml/__init__.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class DiagramType(Enum):
    """The supported PlantUML diagram types"""

    ACTIVITY = auto()
    """Represents the PlantUML `activity' diagram"""
    COMPONENT = auto()
    """Represents the PlantUML `components' diagram"""
    CLASS = auto()
    """Represents the PlantUML `class' diagram"""
    SEQUENCE = auto()
    """Represents the PlantUML `sequence' diagram"""
    STATE = auto()
    """Represents the PlantUML `state' diagram"""
    OGC_PROCESSES_INPUTS = auto()
    """Represents the PlantUML `JSON' diagram for OGC API Processes - Process description inputs"""
    OGC_PROCESSES_OUTPUTS = auto()
    """Represents the PlantUML `JSON' diagram for OGC API Processes - Process description outputs"""

ACTIVITY = auto() class-attribute instance-attribute

Represents the PlantUML `activity' diagram

CLASS = auto() class-attribute instance-attribute

Represents the PlantUML `class' diagram

COMPONENT = auto() class-attribute instance-attribute

Represents the PlantUML `components' diagram

OGC_PROCESSES_INPUTS = auto() class-attribute instance-attribute

Represents the PlantUML `JSON' diagram for OGC API Processes - Process description inputs

OGC_PROCESSES_OUTPUTS = auto() class-attribute instance-attribute

Represents the PlantUML `JSON' diagram for OGC API Processes - Process description outputs

SEQUENCE = auto() class-attribute instance-attribute

Represents the PlantUML `sequence' diagram

STATE = auto() class-attribute instance-attribute

Represents the PlantUML `state' diagram

Serialize the process inputs as indented OGC API Processes JSON.

Source code in src/cwl2puml/__init__.py
162
163
164
def get_ogc_inputs(process: Process) -> str:
    """Serialize the process inputs as indented OGC API Processes JSON."""
    return _dump_json(BaseCWLtypes2OGCConverter(process).get_inputs())

Serialize the process outputs as indented OGC API Processes JSON.

Source code in src/cwl2puml/__init__.py
167
168
169
def get_ogc_outputs(process: Process) -> str:
    """Serialize the process outputs as indented OGC API Processes JSON."""
    return _dump_json(BaseCWLtypes2OGCConverter(process).get_outputs())

Plugin registration

Converts a CWL, given its document model, to PlantUML diagram(s).

Source code in src/cwl2puml/plugin.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
@transpiler_plugin(
    name="cwl2puml",
    description="Converts a CWL, given its document model, to PlantUML diagram(s).",
    options_model=Cwl2PumlOptions,
)
def cwl2puml(context: TranspilerContext, options: Cwl2PumlOptions) -> None:
    """Converts a CWL, given its document model, to PlantUML diagram(s)."""

    try:
        for workflow in context.get_processes_by_type(
            Workflow, [context.process_id] if context.process_id else None
        ):
            for diagram_type in options.diagrams:
                logger.info(f"Converting to {diagram_type.name.lower()} PlantUML diagram...")
                out = StringIO()
                to_puml(
                    cwl_document=context.document,
                    workflow_id=workflow.id,
                    diagram_type=diagram_type,
                    output_stream=out,
                )

                target = Path(options.output, workflow.id, f"{diagram_type.name.lower()}.puml")
                target.parent.mkdir(parents=True, exist_ok=True)

                clear_output = out.getvalue()
                logger.info(
                    f"Saving PlantUML {diagram_type.name.lower()} diagram to {target.absolute()}..."
                )

                with target.open("w") as f:
                    f.write(clear_output)

                logger.success(
                    f"PlantUML {diagram_type.name.lower()} diagram successfully dumped to {target.absolute()}!"
                )

                if options.convert_image:
                    image_format: str = options.image_format.name.lower()

                    logger.info(
                        f"Converting PlantUML {diagram_type.name.lower()} diagram to '{image_format}'..."
                    )

                    encoded = deflate_and_encode(clear_output)
                    diagram_url = f"https://{options.puml_server}/plantuml/{image_format}/{encoded}"
                    response = requests.get(diagram_url, timeout=30)
                    if HTTPStatus.OK.value == response.status_code:
                        target = Path(
                            target.parent,
                            f"{diagram_type.name.lower()}.{image_format}",
                        )
                        logger.info(
                            f"Saving PlantUML {diagram_type.name.lower()} {image_format} image to {target.absolute()}..."
                        )

                        with target.open("wb") as f:
                            f.write(response.content)

                        logger.success(
                            f"PlantUML {diagram_type.name.lower()} {image_format} image successfully dumped to {target.absolute()}!"
                        )
                    else:
                        raise PluginExecutionError(
                            f"Impossible to render {diagram_type.name.lower()} {image_format} image",
                            f"{options.puml_server} server replied: {response.status_code} {response.reason}",
                            f"Deflated and encoded PlantUML Diagram: {encoded}",
                        )
    except Exception as e:
        raise PluginExecutionError(
            f"An error occurred when serializing to {options.output.absolute()}, see nested exception"
        ) from e

Options

Bases: BaseModel

Options accepted by the cwl2puml plugin.

Source code in src/cwl2puml/plugin.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Cwl2PumlOptions(BaseModel):
    """Options accepted by the cwl2puml plugin."""

    model_config = ConfigDict(extra="forbid")

    diagrams: list[DiagramType] = Field(
        default=list(DiagramType),
        description="The PlantUML diagram to serialize (all the supported kinds, by default).",
    )

    output: Path = Field(default=Path("./docs"), description="Output directory path")
    convert_image: bool = Field(
        default=False, description="Flag to turn on/off the image generation."
    )
    puml_server: str = Field(
        default="uml.planttext.com",
        description="The host of a PlantUML as a service server.",
    )

    image_format: ImageFormat = Field(
        default=ImageFormat.PNG,
        description="The output image format of the PlantUML diagram.",
    )

Bases: Enum

Source code in src/cwl2puml/plugin.py
41
42
43
class ImageFormat(Enum):
    PNG = auto()
    SVG = auto()