Esempio n. 1
0
 def test_duplicate_context_warning(self):
     with warnings.catch_warnings(record=True) as w:
         stash.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)
Esempio n. 2
0
    def build_app(cls, import_name, modified_only=False, import_stash=False, 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 = Tango.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 = Tango.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 of stash in a package with config (case #3):
        >>> app = Tango.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 = Tango.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:
        >>> Tango.build_app('simplest.py') # doctest:+ELLIPSIS
        <tango.app.Tango object at 0x...>
        >>>

        Avoids import side effects:
        >>> Tango.build_app('importerror.py') # doctest:+ELLIPSIS
        <tango.app.Tango object at 0x...>
        >>> Tango.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 = cls(root_package_name)
                app.config.from_object(root_package_name + '.config')

        if app is None:
            app = cls(import_name)

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

        # Push app context onto request stack for use in initialization.
        # Use `with` or try-finally, 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_options = {'import_stash': import_stash}
            build_options['logfile'] = logfile
            build_options['modified_only'] = modified_only
            if module_exists(import_name + '.stash'):
                module = __import__(import_name, fromlist=['stash']).stash
                app.routes = build_module_routes(module, **build_options)
            else:
                app.routes = build_module_routes(import_name, **build_options)

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

        @app.context_processor
        def process_view_args():
            """Put view args into template context for tango template writers.

            The Flask Request object has a dictionary of the request handler
            function's arguments, but this is None when there are no view
            arguments. A Flask context processor must always return a
            dictionary.

            http://flask.pocoo.org/docs/api/#flask.Request.view_args
            """
            if request.view_args is None:
                return {}
            return request.view_args

        return app
Esempio n. 3
0
    def build_app(cls,
                  import_name,
                  modified_only=False,
                  import_stash=False,
                  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 = Tango.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 = Tango.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 of stash in a package with config (case #3):
        >>> app = Tango.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 = Tango.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:
        >>> Tango.build_app('simplest.py') # doctest:+ELLIPSIS
        <tango.app.Tango object at 0x...>
        >>>

        Avoids import side effects:
        >>> Tango.build_app('importerror.py') # doctest:+ELLIPSIS
        <tango.app.Tango object at 0x...>
        >>> Tango.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 = cls(root_package_name)
                app.config.from_object(root_package_name + '.config')

        if app is None:
            app = cls(import_name)

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

        # Push app context onto request stack for use in initialization.
        # Use `with` or try-finally, 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_options = {'import_stash': import_stash}
            build_options['logfile'] = logfile
            build_options['modified_only'] = modified_only
            if module_exists(import_name + '.stash'):
                module = __import__(import_name, fromlist=['stash']).stash
                app.routes = build_module_routes(module, **build_options)
            else:
                app.routes = build_module_routes(import_name, **build_options)

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

        @app.context_processor
        def process_view_args():
            """Put view args into template context for tango template writers.

            The Flask Request object has a dictionary of the request handler
            function's arguments, but this is None when there are no view
            arguments. A Flask context processor must always return a
            dictionary.

            http://flask.pocoo.org/docs/api/#flask.Request.view_args
            """
            if request.view_args is None:
                return {}
            return request.view_args

        return app
Esempio n. 4
0
 def test_duplicate_multiple_warnings(self):
     with warnings.catch_warnings(record=True) as w:
         stash.build_module_routes('warningsite.stash',
                                   import_stash=True)
         assert len(w) == 3
Esempio n. 5
0
 def test_duplicate_route_warning(self):
     with warnings.catch_warnings(record=True) as w:
         stash.build_module_routes('warningsite.stash.route')
         assert len(w) == 1
         assert issubclass(w[0].category, DuplicateRouteWarning)
         assert 'duplicate route' in str(w[0].message)