Raise error instead of trying to fix bad names.
Thanks for the advice, @radl97
This commit is contained in:
parent
04c3486b83
commit
ec64a0b5f1
4 changed files with 23 additions and 3 deletions
|
|
@ -64,7 +64,13 @@ class LargeFileBase(ABC):
|
||||||
keep_last_n: Optional[int] = None,
|
keep_last_n: Optional[int] = None,
|
||||||
cache_only_mode: bool = False,
|
cache_only_mode: bool = False,
|
||||||
):
|
):
|
||||||
self._name = re.sub(r"[^a-zA-Z0-9._-]+", "", name)
|
clean_name = re.sub(r"[^!a-zA-Z0-9._-]+", "", name)
|
||||||
|
if clean_name != name:
|
||||||
|
raise ValueError(
|
||||||
|
f"Given name contains illegal characters, consider changing it to: `{clean_name}`"
|
||||||
|
)
|
||||||
|
|
||||||
|
self._name = name
|
||||||
self._version = version
|
self._version = version
|
||||||
self._mode = mode
|
self._mode = mode
|
||||||
self._keep_last_n = keep_last_n
|
self._keep_last_n = keep_last_n
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ class LargeFileMongo(LargeFileBase):
|
||||||
def _client(self) -> GridFSBucket:
|
def _client(self) -> GridFSBucket:
|
||||||
if self.mongo_connection_string is None or self.mongo_database is None:
|
if self.mongo_connection_string is None or self.mongo_database is None:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Please configure the MongoDB access options by calling LargeFileMongo.configure_credentials or set offline_mode=True in the constructor."
|
"Please configure the MongoDB access options by calling LargeFileMongo.configure_credentials or set cache_only_mode=True in the constructor."
|
||||||
)
|
)
|
||||||
|
|
||||||
db: Database = MongoClient(self.mongo_connection_string)[self.mongo_database]
|
db: Database = MongoClient(self.mongo_connection_string)[self.mongo_database]
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ class LargeFileS3(LargeFileBase):
|
||||||
or self.bucket_name is None
|
or self.bucket_name is None
|
||||||
):
|
):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Please configure the S3 access options by calling LargeFileS3.configure_credentials or set offline_mode=True in the constructor."
|
"Please configure the S3 access options by calling LargeFileS3.configure_credentials or set cache_only_mode=True in the constructor."
|
||||||
)
|
)
|
||||||
|
|
||||||
return boto3.client(
|
return boto3.client(
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,20 @@ def test_uninitialized() -> None:
|
||||||
LargeFileS3("test-file")
|
LargeFileS3("test-file")
|
||||||
|
|
||||||
|
|
||||||
|
def test_bad_file_name() -> None:
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
LargeFileS3("../no-no", cache_only_mode=True)
|
||||||
|
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
LargeFileS3("is this wrong?", cache_only_mode=True)
|
||||||
|
|
||||||
|
with pytest.raises(FileNotFoundError):
|
||||||
|
LargeFileS3("!378378_fer-ef", cache_only_mode=True) # this should work
|
||||||
|
|
||||||
|
with pytest.raises(FileNotFoundError):
|
||||||
|
LargeFileS3("3", cache_only_mode=True) # this should work
|
||||||
|
|
||||||
|
|
||||||
def test_bad_file_modes() -> None:
|
def test_bad_file_modes() -> None:
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
LargeFileS3("test-file", "w", version=3)
|
LargeFileS3("test-file", "w", version=3)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue