Flask之Helpers

来源:互联网 发布:学校结核病管理网络 编辑:程序博客网 时间:2024/06/05 00:42

Helper模块是Flask的辅助模块,提供一些公共能力的函数和类定义

_PackageBoundObject类

class _PackageBoundObject(object):    def __init__(self, import_name, template_folder=None, root_path=None):        #: The name of the package or module.  Do not change this once        #: it was set by the constructor.        self.import_name = import_name        #: location of the templates.  ``None`` if templates should not be        #: exposed.        self.template_folder = template_folder        if root_path is None:            root_path = get_root_path(self.import_name)        #: Where is the app root located?        self.root_path = root_path        self._static_folder = None        self._static_url_path = None
该类定义了如下参数:

  • import_name:模块名称
  • template_folder:template目录
  • root_path:根路径
  • _static_folder:static目录
  • _static_url_path:static访问url

root_path根路径可能需要通过import_name模块名导出,如何由模块名导出模块根路径?

模块导入不得不提到pkgutil包,请参考另外一篇文章《python之pkgutil》

def get_root_path(import_name):    """Returns the path to a package or cwd if that cannot be found.  This    returns the path of a package or the folder that contains a module.    Not to be confused with the package path returned by :func:`find_package`.    """    # Module already imported and has a file attribute.  Use that first.    mod = sys.modules.get(import_name)    if mod is not None and hasattr(mod, '__file__'):        return os.path.dirname(os.path.abspath(mod.__file__))    # Next attempt: check the loader.    loader = pkgutil.get_loader(import_name)    # Loader does not exist or we're referring to an unloaded main module    # or a main module without path (interactive sessions), go with the    # current working directory.    if loader is None or import_name == '__main__':        return os.getcwd()    # For .egg, zipimporter does not have get_filename until Python 2.7.    # Some other loaders might exhibit the same behavior.    if hasattr(loader, 'get_filename'):        filepath = loader.get_filename(import_name)    else:        # Fall back to imports.        __import__(import_name)        mod = sys.modules[import_name]        filepath = getattr(mod, '__file__', None)        # If we don't have a filepath it might be because we are a        # namespace package.  In this case we pick the root path from the        # first module that is contained in our package.        if filepath is None:            raise RuntimeError('No root path can be found for the provided '                               'module "%s".  This can happen because the '                               'module came from an import hook that does '                               'not provide file name information or because '                               'it\'s a namespace package.  In this case '                               'the root path needs to be explicitly '                               'provided.' % import_name)    # filepath is import_name.py for a module, or __init__.py for a package.    return os.path.dirname(os.path.abspath(filepath))
get_root_path函数用于根据模块名得到模块路径。
如果模块已经加载过,也即通过sys.modules列表能找到该模块名,并且该模块有__file__属性,则直接返回__file__即可得到模块路径

否则,先通过模块名找到模块加载器,如果加载器为空(说明模块未加载)或者模块是通过执行python模块的方式来访问模块,则直接返回当前进程工作路径作为模块路径

如果加载器方法get_filename,则调用该方法获取模块名;否则导入该模块,并且通过sys.modules获取模块及其属性__file__,如果__file__属性值未空,则直接抛出异常

最后,根据前面确定的文件路径确定根目录