Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/jinja2/environment.py: 49%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

591 statements  

1"""Classes for managing templates and their runtime and compile time 

2options. 

3""" 

4 

5import os 

6import typing 

7import typing as t 

8import weakref 

9from collections import ChainMap 

10from functools import lru_cache 

11from functools import partial 

12from functools import reduce 

13from types import CodeType 

14 

15from markupsafe import Markup 

16 

17from . import nodes 

18from .compiler import CodeGenerator 

19from .compiler import generate 

20from .defaults import BLOCK_END_STRING 

21from .defaults import BLOCK_START_STRING 

22from .defaults import COMMENT_END_STRING 

23from .defaults import COMMENT_START_STRING 

24from .defaults import DEFAULT_FILTERS # type: ignore[attr-defined] 

25from .defaults import DEFAULT_NAMESPACE 

26from .defaults import DEFAULT_POLICIES 

27from .defaults import DEFAULT_TESTS # type: ignore[attr-defined] 

28from .defaults import KEEP_TRAILING_NEWLINE 

29from .defaults import LINE_COMMENT_PREFIX 

30from .defaults import LINE_STATEMENT_PREFIX 

31from .defaults import LSTRIP_BLOCKS 

32from .defaults import NEWLINE_SEQUENCE 

33from .defaults import TRIM_BLOCKS 

34from .defaults import VARIABLE_END_STRING 

35from .defaults import VARIABLE_START_STRING 

36from .exceptions import TemplateNotFound 

37from .exceptions import TemplateRuntimeError 

38from .exceptions import TemplatesNotFound 

39from .exceptions import TemplateSyntaxError 

40from .exceptions import UndefinedError 

41from .lexer import get_lexer 

42from .lexer import Lexer 

43from .lexer import TokenStream 

44from .nodes import EvalContext 

45from .parser import Parser 

46from .runtime import Context 

47from .runtime import new_context 

48from .runtime import Undefined 

49from .utils import _PassArg 

50from .utils import concat 

51from .utils import consume 

52from .utils import import_string 

53from .utils import internalcode 

54from .utils import LRUCache 

55from .utils import missing 

56 

57if t.TYPE_CHECKING: 

58 import typing_extensions as te 

59 

60 from .bccache import BytecodeCache 

61 from .ext import Extension 

62 from .loaders import BaseLoader 

63 

64_env_bound = t.TypeVar("_env_bound", bound="Environment") 

65 

66 

67# for direct template usage we have up to ten living environments 

68@lru_cache(maxsize=10) 

69def get_spontaneous_environment(cls: t.Type[_env_bound], *args: t.Any) -> _env_bound: 

70 """Return a new spontaneous environment. A spontaneous environment 

71 is used for templates created directly rather than through an 

72 existing environment. 

73 

74 :param cls: Environment class to create. 

75 :param args: Positional arguments passed to environment. 

76 """ 

77 env = cls(*args) 

78 env.shared = True 

79 return env 

80 

81 

82def create_cache( 

83 size: int, 

84) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[BaseLoader]", str], "Template"]]: 

85 """Return the cache class for the given size.""" 

86 if size == 0: 

87 return None 

88 

89 if size < 0: 

90 return {} 

91 

92 return LRUCache(size) # type: ignore 

93 

94 

95def copy_cache( 

96 cache: t.Optional[ 

97 t.MutableMapping[t.Tuple["weakref.ref[BaseLoader]", str], "Template"] 

98 ], 

99) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[BaseLoader]", str], "Template"]]: 

100 """Create an empty copy of the given cache.""" 

101 if cache is None: 

102 return None 

103 

104 if type(cache) is dict: # noqa E721 

105 return {} 

106 

107 return LRUCache(cache.capacity) # type: ignore 

108 

109 

110def load_extensions( 

111 environment: "Environment", 

112 extensions: t.Sequence[t.Union[str, t.Type["Extension"]]], 

113) -> t.Dict[str, "Extension"]: 

114 """Load the extensions from the list and bind it to the environment. 

115 Returns a dict of instantiated extensions. 

116 """ 

117 result = {} 

118 

119 for extension in extensions: 

120 if isinstance(extension, str): 

121 extension = t.cast(t.Type["Extension"], import_string(extension)) 

122 

123 result[extension.identifier] = extension(environment) 

124 

125 return result 

126 

127 

128def _environment_config_check(environment: "Environment") -> "Environment": 

129 """Perform a sanity check on the environment.""" 

130 assert issubclass( 

131 environment.undefined, Undefined 

132 ), "'undefined' must be a subclass of 'jinja2.Undefined'." 

133 assert ( 

134 environment.block_start_string 

135 != environment.variable_start_string 

136 != environment.comment_start_string 

137 ), "block, variable and comment start strings must be different." 

138 assert environment.newline_sequence in { 

139 "\r", 

140 "\r\n", 

141 "\n", 

142 }, "'newline_sequence' must be one of '\\n', '\\r\\n', or '\\r'." 

143 return environment 

144 

145 

146class Environment: 

147 r"""The core component of Jinja is the `Environment`. It contains 

148 important shared variables like configuration, filters, tests, 

149 globals and others. Instances of this class may be modified if 

150 they are not shared and if no template was loaded so far. 

151 Modifications on environments after the first template was loaded 

152 will lead to surprising effects and undefined behavior. 

153 

154 Here are the possible initialization parameters: 

155 

156 `block_start_string` 

157 The string marking the beginning of a block. Defaults to ``'{%'``. 

158 

159 `block_end_string` 

160 The string marking the end of a block. Defaults to ``'%}'``. 

161 

162 `variable_start_string` 

163 The string marking the beginning of a print statement. 

164 Defaults to ``'{{'``. 

165 

166 `variable_end_string` 

167 The string marking the end of a print statement. Defaults to 

168 ``'}}'``. 

169 

170 `comment_start_string` 

171 The string marking the beginning of a comment. Defaults to ``'{#'``. 

172 

173 `comment_end_string` 

174 The string marking the end of a comment. Defaults to ``'#}'``. 

175 

176 `line_statement_prefix` 

177 If given and a string, this will be used as prefix for line based 

178 statements. See also :ref:`line-statements`. 

179 

180 `line_comment_prefix` 

181 If given and a string, this will be used as prefix for line based 

182 comments. See also :ref:`line-statements`. 

183 

184 .. versionadded:: 2.2 

185 

186 `trim_blocks` 

187 If this is set to ``True`` the first newline after a block is 

188 removed (block, not variable tag!). Defaults to `False`. 

189 

190 `lstrip_blocks` 

191 If this is set to ``True`` leading spaces and tabs are stripped 

192 from the start of a line to a block. Defaults to `False`. 

193 

194 `newline_sequence` 

195 The sequence that starts a newline. Must be one of ``'\r'``, 

196 ``'\n'`` or ``'\r\n'``. The default is ``'\n'`` which is a 

197 useful default for Linux and OS X systems as well as web 

198 applications. 

199 

200 `keep_trailing_newline` 

201 Preserve the trailing newline when rendering templates. 

202 The default is ``False``, which causes a single newline, 

203 if present, to be stripped from the end of the template. 

204 

205 .. versionadded:: 2.7 

206 

207 `extensions` 

208 List of Jinja extensions to use. This can either be import paths 

209 as strings or extension classes. For more information have a 

210 look at :ref:`the extensions documentation <jinja-extensions>`. 

211 

212 `optimized` 

213 should the optimizer be enabled? Default is ``True``. 

214 

215 `undefined` 

216 :class:`Undefined` or a subclass of it that is used to represent 

217 undefined values in the template. 

218 

219 `finalize` 

220 A callable that can be used to process the result of a variable 

221 expression before it is output. For example one can convert 

222 ``None`` implicitly into an empty string here. 

223 

224 `autoescape` 

225 If set to ``True`` the XML/HTML autoescaping feature is enabled by 

226 default. For more details about autoescaping see 

227 :class:`~markupsafe.Markup`. As of Jinja 2.4 this can also 

228 be a callable that is passed the template name and has to 

229 return ``True`` or ``False`` depending on autoescape should be 

230 enabled by default. 

231 

232 .. versionchanged:: 2.4 

233 `autoescape` can now be a function 

234 

235 `loader` 

236 The template loader for this environment. 

237 

238 `cache_size` 

239 The size of the cache. Per default this is ``400`` which means 

240 that if more than 400 templates are loaded the loader will clean 

241 out the least recently used template. If the cache size is set to 

242 ``0`` templates are recompiled all the time, if the cache size is 

243 ``-1`` the cache will not be cleaned. 

244 

245 .. versionchanged:: 2.8 

246 The cache size was increased to 400 from a low 50. 

247 

248 `auto_reload` 

249 Some loaders load templates from locations where the template 

250 sources may change (ie: file system or database). If 

251 ``auto_reload`` is set to ``True`` (default) every time a template is 

252 requested the loader checks if the source changed and if yes, it 

253 will reload the template. For higher performance it's possible to 

254 disable that. 

255 

256 `bytecode_cache` 

257 If set to a bytecode cache object, this object will provide a 

258 cache for the internal Jinja bytecode so that templates don't 

259 have to be parsed if they were not changed. 

260 

261 See :ref:`bytecode-cache` for more information. 

262 

263 `enable_async` 

264 If set to true this enables async template execution which 

265 allows using async functions and generators. 

266 """ 

