Pyjo.Path - Path

from Pyjo.Path import Path

# Parse
path = Path('/foo%2Fbar%3B/baz.html')
print(path[0])

# Or import the module, as in Google Python Style Guide

import Pyjo.Path

# Parse
path = Pyjo.Path.new('/foo%2Fbar%3B/baz.html')
print(path[0])

# Build
path = Pyjo.Path.new('/i/♥')
path.parts.append('pyjoyment')
print(path)

Pyjo.Path is a container for paths used by Pyjo.URL, based on RFC 3986.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::Path of Mojolicious 9.49.

Classes

class Pyjo.Path.Pyjo_Path(path: str | bytes | Pyjo_Path | None = None)[source]

Pyjo.Path inherits all methods from Pyjo.Base and Pyjo.String.Mixin and implements the following new ones.

__bool__() → bool[source]
boolean = bool(path)

Always true.

__getitem__(index: int) → str[source]
part = path[0]

Alias for parts. Note that this will normalize the path and that %2F will be treated as / for security reasons.

print(path[0])
__iter__() → Iterator[str][source]
parts = list(path)

Alias for parts. Note that this will normalize the path and that %2F will be treated as / for security reasons.

for part in path:
    print(part)
__len__() → int[source]
size = len(path)

Alias for parts. Note that this will normalize the path and that %2F will be treated as / for security reasons.

__str__() → str[source]
string = str(path)

Alias for to_str().

canonicalize() → Self[source]
path = path.canonicalize()

Canonicalize path by resolving . and .., in addition ... will be treated as . to protect from path traversal attacks.

# "/foo/baz"
Pyjo.Path.new('/foo/./bar/../baz').canonicalize()

# "/../baz"
Pyjo.Path.new('/foo/../bar/../../baz').canonicalize()

# "/foo/bar"
Pyjo.Path.new('/foo/.../bar').canonicalize()
charset: str | None
charset = path.charset
path.charset = 'UTF-8'

Charset used for encoding and decoding, defaults to UTF-8.

# Disable encoding and decoding
path.charset = None
clone() → Self[source]
clone = path.clone()

Return a new Pyjo.Path object cloned from this path.

contains(prefix: str) → bool[source]
boolean = path.contains('/i/♥/pyjoyment')

Check if path contains given prefix.

# True
Pyjo.Path.new('/foo/bar').contains('/')
Pyjo.Path.new('/foo/bar').contains('/foo')
Pyjo.Path.new('/foo/bar').contains('/foo/bar')

# False
Pyjo.Path.new('/foo/bar').contains('/f')
Pyjo.Path.new('/foo/bar').contains('/bar')
Pyjo.Path.new('/foo/bar').contains('/whatever')
property leading_slash: bool[source]
boolean = path.leading_slash
path.leading_slash = boolean

Path has a leading slash. Note that this method will normalize the path and that %2F will be treated as / for security reasons.

# "/foo/bar"
Pyjo.Path.new('foo/bar').set(leading_slash=True)

# "foo/bar"
Pyjo.Path.new('/foo/bar').set(leading_slash=False)
merge(path: str | bytes | Pyjo_Path) → Self[source]
path = path.merge('/foo/bar')
path = path.merge('foo/bar')
path = path.merge(Pyjo.Path.new())

Merge paths. Note that this method will normalize both paths if necessary and that %2F will be treated as / for security reasons.

# "/baz/yada"
Pyjo.Path.new('/foo/bar').merge('/baz/yada')

# "/foo/baz/yada"
Pyjo.Path.new('/foo/bar').merge('baz/yada')

# "/foo/bar/baz/yada"
Pyjo.Path.new('/foo/bar/').merge('baz/yada')
parse(path: str | bytes | Pyjo_Path) → Self[source]
path = path.parse('/foo%2Fbar%3B/baz.html')

Parse path, given as characters or as bytes encoded with charset.

property parts: list[str][source]
parts = path.parts
path.parts = ['foo', 'bar', 'baz']

The path parts. Note that this method will normalize the path and that %2F will be treated as / for security reasons.

# Part with slash
path.parts.append('foo/bar')
to_abs_str() → str[source]
string = path.to_abs_str()

Turn path into an absolute string.

# "/i/%E2%99%A5/pyjoyment"
Pyjo.Path.new('/i/%E2%99%A5/pyjoyment').to_abs_str()
Pyjo.Path.new('i/%E2%99%A5/pyjoyment').to_abs_str()
to_dir() → Self[source]
dir = route.to_dir()

Clone path and remove everything after the right-most slash.

# "/i/%E2%99%A5/"
Pyjo.Path.new('/i/%E2%99%A5/pyjoyment').to_dir().to_abs_str()

# "i/%E2%99%A5/"
Pyjo.Path.new('i/%E2%99%A5/pyjoyment').to_dir().to_abs_str()
to_route() → str[source]
route = path.to_route()

Turn path into a route.

# "/i/♥/pyjoyment"
Pyjo.Path.new('/i/%E2%99%A5/pyjoyment').to_route()
Pyjo.Path.new('i/%E2%99%A5/pyjoyment').to_route()
to_str() → str[source]
string = path.to_str()

Turn path into a string.

# "/i/%E2%99%A5/pyjoyment"
Pyjo.Path.new('/i/%E2%99%A5/pyjoyment').to_str()

# "i/%E2%99%A5/pyjoyment"
Pyjo.Path.new('i/%E2%99%A5/pyjoyment').to_str()
property trailing_slash: bool[source]
boolean = path.trailing_slash
path.trailing_slash = boolean

Path has a trailing slash. Note that this method will normalize the path and that %2F will be treated as / for security reasons.

# "/foo/bar/"
Pyjo.Path.new('/foo/bar').set(trailing_slash=True)

# "/foo/bar"
Pyjo.Path.new('/foo/bar/').set(trailing_slash=False)