pykot/pathlib

Search:
Group by:

pythonpathlib is a Nim module that provides an interface for working with paths that is as similar as possible to Python's pathlib module.

Examples:

# Create two paths, check if they are equal, and append another directory to the first path.
var path1 : PythonPath = Path("/home/adam/")
var path2 : PythonPath = Path("/home/user")
echo(path1 == path2) # false
path1 = path1 / "nim" / "pythonpathlib"
echo(path1) # "/home/adam/nim/pythonpathlib"
# Create a path and output the parent directories of the path.
var path : PythonPath = Path("/home/adam/nim/pythonpathlib/pythonpathlib.nim")
var parents : seq[string] = path.parents
for i in parents:
    echo(i)
# output:
# "/"
# "home"
# "adam"
# "nim"
# "pythonpathlib"
# Create a path, get the name and suffix, and then change both.
var path : PythonPath = Path("code/example.nim")
echo(path.name) # "example.nim"
echo(path.suffix) # ".nim"
path = path.with_name("newfile.nim")
echo(path) # "code/newfile.nim"
path = path.with_suffix(".py")
echo(path) # "code/newfile.py"
# Create a path, check whether the path exists, and then see whether it is a file, directory, or symlink.
var path : PythonPath = Path("/home/adam")
echo(path.exists()) # true
echo(path.is_file()) # false
echo(path.is_dir()) # true
echo(path.is_symlink()) # false
# Create a path, rename the path it represents to something else, and then force another rename.
var path : PythonPath = Path("code/example.nim")
path.rename("code/newexample.nim")
# path.rename(Path("code/newexample.nim")) also works
path.replace("code/testing.nim")
# if "code/testing.nim" already existed, if would be overritten by the last method.
# Create a path and get its representation as a file URI.
var path : PythonPath = Path("/home/adam/nim/code.nim")
var fileURI : string = path.as_uri()
echo(fileURI) # "file:///home/adam/nim/code.nim"
# Create a path and compute a version of this path relative to another.
var path : PythonPath = Path("/home/adam/nim/code.nim")
echo(path.relative_to("home")) # "adam/nim/code.nim"
echo(path.relative_to("nim")) # "code.nim"
echo(path.relative_to("usr")) # can't do, not on path

Types

PythonPath = ref object
  p*: string

Procs

proc `!=`(path1: PythonPath; path2: PythonPath): bool {....raises: [], tags: [],
    forbids: [].}
Inequality operator for PythonPath.
proc `$`(path: PythonPath): string {....raises: [], tags: [], forbids: [].}
String operator for PythonPath.
proc `/`(path1: PythonPath; path2: PythonPath): PythonPath {....raises: [],
    tags: [], forbids: [].}
Join operator for PythonPath.
proc `/`(path1: PythonPath; path2: string): PythonPath {....raises: [], tags: [],
    forbids: [].}
Join operator for PythonPath.
proc `==`(path1: PythonPath; path2: PythonPath): bool {....raises: [], tags: [],
    forbids: [].}
Equality operator for PythonPath.
proc as_posix(path: PythonPath): string {....raises: [], tags: [], forbids: [].}
Returns a string representation of the path with forward slashes (/).
proc as_uri(path: PythonPath): string {....raises: [], tags: [], forbids: [].}
Returns the path as a file URI. Will fail if the path is not absolute.
proc chmod(path: PythonPath; mode: set[FilePermission]) {.noreturn,
    ...raises: [OSError], tags: [ReadDirEffect, WriteDirEffect], forbids: [].}
Changes the file permissions.
proc drive(path: PythonPath): string {....raises: [IOError, OSError], tags: [],
                                       forbids: [].}
Returns a string representing the drive letter or name, if any.
proc exists(path: PythonPath): bool {....raises: [], tags: [ReadDirEffect],
                                      forbids: [].}
Returns true if the path points to an existing directory or file, and false otherwise.
proc home(cls: typedesc[PythonPath]): string
Returns the home directory of the user.
proc is_dir(path: PythonPath): bool {....raises: [], tags: [ReadDirEffect],
                                      forbids: [].}
Returns true if the path points to an existing directory, and false otherwise.
proc is_file(path: PythonPath): bool {....raises: [], tags: [ReadDirEffect],
                                       forbids: [].}
Returns true if the path points to an existing file, and false otherwise.
proc is_reserved(path: PythonPath): bool {....raises: [], tags: [], forbids: [].}
Returns true if the path is considered reserved under Windows, and false otherwise.
proc joinpath(paths: varargs[PythonPath]): PythonPath {....raises: [], tags: [],
    forbids: [].}