267 

268 #: if this environment is sandboxed. Modifying this variable won't make 

269 #: the environment sandboxed though. For a real sandboxed environment 

270 #: have a look at jinja2.sandbox. This flag alone controls the code 

271 #: generation by the compiler. 

272 sandboxed = False 

273 

274 #: True if the environment is just an overlay 

275 overlayed = False 

276 

277 #: the environment this environment is linked to if it is an overlay 

278 linked_to: t.Optional["Environment"] = None 

279 

280 #: shared environments have this set to `True`. A shared environment 

281 #: must not be modified 

282 shared = False 

283 

284 #: the class that is used for code generation. See 

285 #: :class:`~jinja2.compiler.CodeGenerator` for more information. 

286 code_generator_class: t.Type["CodeGenerator"] = CodeGenerator 

287 

288 concat = "".join 

289 

290 #: the context class that is used for templates. See 

291 #: :class:`~jinja2.runtime.Context` for more information. 

292 context_class: t.Type[Context] = Context 

293 

294 template_class: t.Type["Template"] 

295 

296 def __init__( 

297 self, 

298 block_start_string: str = BLOCK_START_STRING, 

299 block_end_string: str = BLOCK_END_STRING, 

300 variable_start_string: str = VARIABLE_START_STRING, 

301 variable_end_string: str = VARIABLE_END_STRING, 

302 comment_start_string: str = COMMENT_START_STRING, 

303 comment_end_string: str = COMMENT_END_STRING, 

304 line_statement_prefix: t.Optional[str] = LINE_STATEMENT_PREFIX, 

305 line_comment_prefix: t.Optional[str] = LINE_COMMENT_PREFIX, 

306 trim_blocks: bool = TRIM_BLOCKS, 

307 lstrip_blocks: bool = LSTRIP_BLOCKS, 

308 newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = NEWLINE_SEQUENCE, 

309 keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE, 

310 extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = (), 

311 optimized: bool = True, 

312 undefined: t.Type[Undefined] = Undefined, 

313 finalize: t.Optional[t.Callable[..., t.Any]] = None, 

314 autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = False, 

315 loader: t.Optional["BaseLoader"] = None, 

316 cache_size: int = 400, 

317 auto_reload: bool = True, 

318 bytecode_cache: t.Optional["BytecodeCache"] = None, 

319 enable_async: bool = False, 

320 ): 

321 # !!Important notice!! 

322 # The constructor accepts quite a few arguments that should be 

323 # passed by keyword rather than position. However it's important to 

324 # not change the order of arguments because it's used at least 

325 # internally in those cases: 

326 # - spontaneous environments (i18n extension and Template) 

327 # - unittests 

328 # If parameter changes are required only add parameters at the end 

329 # and don't change the arguments (or the defaults!) of the arguments 

330 # existing already. 

331 

332 # lexer / parser information 

333 self.block_start_string = block_start_string 

334 self.block_end_string = block_end_string 

335 self.variable_start_string = variable_start_string 

336 self.variable_end_string = variable_end_string 

337 self.comment_start_string = comment_start_string 

338 self.comment_end_string = comment_end_string 

339 self.line_statement_prefix = line_statement_prefix 

340 self.line_comment_prefix = line_comment_prefix 

341 self.trim_blocks = trim_blocks 

342 self.lstrip_blocks = lstrip_blocks 

343 self.newline_sequence = newline_sequence 

344 self.keep_trailing_newline = keep_trailing_newline 

345 

346 # runtime information 

347 self.undefined: t.Type[Undefined] = undefined 

348 self.optimized = optimized 

349 self.finalize = finalize 

350 self.autoescape = autoescape 

351 

352 # defaults 

353 self.filters = DEFAULT_FILTERS.copy() 

354 self.tests = DEFAULT_TESTS.copy() 

355 self.globals = DEFAULT_NAMESPACE.copy() 

356 

357 # set the loader provided 

358 self.loader = loader 

359 self.cache = create_cache(cache_size) 

360 self.bytecode_cache = bytecode_cache 

361 self.auto_reload = auto_reload 

362 

363 # configurable policies 

364 self.policies = DEFAULT_POLICIES.copy() 

365 

366 # load extensions 

367 self.extensions = load_extensions(self, extensions) 

368 

369 self.is_async = enable_async 

370 _environment_config_check(self) 

371 

372 def add_extension(self, extension: t.Union[str, t.Type["Extension"]]) -> None: 

373 """Adds an extension after the environment was created. 

374 

375 .. versionadded:: 2.5 

376 """ 

377 self.extensions.update(load_extensions(self, [extension])) 

378 

379 def extend(self, **attributes: t.Any) -> None: 

380 """Add the items to the instance of the environment if they do not exist 

381 yet. This is used by :ref:`extensions <writing-extensions>` to register 

382 callbacks and configuration values without breaking inheritance. 

383 """ 

384 for key, value in attributes.items(): 

385 if not hasattr(self, key): 

386 setattr(self, key, value) 

387 

388 def overlay( 

389 self, 

390 block_start_string: str = missing, 

391 block_end_string: str = missing, 

392 variable_start_string: str = missing, 

393 variable_end_string: str = missing, 

394 comment_start_string: str = missing, 

395 comment_end_string: str = missing, 

396 line_statement_prefix: t.Optional[str] = missing, 

397 line_comment_prefix: t.Optional[str] = missing, 

398 trim_blocks: bool = missing, 

399 lstrip_blocks: bool = missing, 

400 newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = missing, 

401 keep_trailing_newline: bool = missing, 

402 extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = missing, 

403 optimized: bool = missing, 

404 undefined: t.Type[Undefined] = missing, 

405 finalize: t.Optional[t.Callable[..., t.Any]] = missing, 

406 autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = missing, 

407 loader: t.Optional["BaseLoader"] = missing, 

408 cache_size: int = missing, 

409 auto_reload: bool = missing, 

410 bytecode_cache: t.Optional["BytecodeCache"] = missing, 

411 enable_async: bool = False, 

412 ) -> "Environment": 

