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
throw nice error if directory doesn't exist #203
Conversation
Codecov Report
@@ Coverage Diff @@
## master #203 +/- ##
=========================================
+ Coverage 77.38% 77.5% +0.12%
=========================================
Files 11 11
Lines 1110 1116 +6
=========================================
+ Hits 859 865 +6
Misses 251 251 |
@MSeal please review Could you please consider merging the changes? |
papermill/iorw.py
Outdated
@@ -88,6 +88,12 @@ def listdir(cls, path): | |||
|
|||
@classmethod | |||
def write(cls, buf, path): | |||
dir = (path.split("/"))[:-1] |
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.
I am not quite sure what the next few lines do but I think they try and find the directory name in the path. I'd recommend using https://docs.python.org/3/library/os.path.html#os.path.dirname or other tools from that module to do this.
By using an existing tool from the standard library you make it easier for others to read your code and we are less likely to have a bug in the code that manipulates the path.
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.
@betatim Sure. Will make the change as you suggested
papermill/exceptions.py
Outdated
@@ -13,6 +13,10 @@ class FileExistsError(AwsError): | |||
"""Raised when a File already exists on S3.""" | |||
|
|||
|
|||
class FileNotFoundError(AwsError): |
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.
I think we shouldn't inherit from AwsError
here as the exception is only used for local files.
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.
@betatim ok. Can I throw the normal Exception with the message that directory doesn't exists?
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.
@harsham4026 There's already a built-in FileNotFoundError in Python. Let's use that here :)
Thanks for working on this! I left some feedback. It would be super nice to add a test to make sure that the correct exception is raised when the directory does not exist and not raised when the directory is missing. |
@betatim Made the changes you suggested using the standard OS library for getting the directory path. And I removed the exception added in AwsError and raising the exception if dir doesn't exist. Can you please request to merge these changes to Master? |
I should have been clearer with my comment about the exception: I would keep the custom exception but I would not have it inherit from |
papermill/iorw.py
Outdated
if not dir.endswith("/"): | ||
dir += "/" | ||
if not os.path.exists(dir): | ||
raise Exception('output folder {} doesn\'t exist!'.format(dir)) |
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.
I'd rewrite this block as:
dir_name = os.path.dirname(path)
if not os.path.exists(dir_name):
raise NoSuchDirectory("The output folder {} does not exist.".format(dir_name))
and define a NoSuchDirectory
exception
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.
@betatim have you added this NoSuchDirectory exception or shall I add and commit the code?
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.
@betatim added the code block and committed.
@betatim yes I added a custom exception and inherited from Exception instead of AwsException. |
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.
Thanks for making the contribution! A few minor notes here. As tim mentioned, a quick test would be nice to keep out coverage going up instead of down :)
papermill/iorw.py
Outdated
if not dir.endswith("/"): | ||
dir += "/" | ||
if not os.path.exists(dir): | ||
raise NoSuchDirectory('output folder {} doesn\'t exist.'.format(dir)) |
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.
Let's still use FileNotFoundError instead of making a new error class, as it's used for missing directories as well.
From Python docs:
| exception FileNotFoundError
| Raised when a file or directory is requested but doesn’t exist. Corresponds to errno ENOENT.
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.
@MSeal used FileNotFoundError for throwing the error if a folder or file doesn't exist but the build is failing because FileNotFoundError is not directly supported in Python 2.7. Could you please let me know if we can proceed this way.
papermill/iorw.py
Outdated
@@ -88,6 +88,11 @@ def listdir(cls, path): | |||
|
|||
@classmethod | |||
def write(cls, buf, path): | |||
dir = os.path.dirname(path) | |||
if not dir.endswith("/"): |
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.
I don't think you need this
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.
@MSeal removed this piece of code
papermill/iorw.py
Outdated
@@ -88,6 +88,11 @@ def listdir(cls, path): | |||
|
|||
@classmethod | |||
def write(cls, buf, path): | |||
dir = os.path.dirname(path) |
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.
Since dir
is a reserved keyword in python let's use a different name here.
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.
@MSeal removed the dir and used os.path.dir(path)
@MSeal made the changes you suggested but the build is failing as FileNotFoundError directly not available to use in Python 2.7 |
@MSeal added the test case to check if it throws error when we give a folder which doesn't exist and it's passed. Please have a look into the following build |
@harsham4026 You can add
in both the iorw.py and the test file to fix the test error for python 2 (this is the recommended pattern as |
Once that's in place and the tests pass I'll happily hit merge :) Apologies there that there were a lot of small asks on the PR here. |
@MSeal made the changes as you suggested and now the build is successful :) |
@MSeal this addresses the issue of Nicer error when output directory doesn't exist #201
Please review the PR and let me know for any changes. At present I added the exception throwing for local file system. Please let me know if I need to do the same for others as well