Baseclass for all Jinja2 nodes. There are a number of nodes available of different types. There are four major types: - :class:`Stmt`: statements - :class:`Expr`: expressions - :class:`Helper`: helper nodes - :class:`Template`: the outermost wrapper node All nodes
| 105 | |
| 106 | |
| 107 | class Node(with_metaclass(NodeType, object)): |
| 108 | """Baseclass for all Jinja2 nodes. There are a number of nodes available |
| 109 | of different types. There are four major types: |
| 110 | |
| 111 | - :class:`Stmt`: statements |
| 112 | - :class:`Expr`: expressions |
| 113 | - :class:`Helper`: helper nodes |
| 114 | - :class:`Template`: the outermost wrapper node |
| 115 | |
| 116 | All nodes have fields and attributes. Fields may be other nodes, lists, |
| 117 | or arbitrary values. Fields are passed to the constructor as regular |
| 118 | positional arguments, attributes as keyword arguments. Each node has |
| 119 | two attributes: `lineno` (the line number of the node) and `environment`. |
| 120 | The `environment` attribute is set at the end of the parsing process for |
| 121 | all nodes automatically. |
| 122 | """ |
| 123 | fields = () |
| 124 | attributes = ('lineno', 'environment') |
| 125 | abstract = True |
| 126 | |
| 127 | def __init__(self, *fields, **attributes): |
| 128 | if self.abstract: |
| 129 | raise TypeError('abstract nodes are not instanciable') |
| 130 | if fields: |
| 131 | if len(fields) != len(self.fields): |
| 132 | if not self.fields: |
| 133 | raise TypeError('%r takes 0 arguments' % |
| 134 | self.__class__.__name__) |
| 135 | raise TypeError('%r takes 0 or %d argument%s' % ( |
| 136 | self.__class__.__name__, |
| 137 | len(self.fields), |
| 138 | len(self.fields) != 1 and 's' or '' |
| 139 | )) |
| 140 | for name, arg in izip(self.fields, fields): |
| 141 | setattr(self, name, arg) |
| 142 | for attr in self.attributes: |
| 143 | setattr(self, attr, attributes.pop(attr, None)) |
| 144 | if attributes: |
| 145 | raise TypeError('unknown attribute %r' % |
| 146 | next(iter(attributes))) |
| 147 | |
| 148 | def iter_fields(self, exclude=None, only=None): |
| 149 | """This method iterates over all fields that are defined and yields |
| 150 | ``(key, value)`` tuples. Per default all fields are returned, but |
| 151 | it's possible to limit that to some fields by providing the `only` |
| 152 | parameter or to exclude some using the `exclude` parameter. Both |
| 153 | should be sets or tuples of field names. |
| 154 | """ |
| 155 | for name in self.fields: |
| 156 | if (exclude is only is None) or \ |
| 157 | (exclude is not None and name not in exclude) or \ |
| 158 | (only is not None and name in only): |
| 159 | try: |
| 160 | yield name, getattr(self, name) |
| 161 | except AttributeError: |
| 162 | pass |
| 163 | |
| 164 | def iter_child_nodes(self, exclude=None, only=None): |
nothing calls this directly
no test coverage detected
searching dependent graphs…