413 """Create a new overlay environment that shares all the data with the 

414 current environment except for cache and the overridden attributes. 

415 Extensions cannot be removed for an overlayed environment. An overlayed 

416 environment automatically gets all the extensions of the environment it 

417 is linked to plus optional extra extensions. 

418 

419 Creating overlays should happen after the initial environment was set 

420 up completely. Not all attributes are truly linked, some are just 

421 copied over so modifications on the original environment may not shine 

422 through. 

423 

424 .. versionchanged:: 3.1.2 

425 Added the ``newline_sequence``,, ``keep_trailing_newline``, 

426 and ``enable_async`` parameters to match ``__init__``. 

427 """ 

428 args = dict(locals()) 

429 del args["self"], args["cache_size"], args["extensions"], args["enable_async"] 

430 

431 rv = object.__new__(self.__class__) 

432 rv.__dict__.update(self.__dict__) 

433 rv.overlayed = True 

434 rv.linked_to = self 

435 

436 for key, value in args.items(): 

437 if value is not missing: 

438 setattr(rv, key, value) 

439 

440 if cache_size is not missing: 

441 rv.cache = create_cache(cache_size) 

442 else: 

443 rv.cache = copy_cache(self.cache) 

444 

445 rv.extensions = {} 

446 for key, value in self.extensions.items(): 

447 rv.extensions[key] = value.bind(rv) 

448 if extensions is not missing: 

449 rv.extensions.update(load_extensions(rv, extensions)) 

450 

451 if enable_async is not missing: 

452 rv.is_async = enable_async 

453 

454 return _environment_config_check(rv) 

455 

456 @property 

457 def lexer(self) -> Lexer: 

458 """The lexer for this environment.""" 

459 return get_lexer(self) 

460 

461 def iter_extensions(self) -> t.Iterator["Extension"]: 

462 """Iterates over the extensions by priority.""" 

463 return iter(sorted(self.extensions.values(), key=lambda x: x.priority)) 

464 

465 def getitem( 

466 self, obj: t.Any, argument: t.Union[str, t.Any] 

467 ) -> t.Union[t.Any, Undefined]: 

468 """Get an item or attribute of an object but prefer the item.""" 

469 try: 

470 return obj[argument] 

471 except (AttributeError, TypeError, LookupError): 

472 if isinstance(argument, str): 

473 try: 

474 attr = str(argument) 

475 except Exception: 

476 pass 

477 else: 

478 try: 

479 return getattr(obj, attr) 

480 except AttributeError: 

481 pass 

482 return self.undefined(obj=obj, name=argument) 

483 

484 def getattr(self, obj: t.Any, attribute: str) -> t.Any: 

485 """Get an item or attribute of an object but prefer the attribute. 

486 Unlike :meth:`getitem` the attribute *must* be a string. 

487 """ 

488 try: 

489 return getattr(obj, attribute) 

490 except AttributeError: 

491 pass 

492 try: 

493 return obj[attribute] 

494 except (TypeError, LookupError, AttributeError): 

495 return self.undefined(obj=obj, name=attribute) 

496 

497 def _filter_test_common( 

498 self, 

499 name: t.Union[str, Undefined], 

500 value: t.Any, 

501 args: t.Optional[t.Sequence[t.Any]], 

502 kwargs: t.Optional[t.Mapping[str, t.Any]], 

503 context: t.Optional[Context], 

504 eval_ctx: t.Optional[EvalContext], 

505 is_filter: bool, 

506 ) -> t.Any: 

507 if is_filter: 

508 env_map = self.filters 

509 type_name = "filter" 

510 else: 

511 env_map = self.tests 

512 type_name = "test" 

513 

514 func = env_map.get(name) # type: ignore 

515 

516 if func is None: 

517 msg = f"No {type_name} named {name!r}." 

518 

519 if isinstance(name, Undefined): 

520 try: 

521 name._fail_with_undefined_error() 

522 except Exception as e: 

523 msg = f"{msg} ({e}; did you forget to quote the callable name?)" 

524 

525 raise TemplateRuntimeError(msg) 

526 

527 args = [value, *(args if args is not None else ())] 

528 kwargs = kwargs if kwargs is not None else {} 

529 pass_arg = _PassArg.from_obj(func) 

530 

531 if pass_arg is _PassArg.context: 

532 if context is None: 

533 raise TemplateRuntimeError( 

534 f"Attempted to invoke a context {type_name} without context." 

535 ) 

536 

537 args.insert(0, context) 

538 elif pass_arg is _PassArg.eval_context: 

539 if eval_ctx is None: 

540 if context is not None: 

541 eval_ctx = context.eval_ctx 

542 else: 

543 eval_ctx = EvalContext(self) 

544 

545 args.insert(0, eval_ctx) 

546 elif pass_arg is _PassArg.environment: 

547 args.insert(0, self) 

548 

549 return func(*args, **kwargs) 

550 

551 def call_filter( 

552 self, 

553 name: str, 

554 value: t.Any, 

555 args: t.Optional[t.Sequence[t.Any]] = None, 

556 kwargs: t.Optional[t.Mapping[str, t.Any]] = None, 

557 context: t.Optional[Context] = None, 

558 eval_ctx: t.Optional[EvalContext] = None, 

559 ) -> t.Any: 

560 """Invoke a filter on a value the same way the compiler does. 

561 

562 This might return a coroutine if the filter is running from an 

563 environment in async mode and the filter supports async 

564 execution. It's your responsibility to await this if needed. 

565 

566 .. versionadded:: 2.7 

567 """ 

568 return self._filter_test_common( 

569 name, value, args, kwargs, context, eval_ctx, True 

570 ) 

571 

572 def call_test( 

573 self, 

574 name: str, 

575 value: t.Any, 

576 args: t.Optional[t.Sequence[t.Any]] = None, 

577 kwargs: t.Optional[t.Mapping[str, t.Any]] = None, 

578 context: t.Optional[Context] = None, 

579 eval_ctx: t.Optional[EvalContext] = None, 

580 ) -> t.Any: 

581 """Invoke a test on a value the same way the compiler does. 

582 

583 This might return a coroutine if the test is running from an 

584 environment in async mode and the test supports async execution. 

585 It's your responsibility to await this if needed. 

586 

587 .. versionchanged:: 3.0 

588 Tests support ``@pass_context``, etc. decorators. Added 

589 the ``context`` and ``eval_ctx`` parameters. 

590 

591 .. versionadded:: 2.7 

592 """ 

593 return self._filter_test_common( 

594 name, value, args, kwargs, context, eval_ctx, False 

595 ) 

596 

597 @internalcode 

598 def parse( 

599 self, 

600 source: str, 

601 name: t.Optional[str] = None, 

602 filename: t.Optional[str] = None, 

603 ) -> nodes.Template: 

604 """Parse the sourcecode and return the abstract syntax tree. This 

605 tree of nodes is used by the compiler to convert the template into 

606 executable source- or bytecode. This is useful for debugging or to 

607 extract information from templates. 

608 

609 If you are :ref:`developing Jinja extensions <writing-extensions>` 

610 this gives you a good overview of the node tree generated. 

611 """ 

612 try: 

613 return self._parse(source, name, filename) 

614 except TemplateSyntaxError: 

615 self.handle_exception(source=source) 

616 

617 def _parse( 

618 self, source: str, name: t.Optional[str], filename: t.Optional[str] 

619 ) -> nodes.Template: 

620 """Internal parsing function used by `parse` and `compile`.""" 

621 return Parser(self, source, name, filename).parse() 

622 

623 def lex( 

624 self, 

625 source: str, 

626 name: t.Optional[str] = None, 

627 filename: t.Optional[str] = None, 

628 ) -> t.Iterator[t.Tuple[int, str, str]]: 

