summaryrefslogtreecommitdiff
path: root/lib/spack/spack/tengine.py
blob: 08f759c223758f75ca272647e1343bb29854f7c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
##############################################################################
# Copyright (c) 2013-2017, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/llnl/spack
# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################

import textwrap

import jinja2
import llnl.util.lang
import six
import spack


TemplateNotFound = jinja2.TemplateNotFound


class ContextMeta(type):
    """Meta class for Context. It helps reducing the boilerplate in
    client code.
    """
    #: Keeps track of the context properties that have been added
    #: by the class that is being defined
    _new_context_properties = []

    def __new__(mcs, name, bases, attr_dict):
        # Merge all the context properties that are coming from base classes
        # into a list without duplicates.
        context_properties = list(mcs._new_context_properties)
        for x in bases:
            try:
                context_properties.extend(x.context_properties)
            except AttributeError:
                pass
        context_properties = list(llnl.util.lang.dedupe(context_properties))

        # Flush the list
        mcs._new_context_properties = []

        # Attach the list to the class being created
        attr_dict['context_properties'] = context_properties

        return super(ContextMeta, mcs).__new__(mcs, name, bases, attr_dict)

    @classmethod
    def context_property(mcs, func):
        """Decorator that adds a function name to the list of new context
        properties, and then returns a property.
        """
        name = func.__name__
        mcs._new_context_properties.append(name)
        return property(func)


#: A saner way to use the decorator
context_property = ContextMeta.context_property


class Context(six.with_metaclass(ContextMeta, object)):
    """Base class for context classes that are used with the template
    engine.
    """

    def to_dict(self):
        """Returns a dictionary containing all the context properties."""
        d = [(name, getattr(self, name)) for name in self.context_properties]
        return dict(d)


def make_environment(dirs=None):
    """Returns an configured environment for template rendering."""
    if dirs is None:
        # Default directories where to search for templates
        dirs = spack.template_dirs
    # Loader for the templates
    loader = jinja2.FileSystemLoader(dirs)
    # Environment of the template engine
    env = jinja2.Environment(loader=loader, trim_blocks=True)
    # Custom filters
    _set_filters(env)
    return env


# Extra filters for template engine environment

def prepend_to_line(text, token):
    """Prepends a token to each line in text"""
    return [token + line for line in text]


def quote(text):
    """Quotes each line in text"""
    return ['"{0}"'.format(line) for line in text]


def _set_filters(env):
    """Sets custom filters to the template engine environment"""
    env.filters['textwrap'] = textwrap.wrap
    env.filters['prepend_to_line'] = prepend_to_line
    env.filters['join'] = '\n'.join
    env.filters['quote'] = quote