Esempio n. 1
0
 def generate(self, **kwargs):
     """Generate this template with the given arguments."""
     namespace = {
         "escape": escape.xhtml_escape,
         "xhtml_escape": escape.xhtml_escape,
         "url_escape": escape.url_escape,
         "json_encode": escape.json_encode,
         "squeeze": escape.squeeze,
         "linkify": escape.linkify,
         "datetime": datetime,
         "_tt_utf8": escape.utf8,  # for internal use
         "_tt_string_types": (unicode_type, bytes_type),
         # __name__ and __loader__ allow the traceback mechanism to find
         # the generated source code.
         "__name__": self.name.replace('.', '_'),
         "__loader__": ObjectDict(get_source=lambda name: self.code),
     }
     namespace.update(self.namespace)
     namespace.update(kwargs)
     exec_in(self.compiled, namespace)
     execute = namespace["_tt_execute"]
     # Clear the traceback module's cache of source data now that
     # we've generated a new template (mainly for this module's
     # unittests, where different tests reuse the same name).
     linecache.clearcache()
     return execute()
Esempio n. 2
0
 def test_no_inherit_future(self):
     # This file has from __future__ import print_function...
     f = StringIO()
     print('hello', file=f)
     # ...but the template doesn't
     exec_in('print >> f, "world"', dict(f=f))
     self.assertEqual(f.getvalue(), 'hello\nworld\n')
Esempio n. 3
0
    def parse_config_file(self, path, final=True):
        """Parses and loads the Python config file at the given path.

        If ``final`` is ``False``, parse callbacks will not be run.
        This is useful for applications that wish to combine configurations
        from multiple sources.
        """
        config = {}
        with open(path) as f:
            exec_in(f.read(), config, config)
        for name in config:
            if name in self._options:
                self._options[name].set(config[name])

        if final:
            self.run_parse_callbacks()
Esempio n. 4
0
def main():
    """Command-line wrapper to re-run a script whenever its source changes.

    Scripts may be specified by filename or module name::

        python -m webalchemy.tornado.autoreload -m webalchemy.tornado.test.runtests
        python -m webalchemy.tornado.autoreload webalchemy.tornado/test/runtests.py

    Running a script with this wrapper is similar to calling
    `webalchemy.tornado.autoreload.wait` at the end of the script, but this wrapper
    can catch import-time problems like syntax errors that would otherwise
    prevent the script from reaching its call to `wait`.
    """
    original_argv = sys.argv
    sys.argv = sys.argv[:]
    if len(sys.argv) >= 3 and sys.argv[1] == "-m":
        mode = "module"
        module = sys.argv[2]
        del sys.argv[1:3]
    elif len(sys.argv) >= 2:
        mode = "script"
        script = sys.argv[1]
        sys.argv = sys.argv[1:]
    else:
        print(_USAGE, file=sys.stderr)
        sys.exit(1)

    try:
        if mode == "module":
            import runpy
            runpy.run_module(module, run_name="__main__", alter_sys=True)
        elif mode == "script":
            with open(script) as f:
                global __file__
                __file__ = script
                # Use globals as our "locals" dictionary so that
                # something that tries to import __main__ (e.g. the unittest
                # module) will see the right things.
                exec_in(f.read(), globals(), globals())
    except SystemExit as e:
        logging.basicConfig()
        gen_log.info("Script exited with status %s", e.code)
    except Exception as e:
        logging.basicConfig()
        gen_log.warning("Script exited with uncaught exception", exc_info=True)
        # If an exception occurred at import time, the file with the error
        # never made it into sys.modules and so we won't know to watch it.
        # Just to make sure we've covered everything, walk the stack trace
        # from the exception and watch every file.
        for (filename, lineno, name, line) in traceback.extract_tb(sys.exc_info()[2]):
            watch(filename)
        if isinstance(e, SyntaxError):
            # SyntaxErrors are special:  their innermost stack frame is fake
            # so extract_tb won't see it and we have to get the filename
            # from the exception object.
            watch(e.filename)
    else:
        logging.basicConfig()
        gen_log.info("Script exited normally")
    # restore sys.argv so subsequent executions will include autoreload
    sys.argv = original_argv

    if mode == 'module':
        # runpy did a fake import of the module as __main__, but now it's
        # no longer in sys.modules.  Figure out where it is and watch it.
        loader = pkgutil.get_loader(module)
        if loader is not None:
            watch(loader.get_filename())

    wait()