Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 7 additions & 32 deletions st3/sublime_lib/resource_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,6 @@
__all__ = ['ResourcePath']


def _abs_parts(path: Path) -> Tuple[str, ...]:
return (path.drive, path.root) + path.parts[1:]


def _file_relative_to(path: Path, base: Path) -> Optional[Tuple[str, ...]]:
"""
Like Path.relative_to, except:

- Both paths must be relative.
- `base` must be a single Path object.
- The error message is blank.
- Only a tuple of parts is returned.

Surprisingly, this is much, much faster.
"""
child_parts = _abs_parts(path)
base_parts = _abs_parts(base)

n = len(base_parts)
cf = path._flavour.casefold_parts # type: ignore

if cf(child_parts[:n]) != cf(base_parts):
return None

return child_parts[n:]


class ResourceRoot(metaclass=ABCMeta):
"""
Represents a directory containing packages.
Expand Down Expand Up @@ -76,13 +49,15 @@ def file_to_resource_path(self, file_path: Union[Path, str]) -> Optional['Resour
if not file_path.is_absolute():
raise ValueError("Cannot convert a relative file path to a resource path.")

parts = _file_relative_to(file_path, self.file_root)
if parts is None:
try:
relpath = file_path.relative_to(self.file_root)
except ValueError:
return None
elif parts == ():

if not relpath.parts:
return self.resource_root
else:
return self._package_resource_path(*parts)

return self._package_resource_path(*relpath.parts)

@abstractmethod
def _package_file_path(
Expand Down
Loading