Skip to content
Myr

Move get_root_path back to flask.helpers

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Many libraries use it so the move is somewhat disruptive.

authored by Adrian Moennich · committed by Adrian Moennich · parent 1a8549d · Browse files · 2 files+52 added−49 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+52 added−49 removed

  • other:+52 added−49 removed

2 files+52 added−49 removed

modified:src/flask/helpers.py+51 added
1import os2import pkgutil3import socket4import sys5import typing as t6import warnings7from datetime import timedelta8from functools import update_wrapper
669 unmodified lines
678 directory, path, **_prepare_send_file_kwargs(**kwargs)679 )680681682def get_root_path(import_name: str) -> str:683 """Find the root path of a package, or the path that contains a684 module. If it cannot be found, returns the current working685 directory.686687 Not to be confused with the value returned by :func:`find_package`.688689 :meta private:690 """691 # Module already imported and has a file attribute. Use that first.692 mod = sys.modules.get(import_name)693694 if mod is not None and hasattr(mod, "__file__"):695 return os.path.dirname(os.path.abspath(mod.__file__))696697 # Next attempt: check the loader.698 loader = pkgutil.get_loader(import_name)699700 # Loader does not exist or we're referring to an unloaded main701 # module or a main module without path (interactive sessions), go702 # with the current working directory.703 if loader is None or import_name == "__main__":704 return os.getcwd()705706 if hasattr(loader, "get_filename"):707 filepath = loader.get_filename(import_name) # type: ignore708 else:709 # Fall back to imports.710 __import__(import_name)711 mod = sys.modules[import_name]712 filepath = getattr(mod, "__file__", None)713714 # If we don't have a file path it might be because it is a715 # namespace package. In this case pick the root path from the716 # first module that is contained in the package.717 if filepath is None:718 raise RuntimeError(719 "No root path can be found for the provided module"720 f" {import_name!r}. This can happen because the module"721 " came from an import hook that does not provide file"722 " name information or because it's a namespace package."723 " In this case the root path needs to be explicitly"724 " provided."725 )726727 # filepath is import_name.py for a module, or __init__.py for a package.728 return os.path.dirname(os.path.abspath(filepath))729730731class locked_cached_property(werkzeug.utils.cached_property):732 """A :func:`property` that is only evaluated once. Like733 :class:`werkzeug.utils.cached_property` except access uses a lock734 for thread safety.
117 unmodified lines
modified:src/flask/scaffold.py+1 added-49 removed
12 unmodified lines
13from werkzeug.exceptions import HTTPException1415from .cli import AppGroup16from .globals import current_app17from .helpers import get_root_path18from .helpers import locked_cached_property19from .helpers import send_from_directory20from .templating import _default_template_ctx_processor21from .typing import AfterRequestCallable
723 unmodified lines
745 assert view_func is not None, "expected view func if endpoint is not provided."746 return view_func.__name__747748748def get_root_path(import_name: str) -> str:749 """Find the root path of a package, or the path that contains a750 module. If it cannot be found, returns the current working751 directory.752753 Not to be confused with the value returned by :func:`find_package`.754755 :meta private:756 """757 # Module already imported and has a file attribute. Use that first.758 mod = sys.modules.get(import_name)759760 if mod is not None and hasattr(mod, "__file__"):761 return os.path.dirname(os.path.abspath(mod.__file__))762763 # Next attempt: check the loader.764 loader = pkgutil.get_loader(import_name)765766 # Loader does not exist or we're referring to an unloaded main767 # module or a main module without path (interactive sessions), go768 # with the current working directory.769 if loader is None or import_name == "__main__":770 return os.getcwd()771772 if hasattr(loader, "get_filename"):773 filepath = loader.get_filename(import_name) # type: ignore774 else:775 # Fall back to imports.776 __import__(import_name)777 mod = sys.modules[import_name]778 filepath = getattr(mod, "__file__", None)779780 # If we don't have a file path it might be because it is a781 # namespace package. In this case pick the root path from the782 # first module that is contained in the package.783 if filepath is None:784 raise RuntimeError(785 "No root path can be found for the provided module"786 f" {import_name!r}. This can happen because the module"787 " came from an import hook that does not provide file"788 " name information or because it's a namespace package."789 " In this case the root path needs to be explicitly"790 " provided."791 )792793 # filepath is import_name.py for a module, or __init__.py for a package.794 return os.path.dirname(os.path.abspath(filepath))795796749def _matching_loader_thinks_module_is_package(loader, mod_name):750 """Attempt to figure out if the given name is a package or a module.751752 :param: loader: The loader that handled the name.
113 unmodified lines