629 """Lex the given sourcecode and return a generator that yields 

630 tokens as tuples in the form ``(lineno, token_type, value)``. 

631 This can be useful for :ref:`extension development <writing-extensions>` 

632 and debugging templates. 

633 

634 This does not perform preprocessing. If you want the preprocessing 

635 of the extensions to be applied you have to filter source through 

636 the :meth:`preprocess` method. 

637 """ 

638 source = str(source) 

639 try: 

640 return self.lexer.tokeniter(source, name, filename) 

641 except TemplateSyntaxError: 

642 self.handle_exception(source=source) 

643 

644 def preprocess( 

645 self, 

646 source: str, 

647 name: t.Optional[str] = None, 

648 filename: t.Optional[str] = None, 

649 ) -> str: 

650 """Preprocesses the source with all extensions. This is automatically 

651 called for all parsing and compiling methods but *not* for :meth:`lex` 

652 because there you usually only want the actual source tokenized. 

653 """ 

654 return reduce( 

655 lambda s, e: e.preprocess(s, name, filename), 

656 self.iter_extensions(), 

657 str(source), 

658 ) 

659 

660 def _tokenize( 

661 self, 

662 source: str, 

663 name: t.Optional[str], 

664 filename: t.Optional[str] = None, 

665 state: t.Optional[str] = None, 

666 ) -> TokenStream: 

667 """Called by the parser to do the preprocessing and filtering 

668 for all the extensions. Returns a :class:`~jinja2.lexer.TokenStream`. 

669 """ 

670 source = self.preprocess(source, name, filename) 

671 stream = self.lexer.tokenize(source, name, filename, state) 

672 

673 for ext in self.iter_extensions(): 

674 stream = ext.filter_stream(stream) # type: ignore 

675 

676 if not isinstance(stream, TokenStream): 

677 stream = TokenStream(stream, name, filename) # type: ignore[unreachable] 

678 

679 return stream 

680 

681 def _generate( 

682 self, 

683 source: nodes.Template, 

684 name: t.Optional[str], 

685 filename: t.Optional[str], 

686 defer_init: bool = False, 

687 ) -> str: 

688 """Internal hook that can be overridden to hook a different generate 

689 method in. 

690 

691 .. versionadded:: 2.5 

692 """ 

693 return generate( # type: ignore 

694 source, 

695 self, 

696 name, 

697 filename, 

698 defer_init=defer_init, 

699 optimized=self.optimized, 

700 ) 

701 

702 def _compile(self, source: str, filename: str) -> CodeType: 

703 """Internal hook that can be overridden to hook a different compile 

704 method in. 

705 

706 .. versionadded:: 2.5 

707 """ 

708 return compile(source, filename, "exec") 

709 

710 @typing.overload 

711 def compile( # type: ignore 

712 self, 

713 source: t.Union[str, nodes.Template], 

714 name: t.Optional[str] = None, 

715 filename: t.Optional[str] = None, 

716 raw: "te.Literal[False]" = False, 

717 defer_init: bool = False, 

718 ) -> CodeType: ... 

719 

720 @typing.overload 

721 def compile( 

722 self, 

723 source: t.Union[str, nodes.Template], 

724 name: t.Optional[str] = None, 

725 filename: t.Optional[str] = None, 

726 raw: "te.Literal[True]" = ..., 

727 defer_init: bool = False, 

728 ) -> str: ... 

729 

730 @internalcode 

731 def compile( 

732 self, 

733 source: t.Union[str, nodes.Template], 

734 name: t.Optional[str] = None, 

735 filename: t.Optional[str] = None, 

736 raw: bool = False, 

737 defer_init: bool = False, 

738 ) -> t.Union[str, CodeType]: 

739 """Compile a node or template source code. The `name` parameter is 

740 the load name of the template after it was joined using 

741 :meth:`join_path` if necessary, not the filename on the file system. 

742 the `filename` parameter is the estimated filename of the template on 

743 the file system. If the template came from a database or memory this 

744 can be omitted. 

745 

746 The return value of this method is a python code object. If the `raw` 

747 parameter is `True` the return value will be a string with python 

748 code equivalent to the bytecode returned otherwise. This method is 

749 mainly used internally. 

750 

751 `defer_init` is use internally to aid the module code generator. This 

752 causes the generated code to be able to import without the global 

753 environment variable to be set. 

754 

755 .. versionadded:: 2.4 

756 `defer_init` parameter added. 

757 """ 

758 source_hint = None 

759 try: 

760 if isinstance(source, str): 

761 source_hint = source 

762 source = self._parse(source, name, filename) 

763 source = self._generate(source, name, filename, defer_init=defer_init) 

764 if raw: 

765 return source 

766 if filename is None: 

767 filename = "<template>" 

768 return self._compile(source, filename) 

769 except TemplateSyntaxError: 

770 self.handle_exception(source=source_hint) 

771 

772 def compile_expression( 

773 self, source: str, undefined_to_none: bool = True 

774 ) -> "TemplateExpression": 

775 """A handy helper method that returns a callable that accepts keyword 

776 arguments that appear as variables in the expression. If called it 

777 returns the result of the expression. 

778 

779 This is useful if applications want to use the same rules as Jinja 

780 in template "configuration files" or similar situations. 

781 

782 Example usage: 

783 

784 >>> env = Environment() 

785 >>> expr = env.compile_expression('foo == 42') 

786 >>> expr(foo=23) 

787 False 

788 >>> expr(foo=42) 

789 True 

790 

791 Per default the return value is converted to `None` if the 

792 expression returns an undefined value. This can be changed 

793 by setting `undefined_to_none` to `False`. 

794 

795 >>> env.compile_expression('var')() is None 

796 True 

797 >>> env.compile_expression('var', undefined_to_none=False)() 

798 Undefined 

799 

800 .. versionadded:: 2.1 

801 """ 

802 parser = Parser(self, source, state="variable") 

803 try: 

804 expr = parser.parse_expression() 

805 if not parser.stream.eos: 

806 raise TemplateSyntaxError( 

807 "chunk after expression", parser.stream.current.lineno, None, None 

808 ) 

809 expr.set_environment(self) 

810 except TemplateSyntaxError: 

811 self.handle_exception(source=source) 

812 

813 body = [nodes.Assign(nodes.Name("result", "store"), expr, lineno=1)] 

814 template = self.from_string(nodes.Template(body, lineno=1)) 

815 return TemplateExpression(template, undefined_to_none) 

816 

817 def compile_templates( 

818 self, 

819 target: t.Union[str, "os.PathLike[str]"], 

820 extensions: t.Optional[t.Collection[str]] = None, 

821 filter_func: t.Optional[t.Callable[[str], bool]] = None, 

822 zip: t.Optional[str] = "deflated", 

823 log_function: t.Optional[t.Callable[[str], None]] = None, 

824 ignore_errors: bool = True, 

825 ) -> None: 

826 """Finds all the templates the loader can find, compiles them 

827 and stores them in `target`. If `zip` is `None`, instead of in a 

828 zipfile, the templates will be stored in a directory. 

829 By default a deflate zip algorithm is used. To switch to 

830 the stored algorithm, `zip` can be set to ``'stored'``. 

831 

832 `extensions` and `filter_func` are passed to :meth:`list_templates`. 

833 Each template returned will be compiled to the target folder or 

834 zipfile. 

835 

836 By default template compilation errors are ignored. In case a 

837 log function is provided, errors are logged. If you want template 

838 syntax errors to abort the compilation you can set `ignore_errors` 

839 to `False` and you will get an exception on syntax errors. 

840 

841 .. versionadded:: 2.4 

842 """ 

