|
11 | 11 | "cell_type": "markdown", |
12 | 12 | "metadata": {}, |
13 | 13 | "source": [ |
14 | | - "## Command-line template\n", |
| 14 | + "## Command-line templates\n", |
15 | 15 | "\n", |
16 | | - "Define a shell-task specification using a command template string. Input and output fields are both specified by placing the name of the field within enclosing `<` and `>`. Outputs are differentiated by the `out|` prefix." |
| 16 | + "Shell task specs can be defined using from string templates that resemble the command-line usage examples typically used in in-line help. Therefore, they can be quick and intuitive way to specify a shell task. For example, a simple spec for the copy command `cp` that omits optional flags," |
17 | 17 | ] |
18 | 18 | }, |
19 | 19 | { |
20 | 20 | "cell_type": "code", |
21 | | - "execution_count": 5, |
| 21 | + "execution_count": 2, |
| 22 | + "metadata": {}, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "from pydra.design import shell\n", |
| 26 | + "\n", |
| 27 | + "Cp = shell.define(\"cp <in_file> <out|destination>\")" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "Input and output fields are both specified by placing the name of the field within enclosing `<` and `>`. Outputs are differentiated by the `out|` prefix.\n", |
| 35 | + "\n", |
| 36 | + "This shell task can then be run just as a Python task would be run, first parameterising it, then executing" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": 9, |
22 | 42 | "metadata": {}, |
23 | 43 | "outputs": [ |
24 | 44 | { |
25 | 45 | "name": "stdout", |
26 | 46 | "output_type": "stream", |
27 | 47 | "text": [ |
28 | | - "[outarg(name='out_file', type=<class 'fileformats.generic.fsobject.FsObject'>, default=EMPTY, help_string='', requires=[], converter=None, validator=None, xor=(), copy_mode=<CopyMode.any: 15>, copy_collation=<CopyCollation.any: 0>, copy_ext_decomp=<ExtensionDecomposition.single: 1>, readonly=False, argstr='', position=1, sep=None, allowed_values=None, container_path=False, formatter=None, path_template='out_file'), arg(name='executable', type=typing.Union[str, typing.Sequence[str]], default='cp', help_string=\"the first part of the command, can be a string, e.g. 'ls', or a list, e.g. ['ls', '-l', 'dirname']\", requires=[], converter=None, validator=<min_len validator for 1>, xor=(), copy_mode=<CopyMode.any: 15>, copy_collation=<CopyCollation.any: 0>, copy_ext_decomp=<ExtensionDecomposition.single: 1>, readonly=False, argstr='', position=0, sep=None, allowed_values=None, container_path=False, formatter=None)]\n" |
29 | | - ] |
30 | | - }, |
31 | | - { |
32 | | - "ename": "TypeError", |
33 | | - "evalue": "cp.__init__() got an unexpected keyword argument 'in_file'", |
34 | | - "output_type": "error", |
35 | | - "traceback": [ |
36 | | - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
37 | | - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", |
38 | | - "Cell \u001b[0;32mIn[5], line 13\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[38;5;28mprint\u001b[39m(list_fields(Cp))\n\u001b[1;32m 12\u001b[0m \u001b[38;5;66;03m# Parameterise the task spec\u001b[39;00m\n\u001b[0;32m---> 13\u001b[0m cp \u001b[38;5;241m=\u001b[39m \u001b[43mCp\u001b[49m\u001b[43m(\u001b[49m\u001b[43min_file\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtest_file\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout_file\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m./out.txt\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 15\u001b[0m \u001b[38;5;66;03m# Print the cmdline to be run to double check\u001b[39;00m\n\u001b[1;32m 16\u001b[0m \u001b[38;5;28mprint\u001b[39m(cp\u001b[38;5;241m.\u001b[39mcmdline)\n", |
39 | | - "\u001b[0;31mTypeError\u001b[0m: cp.__init__() got an unexpected keyword argument 'in_file'" |
| 48 | + "Command-line to be run: cp /var/folders/mz/yn83q2fd3s758w1j75d2nnw80000gn/T/tmpnw4kzvv0/in.txt /var/folders/mz/yn83q2fd3s758w1j75d2nnw80000gn/T/tmpnw4kzvv0/out.txt\n", |
| 49 | + "Contents of copied file ('/var/folders/mz/yn83q2fd3s758w1j75d2nnw80000gn/T/tmpnw4kzvv0/out.txt'): 'Contents to be copied'\n" |
40 | 50 | ] |
41 | 51 | } |
42 | 52 | ], |
43 | 53 | "source": [ |
| 54 | + "from pathlib import Path\n", |
| 55 | + "from tempfile import mkdtemp\n", |
44 | 56 | "from pydra.design import shell\n", |
45 | 57 | "from pydra.engine.helpers import list_fields\n", |
46 | 58 | "\n", |
47 | | - "test_file = \"./in.txt\"\n", |
| 59 | + "# Make a test file to copy\n", |
| 60 | + "test_dir = Path(mkdtemp())\n", |
| 61 | + "test_file = test_dir / \"in.txt\"\n", |
48 | 62 | "with open(test_file, \"w\") as f:\n", |
49 | | - " f.write(\"this is a test file\\n\")\n", |
50 | | - "\n", |
51 | | - "# Define the shell-command task specification\n", |
52 | | - "Cp = shell.define(\"cp <in_file> <out|out_file>\")\n", |
| 63 | + " f.write(\"Contents to be copied\")\n", |
53 | 64 | "\n", |
54 | 65 | "# Parameterise the task spec\n", |
55 | | - "cp = Cp(in_file=test_file, out_file=\"./out.txt\")\n", |
| 66 | + "cp = Cp(in_file=test_file, destination=test_dir / \"out.txt\")\n", |
56 | 67 | "\n", |
57 | 68 | "# Print the cmdline to be run to double check\n", |
58 | | - "print(cp.cmdline)\n", |
| 69 | + "print(f\"Command-line to be run: {cp.cmdline}\")\n", |
59 | 70 | "\n", |
60 | 71 | "# Run the shell-comand task\n", |
61 | | - "cp()" |
| 72 | + "result = cp()\n", |
| 73 | + "\n", |
| 74 | + "print(\n", |
| 75 | + " f\"Contents of copied file ('{result.output.destination}'): \"\n", |
| 76 | + " f\"'{Path(result.output.destination).read_text()}'\"\n", |
| 77 | + ")" |
62 | 78 | ] |
63 | 79 | }, |
64 | 80 | { |
|
70 | 86 | }, |
71 | 87 | { |
72 | 88 | "cell_type": "code", |
73 | | - "execution_count": null, |
| 89 | + "execution_count": 10, |
74 | 90 | "metadata": {}, |
75 | | - "outputs": [], |
| 91 | + "outputs": [ |
| 92 | + { |
| 93 | + "name": "stdout", |
| 94 | + "output_type": "stream", |
| 95 | + "text": [ |
| 96 | + "cp /var/folders/mz/yn83q2fd3s758w1j75d2nnw80000gn/T/tmpnw4kzvv0/in.txt True\n" |
| 97 | + ] |
| 98 | + } |
| 99 | + ], |
76 | 100 | "source": [ |
77 | 101 | "cp = Cp(in_file=test_file)\n", |
78 | 102 | "print(cp.cmdline)" |
|
0 commit comments