ResourcePath¶
- class lsst.resources.ResourcePath(uri: str | ParseResult | ResourcePath | Path, root: str | ResourcePath | None = None, forceAbsolute: bool = True, forceDirectory: bool = False, isTemporary: bool | None = None)¶
Bases:
objectConvenience wrapper around URI parsers.
Provides access to URI components and can convert file paths into absolute path URIs. Scheme-less URIs are treated as if they are local file system paths and are converted to absolute URIs.
A specialist subclass is created for each supported URI scheme.
- Parameters:
- uri
str,pathlib.Path,urllib.parse.ParseResult, orResourcePath. URI in string form. Can be scheme-less if referring to a relative path or an absolute path on the local file system.
- root
strorResourcePath, optional When fixing up a relative path in a
filescheme or if scheme-less, use this as the root. Must be absolute. IfNonethe current working directory will be used. Can be any supported URI scheme. Not used ifforceAbsoluteisFalse.- forceAbsolute
bool, optional If
True, scheme-less relative URI will be converted to an absolute path using afilescheme. IfFalsescheme-less URI will remain scheme-less and will not be updated tofileor absolute path unless it is already an absolute path, in which case it will be updated to afilescheme.- forceDirectory: `bool`, optional
If
Trueforces the URI to end with a separator, otherwise given URI is interpreted as is.- isTemporary
bool, optional If
Trueindicates that this URI points to a temporary resource. The default isFalse, unlessuriis already aResourcePathinstance anduri.isTemporary is True.
- uri
Notes
A non-standard URI of the form
file:dir/file.txtis always converted to an absolutefileURI.Attributes Summary
Return the fragment component of the URI.
If
Truethis URI refers to a local file.Return whether this URI points to the root of the network location.
Return the URI network location.
Return the path component of the URI localized to current OS.
Return any parameters included in the URI.
Return the path component of the URI.
Return any query strings included in the URI.
True if path-like elements modifying a URI should be quoted.
Return path relative to network location.
Return the URI scheme.
Default mode to use for transferring if
autois specified.Transfer modes supported by this implementation.
Return path component of the URI with any URI quoting reversed.
Methods Summary
abspath()Return URI using an absolute path.
as_local()Return the location of the (possibly remote) resource as local file.
basename()Return the base name, last element of path, of the URI.
dirname()Return the directory component of the path as a new
ResourcePath.exists()Indicate that the resource is available.
Get all the files from a list of values.
Return the file extension(s) associated with this URI path.
geturl()Return the URI in string form.
isabs()Indicate that the resource is fully specified.
isdir()Return True if this URI looks like a directory, else False.
join(path[, isTemporary, forceDirectory])Return new
ResourcePathwith additional path components.mexists(uris)Check for existence of multiple URIs at once.
mkdir()For a dir-like URI, create the directory resource if needed.
open([mode, encoding, prefer_file_temporary])Return a context manager that wraps an object that behaves like an open file at the location of the URI.
parent()Return a
ResourcePathof the parent directory.read([size])Open the resource and return the contents in bytes.
relative_to(other)Return the relative path from this URI to the other URI.
remove()Remove the resource.
replace([forceDirectory, isTemporary])Return new
ResourcePathwith specified components replaced.root_uri()Return the base root URI.
size()For non-dir-like URI, return the size of the resource.
split()Split URI into head and tail.
temporary_uri([prefix, suffix])Create a temporary file-like URI.
transfer_from(src, transfer[, overwrite, ...])Transfer to this URI from another.
updatedExtension(ext)Return a new
ResourcePathwith updated file extension.updatedFile(newfile)Return new URI with an updated final component of the path.
walk([file_filter])Walk the directory tree returning matching files and directories.
write(data[, overwrite])Write the supplied bytes to the new resource.
Attributes Documentation
- fragment¶
Return the fragment component of the URI.
- is_root¶
Return whether this URI points to the root of the network location.
This means that the path components refers to the top level.
- netloc¶
Return the URI network location.
- ospath¶
Return the path component of the URI localized to current OS.
- params¶
Return any parameters included in the URI.
- path¶
Return the path component of the URI.
- query¶
Return any query strings included in the URI.
- quotePaths = True¶
True if path-like elements modifying a URI should be quoted.
All non-schemeless URIs have to internally use quoted paths. Therefore if a new file name is given (e.g. to updatedFile or join) a decision must be made whether to quote it to be consistent.
- relativeToPathRoot¶
Return path relative to network location.
Effectively, this is the path property with posix separator stripped from the left hand side of the path.
Always unquotes.
- scheme¶
Return the URI scheme.
Notes
(
://is not part of the scheme).
- transferModes: tuple[str, ...] = ('copy', 'auto', 'move')¶
Transfer modes supported by this implementation.
Move is special in that it is generally a copy followed by an unlink. Whether that unlink works depends critically on whether the source URI implements unlink. If it does not the move will be reported as a failure.
- unquoted_path¶
Return path component of the URI with any URI quoting reversed.
Methods Documentation
- abspath() ResourcePath¶
Return URI using an absolute path.
- Returns:
- abs
ResourcePath Absolute URI. For non-schemeless URIs this always returns itself. Schemeless URIs are upgraded to file URIs.
- abs
- as_local() Iterator[ResourcePath]¶
Return the location of the (possibly remote) resource as local file.
- Yields:
- local
ResourcePath If this is a remote resource, it will be a copy of the resource on the local file system, probably in a temporary directory. For a local resource this should be the actual path to the resource.
- local
Notes
The context manager will automatically delete any local temporary file.
Examples
Should be used as a context manager:
with uri.as_local() as local: ospath = local.ospath
- basename() str¶
Return the base name, last element of path, of the URI.
- Returns:
- tail
str Last part of the path attribute. Trail will be empty if path ends on a separator.
- tail
Notes
If URI ends on a slash returns an empty string. This is the second element returned by
split().Equivalent of
os.path.basename.
- dirname() ResourcePath¶
Return the directory component of the path as a new
ResourcePath.- Returns:
- head
ResourcePath Everything except the tail of path attribute, expanded and normalized as per ResourcePath rules.
- head
Notes
Equivalent of
os.path.dirname.
- classmethod findFileResources(candidates: Iterable[str | ParseResult | ResourcePath | Path], file_filter: str | Pattern | None, grouped: Literal[True]) Iterator[Iterator[ResourcePath]]¶
- classmethod findFileResources(candidates: Iterable[str | ParseResult | ResourcePath | Path], *, grouped: Literal[True]) Iterator[Iterator[ResourcePath]]
- classmethod findFileResources(candidates: Iterable[str | ParseResult | ResourcePath | Path], file_filter: str | Pattern | None = None, grouped: Literal[False] = False) Iterator[ResourcePath]
Get all the files from a list of values.
- Parameters:
- candidatesiterable [
strorResourcePath] The files to return and directories in which to look for files to return.
- file_filter
strorre.Pattern, optional The regex to use when searching for files within directories. By default returns all the found files.
- grouped
bool, optional If
Truethe results will be grouped by directory and each yielded value will be an iterator over URIs. IfFalseeach URI will be returned separately.
- candidatesiterable [
- Yields:
- found_file:
ResourcePath The passed-in URIs and URIs found in passed-in directories. If grouping is enabled, each of the yielded values will be an iterator yielding members of the group. Files given explicitly will be returned as a single group at the end.
- found_file:
Notes
If a value is a file it is yielded immediately without checking that it exists. If a value is a directory, all the files in the directory (recursively) that match the regex will be yielded in turn.
- getExtension() str¶
Return the file extension(s) associated with this URI path.
- Returns:
- ext
str The file extension (including the
.). Can be empty string if there is no file extension. Usually returns only the last file extension unless there is a special extension modifier indicating file compression, in which case the combined extension (e.g..fits.gz) will be returned.
- ext
- isabs() bool¶
Indicate that the resource is fully specified.
For non-schemeless URIs this is always true.
- join(path: str | ResourcePath, isTemporary: bool | None = None, forceDirectory: bool = False) ResourcePath¶
Return new
ResourcePathwith additional path components.- Parameters:
- path
str,ResourcePath Additional file components to append to the current URI. Assumed to include a file at the end. Will be quoted depending on the associated URI scheme. If the path looks like a URI with a scheme referring to an absolute location, it will be returned directly (matching the behavior of
os.path.join). It can also be aResourcePath.- isTemporary
bool, optional Indicate that the resulting URI represents a temporary resource. Default is
self.isTemporary.- forceDirectory
bool, optional If
Trueforces the URI to end with a separator, otherwise given URI is interpreted as is.
- path
- Returns:
- new
ResourcePath New URI with any file at the end replaced with the new path components.
- new
- Raises:
- ValueError
Raised if the
pathis an absolute scheme-less URI. In that situation it is unclear whether the intent is to return afileURI or it was a mistake and a relative scheme-less URI was meant.- RuntimeError
Raised if this attempts to join a temporary URI to a non-temporary URI.
Notes
Schemeless URIs assume local path separator but all other URIs assume POSIX separator if the supplied path has directory structure. It may be this never becomes a problem but datastore templates assume POSIX separator is being used.
If an absolute
ResourcePathis given forpathis is assumed that this should be returned directly. Giving apathof an absolute scheme-less URI is not allowed for safety reasons as it may indicate a mistake in the calling code.
- classmethod mexists(uris: Iterable[ResourcePath]) dict[lsst.resources._resourcePath.ResourcePath, bool]¶
Check for existence of multiple URIs at once.
- Parameters:
- urisiterable of
ResourcePath The URIs to test.
- urisiterable of
- Returns:
- existence
dictof [ResourcePath,bool] Mapping of original URI to boolean indicating existence.
- existence
- open(mode: str = 'r', *, encoding: str | None = None, prefer_file_temporary: bool = False) Iterator[ResourceHandleProtocol]¶
Return a context manager that wraps an object that behaves like an open file at the location of the URI.
- Parameters:
- mode
str String indicating the mode in which to open the file. Values are the same as those accepted by
open, though intrinsically read-only URI types may only support read modes, andio.IOBase.seekableis not guaranteed to beTrueon the returned object.- encoding
str, optional Unicode encoding for text IO; ignored for binary IO. Defaults to
locale.getpreferredencoding(False), just asopendoes.- prefer_file_temporary
bool, optional If
True, for implementations that require transfers from a remote system to temporary local storage and/or back, use a temporary file instead of an in-memory buffer; this is generally slower, but it may be necessary to avoid excessive memory usage by large files. Ignored by implementations that do not require a temporary.
- mode
- Yields:
- cm
AbstractContextManager A context manager that wraps a
ResourceHandleProtocolfile-like object.
- cm
Notes
The default implementation of this method uses a local temporary buffer (in-memory or file, depending on
prefer_file_temporary) with calls toread,write,as_local, andtransfer_fromas necessary to read and write from/to remote systems. Remote writes thus occur only when the context manager is exited.ResourcePathimplementations that can return a more efficient native buffer should do so whenever possible (as is guaranteed for local files).ResourcePathimplementations for whichas_localdoes not return a temporary are required to reimplementopen, though they may delegate tosuperwhenprefer_file_temporaryisFalse.
- parent() ResourcePath¶
Return a
ResourcePathof the parent directory.- Returns:
- head
ResourcePath Everything except the tail of path attribute, expanded and normalized as per
ResourcePathrules.
- head
Notes
For a file-like URI this will be the same as calling
dirname().
- read(size: int = -1) bytes¶
Open the resource and return the contents in bytes.
- Parameters:
- size
int, optional The number of bytes to read. Negative or omitted indicates that all data should be read.
- size
- relative_to(other: ResourcePath) str | None¶
Return the relative path from this URI to the other URI.
- Parameters:
- other
ResourcePath URI to use to calculate the relative path. Must be a parent of this URI.
- other
- Returns:
- replace(forceDirectory: bool = False, isTemporary: bool = False, **kwargs: Any) ResourcePath¶
Return new
ResourcePathwith specified components replaced.- Parameters:
- forceDirectory
bool, optional Parameter passed to ResourcePath constructor to force this new URI to be dir-like.
- isTemporary
bool, optional Indicate that the resulting URI is temporary resource.
- **kwargs
Components of a
urllib.parse.ParseResultthat should be modified for the newly-createdResourcePath.
- forceDirectory
- Returns:
- new
ResourcePath New
ResourcePathobject with updated values.
- new
Notes
Does not, for now, allow a change in URI scheme.
- root_uri() ResourcePath¶
Return the base root URI.
- Returns:
- uri
ResourcePath root URI.
- uri
- size() int¶
For non-dir-like URI, return the size of the resource.
- Returns:
- sz
int The size in bytes of the resource associated with this URI. Returns 0 if dir-like.
- sz
- split() tuple[lsst.resources._resourcePath.ResourcePath, str]¶
Split URI into head and tail.
- Returns:
- head:
ResourcePath Everything leading up to tail, expanded and normalized as per ResourcePath rules.
- tail
str Last path component. Tail will be empty if path ends on a separator. Tail will never contain separators. It will be unquoted.
- head:
Notes
Equivalent to
os.path.splitwhere head preserves the URI components.
- classmethod temporary_uri(prefix: ResourcePath | None = None, suffix: str | None = None) Iterator[ResourcePath]¶
Create a temporary file-like URI.
- Parameters:
- prefix
ResourcePath, optional Prefix to use. Without this the path will be formed as a local file URI in a temporary directory. Ensuring that the prefix location exists is the responsibility of the caller.
- suffix
str, optional A file suffix to be used. The
.should be included in this suffix.
- prefix
- Yields:
- uri
ResourcePath The temporary URI. Will be removed when the context is completed.
- uri
- transfer_from(src: ResourcePath, transfer: str, overwrite: bool = False, transaction: TransactionProtocol | None = None) None¶
Transfer to this URI from another.
- Parameters:
- src
ResourcePath Source URI.
- transfer
str Mode to use for transferring the resource. Generically there are many standard options: copy, link, symlink, hardlink, relsymlink. Not all URIs support all modes.
- overwrite
bool, optional Allow an existing file to be overwritten. Defaults to
False.- transaction
TransactionProtocol, optional A transaction object that can (depending on implementation) rollback transfers on error. Not guaranteed to be implemented.
- src
Notes
Conceptually this is hard to scale as the number of URI schemes grow. The destination URI is more important than the source URI since that is where all the transfer modes are relevant (with the complication that “move” deletes the source).
Local file to local file is the fundamental use case but every other scheme has to support “copy” to local file (with implicit support for “move”) and copy from local file. All the “link” options tend to be specific to local file systems.
“move” is a “copy” where the remote resource is deleted at the end. Whether this works depends on the source URI rather than the destination URI. Reverting a move on transaction rollback is expected to be problematic if a remote resource was involved.
- updatedExtension(ext: str | None) ResourcePath¶
Return a new
ResourcePathwith updated file extension.All file extensions are replaced.
- Parameters:
- Returns:
- updated
ResourcePath URI with the specified extension. Can return itself if no extension was specified.
- updated
- updatedFile(newfile: str) ResourcePath¶
Return new URI with an updated final component of the path.
- Parameters:
- newfile
str File name with no path component.
- newfile
- Returns:
- updated
ResourcePath
- updated
Notes
Forces the ResourcePath.dirLike attribute to be false. The new file path will be quoted if necessary.
- walk(file_filter: str | Pattern | None = None) Iterator[list | tuple[lsst.resources._resourcePath.ResourcePath, list[str], list[str]]]¶
Walk the directory tree returning matching files and directories.