in src/huggingface_hub/hf_file_system.py [0:0]
def resolve_path(self, path: str, revision: Optional[str] = None) -> HfFileSystemResolvedPath:
"""
Resolve a Hugging Face file system path into its components.
Args:
path (`str`):
Path to resolve.
revision (`str`, *optional*):
The revision of the repo to resolve. Defaults to the revision specified in the path.
Returns:
[`HfFileSystemResolvedPath`]: Resolved path information containing `repo_type`, `repo_id`, `revision` and `path_in_repo`.
Raises:
`ValueError`:
If path contains conflicting revision information.
`NotImplementedError`:
If trying to list repositories.
"""
def _align_revision_in_path_with_revision(
revision_in_path: Optional[str], revision: Optional[str]
) -> Optional[str]:
if revision is not None:
if revision_in_path is not None and revision_in_path != revision:
raise ValueError(
f'Revision specified in path ("{revision_in_path}") and in `revision` argument ("{revision}")'
" are not the same."
)
else:
revision = revision_in_path
return revision
path = self._strip_protocol(path)
if not path:
# can't list repositories at root
raise NotImplementedError("Access to repositories lists is not implemented.")
elif path.split("/")[0] + "/" in constants.REPO_TYPES_URL_PREFIXES.values():
if "/" not in path:
# can't list repositories at the repository type level
raise NotImplementedError("Access to repositories lists is not implemented.")
repo_type, path = path.split("/", 1)
repo_type = constants.REPO_TYPES_MAPPING[repo_type]
else:
repo_type = constants.REPO_TYPE_MODEL
if path.count("/") > 0:
if "@" in path:
repo_id, revision_in_path = path.split("@", 1)
if "/" in revision_in_path:
match = SPECIAL_REFS_REVISION_REGEX.search(revision_in_path)
if match is not None and revision in (None, match.group()):
# Handle `refs/convert/parquet` and PR revisions separately
path_in_repo = SPECIAL_REFS_REVISION_REGEX.sub("", revision_in_path).lstrip("/")
revision_in_path = match.group()
else:
revision_in_path, path_in_repo = revision_in_path.split("/", 1)
else:
path_in_repo = ""
revision = _align_revision_in_path_with_revision(unquote(revision_in_path), revision)
repo_and_revision_exist, err = self._repo_and_revision_exist(repo_type, repo_id, revision)
if not repo_and_revision_exist:
_raise_file_not_found(path, err)
else:
revision_in_path = None
repo_id_with_namespace = "/".join(path.split("/")[:2])
path_in_repo_with_namespace = "/".join(path.split("/")[2:])
repo_id_without_namespace = path.split("/")[0]
path_in_repo_without_namespace = "/".join(path.split("/")[1:])
repo_id = repo_id_with_namespace
path_in_repo = path_in_repo_with_namespace
repo_and_revision_exist, err = self._repo_and_revision_exist(repo_type, repo_id, revision)
if not repo_and_revision_exist:
if isinstance(err, (RepositoryNotFoundError, HFValidationError)):
repo_id = repo_id_without_namespace
path_in_repo = path_in_repo_without_namespace
repo_and_revision_exist, _ = self._repo_and_revision_exist(repo_type, repo_id, revision)
if not repo_and_revision_exist:
_raise_file_not_found(path, err)
else:
_raise_file_not_found(path, err)
else:
repo_id = path
path_in_repo = ""
if "@" in path:
repo_id, revision_in_path = path.split("@", 1)
revision = _align_revision_in_path_with_revision(unquote(revision_in_path), revision)
else:
revision_in_path = None
repo_and_revision_exist, _ = self._repo_and_revision_exist(repo_type, repo_id, revision)
if not repo_and_revision_exist:
raise NotImplementedError("Access to repositories lists is not implemented.")
revision = revision if revision is not None else constants.DEFAULT_REVISION
return HfFileSystemResolvedPath(repo_type, repo_id, revision, path_in_repo, _raw_revision=revision_in_path)