Receives feed export params (from the 'crawl' or 'runspider' commands), checks for inconsistencies in their quantities and returns a dictionary suitable to be used as the FEEDS setting.
(
settings: BaseSettings,
output: list[str],
*,
overwrite_output: list[str] | None = None,
)
| 142 | |
| 143 | |
| 144 | def feed_process_params_from_cli( |
| 145 | settings: BaseSettings, |
| 146 | output: list[str], |
| 147 | *, |
| 148 | overwrite_output: list[str] | None = None, |
| 149 | ) -> dict[str, dict[str, Any]]: |
| 150 | """ |
| 151 | Receives feed export params (from the 'crawl' or 'runspider' commands), |
| 152 | checks for inconsistencies in their quantities and returns a dictionary |
| 153 | suitable to be used as the FEEDS setting. |
| 154 | """ |
| 155 | valid_output_formats: Iterable[str] = without_none_values( |
| 156 | cast("dict[str, str]", settings.getwithbase("FEED_EXPORTERS")) |
| 157 | ).keys() |
| 158 | |
| 159 | def check_valid_format(output_format: str) -> None: |
| 160 | if output_format not in valid_output_formats: |
| 161 | raise UsageError( |
| 162 | f"Unrecognized output format '{output_format}'. " |
| 163 | f"Set a supported one ({tuple(valid_output_formats)}) " |
| 164 | "after a colon at the end of the output URI (i.e. -o/-O " |
| 165 | "<URI>:<FORMAT>) or as a file extension." |
| 166 | ) |
| 167 | |
| 168 | overwrite = False |
| 169 | if overwrite_output: |
| 170 | if output: |
| 171 | raise UsageError( |
| 172 | "Please use only one of -o/--output and -O/--overwrite-output" |
| 173 | ) |
| 174 | output = overwrite_output |
| 175 | overwrite = True |
| 176 | |
| 177 | result: dict[str, dict[str, Any]] = {} |
| 178 | for element in output: |
| 179 | try: |
| 180 | feed_uri, feed_format = element.rsplit(":", 1) |
| 181 | check_valid_format(feed_format) |
| 182 | except (ValueError, UsageError): |
| 183 | feed_uri = element |
| 184 | feed_format = Path(element).suffix.replace(".", "") |
| 185 | else: |
| 186 | if feed_uri == "-": |
| 187 | feed_uri = "stdout:" |
| 188 | check_valid_format(feed_format) |
| 189 | result[feed_uri] = {"format": feed_format} |
| 190 | if overwrite: |
| 191 | result[feed_uri]["overwrite"] = True |
| 192 | |
| 193 | # FEEDS setting should take precedence over the matching CLI options |
| 194 | result.update(settings.getdict("FEEDS")) |
| 195 | |
| 196 | return result |