843 from .loaders import ModuleLoader 

844 

845 if log_function is None: 

846 

847 def log_function(x: str) -> None: 

848 pass 

849 

850 assert log_function is not None 

851 assert self.loader is not None, "No loader configured." 

852 

853 def write_file(filename: str, data: str) -> None: 

854 if zip: 

855 info = ZipInfo(filename) 

856 info.external_attr = 0o755 << 16 

857 zip_file.writestr(info, data) 

858 else: 

859 with open(os.path.join(target, filename), "wb") as f: 

860 f.write(data.encode("utf8")) 

861 

862 if zip is not None: 

863 from zipfile import ZIP_DEFLATED 

864 from zipfile import ZIP_STORED 

865 from zipfile import ZipFile 

866 from zipfile import ZipInfo 

867 

868 zip_file = ZipFile( 

869 target, "w", dict(deflated=ZIP_DEFLATED, stored=ZIP_STORED)[zip] 

870 ) 

871 log_function(f"Compiling into Zip archive {target!r}") 

872 else: 

873 if not os.path.isdir(target): 

874 os.makedirs(target) 

875 log_function(f"Compiling into folder {target!r}") 

876 

877 try: 

878 for name in self.list_templates(extensions, filter_func): 

879 source, filename, _ = self.loader.get_source(self, name) 

880 try: 

881 code = self.compile(source, name, filename, True, True) 

882 except TemplateSyntaxError as e: 

883 if not ignore_errors: 

884 raise 

885 log_function(f'Could not compile "{name}": {e}') 

886 continue 

887 

888 filename = ModuleLoader.get_module_filename(name) 

889 

890 write_file(filename, code) 

891 log_function(f'Compiled "{name}" as {filename}') 

892 finally: 

893 if zip: 

894 zip_file.close() 

895 

896 log_function("Finished compiling templates") 

897 

898 def list_templates( 

899 self, 

900 extensions: t.Optional[t.Collection[str]] = None, 

901 filter_func: t.Optional[t.Callable[[str], bool]] = None, 

902 ) -> t.List[str]: 

903 """Returns a list of templates for this environment. This requires 

904 that the loader supports the loader's 

905 :meth:`~BaseLoader.list_templates` method. 

906 

907 If there are other files in the template folder besides the 

908 actual templates, the returned list can be filtered. There are two 

909 ways: either `extensions` is set to a list of file extensions for 

910 templates, or a `filter_func` can be provided which is a callable that 

911 is passed a template name and should return `True` if it should end up 

912 in the result list. 

913 

914 If the loader does not support that, a :exc:`TypeError` is raised. 

915 

916 .. versionadded:: 2.4 

917 """ 

918 assert self.loader is not None, "No loader configured." 

919 names = self.loader.list_templates() 

920 

921 if extensions is not None: 

922 if filter_func is not None: 

923 raise TypeError( 

924 "either extensions or filter_func can be passed, but not both" 

925 ) 

926 

927 def filter_func(x: str) -> bool: 

928 return "." in x and x.rsplit(".", 1)[1] in extensions 

929 

930 if filter_func is not None: 

931 names = [name for name in names if filter_func(name)] 

932 

933 return names 

934 

935 def handle_exception(self, source: t.Optional[str] = None) -> "te.NoReturn": 

936 """Exception handling helper. This is used internally to either raise 

937 rewritten exceptions or return a rendered traceback for the template. 

938 """ 

939 from .debug import rewrite_traceback_stack 

940 

941 raise rewrite_traceback_stack(source=source) 

942 

943 def join_path(self, template: str, parent: str) -> str: 

944 """Join a template with the parent. By default all the lookups are 

945 relative to the loader root so this method returns the `template` 

946 parameter unchanged, but if the paths should be relative to the 

947 parent template, this function can be used to calculate the real 

948 template name. 

949 

950 Subclasses may override this method and implement template path 

951 joining here. 

952 """ 

953 return template 

954 

955 @internalcode 

956 def _load_template( 

957 self, name: str, globals: t.Optional[t.MutableMapping[str, t.Any]] 

958 ) -> "Template": 

959 if self.loader is None: 

960 raise TypeError("no loader for this environment specified") 

961 cache_key = (weakref.ref(self.loader), name) 

962 if self.cache is not None: 

963 template = self.cache.get(cache_key) 

964 if template is not None and ( 

965 not self.auto_reload or template.is_up_to_date 

966 ): 

967 # template.globals is a ChainMap, modifying it will only 

968 # affect the template, not the environment globals. 

969 if globals: 

970 template.globals.update(globals) 

971 

972 return template 

973 

974 template = self.loader.load(self, name, self.make_globals(globals)) 

975 

976 if self.cache is not None: 

977 self.cache[cache_key] = template 

978 return template 

979 

980 @internalcode 

981 def get_template( 

982 self, 

983 name: t.Union[str, "Template"], 

984 parent: t.Optional[str] = None, 

985 globals: t.Optional[t.MutableMapping[str, t.Any]] = None, 

986 ) -> "Template": 

987 """Load a template by name with :attr:`loader` and return a 

988 :class:`Template`. If the template does not exist a 

989 :exc:`TemplateNotFound` exception is raised. 

990 

991 :param name: Name of the template to load. When loading 

992 templates from the filesystem, "/" is used as the path 

993 separator, even on Windows. 

994 :param parent: The name of the parent template importing this 

995 template. :meth:`join_path` can be used to implement name 

996 transformations with this. 

997 :param globals: Extend the environment :attr:`globals` with 

998 these extra variables available for all renders of this 

999 template. If the template has already been loaded and 

1000 cached, its globals are updated with any new items. 

1001 

1002 .. versionchanged:: 3.0 

1003 If a template is loaded from cache, ``globals`` will update 

1004 the template's globals instead of ignoring the new values. 

1005 

1006 .. versionchanged:: 2.4 

1007 If ``name`` is a :class:`Template` object it is returned 

1008 unchanged. 

1009 """ 

1010 if isinstance(name, Template): 

1011 return name 

1012 if parent is not None: 

1013 name = self.join_path(name, parent) 

1014 

1015 return self._load_template(name, globals) 

1016 

1017 @internalcode 

1018 def select_template( 

1019 self, 

1020 names: t.Iterable[t.Union[str, "Template"]], 

1021 parent: t.Optional[str] = None, 

1022 globals: t.Optional[t.MutableMapping[str, t.Any]] = None, 

1023 ) -> "Template": 

1024 """Like :meth:`get_template`, but tries loading multiple names. 

1025 If none of the names can be loaded a :exc:`TemplatesNotFound` 

1026 exception is raised. 

1027 

1028 :param names: List of template names to try loading in order. 

1029 :param parent: The name of the parent template importing this 

1030 template. :meth:`join_path` can be used to implement name 

1031 transformations with this. 

1032 :param globals: Extend the environment :attr:`globals` with 

1033 these extra variables available for all renders of this 

1034 template. If the template has already been loaded and 

1035 cached, its globals are updated with any new items. 

1036 

1037 .. versionchanged:: 3.0 

1038 If a template is loaded from cache, ``globals`` will update 

1039 the template's globals instead of ignoring the new values. 

1040 

1041 .. versionchanged:: 2.11 

1042 If ``names`` is :class:`Undefined`, an :exc:`UndefinedError` 

1043 is raised instead. If no templates were found and ``names`` 

1044 contains :class:`Undefined`, the message is more helpful. 

1045 

1046 .. versionchanged:: 2.4 

1047 If ``names`` contains a :class:`Template` object it is 

1048 returned unchanged. 

1049 

1050 .. versionadded:: 2.3 

1051 """ 

1052 if isinstance(names, Undefined): 

