Ejemplo n.º 1
0
def register_package(root_pkg=None, template="templates", static="static"):
    """
    Register a package's template directory and static
    :param root_pkg: str - The root dir,
                    or the dotted resource package (package.path.path,
                    usually __name__ of templates and static
    :param template: str - The template dir name. If root_dir is none,
                    template should be a path
    :param static: str - The static dir name. If root_dir is none,
                    static should be a path
    """
    if root_pkg:
        if not os.path.isdir(root_pkg) and "." in root_pkg:
            root_pkg = utils.get_pkg_resources_filename(root_pkg)

        template_path = os.path.join(root_pkg, template)
        static_path = os.path.join(root_pkg, static)
    else:
        template_path = template
        static_path = static

    if os.path.isdir(template_path):
        template_path = jinja2.FileSystemLoader(template_path)
        WebPortfolio._template_paths.add(template_path)
    else:
        logging.warn("Package registration: Not a <template> directory '%s' " % template_path)

    if os.path.isdir(static_path):
        WebPortfolio._static_paths.add(static_path)
        WebPortfolio._add_asset_bundle(static_path)
    else:
        logging.warn("Package registration: Not a <static> directory '%s' " % static_path)
Ejemplo n.º 2
0
def register_package(pkg):
    """
    Allow to register packages by loading and exposing: templates, static,
    and exceptions for abort()

    Structure of package
        root
            | $package_name
                | __init__.py
                |
                | exceptions.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
    """

    root_pkg_dir = pkg
    if not os.path.isdir(pkg) and "." in pkg:
        root_pkg_dir = utils.get_pkg_resources_filename(pkg)

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

    logging.info("Registering Package: " + pkg)
    if os.path.isdir(template_path):
        template_path = jinja2.FileSystemLoader(template_path)
        View._template_paths.add(template_path)

    if os.path.isdir(static_path):
        View._static_paths.add(static_path)
        View._add_asset_bundle(static_path)

    if os.path.isfile(os.path.join(root_pkg_dir, "exceptions.py")):
        exceptions = utils.import_string(pkg + ".exceptions")
        init_app(lambda x: abort.map_from_module(exceptions))