Commit 38d1db0f authored by Matthias Braun's avatar Matthias Braun
Browse files

gen_ir: Move SimpleLoader to jinjautil file

Also improve the comment a bit
parent 056136a5
......@@ -114,7 +114,7 @@ IR_SPEC_GENERATED_INCLUDES := \
$(gendir)/ir/ir/gen_irdump.c.inl \
$(gendir)/ir/ir/gen_irnode.h
IR_SPEC_GENERATOR := $(srcdir)/scripts/gen_ir.py
IR_SPEC_GENERATOR_DEPS := $(IR_SPEC_GENERATOR) $(srcdir)/scripts/spec_util.py $(srcdir)/scripts/filters.py
IR_SPEC_GENERATOR_DEPS := $(IR_SPEC_GENERATOR) $(srcdir)/scripts/jinjautil.py $(srcdir)/scripts/spec_util.py $(srcdir)/scripts/filters.py
IR_SPEC := $(srcdir)/scripts/ir_spec.py
libfirm_BUILDDIRS += $(gendir)/include/libfirm
......
......@@ -135,8 +135,8 @@ def specgen(out, template=None):
template = "$srcdir/scripts/templates/" + basename(out)
generator = "$srcdir/scripts/gen_ir.py"
specfile = "$srcdir/scripts/ir_spec.py"
deps = [template, specfile, generator, "$srcdir/scripts/spec_util.py",
"$srcdir/scripts/filters.py"]
deps = [template, specfile, generator, "$srcdir/scripts/jinjautil.py",
"$srcdir/scripts/spec_util.py", "$srcdir/scripts/filters.py"]
return build("GEN_IR", out, deps, specfile=specfile,
template=template, generatortool=generator)
......
......@@ -5,7 +5,8 @@
import sys
import argparse
from datetime import datetime
from jinja2 import Environment, Template, BaseLoader
from jinja2 import Environment, Template
import jinjautil
from spec_util import is_dynamic_pinned, isAbstract, setdefault, load_spec, Attribute
from filters import arguments, filtjoin, has, hasnot
......@@ -232,30 +233,7 @@ def parse_tagfile(filename):
except:
tags = None
# simple FileSystemLoader variant. Compared to the default loader in jinja
# it does not perform searchpath magic and does not reject paths containig ".."
# for security.
# Note that we want to use a loader instead of simply env.from_string because
# we want to see the filename in error messages from jinja
class SimpleLoader(BaseLoader):
def __init__(self):
super(SimpleLoader, self).__init__()
self.includedirs = [""]
def get_source(self, environment, name):
for dir in self.includedirs:
try:
path = name if dir == "" else "%s/%s" % (dir, name)
contents = open(path).read()
except:
continue
def uptodate():
return False
return contents, name, uptodate
raise Exception("Could not open '%s'" % name)
def list_template(self):
return []
loader = SimpleLoader()
loader = jinjautil.SimpleLoader()
env = Environment(loader=loader, keep_trailing_newline=True)
env.filters['a_an'] = a_an
env.filters['args'] = args
......
# This file is part of libFirm.
# Copyright (C) 2015 Matthias Braun
from jinja2 import BaseLoader
# Simple FileSystemLoader variant. Compared to the default loader in jinja
# it does not perform searchpath magic and does not reject paths containig ".."
# (the jinja one does that for security reasons).
# Note that simply using env.from_string is a not a good alternative as then we
# don't see filename and proper line in error messages from jinja
class SimpleLoader(BaseLoader):
def __init__(self):
super(SimpleLoader, self).__init__()
self.includedirs = [""]
def get_source(self, environment, name):
for dir in self.includedirs:
try:
path = name if dir == "" else "%s/%s" % (dir, name)
contents = open(path).read()
except:
continue
def uptodate():
return False
return contents, name, uptodate
raise Exception("Could not open '%s'" % name)
def list_template(self):
return []
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment