MCPcopy
hub / github.com/scrapy/scrapy / _spider_exists

Method _spider_exists

scrapy/commands/genspider.py:192–224  ·  view source on GitHub ↗
(self, name: str)

Source from the content-addressed store, hash-verified

190 )
191
192 def _spider_exists(self, name: str) -> bool:
193 assert self.settings is not None
194 if not self.settings.get("NEWSPIDER_MODULE"):
195 # if run as a standalone command and file with same filename already exists
196 path = Path(name + ".py")
197 if path.exists():
198 print(f"{path.resolve()} already exists")
199 return True
200 return False
201
202 spider_loader = get_spider_loader(self.settings)
203 try:
204 spidercls = spider_loader.load(name)
205 except KeyError:
206 pass
207 else:
208 # if spider with same name exists
209 print(
210 f"Spider {name!r} already exists in module:\n",
211 f" {spidercls.__module__}",
212 )
213 return True
214
215 # a file with the same name exists in the target directory
216 spiders_module = import_module(self.settings["NEWSPIDER_MODULE"])
217 spiders_dir = Path(cast("str", spiders_module.__file__)).parent
218 spiders_dir_abs = spiders_dir.resolve()
219 path = spiders_dir_abs / (name + ".py")
220 if path.exists():
221 print(f"{path} already exists")
222 return True
223
224 return False
225
226 @property
227 def templates_dir(self) -> str:

Callers 1

runMethod · 0.95

Calls 3

get_spider_loaderFunction · 0.90
getMethod · 0.45
loadMethod · 0.45

Tested by

no test coverage detected