1053 names._fail_with_undefined_error() 

1054 

1055 if not names: 

1056 raise TemplatesNotFound( 

1057 message="Tried to select from an empty list of templates." 

1058 ) 

1059 

1060 for name in names: 

1061 if isinstance(name, Template): 

1062 return name 

1063 if parent is not None: 

1064 name = self.join_path(name, parent) 

1065 try: 

1066 return self._load_template(name, globals) 

1067 except (TemplateNotFound, UndefinedError): 

1068 pass 

1069 raise TemplatesNotFound(names) # type: ignore 

1070 

1071 @internalcode 

1072 def get_or_select_template( 

1073 self, 

1074 template_name_or_list: t.Union[ 

1075 str, "Template", t.List[t.Union[str, "Template"]] 

1076 ], 

1077 parent: t.Optional[str] = None, 

1078 globals: t.Optional[t.MutableMapping[str, t.Any]] = None, 

1079 ) -> "Template": 

1080 """Use :meth:`select_template` if an iterable of template names 

1081 is given, or :meth:`get_template` if one name is given. 

1082 

1083 .. versionadded:: 2.3 

1084 """ 

1085 if isinstance(template_name_or_list, (str, Undefined)): 

1086 return self.get_template(template_name_or_list, parent, globals) 

1087 elif isinstance(template_name_or_list, Template): 

1088 return template_name_or_list 

1089 return self.select_template(template_name_or_list, parent, globals) 

1090 

1091 def from_string( 

1092 self, 

1093 source: t.Union[str, nodes.Template], 

1094 globals: t.Optional[t.MutableMapping[str, t.Any]] = None, 

1095 template_class: t.Optional[t.Type["Template"]] = None, 

1096 ) -> "Template": 

1097 """Load a template from a source string without using 

1098 :attr:`loader`. 

1099 

1100 :param source: Jinja source to compile into a template. 

1101 :param globals: Extend the environment :attr:`globals` with 

1102 these extra variables available for all renders of this 

1103 template. If the template has already been loaded and 

1104 cached, its globals are updated with any new items. 

1105 :param template_class: Return an instance of this 

1106 :class:`Template` class. 

1107 """ 

1108 gs = self.make_globals(globals) 

1109 cls = template_class or self.template_class 

1110 return cls.from_code(self, self.compile(source), gs, None) 

1111 

1112 def make_globals( 

1113 self, d: t.Optional[t.MutableMapping[str, t.Any]] 

1114 ) -> t.MutableMapping[str, t.Any]: 

1115 """Make the globals map for a template. Any given template 

1116 globals overlay the environment :attr:`globals`. 

1117 

1118 Returns a :class:`collections.ChainMap`. This allows any changes 

1119 to a template's globals to only affect that template, while 

1120 changes to the environment's globals are still reflected. 

1121 However, avoid modifying any globals after a template is loaded. 

1122 

1123 :param d: Dict of template-specific globals. 

1124 

1125 .. versionchanged:: 3.0 

1126 Use :class:`collections.ChainMap` to always prevent mutating 

1127 environment globals. 

1128 """ 

1129 if d is None: 

1130 d = {} 

1131 

1132 return ChainMap(d, self.globals) 

1133 

1134 

1135class Template: 

1136 """A compiled template that can be rendered. 

1137 

1138 Use the methods on :class:`Environment` to create or load templates. 

1139 The environment is used to configure how templates are compiled and 

1140 behave. 

1141 

1142 It is also possible to create a template object directly. This is 

1143 not usually recommended. The constructor takes most of the same 

1144 arguments as :class:`Environment`. All templates created with the 

1145 same environment arguments share the same ephemeral ``Environment`` 

1146 instance behind the scenes. 

1147 

1148 A template object should be considered immutable. Modifications on 

1149 the object are not supported. 

1150 """ 

1151 

1152 #: Type of environment to create when creating a template directly 

1153 #: rather than through an existing environment. 

1154 environment_class: t.Type[Environment] = Environment 

1155 

1156 environment: Environment 

1157 globals: t.MutableMapping[str, t.Any] 

1158 name: t.Optional[str] 

1159 filename: t.Optional[str] 

1160 blocks: t.Dict[str, t.Callable[[Context], t.Iterator[str]]] 

1161 root_render_func: t.Callable[[Context], t.Iterator[str]] 

1162 _module: t.Optional["TemplateModule"] 

1163 _debug_info: str 

1164 _uptodate: t.Optional[t.Callable[[], bool]] 

1165 

1166 def __new__( 

1167 cls, 

1168 source: t.Union[str, nodes.Template], 

1169 block_start_string: str = BLOCK_START_STRING, 

1170 block_end_string: str = BLOCK_END_STRING, 

1171 variable_start_string: str = VARIABLE_START_STRING, 

1172 variable_end_string: str = VARIABLE_END_STRING, 

1173 comment_start_string: str = COMMENT_START_STRING, 

1174 comment_end_string: str = COMMENT_END_STRING, 

1175 line_statement_prefix: t.Optional[str] = LINE_STATEMENT_PREFIX, 

1176 line_comment_prefix: t.Optional[str] = LINE_COMMENT_PREFIX, 

1177 trim_blocks: bool = TRIM_BLOCKS, 

1178 lstrip_blocks: bool = LSTRIP_BLOCKS, 

1179 newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = NEWLINE_SEQUENCE, 

1180 keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE, 

1181 extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = (), 

1182 optimized: bool = True, 

1183 undefined: t.Type[Undefined] = Undefined, 

1184 finalize: t.Optional[t.Callable[..., t.Any]] = None, 

1185 autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = False, 

1186 enable_async: bool = False, 

1187 ) -> t.Any: # it returns a `Template`, but this breaks the sphinx build... 

1188 env = get_spontaneous_environment( 

1189 cls.environment_class, # type: ignore 

1190 block_start_string, 

1191 block_end_string, 

1192 variable_start_string, 

1193 variable_end_string, 

1194 comment_start_string, 

1195 comment_end_string, 

1196 line_statement_prefix, 

1197 line_comment_prefix, 

1198 trim_blocks, 

1199 lstrip_blocks, 

1200 newline_sequence, 

1201 keep_trailing_newline, 

1202 frozenset(extensions), 

1203 optimized, 

1204 undefined, # type: ignore 

1205 finalize, 

1206 autoescape, 

1207 None, 

1208 0, 

1209 False, 

1210 None, 

1211 enable_async, 

1212 ) 

1213 return env.from_string(source, template_class=cls) 

1214 

1215 @classmethod 

1216 def from_code( 

1217 cls, 

1218 environment: Environment, 

1219 code: CodeType, 

1220 globals: t.MutableMapping[str, t.Any], 

1221 uptodate: t.Optional[t.Callable[[], bool]] = None, 

1222 ) -> "Template": 

1223 """Creates a template object from compiled code and the globals. This 

1224 is used by the loaders and environment to create a template object. 

1225 """ 

1226 namespace = {"environment": environment, "__file__": code.co_filename} 

1227 exec(code, namespace) 

1228 rv = cls._from_namespace(environment, namespace, globals) 

1229 rv._uptodate = uptodate 

1230 return rv 

1231 

1232 @classmethod 

1233 def from_module_dict( 

1234 cls, 

1235 environment: Environment, 

1236 module_dict: t.MutableMapping[str, t.Any], 

1237 globals: t.MutableMapping[str, t.Any], 

1238 ) -> "Template": 

1239 """Creates a template object from a module. This is used by the 

1240 module loader to create a template object. 

1241 

1242 .. versionadded:: 2.4 

1243 """ 

1244 return cls._from_namespace(environment, module_dict, globals) 

1245 

