Skip to content

Commit d21c196

Browse files
committed
🎯 feat: add detail method for model dump with alias.
1 parent 975c9b4 commit d21c196

3 files changed

Lines changed: 31 additions & 25 deletions

File tree

src/ddeutil/workflow/job.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@
4848
from functools import lru_cache
4949
from textwrap import dedent
5050
from threading import Event
51-
from typing import Annotated, Any, Literal, Optional, Union
51+
from typing import Annotated, Any, Optional, Union
5252

5353
from ddeutil.core import freeze_args
5454
from pydantic import BaseModel, Discriminator, Field, SecretStr, Tag
55+
from pydantic.functional_serializers import field_serializer
5556
from pydantic.functional_validators import field_validator, model_validator
5657
from typing_extensions import Self
5758

@@ -263,24 +264,20 @@ class BaseRunsOn(BaseModel): # pragma: no cov
263264
object and override execute method.
264265
"""
265266

266-
type: RunsOn = Field(description="A runs-on type.")
267+
type: RunsOn = LOCAL
267268
args: DictData = Field(
268269
default_factory=dict,
269-
alias="with",
270270
description=(
271271
"An argument that pass to the runs-on execution function. This "
272272
"args will override by this child-model with specific args model."
273273
),
274+
alias="with",
274275
)
275276

276277

277278
class OnLocal(BaseRunsOn): # pragma: no cov
278279
"""Runs-on local."""
279280

280-
type: Literal[RunsOn.LOCAL] = Field(
281-
default=RunsOn.LOCAL, validate_default=True
282-
)
283-
284281

285282
class SelfHostedArgs(BaseModel):
286283
"""Self-Hosted arguments."""
@@ -292,9 +289,7 @@ class SelfHostedArgs(BaseModel):
292289
class OnSelfHosted(BaseRunsOn): # pragma: no cov
293290
"""Runs-on self-hosted."""
294291

295-
type: Literal[RunsOn.SELF_HOSTED] = Field(
296-
default=RunsOn.SELF_HOSTED, validate_default=True
297-
)
292+
type: RunsOn = SELF_HOSTED
298293
args: SelfHostedArgs = Field(alias="with")
299294

300295

@@ -310,9 +305,7 @@ class AzBatchArgs(BaseModel):
310305

311306
class OnAzBatch(BaseRunsOn): # pragma: no cov
312307

313-
type: Literal[RunsOn.AZ_BATCH] = Field(
314-
default=RunsOn.AZ_BATCH, validate_default=True
315-
)
308+
type: RunsOn = AZ_BATCH
316309
args: AzBatchArgs = Field(alias="with")
317310

318311

@@ -331,23 +324,21 @@ class DockerArgs(BaseModel):
331324
class OnDocker(BaseRunsOn): # pragma: no cov
332325
"""Runs-on Docker container."""
333326

334-
type: Literal[RunsOn.DOCKER] = Field(
335-
default=RunsOn.DOCKER, validate_default=True
336-
)
337-
args: DockerArgs = Field(alias="with", default_factory=DockerArgs)
327+
type: RunsOn = DOCKER
328+
args: DockerArgs = Field(default_factory=DockerArgs, alias="with")
338329

339330

340331
def get_discriminator_runs_on(model: dict[str, Any]) -> RunsOn:
341332
"""Get discriminator of the RunsOn models."""
342333
t: str = model.get("type")
343-
return RunsOn(t) if t else RunsOn.LOCAL
334+
return RunsOn(t) if t else LOCAL
344335

345336

346337
RunsOnModel = Annotated[
347338
Union[
348-
Annotated[OnSelfHosted, Tag(RunsOn.SELF_HOSTED)],
349-
Annotated[OnDocker, Tag(RunsOn.DOCKER)],
350-
Annotated[OnLocal, Tag(RunsOn.LOCAL)],
339+
Annotated[OnSelfHosted, Tag(SELF_HOSTED)],
340+
Annotated[OnDocker, Tag(DOCKER)],
341+
Annotated[OnLocal, Tag(LOCAL)],
351342
],
352343
Discriminator(get_discriminator_runs_on),
353344
]
@@ -490,6 +481,10 @@ def __validate_job_id__(self) -> Self:
490481

491482
return self
492483

484+
@field_serializer("runs_on")
485+
def __serialize_runs_on(self, value: RunsOnModel):
486+
return value.model_dump(by_alias=True)
487+
493488
def stage(self, stage_id: str) -> Stage:
494489
"""Return stage instance that exists in this job via passing an input
495490
stage ID.

src/ddeutil/workflow/workflow.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class Workflow(BaseModel):
145145
description="A parameters that need to use on this workflow.",
146146
)
147147
on: Event = Field(
148-
default_factory=list,
148+
default_factory=Event,
149149
description="An events for this workflow.",
150150
)
151151
jobs: dict[str, Job] = Field(
@@ -284,14 +284,18 @@ def __validate_jobs_need__(self) -> Self:
284284

285285
return self
286286

287-
def md(self) -> str: # pragma: no cov
287+
def detail(self) -> DictData: # pragma: no cov
288+
"""Return the detail of this workflow for generate markdown."""
289+
return self.model_dump(by_alias=True)
290+
291+
def md(self, author: Optional[str] = None) -> str: # pragma: no cov
288292
"""Generate the markdown template."""
289293

290294
def align_newline(value: str) -> str:
291295
return value.rstrip("\n").replace("\n", "\n ")
292296

293297
info: str = (
294-
f"| Author: nobody "
298+
f"| Author: {author or 'nobody'} "
295299
f"| created_at: `{self.created_at:%Y-%m-%d %H:%M:%S}` "
296300
f"| updated_at: `{self.updated_dt:%Y-%m-%d %H:%M:%S}` |\n"
297301
f"| --- | --- | --- |"
@@ -412,7 +416,7 @@ def validate_release(self, dt: datetime) -> datetime:
412416
dt = dt.replace(tzinfo=UTC)
413417

414418
release: datetime = replace_sec(dt.astimezone(UTC))
415-
if not self.on:
419+
if not self.on.schedule:
416420
return release
417421

418422
for on in self.on.schedule:

tests/test_workflow.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,13 @@ def test_workflow_parameterize(test_path):
427427
workflow.parameterize({"foo": "bar"})
428428

429429

430+
def test_workflow_detail(test_path):
431+
workflow = Workflow.from_conf(
432+
"stream-workflow", path=test_path / "conf/example"
433+
)
434+
print(workflow.detail())
435+
436+
430437
def test_workflow_markdown(test_path):
431438
workflow = Workflow.from_conf(
432439
"stream-workflow", path=test_path / "conf/example"

0 commit comments

Comments
 (0)