Exemplo n.º 1
0
 def test_duplicate_context_warning(self):
     with warnings.catch_warnings(record=True) as w:
         context.build_module_routes('warningsite.stash.context',
                                     import_stash=True)
         assert len(w) == 1
         assert issubclass(w[0].category, DuplicateContextWarning)
         assert 'duplicate context' in str(w[0].message)
Exemplo n.º 2
0
 def test_duplicate_multiple_warnings(self):
     with warnings.catch_warnings(record=True) as w:
         context.build_module_routes('warningsite.stash',
                                     import_stash=True)
         assert len(w) == 3
Exemplo n.º 3
0
 def test_duplicate_route_warning(self):
     with warnings.catch_warnings(record=True) as w:
         context.build_module_routes('warningsite.stash.route')
         assert len(w) == 1
         assert issubclass(w[0].category, DuplicateRouteWarning)
         assert 'duplicate route' in str(w[0].message)
Exemplo n.º 4
0
def build_app(import_name, import_stash=False, use_snapshot=True,
              logfile=None):
    """Create a Tango application object from a Python import name.

    This function accepts three kinds of import names:

    1. a single-module name where the module is itself a stash
    2. a package name which has a submodule or sub-package named 'stash'.
    3. a dotted module name referring to a module inside a package's stash.

    In case #1, the module is a self-contained application in one .py file.

    In case #2, the package is arbitrarily laid out, but the stash module
    inside it is one or more modules conforming to Tango's stash conventions.

    In case #3, the module is inside a package matching case #2, but the import
    name refers to a module which by itself would otherwise match case #1.
    Case #3 is treated like case #1 with one important exception.  Since the
    module is inside a package, that package is used as the application
    object's import name for the purposes of loading configuration directives,
    as stash modules are allowed to and encouraged to use their projects
    config.py.  This is essential to shelving modules in isolation when working
    with a stash package with more than one stash module.

    Example, using a single module called simplest.py (case #1):
    >>> app = build_app('simplest')
    >>> sorted(app.url_map.iter_rules(), key=lambda rule: rule.rule)
    ... # doctest:+NORMALIZE_WHITESPACE
    [<Rule '/' (HEAD, OPTIONS, GET) -> />,
     <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>]
    >>>

    Example, using the simplesite module in this project (case #2):
    >>> app = build_app('simplesite')
    >>> sorted(app.url_map.iter_rules(), key=lambda rule: rule.rule)
    ... # doctest:+NORMALIZE_WHITESPACE
    [<Rule '/' (HEAD, OPTIONS, GET) -> />,
     <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>]
    >>>

    Example, using submodule in the stash in a package with config (case #3):
    >>> app = build_app('simplesite.stash.index')
    >>> sorted(app.url_map.iter_rules(), key=lambda rule: rule.rule)
    ... # doctest:+NORMALIZE_WHITESPACE
    [<Rule '/' (HEAD, OPTIONS, GET) -> />,
     <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>]
    >>>

    Example, using submodule in the stash in a package without config
    (case #3 but identical to case #1):
    >>> app = build_app('testsite.stash.package.module')
    >>> sorted(app.url_map.iter_rules(), key=lambda rule: rule.rule)
    ... # doctest:+NORMALIZE_WHITESPACE
    [<Rule '/index.json' (HEAD, OPTIONS, GET) -> /index.json>,
     <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>]
    >>>

    For convenience in development, particularly with shell tab completion,
    a .py module name is also accepted:
    >>> build_app('simplest.py') # doctest:+ELLIPSIS
    <tango.app.Tango object at 0x...>
    >>>

    Avoids import side effects:
    >>> build_app('importerror.py') # doctest:+ELLIPSIS
    <tango.app.Tango object at 0x...>
    >>> build_app('importerror') # doctest:+ELLIPSIS
    <tango.app.Tango object at 0x...>
    >>>
    """
    import_name = fix_import_name_if_pyfile(import_name)

    # Initialize application. See docstring above for construction logic.
    app = None
    package_name, module_name = package_submodule(import_name)
    if package_name and module_name:
        # import_name points to submodule, look for package config.
        root_package_name = namespace_segments(import_name)[0]
        if module_exists(root_package_name + '.config'):
            app = Tango(root_package_name)
            app.config.from_object(root_package_name + '.config')

    if app is None:
        app = Tango(import_name)

    # Check for a site config.
    if module_exists(import_name + '.config'):
        app.config.from_object(import_name + '.config')

    # Create app context, push it onto request stack for use in initialization.
    # Use `with` or try-finally to ensure context is popped in case of error.
    with app.request_context(create_environ()):
        # Load Tango filters.
        # Only needed for packages; single modules do not have implicit templates.
        if module_is_package(import_name):
            tango.filters.init_app(app)

        # Build application routes, checking for a snapshot first.
        routes = None
        if use_snapshot:
            routes = get_snapshot(import_name)

        if routes is None:
            build_options = {'import_stash': import_stash}
            build_options['logfile'] = logfile
            if module_exists(import_name + '.stash'):
                module = __import__(import_name, fromlist=['stash']).stash
                routes = build_module_routes(module, **build_options)
            else:
                routes = build_module_routes(import_name, **build_options)
        else:
            print 'Using snapshot with stashed routes.'
        app.routes = routes

        # Stitch together context, template, and path.
        for route in app.routes:
            app.build_view(route)

    app.context_processor(lambda: request.view_args)
    return app