Joins the paths and returns a new PythonPath representing the joined paths.
proc mkdir(path: PythonPath) {.noreturn, ...raises: [OSError, IOError],
                               tags: [WriteDirEffect, ReadDirEffect],
                               forbids: [].}
Creates a new directory with the name of the path.
proc name(path: PythonPath): string {....raises: [], tags: [], forbids: [].}
Returns a string representing the final path component, excluding the drive and root, if any.
proc open(path: PythonPath; mode: string = "r"; buffering: int = -1): File {.
    ...raises: [IOError], tags: [], forbids: [].}
Opens the file that the path represents.
proc parent(path: PythonPath): PythonPath {....raises: [], tags: [], forbids: [].}
Returns the logical parent of the path.
proc parents(path: PythonPath): seq[PythonPath] {....raises: [], tags: [],
    forbids: [].}
Returns a sequence providing access to the logical ancestors of the path.
proc parts(path: PythonPath): seq[string] {....raises: [], tags: [], forbids: [].}
Returns a seq giving access to the pathโ€™s various components.
proc Path(p: string): PythonPath {....raises: [], tags: [], forbids: [].}
Creates a new PythonPath representing the specified file or directory.
proc PosixPath(p: string): PythonPath {....raises: [], tags: [], forbids: [].}
proc PurePath(p: string): PythonPath {....raises: [], tags: [], forbids: [].}
proc PurePosixPath(p: string): PythonPath {....raises: [], tags: [], forbids: [].}
proc PureWindowsPath(p: string): PythonPath {....raises: [], tags: [], forbids: [].}
proc relative_to(path: PythonPath; other: PythonPath): PythonPath {....raises: [],
    tags: [], forbids: [].}
Returns a new path of this path relative to the path represented by other.
proc relative_to(path: PythonPath; other: string): PythonPath {....raises: [],
    tags: [], forbids: [].}
Returns a new path of this path relative to the path represented by other.
proc rename(path1: PythonPath; path2: PythonPath) {.noreturn,
    ...raises: [OSError, IOError, Exception],
    tags: [ReadDirEffect, ReadIOEffect, WriteIOEffect], forbids: [].}
Renames the path to the given new path. Updates the first path to point to the new path.
proc rename(path1: PythonPath; path2: string) {.noreturn,
    ...raises: [OSError, IOError, Exception],
    tags: [ReadDirEffect, ReadIOEffect, WriteIOEffect], forbids: [].}
Renames the path to the given new path. Updates the first path to point to the new path.
proc replace(path1: PythonPath; path2: PythonPath) {.noreturn,
    ...raises: [OSError, IOError, Exception],
    tags: [WriteDirEffect, ReadDirEffect, ReadIOEffect, WriteIOEffect],
    forbids: [].}
Renames this file or directory to the given target. If target points to an existing file or directory, it will be unconditionally replaced.
proc replace(path1: PythonPath; path2: string) {.noreturn,
    ...raises: [OSError, IOError, Exception],
    tags: [WriteDirEffect, ReadDirEffect, ReadIOEffect, WriteIOEffect],
    forbids: [].}
Renames this file or directory to the given target. If target points to an existing file or directory, it will be unconditionally replaced.
proc resolve(path: PythonPath) {.noreturn, ...raises: [OSError],
                                 tags: [ReadDirEffect], forbids: [].}
Sets the path to the full name of the path.
proc rmdir(path: PythonPath) {.noreturn, ...raises: [OSError],
                               tags: [WriteDirEffect, ReadDirEffect],
                               forbids: [].}
Removes the directory specified by the path.
proc root(path: PythonPath): string {....raises: [], tags: [], forbids: [].}
Returns a string representing the root directory, if any.
proc stat(path: PythonPath): FileInfo {....raises: [IOError, OSError], tags: [],
                                        forbids: [].}
Returns information about this path.
proc stem(path: PythonPath): string {....raises: [], tags: [], forbids: [].}
Returns the final path component, without its suffix.
proc suffix(path: PythonPath): string {....raises: [], tags: [], forbids: [].}
Returns the file extension of the final component, if any.
proc suffixes(path: PythonPath): seq[string] {....raises: [], tags: [], forbids: [].}
Returns a sequence of the pathโ€™s file extensions, if any.
proc WindowsPath(p: string): PythonPath {....raises: [], tags: [], forbids: [].}
proc with_name(path: PythonPath; newName: string): PythonPath {....raises: [],
    tags: [], forbids: [].}
Returns a new path with the name changed.
proc with_suffix(path: PythonPath; newSuffix: string): PythonPath {....raises: [],
    tags: [], forbids: [].}
Returns a new path with the suffix changed.