Sorts any imports within the provided code stream, outputs to the provided output stream. Returns `True` if anything is modified from the original input stream, otherwise `False`. - **input_stream**: The stream of code with imports that need to be sorted. - **output_stream**: The strea
(
input_stream: TextIO,
output_stream: TextIO,
extension: str | None = None,
config: Config = DEFAULT_CONFIG,
file_path: Path | None = None,
disregard_skip: bool = False,
show_diff: bool | TextIO = False,
raise_on_skip: bool = True,
**config_kwargs: Any,
)
| 131 | |
| 132 | |
| 133 | def sort_stream( |
| 134 | input_stream: TextIO, |
| 135 | output_stream: TextIO, |
| 136 | extension: str | None = None, |
| 137 | config: Config = DEFAULT_CONFIG, |
| 138 | file_path: Path | None = None, |
| 139 | disregard_skip: bool = False, |
| 140 | show_diff: bool | TextIO = False, |
| 141 | raise_on_skip: bool = True, |
| 142 | **config_kwargs: Any, |
| 143 | ) -> bool: |
| 144 | """Sorts any imports within the provided code stream, outputs to the provided output stream. |
| 145 | Returns `True` if anything is modified from the original input stream, otherwise `False`. |
| 146 | |
| 147 | - **input_stream**: The stream of code with imports that need to be sorted. |
| 148 | - **output_stream**: The stream where sorted imports should be written to. |
| 149 | - **extension**: The file extension that contains imports. Defaults to filename extension or py. |
| 150 | - **config**: The config object to use when sorting imports. |
| 151 | - **file_path**: The disk location where the code string was pulled from. |
| 152 | - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file. |
| 153 | - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a |
| 154 | TextIO stream is provided results will be written to it, otherwise no diff will be computed. |
| 155 | - ****config_kwargs**: Any config modifications. |
| 156 | """ |
| 157 | extension = extension or (file_path and file_path.suffix.lstrip(".")) or "py" |
| 158 | if show_diff: |
| 159 | _output_stream = StringIO() |
| 160 | _input_stream = StringIO(input_stream.read()) |
| 161 | changed = sort_stream( |
| 162 | input_stream=_input_stream, |
| 163 | output_stream=_output_stream, |
| 164 | extension=extension, |
| 165 | config=config, |
| 166 | file_path=file_path, |
| 167 | disregard_skip=disregard_skip, |
| 168 | raise_on_skip=raise_on_skip, |
| 169 | **config_kwargs, |
| 170 | ) |
| 171 | _output_stream.seek(0) |
| 172 | _input_stream.seek(0) |
| 173 | show_unified_diff( |
| 174 | file_input=_input_stream.read(), |
| 175 | file_output=_output_stream.read(), |
| 176 | file_path=file_path, |
| 177 | output=output_stream if show_diff is True else show_diff, |
| 178 | color_output=config.color_output, |
| 179 | ) |
| 180 | return changed |
| 181 | |
| 182 | config = _config(path=file_path, config=config, **config_kwargs) |
| 183 | content_source = str(file_path or "Passed in content") |
| 184 | if not disregard_skip and file_path and config.is_skipped(file_path): |
| 185 | raise FileSkipSetting(content_source) |
| 186 | |
| 187 | _internal_output = output_stream |
| 188 | |
| 189 | if config.atomic: |
| 190 | try: |
no test coverage detected
searching dependent graphs…