Skip to content

Commit 510b0ff

Browse files
Add **kwargs parameters to _mkdir, _cat_file, adhere to fsspec API (#471)
* Adhere to fsspec API * Cover mkdir cases Co-authored-by: Martin Durant <martin.durant@alumni.utoronto.ca>
1 parent e4c16db commit 510b0ff

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

gcsfs/core.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,17 +605,23 @@ def invalidate_cache(self, path=None):
605605

606606
async def _mkdir(
607607
self,
608-
bucket,
608+
path,
609609
acl="projectPrivate",
610610
default_acl="bucketOwnerFullControl",
611611
location=None,
612+
create_parents=True,
613+
**kwargs,
612614
):
613615
"""
614616
New bucket
615617
618+
If path is more than just a bucket, will create bucket if create_parents=True;
619+
otherwise is a noop. If create_parents is False and bucket does not exist,
620+
will produce FileNotFFoundError.
621+
616622
Parameters
617623
----------
618-
bucket: str
624+
path: str
619625
bucket name. If contains '/' (i.e., looks like subdir), will
620626
have no effect because GCS doesn't have real directories.
621627
acl: string, one of bACLs
@@ -627,11 +633,18 @@ async def _mkdir(
627633
If not provided, defaults to `self.default_location`.
628634
You can find a list of all available locations here:
629635
https://cloud.google.com/storage/docs/locations#available-locations
636+
create_parents: bool
637+
If True, creates the bucket in question, if it doesn't already exist
630638
"""
639+
bucket, object = self.split_path(path)
631640
if bucket in ["", "/"]:
632641
raise ValueError("Cannot create root bucket")
633-
if "/" in bucket:
642+
if "/" in path and create_parents and await self._exists(bucket):
643+
# nothing to do
634644
return
645+
if "/" in path and not create_parents and not await self._exists(bucket):
646+
raise FileNotFoundError(bucket)
647+
635648
json_data = {"name": bucket}
636649
location = location or self.default_location
637650
if location:
@@ -758,7 +771,7 @@ def url(self, path):
758771
object = quote_plus(object)
759772
return u.format(self._location, bucket, object)
760773

761-
async def _cat_file(self, path, start=None, end=None):
774+
async def _cat_file(self, path, start=None, end=None, **kwargs):
762775
"""Simple one-shot get of file data"""
763776
u2 = self.url(path)
764777
if start or end:

gcsfs/tests/test_core.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,3 +1035,15 @@ def test_dir_marker(gcs):
10351035
out3 = gcs.info(f"{TEST_BUCKET}/placeholder/")
10361036
assert out2 == out3
10371037
assert out2["type"] == "directory"
1038+
1039+
1040+
def test_mkdir_with_path(gcs):
1041+
with pytest.raises(FileNotFoundError):
1042+
gcs.mkdir("new/path", create_parents=False)
1043+
assert not gcs.exists("new")
1044+
gcs.mkdir("new/path", create_parents=True)
1045+
assert gcs.exists("new")
1046+
1047+
# these lines do nothing, but should not fail
1048+
gcs.mkdir("new/path", create_parents=False)
1049+
gcs.mkdir("new/path", create_parents=True)

0 commit comments

Comments
 (0)