1246 @classmethod 

1247 def _from_namespace( 

1248 cls, 

1249 environment: Environment, 

1250 namespace: t.MutableMapping[str, t.Any], 

1251 globals: t.MutableMapping[str, t.Any], 

1252 ) -> "Template": 

1253 t: Template = object.__new__(cls) 

1254 t.environment = environment 

1255 t.globals = globals 

1256 t.name = namespace["name"] 

1257 t.filename = namespace["__file__"] 

1258 t.blocks = namespace["blocks"] 

1259 

1260 # render function and module 

1261 t.root_render_func = namespace["root"] 

1262 t._module = None 

1263 

1264 # debug and loader helpers 

1265 t._debug_info = namespace["debug_info"] 

1266 t._uptodate = None 

1267 

1268 # store the reference 

1269 namespace["environment"] = environment 

1270 namespace["__jinja_template__"] = t 

1271 

1272 return t 

1273 

1274 def render(self, *args: t.Any, **kwargs: t.Any) -> str: 

1275 """This method accepts the same arguments as the `dict` constructor: 

1276 A dict, a dict subclass or some keyword arguments. If no arguments 

1277 are given the context will be empty. These two calls do the same:: 

1278 

1279 template.render(knights='that say nih') 

1280 template.render({'knights': 'that say nih'}) 

1281 

1282 This will return the rendered template as a string. 

1283 """ 

1284 if self.environment.is_async: 

1285 import asyncio 

1286 

1287 return asyncio.run(self.render_async(*args, **kwargs)) 

1288 

1289 ctx = self.new_context(dict(*args, **kwargs)) 

1290 

1291 try: 

1292 return self.environment.concat(self.root_render_func(ctx)) # type: ignore 

1293 except Exception: 

1294 self.environment.handle_exception() 

1295 

1296 async def render_async(self, *args: t.Any, **kwargs: t.Any) -> str: 

1297 """This works similar to :meth:`render` but returns a coroutine 

1298 that when awaited returns the entire rendered template string. This 

1299 requires the async feature to be enabled. 

1300 

1301 Example usage:: 

1302 

1303 await template.render_async(knights='that say nih; asynchronously') 

1304 """ 

1305 if not self.environment.is_async: 

1306 raise RuntimeError( 

1307 "The environment was not created with async mode enabled." 

1308 ) 

1309 

1310 ctx = self.new_context(dict(*args, **kwargs)) 

1311 

1312 try: 

1313 return self.environment.concat( # type: ignore 

1314 [n async for n in self.root_render_func(ctx)] # type: ignore 

1315 ) 

1316 except Exception: 

1317 return self.environment.handle_exception() 

1318 

1319 def stream(self, *args: t.Any, **kwargs: t.Any) -> "TemplateStream": 

1320 """Works exactly like :meth:`generate` but returns a 

1321 :class:`TemplateStream`. 

1322 """ 

1323 return TemplateStream(self.generate(*args, **kwargs)) 

1324 

1325 def generate(self, *args: t.Any, **kwargs: t.Any) -> t.Iterator[str]: 

1326 """For very large templates it can be useful to not render the whole 

1327 template at once but evaluate each statement after another and yield 

1328 piece for piece. This method basically does exactly that and returns 

1329 a generator that yields one item after another as strings. 

1330 

1331 It accepts the same arguments as :meth:`render`. 

1332 """ 

1333 if self.environment.is_async: 

1334 import asyncio 

1335 

1336 async def to_list() -> t.List[str]: 

1337 return [x async for x in self.generate_async(*args, **kwargs)] 

1338 

1339 yield from asyncio.run(to_list()) 

1340 return 

1341 

1342 ctx = self.new_context(dict(*args, **kwargs)) 

1343 

1344 try: 

1345 yield from self.root_render_func(ctx) 

1346 except Exception: 

1347 yield self.environment.handle_exception() 

1348 

1349 async def generate_async( 

1350 self, *args: t.Any, **kwargs: t.Any 

1351 ) -> t.AsyncGenerator[str, object]: 

1352 """An async version of :meth:`generate`. Works very similarly but 

1353 returns an async iterator instead. 

1354 """ 

1355 if not self.environment.is_async: 

1356 raise RuntimeError( 

1357 "The environment was not created with async mode enabled." 

1358 ) 

1359 

1360 ctx = self.new_context(dict(*args, **kwargs)) 

1361 

1362 try: 

1363 agen = self.root_render_func(ctx) 

1364 try: 

1365 async for event in agen: # type: ignore 

1366 yield event 

1367 finally: 

1368 # we can't use async with aclosing(...) because that's only 

1369 # in 3.10+ 

1370 await agen.aclose() # type: ignore 

1371 except Exception: 

1372 yield self.environment.handle_exception() 

1373 

1374 def new_context( 

1375 self, 

1376 vars: t.Optional[t.Dict[str, t.Any]] = None, 

1377 shared: bool = False, 

1378 locals: t.Optional[t.Mapping[str, t.Any]] = None, 

1379 ) -> Context: 

1380 """Create a new :class:`Context` for this template. The vars 

1381 provided will be passed to the template. Per default the globals 

1382 are added to the context. If shared is set to `True` the data 

1383 is passed as is to the context without adding the globals. 

1384 

1385 `locals` can be a dict of local variables for internal usage. 

1386 """ 

1387 return new_context( 

1388 self.environment, self.name, self.blocks, vars, shared, self.globals, locals 

1389 ) 

1390 

1391 def make_module( 

1392 self, 

1393 vars: t.Optional[t.Dict[str, t.Any]] = None, 

1394 shared: bool = False, 

1395 locals: t.Optional[t.Mapping[str, t.Any]] = None, 

1396 ) -> "TemplateModule": 

1397 """This method works like the :attr:`module` attribute when called 

1398 without arguments but it will evaluate the template on every call 

1399 rather than caching it. It's also possible to provide 

1400 a dict which is then used as context. The arguments are the same 

1401 as for the :meth:`new_context` method. 

1402 """ 

1403 ctx = self.new_context(vars, shared, locals) 

1404 return TemplateModule(self, ctx) 

1405 

1406 async def make_module_async( 

1407 self, 

1408 vars: t.Optional[t.Dict[str, t.Any]] = None, 

1409 shared: bool = False, 

1410 locals: t.Optional[t.Mapping[str, t.Any]] = None, 

1411 ) -> "TemplateModule": 

1412 """As template module creation can invoke template code for 

1413 asynchronous executions this method must be used instead of the 

1414 normal :meth:`make_module` one. Likewise the module attribute 

1415 becomes unavailable in async mode. 

1416 """ 

1417 ctx = self.new_context(vars, shared, locals) 

1418 return TemplateModule( 

1419 self, 

1420 ctx, 

1421 [x async for x in self.root_render_func(ctx)], # type: ignore 

1422 ) 

1423 

1424 @internalcode 

1425 def _get_default_module(self, ctx: t.Optional[Context] = None) -> "TemplateModule": 

1426 """If a context is passed in, this means that the template was 

1427 imported. Imported templates have access to the current 

1428 template's globals by default, but they can only be accessed via 

1429 the context during runtime. 

1430 

1431 If there are new globals, we need to create a new module because 

1432 the cached module is already rendered and will not have access 

1433 to globals from the current context. This new module is not 

1434 cached because the template can be imported elsewhere, and it 

1435 should have access to only the current template's globals. 

1436 """ 

1437 if self.environment.is_async: 

1438 raise RuntimeError("Module is not available in async mode.") 

1439 

1440 if ctx is not None: 

1441 keys = ctx.globals_keys - self.globals.keys() 

1442 

1443 if keys: 

1444 return self.make_module({k: ctx.parent[k] for k in keys}) 

