Skip to content

Commit 71fc7bc

Browse files
authored
Fix/directory issue (#155)
* allowing directories to be part of metadata when opening a file * poetry updates * poetry updates * changing variable name to directory prefix * updating packages * Adding tests * patch updates * updated change log * updated __init__.py * removing print
1 parent 56c21c3 commit 71fc7bc

File tree

6 files changed

+735
-686
lines changed

6 files changed

+735
-686
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,9 @@ Changes are grouped as follows
190190
### Fixed
191191
- Dependency updates for source code.
192192
- Included vendor directory as package to be included in the final distribution.
193+
194+
## [0.3.7] - 2024-09-04
195+
196+
### Fixed
197+
- Dependency updates for source code.
198+
- Updated to use directory names from file metadata.

cognite/cdffs/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .spec import CdfFileSystem
66

7-
__version__ = "0.3.6"
7+
__version__ = "0.3.7"
88
__all__ = ["CdfFileSystem"]
99

1010
fsspec.register_implementation(CdfFileSystem.protocol, CdfFileSystem)

cognite/cdffs/spec.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""File System Specification for CDF Files."""
2+
23
import logging
34
import time
45
from concurrent.futures import ThreadPoolExecutor
@@ -147,22 +148,26 @@ def _suffix_exists(self, path: str) -> List:
147148
"""
148149
return [file_part for file_part in path.split("/") if Path(file_part).suffix]
149150

150-
def split_path(self, path: str, validate_suffix: bool = True) -> Tuple[str, str, str]:
151+
def split_path(self, path: str, validate_suffix: bool = True, directory_prefix: str = "") -> Tuple[str, str, str]:
151152
"""Split the path and extract root_dir, external_id and a filename.
152153
153154
Args:
154155
path (str): Path to split.
155156
validate_suffix (bool): Flag to validate if the file name must have a valid suffix.
157+
directory_prefix(str): Directory prefix.
156158
157159
Returns:
158160
Tuple: Returns a tuple with root_dir, external_id_prefix and external_id.
159161
160162
Raises:
161163
ValueError: When an invalid input path is given.
162164
"""
163-
if self.file_metadata.directory:
164-
root_dir = self.file_metadata.directory.strip("/")
165-
external_id = path.replace(root_dir, "").lstrip("/")
165+
if directory_prefix or self.file_metadata.directory:
166+
root_dir = directory_prefix.strip("/") if directory_prefix else self.file_metadata.directory.strip("/")
167+
if root_dir in path:
168+
external_id = path.replace(root_dir, "").lstrip("/")
169+
else:
170+
external_id = path.split("/")[-1]
166171
external_id_prefix = Path(external_id).parts[0]
167172

168173
elif self._suffix_exists(path):
@@ -505,7 +510,12 @@ def open(
505510
Returns:
506511
CdfFile: An instance of a 'CdfFile' to allow reading/writing file to Cdf.
507512
"""
508-
root_dir, _, external_id = self.split_path(path)
513+
if isinstance(kwargs.get("file_metadata"), FileMetadata):
514+
file_metadata: FileMetadata = kwargs.get("file_metadata")
515+
root_dir, _, external_id = self.split_path(path, directory_prefix=file_metadata.directory)
516+
else:
517+
root_dir, _, external_id = self.split_path(path)
518+
509519
return CdfFile(
510520
self,
511521
self.cognite_client,

0 commit comments

Comments
 (0)