core.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:Mocha 作者: mardix 项目源码 文件源码
def register_package(pkg, prefix=None):
    """
    Allow to register an app packages by loading and exposing: templates, static,
    and exceptions for abort()

    Structure of package
        root
            | $package_name
                | __init__.py
                |
                | /templates
                    |
                    |
                |
                | /static
                    |
                    | assets.yml

    :param pkg: str - __package__
                    or __name__
                    or The root dir
                    or the dotted resource package (package.path.path,
                    usually __name__ of templates and static
    :param prefix: str - to prefix the template path
    """

    root_pkg_dir = pkg
    if not os.path.isdir(pkg) and "." in pkg:
        root_pkg_dir = pkg_resources.resource_filename(pkg, "")

    template_path = os.path.join(root_pkg_dir, "templates")
    static_path = os.path.join(root_pkg_dir, "static")

    logging.info("Registering App: " + pkg)
    if os.path.isdir(template_path):
        loader = jinja2.FileSystemLoader(template_path)
        if prefix:
            ploader = jinja2.PrefixLoader({
                prefix: loader
            })
            loader = ploader
        Mocha._template_paths.add(loader)

    if os.path.isdir(static_path):
        Mocha._static_paths.add(static_path)
        Mocha._add_asset_bundle(static_path)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号