subprocess
SubprocessDataclass
Bases: DataclassMixin
Mixin class providing a means of converting dataclass fields to command-line arguments that can be used to make a subprocess call.
Per-field settings can be passed into the metadata
argument of each dataclasses.field
. See SubprocessDataclassFieldSettings
for the full list of settings.
Source code in fancy_dataclass/subprocess.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
get_arg(name, suppress_defaults=False)
Gets the command-line arguments for the given dataclass field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
Name of dataclass field |
required |
suppress_defaults |
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
List[str]
|
List of command-line args corresponding to the field |
Source code in fancy_dataclass/subprocess.py
get_args(suppress_defaults=False)
Converts dataclass fields to a list of command-line arguments for a subprocess call.
This includes the executable name itself as the first argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
suppress_defaults |
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
List[str]
|
List of command-line args corresponding to the dataclass fields |
Source code in fancy_dataclass/subprocess.py
get_executable()
Gets the name of an executable to run with the appropriate arguments.
By default, this obtains the name of the executable as follows:
- If the class settings specify an
exec
member, uses that. - Otherwise, returns the value of the first dataclass field whose
exec
metadata flag is set toTrue
, andNone
otherwise.
Returns:
Type | Description |
---|---|
Optional[str]
|
Name of the executable to run |
Raises:
Type | Description |
---|---|
ValueError
|
If the executable is not a string |
Source code in fancy_dataclass/subprocess.py
run_subprocess(**kwargs)
Executes the full subprocess command corresponding to the dataclass parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kwargs |
Any
|
Keyword arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
CompletedProcess
|
|
Raises:
Type | Description |
---|---|
ValueError
|
If no executable was found from the |
Source code in fancy_dataclass/subprocess.py
SubprocessDataclassFieldSettings
dataclass
Bases: FieldSettings
Settings for SubprocessDataclass
fields.
Each field may define a metadata
dict containing any of the following entries:
exec
: ifTrue
, use this field as the name of the executable, rather than an argumentargs
: command-line arguments corresponding to the field- If a non-empty list, use the first entry as the argument name if it starts with a dash, and treat it as a positional argument otherwise
- If
None
, use the field name prefixed by one dash (if single letter) or two dashes, with underscores replaced by dashes - If an empty list, exclude this field from the arguments
- If the field type is
bool
, will provide the argument as a flag if the value isTrue
, and omit it otherwise
Source code in fancy_dataclass/subprocess.py
SubprocessDataclassSettings
dataclass
Bases: DataclassMixinSettings
Class-level settings for the SubprocessDataclass
mixin.
Subclasses of SubprocessDataclass
may set the following fields as keyword arguments during inheritance:
exec
: name of command-line executable to call