Example #1
0
    def mainloop(self):
        """ The main loop.
        """
        # Print usage if not enough args or bad options
        if len(self.args) < 1:
            self.parser.error("You have to provide the root directory of your watch tree, or a metafile path!")

        configuration.engine.load_config()

        pathname = os.path.abspath(self.args[0])
        if os.path.isdir(pathname):
            watch = TreeWatch(Bunch(path=pathname, job_name="watch", active=True, dry_run=True, load_mode=None))
            asyncore.loop(timeout=~0, use_poll=True)
        else:
            config = Bunch()
            config.update(dict((key.split('.', 2)[-1], val)
                for key, val in configuration.torque.items()
                if key.startswith("job.treewatch.")
            ))
            config.update(dict(path=os.path.dirname(os.path.dirname(pathname)), job_name="treewatch", active=False, dry_run=True))
            watch = TreeWatch(config)
            handler = MetafileHandler(watch, pathname)

            ok = handler.parse()
            self.LOG.debug("Metafile '%s' would've %sbeen loaded" % (pathname, "" if ok else "NOT "))

            if ok:
                handler.addinfo()
                post_process = str if self.options.verbose else logutil.shorten
                self.LOG.info("Templating values are:\n    %s" % "\n    ".join("%s=%s" % (key, post_process(repr(val)))
                    for key, val in sorted(handler.ns.items())
                ))
Example #2
0
    def mainloop(self):
        """ The main loop.
        """
        # Print usage if not enough args or bad options
        if len(self.args) < 1:
            self.parser.error("You have to provide the root directory of your watch tree, or a metafile path!")

        configuration.engine.load_config()

        pathname = os.path.abspath(self.args[0])
        if os.path.isdir(pathname):
            watch = TreeWatch(Bunch(path=pathname, job_name="watch", active=True, dry_run=True, load_mode=None))
            asyncore.loop(timeout=~0, use_poll=True)
        else:
            config = Bunch()
            config.update(dict((key.split('.', 2)[-1], val)
                for key, val in configuration.torque.items()
                if key.startswith("job.treewatch.")
            ))
            config.update(dict(path=os.path.dirname(os.path.dirname(pathname)), job_name="treewatch", active=False, dry_run=True))
            watch = TreeWatch(config)
            handler = MetafileHandler(watch, pathname)

            ok = handler.parse()
            self.LOG.debug("Metafile '%s' would've %sbeen loaded" % (pathname, "" if ok else "NOT "))

            handler.addinfo()
            post_process = str if self.options.verbose else logutil.shorten
            self.LOG.info("Templating values are:\n    %s" % "\n    ".join("%s=%s" % (key, post_process(repr(val)))
                for key, val in sorted(handler.ns.items())
            ))
Example #3
0
def expand_template(template, namespace):
    """ Expand the given (preparsed) template.
        Currently, only Tempita templates are supported.

        @param template: The template, in preparsed form, or as a string (which then will be preparsed).
        @param namespace: Custom namespace that is added to the predefined defaults
            and takes precedence over those.
        @return: The expanded template.
        @raise LoggableError: In case of typical errors during template execution.
    """
    # Create helper namespace
    formatters = dict((name.split('_', 1)[1], method)
                      for name, method in globals().items()
                      if name.startswith("fmt_") or name.startswith("filter_"))
    helpers = Bunch()
    helpers.update(formatters)

    # Default templating namespace
    variables = dict(h=helpers, c=config.custom_template_helpers)
    variables.update(formatters)  # redundant, for backwards compatibility

    # Provided namespace takes precedence
    variables.update(namespace)

    # Expand template
    try:
        template = preparse(template)
        return template.substitute(**variables)
    except (AttributeError, ValueError, NameError, TypeError) as exc:
        hint = ''
        if "column" in str(exc):
            try:
                col = int(str(exc).split("column")[1].split()[0])
            except (TypeError, ValueError):
                pass
            else:
                hint = "%svVv\n" % (' ' * (col + 4))

        content = getattr(template, "content", template)
        raise error.LoggableError(
            "%s: %s in template:\n%s%s" %
            (type(exc).__name__, exc, hint, "\n".join(
                "%3d: %s" % (i + 1, line)
                for i, line in enumerate(content.splitlines()))))