Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Refactor genspider slightly so template variables can be overridden (#6470)

Daniel O'Connor67ab8d4650c1
diff --git a/scrapy/commands/genspider.py b/scrapy/commands/genspider.py
index 2649fb23d..6c3713f8f 100644
--- a/scrapy/commands/genspider.py
+++ b/scrapy/commands/genspider.py
@@ -116,26 +116,34 @@ class Command(ScrapyCommand):
if opts.edit:
self.exitcode = os.system(f'scrapy edit "{name}"') # nosec
- def _genspider(
+ def _generate_template_variables(
self,
module: str,
name: str,
url: str,
template_name: str,
- template_file: Union[str, os.PathLike],
- ) -> None:
- """Generate the spider module, based on the given template"""
+ ):
capitalized_module = "".join(s.capitalize() for s in module.split("_"))
- domain = extract_domain(url)
- tvars = {
+ return {
"project_name": self.settings.get("BOT_NAME"),
"ProjectName": string_camelcase(self.settings.get("BOT_NAME")),
"module": module,
"name": name,
"url": url,
- "domain": domain,
+ "domain": extract_domain(url),
"classname": f"{capitalized_module}Spider",
}
+
+ def _genspider(
+ self,
+ module: str,
+ name: str,
+ url: str,
+ template_name: str,
+ template_file: Union[str, os.PathLike],
+ ) -> None:
+ """Generate the spider module, based on the given template"""
+ tvars = self._generate_template_variables(module, name, url, template_name)
if self.settings.get("NEWSPIDER_MODULE"):
spiders_module = import_module(self.settings["NEWSPIDER_MODULE"])
assert spiders_module.__file__