-
Notifications
You must be signed in to change notification settings - Fork 532
RF: Provide functions to augment old Path.mkdir, Path.resolve methods #3050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,34 +59,42 @@ def __init__(self, path): | |
from pathlib2 import Path | ||
USING_PATHLIB2 = True | ||
|
||
try: # PY35 - strict mode was added in 3.6 | ||
Path('/invented/file/path').resolve(strict=True) | ||
except TypeError: | ||
def _patch_resolve(self, strict=False): | ||
"""Add the argument strict to signature in Python>3,<3.6.""" | ||
resolved = Path().old_resolve() / self | ||
|
||
if strict and not resolved.exists(): | ||
raise FileNotFoundError(resolved) | ||
return resolved | ||
|
||
Path.old_resolve = Path.resolve | ||
Path.resolve = _patch_resolve | ||
except FileNotFoundError: | ||
pass | ||
except OSError: | ||
# PY2 | ||
def _patch_resolve(self, strict=False): | ||
"""Raise FileNotFoundError instead of OSError with pathlib2.""" | ||
try: | ||
resolved = self.old_resolve(strict=strict) | ||
except OSError: | ||
raise FileNotFoundError(self.old_resolve()) | ||
|
||
return resolved | ||
def _resolve_with_filenotfound(path, **kwargs): | ||
""" Raise FileNotFoundError instead of OSError """ | ||
try: | ||
return path.resolve(**kwargs) | ||
except OSError as e: | ||
if isinstance(e, FileNotFoundError): | ||
raise | ||
raise FileNotFoundError(str(path)) | ||
|
||
|
||
def path_resolve(path, strict=False): | ||
try: | ||
return _resolve_with_filenotfound(path, strict=strict) | ||
except TypeError: # PY35 | ||
pass | ||
|
||
path = path.absolute() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this method documented? I had to go to the code to find it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, it looks like it's not on the web docs. Is that a problem? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nono, just confirming. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. I did check that it exists in py35. |
||
if strict or path.exists(): | ||
return _resolve_with_filenotfound(path) | ||
|
||
# This is a hacky shortcut, using path.absolute() unmodified | ||
# In cases where the existing part of the path contains a | ||
# symlink, different results will be produced | ||
return path | ||
|
||
|
||
def path_mkdir(path, mode=0o777, parents=False, exist_ok=False): | ||
try: | ||
return path.mkdir(mode=mode, parents=parents, exist_ok=exist_ok) | ||
except TypeError: # PY27/PY34 | ||
if parents: | ||
return makedirs(str(path), mode=mode, exist_ok=exist_ok) | ||
elif not exist_ok or not path.exists(): | ||
return os.mkdir(str(path), mode=mode) | ||
|
||
Path.old_resolve = Path.resolve | ||
Path.resolve = _patch_resolve | ||
|
||
if not hasattr(Path, 'write_text'): | ||
# PY34 - Path does not have write_text | ||
|
@@ -95,19 +103,6 @@ def _write_text(self, text): | |
f.write(text) | ||
Path.write_text = _write_text | ||
|
||
try: # PY27/PY34 - mkdir does not have exist_ok | ||
from .tmpdirs import TemporaryDirectory | ||
with TemporaryDirectory() as tmpdir: | ||
(Path(tmpdir) / 'exist_ok_test').mkdir(exist_ok=True) | ||
except TypeError: | ||
def _mkdir(self, mode=0o777, parents=False, exist_ok=False): | ||
if parents: | ||
makedirs(str(self), mode=mode, exist_ok=exist_ok) | ||
elif not exist_ok or not self.exists(): | ||
os.mkdir(str(self), mode=mode) | ||
|
||
Path.mkdir = _mkdir | ||
|
||
|
||
def split_filename(fname): | ||
"""Split a filename into parts: path, base filename and extension. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this is intended (PY35):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is. It's a helper function to promote OSErrors to FileNotFoundErrors, and lets all others get caught higher.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, yes, I see the
TypeError
inpath_resolve
. Thanks!