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
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
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, if there is one.
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
execmember, uses that. - Otherwise, returns the value of the first dataclass field whose
execmetadata flag is set toTrue, andNoneotherwise.
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
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 argumentoption_name: command-line option name for this field- If a string, use this as the option name, prepending with one or two dashes if not provided
- If
None, use the field name prefixed by one dash (if single letter) or two dashes, with underscores replaced by dashes - If the field type is
bool, will provide the argument as a flag if the value isTrue, and omit it otherwise
subprocess_exclude:- If
True, exclude this field in the subprocess args - If
False, include this field in the subprocess args - If
None(default), exclude this field in the subprocess args if the field is aClassVar
- If
subprocess_positional: ifTrue, make this a positional argument rather than an optionsubprocess_flag: ifFalseand the field type isbool, treat the field as a regular option rather than a flagrepeat_option_name: ifTrueand the field type is a list, repeat the option name for each list value- Examples:
- If
False(default), generate--my-option value1 value2 value3 - If
True, generate--my-option value1 --my-option value2 --my-option value3
- If
- Examples:
Source code in fancy_dataclass/subprocess.py
SubprocessDataclassSettings
Bases: MixinSettings
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