[feature] add `self.*_path` properties that return `pathlib.Path`
See original GitHub issue- I’ve read the CONTRIBUTING guide.
We have recipe_folder
, source_folder
, etc that return Optional[str]
.
It would be nice to work with filesystem paths using pathlib.Path
.
As the *_folder
can be None
in certain stages, it is usually a programming error accessing them when they are None
.
It would be nice to have the following accessors on the ConanFile
:
@property
def package_path(self) -> Path:
"""
:returns: The location of the final packaging location
"""
assert self.package_folder is not None, "`package_folder` is `None`"
return Path(self.package_folder)
Then we can use it like so:
def build(self) -> None:
exe = self.package_path / "bin" / "hello-world"
exe.parent.mkdir()
with exe.open(encoding="utf8") as stream:
stream.write("#! /bin/sh\necho 'Hello, world!'')
exe.chmod(0o755)
Which is much nicer that the equivalent code with os.path
.
Issue Analytics
- State:
- Created a year ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
pathlib — Object-oriented filesystem paths — Python 3.11.1 ...
Source code: Lib/pathlib.py This module offers classes representing filesystem paths with semantics appropriate for different operating systems.
Read more >Python 3's pathlib Module: Taming the File System
It gathers the necessary functionality in one place and makes it available through methods and properties on an easy-to-use Path object.
Read more >How to use the pathlib.Path.home function in pathlib - Snyk
To help you get started, we've selected a few pathlib examples, based on popular ways it is used in public projects. ; is...
Read more >python 3.7 - Custom pathlib.Path() - Stack Overflow
It works if you subclass from a derived concrete class instead of Path. from pathlib import PosixPath import os class mypath(PosixPath): def ...
Read more >pathlib — Filesystem Paths as Objects — PyMOTW 3
Path objects have methods and properties for extracting partial values from the name. For example, the parts property produces a sequence of path...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
here we go: #11585
The problem with this is that Conan 1.X still supports Python 2.7 (sigh, I know…), so this couldn’t be added. Then we need to keep the current
xxx_folder
as strings for the Conan 1.X -> Conan 2.0 migration.We will probably start to use
Pathlib
gradually at Conan 2.X (once the migration cliff has been surpassed), and from there we might be able to consider this request.