Skip to content

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

Merge branch 'master' into path_object_error_#5739

Alex5433015a25fb
diff --cc scrapy/pipelines/files.py
index 94b0b1b70,01a9c41fe..143e6300d
--- a/scrapy/pipelines/files.py
+++ b/scrapy/pipelines/files.py
@@@ -46,10 -41,9 +46,10 @@@ class FileException(Exception)
class FSFilesStore:
- def __init__(self, basedir: str):
+ def __init__(self, basedir: Union[str, PathLike]):
+ basedir = _to_string(basedir)
- if '://' in basedir:
- basedir = basedir.split('://', 1)[1]
+ if "://" in basedir:
+ basedir = basedir.split("://", 1)[1]
self.basedir = basedir
self._mkdir(Path(self.basedir))
self.created_directories: DefaultDict[str, Set[str]] = defaultdict(set)
@@@ -66,13 -60,13 +66,13 @@@
except os.error:
return {}
- with absolute_path.open('rb') as f:
+ with absolute_path.open("rb") as f:
checksum = md5sum(f)
- return {'last_modified': last_modified, 'checksum': checksum}
+ return {"last_modified": last_modified, "checksum": checksum}
- def _get_filesystem_path(self, path: str) -> Path:
- path_comps = path.split("/")
+ def _get_filesystem_path(self, path: Union[str, PathLike]) -> Path:
- path_comps = _to_string(path).split('/')
++ path_comps = _to_string(path).split("/")
return Path(self.basedir, *path_comps)
def _mkdir(self, dirname: Path, domain: Optional[str] = None):
@@@ -318,30 -324,28 +330,28 @@@ class FilesPipeline(MediaPipeline)
MEDIA_NAME = "file"
EXPIRES = 90
STORE_SCHEMES = {
- '': FSFilesStore,
- 'file': FSFilesStore,
- 's3': S3FilesStore,
- 'gs': GCSFilesStore,
- 'ftp': FTPFilesStore
+ "": FSFilesStore,
+ "file": FSFilesStore,
+ "s3": S3FilesStore,
+ "gs": GCSFilesStore,
+ "ftp": FTPFilesStore,
}
- DEFAULT_FILES_URLS_FIELD = 'file_urls'
- DEFAULT_FILES_RESULT_FIELD = 'files'
+ DEFAULT_FILES_URLS_FIELD = "file_urls"
+ DEFAULT_FILES_RESULT_FIELD = "files"
def __init__(self, store_uri, download_func=None, settings=None):
+ store_uri = _to_string(store_uri)
if not store_uri:
raise NotConfigured
if isinstance(settings, dict) or settings is None:
settings = Settings(settings)
-
cls_name = "FilesPipeline"
self.store = self._get_store(store_uri)
- resolve = functools.partial(self._key_for_pipe,
- base_class_name=cls_name,
- settings=settings)
- self.expires = settings.getint(
- resolve('FILES_EXPIRES'), self.EXPIRES
+ resolve = functools.partial(
+ self._key_for_pipe, base_class_name=cls_name, settings=settings
)
+ self.expires = settings.getint(resolve("FILES_EXPIRES"), self.EXPIRES)
if not hasattr(self, "FILES_URLS_FIELD"):
self.FILES_URLS_FIELD = self.DEFAULT_FILES_URLS_FIELD
if not hasattr(self, "FILES_RESULT_FIELD"):
diff --cc tests/test_pipeline_files.py
index 5280ab0d2,43942e53e..76c16e57f
--- a/tests/test_pipeline_files.py
+++ b/tests/test_pipeline_files.py
@@@ -402,29 -481,10 +481,27 @@@ class FilesPipelineTestCaseCustomSettin
for pipe_attr, settings_attr, pipe_inst_attr in self.file_cls_attr_settings_map:
expected_value = settings.get(settings_attr)
- self.assertEqual(getattr(pipeline_cls, pipe_inst_attr),
- expected_value)
+ self.assertEqual(getattr(pipeline_cls, pipe_inst_attr), expected_value)
+ def test_file_pipeline_using_pathlike_objects(self):
+
+ class CustomFilesPipelineWithPathLikeDir(FilesPipeline):
+ def file_path(self, request, response=None, info=None, *, item=None):
+ return Path('subdir') / Path(request.url).name
+
+ pipeline = CustomFilesPipelineWithPathLikeDir.from_settings(
+ Settings({'FILES_STORE': Path('./Temp')})
+ )
+ request = Request("http://example.com/image01.jpg")
+ self.assertEqual(pipeline.file_path(request), Path('subdir/image01.jpg'))
+
+ def test_files_store_constructor_with_pathlike_object(self):
+ path = Path('./FileDir')
+ fs_store = FSFilesStore(path)
+ self.assertEqual(fs_store.basedir, str(path))
+
class TestS3FilesStore(unittest.TestCase):
-
@defer.inlineCallbacks
def test_persist(self):
skip_if_no_boto()