Opened 2 years ago

Closed 9 months ago

#119 closed enhancement (fixed)

Allow "pathlike" objects for paths

Reported by: ftputiluser Owned by: schwa
Priority: major Milestone: 4.0.0
Component: Library Version: 3.4
Keywords: pathlib pathlike Cc:

Description (last modified by schwa)

It will be very useful to have a support for the Pathlib acts like input parameters for methods. E.g. you can do something like ftp_host.chdir(Path("/my/dir/"))

Change History (6)

comment:1 Changed 2 years ago by schwa

Generally, this sounds like a sensible idea. However, according to the documentation at https://docs.python.org/3/library/pathlib.html , Path(...) instantiates a filesystem path for the operating system the Python process runs on.

A path object to be used as an FTP remote path should probably be an FTPPath (or something with a similar name). I'm not sure yet how to implement this. From a pure implementation perspective, an FTPPath could be aliased to a PurePosixPath, but having isinstance(FTPPath(...), PurePosixPath) evaluate to True would be confusing. Although most FTP servers will run on a Posix system, an FTPPath shouldn't assume this. Also, some PurePosixPath operations may not make sense for a remote filesystem.

comment:2 Changed 21 months ago by schwa

Description: modified (diff)

comment:3 Changed 9 months ago by schwa

Since Python 3.6 there's os.PathLike. I think that's what a custom path class should inherit from.

See also
https://docs.python.org/3/library/os.html#os.PathLike
https://docs.python.org/3/glossary.html#term-path-like-object
https://www.python.org/dev/peps/pep-0519/

Last edited 9 months ago by schwa (previous) (diff)

comment:4 Changed 9 months ago by schwa

TODO:

Change tool.as_unicode to tool.as_str and let it accept PathLike objects

Implement an FTPPath class with the following attributes and methods:

Attributes

parts
drive
root
anchor
parents
parent
name
suffix
suffixes
stem

PurePath attributes

__truediv__
as_posix
as_uri
is_absolute
is_reserved
joinpath
match
relative_to
with_name
with_suffix

Non-PurePath methods

cwd
home
stat
chmod
exists
expanduser
glob
group
is_dir
is_file
is_mount
is_symlink
is_socket
is_fifo
is_block_device
is_char_device
iterdir
lchmod
lstat
mkdir
open
owner
read_bytes
read_text
rename
replace
resolve
rglob
rmdir
samefile
stat
symlink_to
touch
unlink
link_to
write_bytes
write_text

I assume the attributes and PurePath methods can be implemented by delegating to an internal PurePosixPath object.

Last edited 9 months ago by schwa (previous) (diff)

comment:5 Changed 9 months ago by schwa

As of [a3f58fa3920e], methods that used to accept "only" bytes and str objects now also accept PathLike objects that can be converted to bytes and str. Actually, this is the functionality the description asked for.

Implementing an FTPPath class as described above is much more effort.

comment:6 Changed 9 months ago by schwa

Keywords: pathlib pathlike added
Milestone: 4.0.0
Resolution: fixed
Status: newclosed
Summary: support for the PathlibAllow "pathlike" objects for paths
Version: 3.4

I changed the title to let it reflect the ticket description. If support for a custom FTPPath is requested, this should better go into a new ticket.

Note: See TracTickets for help on using tickets.