mirror of https://github.com/scrapy/scrapy.git
Support Path Objects Issue #5739
This commit is contained in:
parent
4af5a06842
commit
b1dd893fbb
|
|
@ -14,7 +14,7 @@ from contextlib import suppress
|
||||||
from ftplib import FTP
|
from ftplib import FTP
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import DefaultDict, Optional, Set
|
from typing import DefaultDict, Optional, Set, Union
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from itemadapter import ItemAdapter
|
from itemadapter import ItemAdapter
|
||||||
|
|
@ -41,7 +41,8 @@ class FileException(Exception):
|
||||||
|
|
||||||
|
|
||||||
class FSFilesStore:
|
class FSFilesStore:
|
||||||
def __init__(self, basedir: str):
|
def __init__(self, basedir: Union[str, os.PathLike]):
|
||||||
|
basedir = str(basedir) # support Path object
|
||||||
if '://' in basedir:
|
if '://' in basedir:
|
||||||
basedir = basedir.split('://', 1)[1]
|
basedir = basedir.split('://', 1)[1]
|
||||||
self.basedir = basedir
|
self.basedir = basedir
|
||||||
|
|
@ -65,8 +66,8 @@ class FSFilesStore:
|
||||||
|
|
||||||
return {'last_modified': last_modified, 'checksum': checksum}
|
return {'last_modified': last_modified, 'checksum': checksum}
|
||||||
|
|
||||||
def _get_filesystem_path(self, path: str) -> Path:
|
def _get_filesystem_path(self, path: Union[str, os.PathLike]) -> Path:
|
||||||
path_comps = path.split('/')
|
path_comps = str(path).split('/')
|
||||||
return Path(self.basedir, *path_comps)
|
return Path(self.basedir, *path_comps)
|
||||||
|
|
||||||
def _mkdir(self, dirname: Path, domain: Optional[str] = None):
|
def _mkdir(self, dirname: Path, domain: Optional[str] = None):
|
||||||
|
|
|
||||||
|
|
@ -405,6 +405,23 @@ class FilesPipelineTestCaseCustomSettings(unittest.TestCase):
|
||||||
self.assertEqual(getattr(pipeline_cls, pipe_inst_attr),
|
self.assertEqual(getattr(pipeline_cls, pipe_inst_attr),
|
||||||
expected_value)
|
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):
|
class TestS3FilesStore(unittest.TestCase):
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue