-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support passing pathlib.Path in some functionalities #1499
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
base: master
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| """ | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| __author__ = 'Marcel Hellkamp' | ||
| __version__ = '0.14-dev' | ||
|
|
@@ -2306,10 +2307,16 @@ def load_config(self, filename, **options): | |
| other sections. | ||
|
|
||
| :param filename: The path of a config file, or a list of paths. | ||
| Can be a string or a :class:`pathlib.Path` object. | ||
| :param options: All keyword parameters are passed to the underlying | ||
| :class:`python:configparser.ConfigParser` constructor call. | ||
|
|
||
| """ | ||
| if isinstance(filename, Path): | ||
| filename = str(filename) | ||
| elif isinstance(filename, (list, tuple)): | ||
| filename = [str(f) if isinstance(f, Path) else f for f in filename] | ||
|
|
||
| options.setdefault('allow_no_value', True) | ||
| options.setdefault('interpolation', configparser.ExtendedInterpolation()) | ||
| conf = configparser.ConfigParser(**options) | ||
|
|
@@ -2746,8 +2753,9 @@ def static_file(filename, root, | |
| that can be sent back to the client. | ||
|
|
||
| :param filename: Name or path of the file to send, relative to ``root``. | ||
| Can be a string or a :class:`pathlib.Path` object. | ||
| :param root: Root path for file lookups. Should be an absolute directory | ||
| path. | ||
| path. Can be a string or a :class:`pathlib.Path` object. | ||
| :param mimetype: Provide the content-type header (default: guess from | ||
| file extension) | ||
| :param download: If True, ask the browser to open a `Save as...` dialog | ||
|
|
@@ -2773,6 +2781,11 @@ def static_file(filename, root, | |
| check or continue partial downloads) are also handled automatically. | ||
| """ | ||
|
|
||
| if isinstance(root, Path): | ||
| root = str(root) | ||
| if isinstance(filename, Path): | ||
| filename = str(filename) | ||
|
|
||
| root = os.path.join(os.path.abspath(root), '') | ||
| filename = os.path.abspath(os.path.join(root, filename.strip('/\\'))) | ||
| headers = headers.copy() if headers else {} | ||
|
|
@@ -3976,7 +3989,10 @@ def __init__(self, | |
| self.name = name | ||
| self.source = source.read() if hasattr(source, 'read') else source | ||
| self.filename = source.filename if hasattr(source, 'filename') else None | ||
| self.lookup = [os.path.abspath(x) for x in lookup] if lookup else [] | ||
| if lookup: | ||
|
Member
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. Same here, just call
Contributor
Author
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. Unfortunately, the default value of |
||
| self.lookup = [os.path.abspath(str(x) if isinstance(x, Path) else x) for x in lookup] | ||
| else: | ||
| self.lookup = [] | ||
| self.encoding = encoding | ||
| self.settings = self.settings.copy() # Copy from class variable | ||
| self.settings.update(settings) # Apply | ||
|
|
@@ -3999,6 +4015,8 @@ def search(cls, name, lookup=None): | |
| raise depr(0, 12, "Use of absolute path for template name.", | ||
| "Refer to templates with names or paths relative to the lookup path.") | ||
|
|
||
| lookup = [str(path) if isinstance(path, Path) else path for path in lookup] | ||
|
|
||
| for spath in lookup: | ||
| spath = os.path.abspath(spath) + os.sep | ||
| fname = os.path.abspath(os.path.join(spath, name)) | ||
|
|
||
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.
You can skip the type check and just call
str(var)orPath(var)to get what you need. This may even be cheaper than the type check :)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.
Hi @defnull, thank you for your review!
I have a concern about just calling
str(var)on it. If someone passes an invalid type here, like None, the value will become a string literal"None", and it may not fail here, but fail eventually later in the codebase somewhere else. So I think it's better to just handle the expected type here, to reduce the risk of this kind of error.