Ejemplo n.º 1
0
def crear_plantilla(archivo_fuente='user.c'):
    print "creando plantilla para archivo %s" % archivo_fuente
    renderer = Renderer()
    template = renderer.load_template(SOURCE_TEMPLATE)
    dato_plantilla = renderer.render(template, {'include_path': archivo_fuente})
    archivo = open(SOURCE_FILE, 'w')
    archivo.write(dato_plantilla)
    archivo.close()
    print "plantilla creada"
Ejemplo n.º 2
0
def render(template, context, out):
    renderer = Renderer(file_encoding="utf-8")

    try:
        template = renderer.load_template(template)
    except TemplateNotFoundError:
        pass

    try:
        context = json.load(open(context))
    except IOError:
        context = json.loads(context)

    rendered = renderer.render(template, context)

    file = codecs.open(out, "w", "utf-8")
    file.write(rendered)
    file.close()
Ejemplo n.º 3
0
def main(sys_argv=sys.argv):
    template, context, c_format, multiple = parse_args(sys_argv, USAGE)

    if template.endswith('.mustache'):
        template = template[:-9]

    renderer = Renderer()

    try:
        template = renderer.load_template(template)
    except TemplateNotFoundError:
        pass

    if context.endswith(".csv") or (c_format and (c_format == "csv")):
        try:
            context = csv.DictReader(open(context, 'rb'))#, delimiter=',', quotechar='"')
        except IOError:
            print('ERROR: Could not parse context as CSV file. Check usage for input format options')
            exit(-1)            
    else:
        try:
            context = json.load(open(context))
        except IOError:
            context = json.loads(context)
        except ValueError: #likely a not well-formed JSON string, or user forgot -f csv.
            print('ERROR: Could not parse context as JSON file or text, check usage for input format options')
            exit(1)

    if (multiple):
        print ("multiple render on field %s" % multiple)
        fileName, fileExt = os.path.splitext(multiple)
        for i,c in enumerate(context):
            if multiple in c:
                f_name = str(c[multiple])
            else:                
                f_name = "%s-%03d%s" % (fileName, i, fileExt)
            with open(f_name, "w") as f: # mode "wx" could be used to prevent overwriting, + pass IOError, adding "--force" option to override.
                rendered = renderer.render(template, c)
                f.write(rendered)
                print ("%s done") % f_name
    else:
        rendered = renderer.render(template, context)
        print rendered
Ejemplo n.º 4
0
def main(sys_argv):
    template, context = parse_args(sys_argv, USAGE)

    if template.endswith('.mustache'):
        template = template[:-9]

    renderer = Renderer()

    try:
        template = renderer.load_template(template)
    except IOError:
        pass

    try:
        context = json.load(open(context))
    except IOError:
        context = json.loads(context)

    rendered = renderer.render(template, context)
    print rendered
Ejemplo n.º 5
0
def main(sys_argv=sys.argv):
    template, context = parse_args(sys_argv, USAGE)

    if template.endswith('.mustache'):
        template = template[:-9]

    renderer = Renderer()

    try:
        template = renderer.load_template(template)
    except IOError:
        pass

    try:
        context = json.load(open(context))
    except IOError:
        context = json.loads(context)

    rendered = renderer.render(template, context)
    print rendered
Ejemplo n.º 6
0
class MustacheEngine(TemplateEngine):

    """
    Mustache template engine.
    """

    def __init__(self, **kw):
        """
        Initialize the template renderer.

        :param directories: Keyword parameter. A list of directories used as
            the search path for templates.
        """
        self.renderer = Renderer(search_dirs=kw['directories'])
        TemplateEngine.__init__(self, **kw)

    def get_template(self, name, **kw):
        """
        Get a template.

        :param name: The name of the template.
        """
        parts = name.rsplit('.', 2)
        if len(parts) > 1 and parts[1] == 'mustache':
            name = parts[0]
        return self.renderer.load_template(name)

    def render(self, name, **kw):
        """
        Render a template.

        :param name: The name of the template.
        :param **kw: The values to substitute in the template.
        """
        template = self.templates.get(name, None)
        return self.renderer.render(template, kw)
Ejemplo n.º 7
0
 def render(self, *context, **kwargs):
     renderer = Renderer(search_dirs=[thisDir()])
     template = renderer.load_template('govuk_template')
     return renderer.render(template, *context, **kwargs)
 def render(self, *context, **kwargs):
     renderer = Renderer(search_dirs=[thisDir()])
     template = renderer.load_template('govuk_template')
     return renderer.render(template, *context, **kwargs)
Ejemplo n.º 9
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    template, context, options = parse_args(argv, USAGE)

    if context is None and not sys.stdin.isatty():
        user_context, _ = read_yaml_frontmatter(sys.stdin)
    elif context:
        content = arg2text(context)
        user_context, _ = extract_context(content, greedy=True)
    else:
        user_context = {}

    # assuming first arg is a filename or template literal
    template = arg2text(template)
    template_context, template = extract_context(template)
    template_context.update(user_context)
    renderer = Renderer()
    rendered = renderer.render(template, template_context)
    print(rendered.encode('utf-8'))

    renderer = Renderer()

    try:
        template = renderer.load_template(template)
    except TemplateNotFoundError:
        pass

    if context.endswith(".csv") or (c_format and (c_format == "csv")):
        try:
            context = csv.DictReader(open(
                context, 'rb'))  #, delimiter=',', quotechar='"')
        except IOError:
            print(
                'ERROR: Could not parse context as CSV file. Check usage for input format options'
            )
            exit(-1)
    else:
        try:
            context = json.load(open(context))
        except IOError:
            context = json.loads(context)
        except ValueError:  #likely a not well-formed JSON string, or user forgot -f csv.
            print(
                'ERROR: Could not parse context as JSON file or text, check usage for input format options'
            )
            exit(1)

    if (multiple):
        print("multiple render on field %s" % multiple)
        fileName, fileExt = os.path.splitext(multiple)
        for i, c in enumerate(context):
            if multiple in c:
                f_name = str(c[multiple])
            else:
                f_name = "%s-%03d%s" % (fileName, i, fileExt)
            with open(
                    f_name, "w"
            ) as f:  # mode "wx" could be used to prevent overwriting, + pass IOError, adding "--force" option to override.
                rendered = renderer.render(template, c)
                f.write(rendered)
                print("%s done") % f_name
    else:
        rendered = renderer.render(template, context)
        print rendered