Skip to content

Commit ec79a99

Browse files
committed
fixed up task_help
1 parent 53cc36c commit ec79a99

File tree

3 files changed

+66
-18
lines changed

3 files changed

+66
-18
lines changed

docs/source/tutorial/tst.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
from pathlib import Path
3+
from fileformats.generic import File
4+
from pydra.compose import shell
5+
from pydra.utils import print_help
6+
7+
8+
# Arguments to the callable function can be one of
9+
def get_file_size(out_file: Path) -> int:
10+
"""Calculate the file size"""
11+
result = os.stat(out_file)
12+
return result.st_size
13+
14+
15+
ACommand = shell.define(
16+
"a-command",
17+
inputs={
18+
"in_file": shell.arg(type=File, help="output file", argstr="", position=-2)
19+
},
20+
outputs={
21+
"out_file": shell.outarg(type=File, help="output file", argstr="", position=-1),
22+
"out_file_size": {
23+
"type": int,
24+
"help": "size of the output directory",
25+
"callable": get_file_size,
26+
},
27+
},
28+
)
29+
30+
print_help(ACommand)

pydra/engine/tests/test_task.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,18 @@ def TestFunc(a: int, b: float = 0.1) -> float:
9292

9393
help = task_help(funky)
9494
assert help == [
95+
"-------------------------",
9596
"Help for 'TestFunc' tasks",
9697
"-------------------------",
98+
"",
9799
"Inputs:",
98100
"- a: int",
99101
"- b: float (default: 0.1)",
100102
"- function: Callable (default: TestFunc())",
103+
"",
101104
"Outputs:",
102105
"- out_out: float",
106+
"",
103107
]
104108

105109

@@ -152,14 +156,18 @@ def TestFunc(
152156

153157
help = task_help(funky)
154158
assert help == [
159+
"-------------------------",
155160
"Help for 'TestFunc' tasks",
156161
"-------------------------",
162+
"",
157163
"Inputs:",
158164
"- a: float",
159165
"- function: Callable (default: TestFunc())",
166+
"",
160167
"Outputs:",
161168
"- fractional: float",
162169
"- integer: int",
170+
"",
163171
]
164172

165173

@@ -457,14 +465,18 @@ def TestFunc(a, b) -> int:
457465
help = task_help(funky)
458466

459467
assert help == [
468+
"-------------------------",
460469
"Help for 'TestFunc' tasks",
461470
"-------------------------",
471+
"",
462472
"Inputs:",
463473
"- a: Any",
464474
"- b: Any",
465475
"- function: Callable (default: TestFunc())",
476+
"",
466477
"Outputs:",
467478
"- out: int",
479+
"",
468480
]
469481

470482

@@ -500,15 +512,19 @@ def TestFunc(a, b) -> tuple[int, int]:
500512
help = task_help(funky)
501513

502514
assert help == [
515+
"-------------------------",
503516
"Help for 'TestFunc' tasks",
504517
"-------------------------",
518+
"",
505519
"Inputs:",
506520
"- a: Any",
507521
"- b: Any",
508522
"- function: Callable (default: TestFunc())",
523+
"",
509524
"Outputs:",
510525
"- out1: int",
511526
"- out2: int",
527+
"",
512528
]
513529

514530

pydra/utils/general.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -369,24 +369,24 @@ def from_list_if_single(obj: ty.Any) -> ty.Any:
369369
return obj
370370

371371

372-
def fold_text(text: str, width: int = 70) -> str:
373-
"""Fold text to a given width, respecting word boundaries."""
374-
if not isinstance(text, str):
375-
return text
372+
def wrap_text(text: str, width: int = 70, indent_size=4) -> str:
373+
"""Wraps text to a given width, respecting word boundaries."""
374+
indent = " " * indent_size
376375
if len(text) <= width:
377-
return text
376+
return indent + text
378377
lines = []
379378
for line in text.splitlines():
380-
while len(line) > width:
379+
if len(line) > width:
381380
words = line.split()
382-
split_line = ""
381+
split_line = indent
383382
for word in words:
384383
if len(split_line) + len(word) + 1 > width:
385-
lines.append(split_line.strip())
386-
split_line = ""
384+
lines.append(split_line.rstrip())
385+
split_line = indent
387386
split_line += word + " "
388-
lines.append(split_line.strip())
389-
lines.append(line)
387+
lines.append(split_line.rstrip())
388+
else:
389+
lines.append(indent + line)
390390
return "\n".join(lines)
391391

392392

@@ -405,27 +405,29 @@ def field_listing(field: "Field") -> str:
405405
type_str = str(field.type)
406406
field_str = f"- {field.name}: {type_str}"
407407
if isinstance(field.default, attrs.Factory):
408-
field_str += f" (factory: {field.default.factory.__name__})"
408+
field_str += f" (factory: {field.default.factory.__name__}())"
409409
elif callable(field.default):
410410
field_str += f" (default: {field.default.__name__}())"
411411
elif field.default is not NO_DEFAULT:
412-
field_str += f" (default: {field.default})"
412+
field_str += f" (default: {field.default!r})"
413413
if field.help:
414-
field_str += f"\n {fold_text(field.help, width=line_width - 4)}"
414+
field_str += f"\n{wrap_text(field.help, width=line_width, indent_size=4)}"
415415
return field_str
416416

417-
lines = [f"Help for '{task_type.__name__}' tasks"]
418-
lines.append("-" * len(lines[0]))
417+
header = f"Help for '{task_type.__name__}' tasks"
418+
hyphen_line = "-" * len(header)
419+
lines = [hyphen_line, header, hyphen_line]
419420
inputs = task_fields(task_type)
420421
if inputs:
421-
lines.append("Inputs:")
422+
lines.extend(["", "Inputs:"])
422423
for inpt in inputs:
423424
lines.append(field_listing(inpt))
424425
outputs = task_fields(task_type.Outputs)
425426
if outputs:
426-
lines.append("Outputs:")
427+
lines.extend(["", "Outputs:"])
427428
for output in outputs:
428429
lines.append(field_listing(output))
430+
lines.append("")
429431
return lines
430432

431433

0 commit comments

Comments
 (0)