Skip to content
Myr

merged

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parents 42e9b75, 8e90952 · Browse files · 3 files+60 added−113 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+60 added−113 removed

  • other:+60 added−113 removed

3 files+60 added−113 removed

modified:jinja/translators/python.py+47 added-109 removed
135 unmodified lines
136137 raise exc_type, exc_value, traceback138139140class TranslationOperator(object):141 """142 A translation operator has a single string representing the operation143 """144 def __init__(self, operator, translator):145 self.operator = operator146 self.translator = translator147148class UnaryOperator(TranslationOperator):149 """150 A unary operator has one side to the operation.151 """152 def __call__(self, node):153 """154 Apply operator.155 """156 return '( %s %s)' % (157 self.operator,158 self.translator.handle_node(node.node),159 )160161class BinaryOperator(TranslationOperator):162 """163 A binary operator has two sides to the operation.164 """165 def __call__(self, node):166 """167 Apply operator.168 """169 return '(%s %s %s)' % (170 self.translator.handle_node(node.left),171 self.operator,172 self.translator.handle_node(node.right),173 )174175class PythonTranslator(Translator):176 """177 Pass this translator a ast tree to get valid python code.178 """
53 unmodified lines
232 nodes.RegexExpression: self.handle_regex,233 nodes.SubscriptExpression: self.handle_subscript,234 nodes.FilterExpression: self.handle_filter_expr,235 nodes.CallExpression: self.handle_call_expr,201 nodes.AddExpression: self.handle_add,202 nodes.SubExpression: self.handle_sub,236 nodes.AddExpression: BinaryOperator('+', self),237 nodes.SubExpression: BinaryOperator('-', self),238 nodes.ConcatExpression: self.handle_concat,204 nodes.DivExpression: self.handle_div,205 nodes.FloorDivExpression: self.handle_floor_div,206 nodes.MulExpression: self.handle_mul,207 nodes.ModExpression: self.handle_mod,208 nodes.PosExpression: self.handle_pos,209 nodes.NegExpression: self.handle_neg,210 nodes.PowExpression: self.handle_pow,239 nodes.DivExpression: BinaryOperator('/', self),240 nodes.FloorDivExpression: BinaryOperator('//', self),241 nodes.MulExpression: BinaryOperator('*', self),242 nodes.ModExpression: BinaryOperator('%', self),243 nodes.PosExpression: UnaryOperator('+', self),244 nodes.NegExpression: UnaryOperator('-', self),245 nodes.PowExpression: BinaryOperator('**', self),246 nodes.DictExpression: self.handle_dict,247 nodes.SetExpression: self.handle_set_expr,248 nodes.ListExpression: self.handle_list,249 nodes.TupleExpression: self.handle_tuple,250 nodes.UndefinedExpression: self.handle_undefined,216 nodes.AndExpression: self.handle_and,217 nodes.OrExpression: self.handle_or,218 nodes.NotExpression: self.handle_not,251 nodes.AndExpression: BinaryOperator(' and ', self),252 nodes.OrExpression: BinaryOperator(' or ', self),253 nodes.NotExpression: UnaryOperator(' not ', self),254 nodes.SliceExpression: self.handle_slice,255 nodes.ConditionalExpression: self.handle_conditional_expr256 }257
769 unmodified lines
1027 dyn_args,1028 dyn_kwargs1029 )1030996 def handle_add(self, node):997 """998 Add two items.999 """1000 return '(%s + %s)' % (1001 self.handle_node(node.left),1002 self.handle_node(node.right)1003 )10041005 def handle_sub(self, node):1006 """1007 Sub two items.1008 """1009 return '(%s - %s)' % (1010 self.handle_node(node.left),1011 self.handle_node(node.right)1012 )10131031 def handle_concat(self, node):1032 """1033 Convert some objects to unicode and concatenate them.1034 """
2 unmodified lines
1037 'to_unicode(%s)' % self.handle_node(arg)1038 for arg in node.args1039 ])10401024 def handle_div(self, node):1025 """1026 Divide two items.1027 """1028 return '(%s / %s)' % (1029 self.handle_node(node.left),1030 self.handle_node(node.right)1031 )10411033 def handle_floor_div(self, node):1034 """1035 Divide two items, return truncated result.1036 """1037 return '(%s // %s)' % (1038 self.handle_node(node.left),1039 self.handle_node(node.right)1040 )10411042 def handle_mul(self, node):1043 """1044 Multiply two items.1045 """1046 return '(%s * %s)' % (1047 self.handle_node(node.left),1048 self.handle_node(node.right)1049 )10501051 def handle_mod(self, node):1052 """1053 Apply modulo.1054 """1055 return '(%s %% %s)' % (1056 self.handle_node(node.left),1057 self.handle_node(node.right)1058 )10591060 def handle_pos(self, node):1061 """1062 One of the more or less unused nodes.1063 """1064 return '(+%s)' % self.handle_node(node.node)10651066 def handle_neg(self, node):1067 """1068 Make a number negative.1069 """1070 return '(-%s)' % self.handle_node(node.node)10711072 def handle_pow(self, node):1073 """1074 handle foo**bar1075 """1076 return '(%s**%s)' % (1077 self.handle_node(node.left),1078 self.handle_node(node.right)1079 )10801042 def handle_dict(self, node):1043 """1044 Dict constructor syntax.1045 """
25 unmodified lines
1071 Return the current undefined literal.1072 """1073 return 'undefined_singleton'10741114 def handle_and(self, node):1115 """1116 Handle foo and bar.1117 """1118 return '(%s and %s)' % (1119 self.handle_node(node.left),1120 self.handle_node(node.right)1121 )10751123 def handle_or(self, node):1124 """1125 handle foo or bar.1126 """1127 return '(%s or %s)' % (1128 self.handle_node(node.left),1129 self.handle_node(node.right)1130 )11311132 def handle_not(self, node):1133 """1134 handle not operator.1135 """1136 return '(not %s)' % self.handle_node(node.node)11371076 def handle_slice(self, node, getslice_test=False):1077 """1078 Slice access. Because of backwards compatibilty to python's1079 `__getslice__` this function takes a second parameter that lets this
36 unmodified lines
modified:jinja/environment.py+11 added-2 removed
10 unmodified lines
11from jinja.lexer import Lexer12from jinja.parser import Parser13from jinja.loaders import LoaderWrapper14from jinja.datastructure import SilentUndefined, Markup, Context, FakeTranslator15from jinja.translators.python import PythonTranslator 16from jinja.utils import collect_translations, get_attribute17from jinja.exceptions import FilterNotFound, TestNotFound, \18 SecurityException, TemplateSyntaxError19from jinja.defaults import DEFAULT_FILTERS, DEFAULT_TESTS, DEFAULT_NAMESPACE
34 unmodified lines
54 context_class=Context,55 undefined_singleton=SilentUndefined,56 disable_regexps=False,57 friendly_traceback=True,57 translator_factory=None):58 translator_factory=None,59 template_translator=PythonTranslator):60 """61 Here the possible initialization parameters:6263 ========================= ============================================
54 unmodified lines
118 `translator_factory` A callback function that is called with119 the context as first argument to get the120 translator for the current instance.121 *new in Jinja 1.2*122 `template_translator` An class that defines a static method called123 process which can be used to process the 124 template's AST into a compiled python module.125 *new in Jinja 1.2*126 ========================= ============================================127128 All of these variables except those marked with a star (*) are129 modifiable after environment initialization.
36 unmodified lines
166167 # and here the translator factory168 self.translator_factory = translator_factory169170 # and here the AST translator171 self.template_translator = template_translator172173 # create lexer174 self.lexer = Lexer(self)175176 def loader(self, value):
50 unmodified lines
227 """228 Load a template from a loader. If the template does not exist, you229 will get a `TemplateNotFound` exception.230 """222 return self._loader.load(filename)231 return self._loader.load(filename, translator=self.template_translator)232233 def to_unicode(self, value):234 """235 Convert a value to unicode with the rules defined on the environment.
169 unmodified lines
modified:jinja/loaders.py+2 added-2 removed
237 unmodified lines
238 self.__lock.acquire()239 try:240 # caching is only possible for the python translator. skip241 # all other translators242 if translator is not PythonTranslator:242 if not issubclass(translator, PythonTranslator):243 return super(CachedLoaderMixin, self).load(244 environment, name, translator)245246 tmpl = None
115 unmodified lines
362 self.__lock.acquire()363 try:364 # caching is only possible for the python translator. skip365 # all other translators366 if translator is not PythonTranslator:366 if not issubclass(translator, PythonTranslator):367 return super(MemcachedLoaderMixin, self).load(368 environment, name, translator)369 tmpl = None370 push_to_memory = False
455 unmodified lines