How to use the yay.errors.Anchor function in yay

To help you get started, we’ve selected a few yay examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github yaybu / yaybu / yaybu / util / templates.py View on Github external
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

from jinja2 import Environment, BaseLoader, StrictUndefined
from jinja2.exceptions import UndefinedError, TemplateSyntaxError

from yay import errors
from yaybu import error


class JinjaTracebackAnchor(errors.Anchor):
    """
    Most Jinja2 exceptions have quite poor error information :-(
    One of the features is that the Jinja errors appear in the TB...
    """
    def __init__(self, template, tb):
        self.template = template
        self.tb = tb

    def long_description_lines(self):
        t = self.tb
        while t.tb_next:
            t = t.tb_next
        f = t.tb_frame
        yield "'%s' at line %d" % (self.template.name, f.f_lineno-1)

        if self.template.filename and self.template.filename != "<template>":</template>
github yaybu / yaybu / yaybu / util / templates.py View on Github external
self.tb = tb

    def long_description_lines(self):
        t = self.tb
        while t.tb_next:
            t = t.tb_next
        f = t.tb_frame
        yield "'%s' at line %d" % (self.template.name, f.f_lineno-1)

        if self.template.filename and self.template.filename != "<template>":
            template = self.template
            source, t, _ = template.environment.loader.get_source(template.environment, template.filename)
            yield "  " + source.splitlines()[f.f_lineno-1]


class JinjaSyntaxAnchor(errors.Anchor):
    """
    Provide something like a yay anchor so that Jinja errors have lots of context
    """

    def __init__(self, orig_exception):
        self.orig_exception = orig_exception

    def long_description_lines(self):
        e = self.orig_exception
        yield "'%s' at line %d" % (e.filename or e.name, e.lineno) #, column 7
        if e.source is not None:
            try:
                yield "  " + e.source.splitlines()[e.lineno - 1]
            except IndexError:
                pass
</template>