1445 

1446 if self._module is None: 

1447 self._module = self.make_module() 

1448 

1449 return self._module 

1450 

1451 async def _get_default_module_async( 

1452 self, ctx: t.Optional[Context] = None 

1453 ) -> "TemplateModule": 

1454 if ctx is not None: 

1455 keys = ctx.globals_keys - self.globals.keys() 

1456 

1457 if keys: 

1458 return await self.make_module_async({k: ctx.parent[k] for k in keys}) 

1459 

1460 if self._module is None: 

1461 self._module = await self.make_module_async() 

1462 

1463 return self._module 

1464 

1465 @property 

1466 def module(self) -> "TemplateModule": 

1467 """The template as module. This is used for imports in the 

1468 template runtime but is also useful if one wants to access 

1469 exported template variables from the Python layer: 

1470 

1471 >>> t = Template('{% macro foo() %}42{% endmacro %}23') 

1472 >>> str(t.module) 

1473 '23' 

1474 >>> t.module.foo() == u'42' 

1475 True 

1476 

1477 This attribute is not available if async mode is enabled. 

1478 """ 

1479 return self._get_default_module() 

1480 

1481 def get_corresponding_lineno(self, lineno: int) -> int: 

1482 """Return the source line number of a line number in the 

1483 generated bytecode as they are not in sync. 

1484 """ 

1485 for template_line, code_line in reversed(self.debug_info): 

1486 if code_line <= lineno: 

1487 return template_line 

1488 return 1 

1489 

1490 @property 

1491 def is_up_to_date(self) -> bool: 

1492 """If this variable is `False` there is a newer version available.""" 

1493 if self._uptodate is None: 

1494 return True 

1495 return self._uptodate() 

1496 

1497 @property 

1498 def debug_info(self) -> t.List[t.Tuple[int, int]]: 

1499 """The debug info mapping.""" 

1500 if self._debug_info: 

1501 return [ 

1502 tuple(map(int, x.split("="))) # type: ignore 

1503 for x in self._debug_info.split("&") 

1504 ] 

1505 

1506 return [] 

1507 

1508 def __repr__(self) -> str: 

1509 if self.name is None: 

1510 name = f"memory:{id(self):x}" 

1511 else: 

1512 name = repr(self.name) 

1513 return f"<{type(self).__name__} {name}>" 

1514 

1515 

1516class TemplateModule: 

1517 """Represents an imported template. All the exported names of the 

1518 template are available as attributes on this object. Additionally 

1519 converting it into a string renders the contents. 

1520 """ 

1521 

1522 def __init__( 

1523 self, 

1524 template: Template, 

1525 context: Context, 

1526 body_stream: t.Optional[t.Iterable[str]] = None, 

1527 ) -> None: 

1528 if body_stream is None: 

1529 if context.environment.is_async: 

1530 raise RuntimeError( 

1531 "Async mode requires a body stream to be passed to" 

1532 " a template module. Use the async methods of the" 

1533 " API you are using." 

1534 ) 

1535 

1536 body_stream = list(template.root_render_func(context)) 

1537 

1538 self._body_stream = body_stream 

1539 self.__dict__.update(context.get_exported()) 

1540 self.__name__ = template.name 

1541 

1542 def __html__(self) -> Markup: 

1543 return Markup(concat(self._body_stream)) 

1544 

1545 def __str__(self) -> str: 

1546 return concat(self._body_stream) 

1547 

1548 def __repr__(self) -> str: 

1549 if self.__name__ is None: 

1550 name = f"memory:{id(self):x}" 

1551 else: 

1552 name = repr(self.__name__) 

1553 return f"<{type(self).__name__} {name}>" 

1554 

1555 

1556class TemplateExpression: 

1557 """The :meth:`jinja2.Environment.compile_expression` method returns an 

1558 instance of this object. It encapsulates the expression-like access 

1559 to the template with an expression it wraps. 

1560 """ 

1561 

1562 def __init__(self, template: Template, undefined_to_none: bool) -> None: 

1563 self._template = template 

1564 self._undefined_to_none = undefined_to_none 

1565 

1566 def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Optional[t.Any]: 

1567 context = self._template.new_context(dict(*args, **kwargs)) 

1568 consume(self._template.root_render_func(context)) 

1569 rv = context.vars["result"] 

1570 if self._undefined_to_none and isinstance(rv, Undefined): 

1571 rv = None 

1572 return rv 

1573 

1574 

1575class TemplateStream: 

1576 """A template stream works pretty much like an ordinary python generator 

1577 but it can buffer multiple items to reduce the number of total iterations. 

1578 Per default the output is unbuffered which means that for every unbuffered 

1579 instruction in the template one string is yielded. 

1580 

1581 If buffering is enabled with a buffer size of 5, five items are combined 

1582 into a new string. This is mainly useful if you are streaming 

1583 big templates to a client via WSGI which flushes after each iteration. 

1584 """ 

1585 

1586 def __init__(self, gen: t.Iterator[str]) -> None: 

1587 self._gen = gen 

1588 self.disable_buffering() 

1589 

1590 def dump( 

1591 self, 

1592 fp: t.Union[str, t.IO[bytes]], 

1593 encoding: t.Optional[str] = None, 

1594 errors: t.Optional[str] = "strict", 

1595 ) -> None: 

1596 """Dump the complete stream into a file or file-like object. 

1597 Per default strings are written, if you want to encode 

1598 before writing specify an `encoding`. 

1599 

1600 Example usage:: 

1601 

1602 Template('Hello {{ name }}!').stream(name='foo').dump('hello.html') 

1603 """ 

1604 close = False 

1605 

1606 if isinstance(fp, str): 

1607 if encoding is None: 

1608 encoding = "utf-8" 

1609 

1610 real_fp: t.IO[bytes] = open(fp, "wb") 

1611 close = True 

1612 else: 

1613 real_fp = fp 

1614 

1615 try: 

1616 if encoding is not None: 

1617 iterable = (x.encode(encoding, errors) for x in self) # type: ignore 

1618 else: 

1619 iterable = self # type: ignore 

1620 

1621 if hasattr(real_fp, "writelines"): 

1622 real_fp.writelines(iterable) 

1623 else: 

1624 for item in iterable: 

1625 real_fp.write(item) 

1626 finally: 

1627 if close: 

1628 real_fp.close() 

1629 

1630 def disable_buffering(self) -> None: 

1631 """Disable the output buffering.""" 

1632 self._next = partial(next, self._gen) 

1633 self.buffered = False 

1634 

1635 def _buffered_generator(self, size: int) -> t.Iterator[str]: 

1636 buf: t.List[str] = [] 

1637 c_size = 0 

1638 push = buf.append 

1639 

1640 while True: 

1641 try: 

1642 while c_size < size: 

1643 c = next(self._gen) 

1644 push(c) 

1645 if c: 

1646 c_size += 1 

1647 except StopIteration: 

1648 if not c_size: 

1649 return 

1650 yield concat(buf) 

1651 del buf[:] 

1652 c_size = 0 

1653 

1654 def enable_buffering(self, size: int = 5) -> None: 

1655 """Enable buffering. Buffer `size` items before yielding them.""" 

1656 if size <= 1: 

1657 raise ValueError("buffer size too small") 

1658 

1659 self.buffered = True 

1660 self._next = partial(next, self._buffered_generator(size)) 

1661 

1662 def __iter__(self) -> "TemplateStream": 

1663 return self 

1664 

1665 def __next__(self) -> str: 

1666 return self._next() # type: ignore 

1667 

1668 

1669# hook in default template class. if anyone reads this comment: ignore that 

1670# it's possible to use custom templates ;-) 

1671Environment.template_class = Template