Pyjo.Path - Path¶
import Pyjo.Path
# Parse
path = Pyjo.Path.new('/foo%2Fbar%3B/baz.html')
print(path[0])
# Build
path = Pyjo.Path.new(u'/i/♥')
path.append('pyjo')
print(path)
Pyjo.Path
is a container for paths used by Pyjo.URL
and based on
RFC 3986.
Classes¶
-
class
Pyjo.Path.
Pyjo_Path
(path=None, **kwargs)¶ Pyjo.Path
inherits all attributes and methods fromPyjo.Base
andPyjo.String.Mixin
and implements the following new ones.-
__bool__
()¶ boolean = bool(path)
Always true. (Python 3.x)
-
__init__
(path=None, **kwargs)¶ path = Pyjo.Path.new() path = Pyjo.Path.new('/foo%2Fbar%3B/baz.html')
Construct a new :mod`Pyjo.Path` object and
parse()
path if necessary.
-
__iter__
()¶ parts = list(path)
Iterator based on
parts
. Note that this will normalize the path and that%2F
will be treated as/
for security reasons.
-
__nonzero__
()¶ boolean = bool(path)
Always true. (Python 2.x)
-
canonicalize
()¶ 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
= 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
()¶ clone = path.clone()
Clone path.
-
contains
(prefix)¶ boolean = path.contains(u'/i/♥/pyjo')
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')
-
leading_slash
¶ 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.
-
merge
(path)¶ path = path.merge('/foo/bar') path = path.merge('foo/bar') path = path.merge(Pyjo.Path.new('foo/bar'))
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)¶ path = path.parse('/foo%2Fbar%3B/baz.html')
Parse path.
-
parts
¶ 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 = path.to_abs_str()
Turn path into an absolute string.
# "/i/%E2%99%A5/pyjo" Pyjo.Path.new('/i/%E2%99%A5/pyjo').to_abs_str() Pyjo.Path.new('i/%E2%99%A5/pyjo').to_abs_str()
-
to_bytes
()¶ bstring = path.to_bytes()
Turn path into a bytes string.
# b"/i/%E2%99%A5/pyjo" Pyjo.Path.new('/i/%E2%99%A5/pyjo').to_bytes() # b"i/%E2%99%A5/pyjo" Pyjo.Path.new('i/%E2%99%A5/pyjo').to_bytes()
-
to_dir
()¶ 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/pyjo').to_dir() # "i/%E2%99%A5/" Pyjo.Path.new('i/%E2%99%A5/pyjo').to_dir()
-
to_json
()¶ string = path.to_json()
Turn path into a JSON representation. The same as
to_str()
.# "/i/%E2%99%A5/pyjo" Pyjo.Path.new('/i/%E2%99%A5/pyjo').to_json()
-
to_route
()¶ route = path.to_route()
Turn path into a route.
# "/i/♥/pyjo" Pyjo.Path.new('/i/%E2%99%A5/pyjo').to_route() Pyjo.Path.new('i/%E2%99%A5/pyjo').to_route()
-
to_str
()¶ string = path.to_str()
Turn path into a string.
# "/i/%E2%99%A5/pyjo" Pyjo.Path.new('/i/%E2%99%A5/pyjo').to_str() # "i/%E2%99%A5/pyjo" Pyjo.Path.new('i/%E2%99%A5/pyjo').to_str()
-
trailing_slash
¶ 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.
-