Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/flask/helpers.py: 31%

182 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:03 +0000

1import os 

2import pkgutil 

3import socket 

4import sys 

5import typing as t 

6import warnings 

7from datetime import datetime 

8from functools import lru_cache 

9from functools import update_wrapper 

10from threading import RLock 

11 

12import werkzeug.utils 

13from werkzeug.routing import BuildError 

14from werkzeug.urls import url_quote 

15 

16from .globals import _app_ctx_stack 

17from .globals import _request_ctx_stack 

18from .globals import current_app 

19from .globals import request 

20from .globals import session 

21from .signals import message_flashed 

22 

23if t.TYPE_CHECKING: 

24 from .wrappers import Response 

25 

26 

27def get_env() -> str: 

28 """Get the environment the app is running in, indicated by the 

29 :envvar:`FLASK_ENV` environment variable. The default is 

30 ``'production'``. 

31 """ 

32 return os.environ.get("FLASK_ENV") or "production" 

33 

34 

35def get_debug_flag() -> bool: 

36 """Get whether debug mode should be enabled for the app, indicated 

37 by the :envvar:`FLASK_DEBUG` environment variable. The default is 

38 ``True`` if :func:`.get_env` returns ``'development'``, or ``False`` 

39 otherwise. 

40 """ 

41 val = os.environ.get("FLASK_DEBUG") 

42 

43 if not val: 

44 return get_env() == "development" 

45 

46 return val.lower() not in ("0", "false", "no") 

47 

48 

49def get_load_dotenv(default: bool = True) -> bool: 

50 """Get whether the user has disabled loading dotenv files by setting 

51 :envvar:`FLASK_SKIP_DOTENV`. The default is ``True``, load the 

52 files. 

53 

54 :param default: What to return if the env var isn't set. 

55 """ 

56 val = os.environ.get("FLASK_SKIP_DOTENV") 

57 

58 if not val: 

59 return default 

60 

61 return val.lower() in ("0", "false", "no") 

62 

63 

64def stream_with_context( 

65 generator_or_function: t.Union[ 

66 t.Iterator[t.AnyStr], t.Callable[..., t.Iterator[t.AnyStr]] 

67 ] 

68) -> t.Iterator[t.AnyStr]: 

69 """Request contexts disappear when the response is started on the server. 

70 This is done for efficiency reasons and to make it less likely to encounter 

71 memory leaks with badly written WSGI middlewares. The downside is that if 

72 you are using streamed responses, the generator cannot access request bound 

73 information any more. 

74 

75 This function however can help you keep the context around for longer:: 

76 

77 from flask import stream_with_context, request, Response 

78 

79 @app.route('/stream') 

80 def streamed_response(): 

81 @stream_with_context 

82 def generate(): 

83 yield 'Hello ' 

84 yield request.args['name'] 

85 yield '!' 

86 return Response(generate()) 

87 

88 Alternatively it can also be used around a specific generator:: 

89 

90 from flask import stream_with_context, request, Response 

91 

92 @app.route('/stream') 

93 def streamed_response(): 

94 def generate(): 

95 yield 'Hello ' 

96 yield request.args['name'] 

97 yield '!' 

98 return Response(stream_with_context(generate())) 

99 

100 .. versionadded:: 0.9 

101 """ 

102 try: 

103 gen = iter(generator_or_function) # type: ignore 

104 except TypeError: 

105 

106 def decorator(*args: t.Any, **kwargs: t.Any) -> t.Any: 

107 gen = generator_or_function(*args, **kwargs) # type: ignore 

108 return stream_with_context(gen) 

109 

110 return update_wrapper(decorator, generator_or_function) # type: ignore 

111 

112 def generator() -> t.Generator: 

113 ctx = _request_ctx_stack.top 

114 if ctx is None: 

115 raise RuntimeError( 

116 "Attempted to stream with context but " 

117 "there was no context in the first place to keep around." 

118 ) 

119 with ctx: 

120 # Dummy sentinel. Has to be inside the context block or we're 

121 # not actually keeping the context around. 

122 yield None 

123 

124 # The try/finally is here so that if someone passes a WSGI level 

125 # iterator in we're still running the cleanup logic. Generators 

126 # don't need that because they are closed on their destruction 

127 # automatically. 

128 try: 

129 yield from gen 

130 finally: 

131 if hasattr(gen, "close"): 

132 gen.close() # type: ignore 

133 

134 # The trick is to start the generator. Then the code execution runs until 

135 # the first dummy None is yielded at which point the context was already 

136 # pushed. This item is discarded. Then when the iteration continues the 

137 # real generator is executed. 

138 wrapped_g = generator() 

139 next(wrapped_g) 

140 return wrapped_g 

141 

142 

143def make_response(*args: t.Any) -> "Response": 

144 """Sometimes it is necessary to set additional headers in a view. Because 

145 views do not have to return response objects but can return a value that 

146 is converted into a response object by Flask itself, it becomes tricky to 

147 add headers to it. This function can be called instead of using a return 

148 and you will get a response object which you can use to attach headers. 

149 

150 If view looked like this and you want to add a new header:: 

151 

152 def index(): 

153 return render_template('index.html', foo=42) 

154 

155 You can now do something like this:: 

156 

157 def index(): 

158 response = make_response(render_template('index.html', foo=42)) 

159 response.headers['X-Parachutes'] = 'parachutes are cool' 

160 return response 

161 

162 This function accepts the very same arguments you can return from a 

163 view function. This for example creates a response with a 404 error 

164 code:: 

165 

166 response = make_response(render_template('not_found.html'), 404) 

167 

168 The other use case of this function is to force the return value of a 

169 view function into a response which is helpful with view 

170 decorators:: 

171 

172 response = make_response(view_function()) 

173 response.headers['X-Parachutes'] = 'parachutes are cool' 

174 

175 Internally this function does the following things: 

176 

177 - if no arguments are passed, it creates a new response argument 

178 - if one argument is passed, :meth:`flask.Flask.make_response` 

179 is invoked with it. 

180 - if more than one argument is passed, the arguments are passed 

181 to the :meth:`flask.Flask.make_response` function as tuple. 

182 

183 .. versionadded:: 0.6 

184 """ 

185 if not args: 

186 return current_app.response_class() 

187 if len(args) == 1: 

188 args = args[0] 

189 return current_app.make_response(args) # type: ignore 

190 

191 

192def url_for(endpoint: str, **values: t.Any) -> str: 

193 """Generates a URL to the given endpoint with the method provided. 

194 

195 Variable arguments that are unknown to the target endpoint are appended 

196 to the generated URL as query arguments. If the value of a query argument 

197 is ``None``, the whole pair is skipped. In case blueprints are active 

198 you can shortcut references to the same blueprint by prefixing the 

199 local endpoint with a dot (``.``). 

200 

201 This will reference the index function local to the current blueprint:: 

202 

203 url_for('.index') 

204 

205 See :ref:`url-building`. 

206 

207 Configuration values ``APPLICATION_ROOT`` and ``SERVER_NAME`` are only used when 

208 generating URLs outside of a request context. 

209 

210 To integrate applications, :class:`Flask` has a hook to intercept URL build 

211 errors through :attr:`Flask.url_build_error_handlers`. The `url_for` 

212 function results in a :exc:`~werkzeug.routing.BuildError` when the current 

213 app does not have a URL for the given endpoint and values. When it does, the 

214 :data:`~flask.current_app` calls its :attr:`~Flask.url_build_error_handlers` if 

215 it is not ``None``, which can return a string to use as the result of 

216 `url_for` (instead of `url_for`'s default to raise the 

217 :exc:`~werkzeug.routing.BuildError` exception) or re-raise the exception. 

218 An example:: 

219 

220 def external_url_handler(error, endpoint, values): 

221 "Looks up an external URL when `url_for` cannot build a URL." 

222 # This is an example of hooking the build_error_handler. 

223 # Here, lookup_url is some utility function you've built 

224 # which looks up the endpoint in some external URL registry. 

225 url = lookup_url(endpoint, **values) 

226 if url is None: 

227 # External lookup did not have a URL. 

228 # Re-raise the BuildError, in context of original traceback. 

229 exc_type, exc_value, tb = sys.exc_info() 

230 if exc_value is error: 

231 raise exc_type(exc_value).with_traceback(tb) 

232 else: 

233 raise error 

234 # url_for will use this result, instead of raising BuildError. 

235 return url 

236 

237 app.url_build_error_handlers.append(external_url_handler) 

238 

239 Here, `error` is the instance of :exc:`~werkzeug.routing.BuildError`, and 

240 `endpoint` and `values` are the arguments passed into `url_for`. Note 

241 that this is for building URLs outside the current application, and not for 

242 handling 404 NotFound errors. 

243 

244 .. versionadded:: 0.10 

245 The `_scheme` parameter was added. 

246 

247 .. versionadded:: 0.9 

248 The `_anchor` and `_method` parameters were added. 

249 

250 .. versionadded:: 0.9 

251 Calls :meth:`Flask.handle_build_error` on 

252 :exc:`~werkzeug.routing.BuildError`. 

253 

254 :param endpoint: the endpoint of the URL (name of the function) 

255 :param values: the variable arguments of the URL rule 

256 :param _external: if set to ``True``, an absolute URL is generated. Server 

257 address can be changed via ``SERVER_NAME`` configuration variable which 

258 falls back to the `Host` header, then to the IP and port of the request. 

259 :param _scheme: a string specifying the desired URL scheme. The `_external` 

260 parameter must be set to ``True`` or a :exc:`ValueError` is raised. The default 

261 behavior uses the same scheme as the current request, or 

262 :data:`PREFERRED_URL_SCHEME` if no request context is available. 

263 This also can be set to an empty string to build protocol-relative 

264 URLs. 

265 :param _anchor: if provided this is added as anchor to the URL. 

266 :param _method: if provided this explicitly specifies an HTTP method. 

267 """ 

268 appctx = _app_ctx_stack.top 

269 reqctx = _request_ctx_stack.top 

270 

271 if appctx is None: 

272 raise RuntimeError( 

273 "Attempted to generate a URL without the application context being" 

274 " pushed. This has to be executed when application context is" 

275 " available." 

276 ) 

277 

278 # If request specific information is available we have some extra 

279 # features that support "relative" URLs. 

280 if reqctx is not None: 

281 url_adapter = reqctx.url_adapter 

282 blueprint_name = request.blueprint 

283 

284 if endpoint[:1] == ".": 

285 if blueprint_name is not None: 

286 endpoint = f"{blueprint_name}{endpoint}" 

287 else: 

288 endpoint = endpoint[1:] 

289 

290 external = values.pop("_external", False) 

291 

292 # Otherwise go with the url adapter from the appctx and make 

293 # the URLs external by default. 

294 else: 

295 url_adapter = appctx.url_adapter 

296 

297 if url_adapter is None: 

298 raise RuntimeError( 

299 "Application was not able to create a URL adapter for request" 

300 " independent URL generation. You might be able to fix this by" 

301 " setting the SERVER_NAME config variable." 

302 ) 

303 

304 external = values.pop("_external", True) 

305 

306 anchor = values.pop("_anchor", None) 

307 method = values.pop("_method", None) 

308 scheme = values.pop("_scheme", None) 

309 appctx.app.inject_url_defaults(endpoint, values) 

310 

311 # This is not the best way to deal with this but currently the 

312 # underlying Werkzeug router does not support overriding the scheme on 

313 # a per build call basis. 

314 old_scheme = None 

315 if scheme is not None: 

316 if not external: 

317 raise ValueError("When specifying _scheme, _external must be True") 

318 old_scheme = url_adapter.url_scheme 

319 url_adapter.url_scheme = scheme 

320 

321 try: 

322 try: 

323 rv = url_adapter.build( 

324 endpoint, values, method=method, force_external=external 

325 ) 

326 finally: 

327 if old_scheme is not None: 

328 url_adapter.url_scheme = old_scheme 

329 except BuildError as error: 

330 # We need to inject the values again so that the app callback can 

331 # deal with that sort of stuff. 

332 values["_external"] = external 

333 values["_anchor"] = anchor 

334 values["_method"] = method 

335 values["_scheme"] = scheme 

336 return appctx.app.handle_url_build_error(error, endpoint, values) 

337 

338 if anchor is not None: 

339 rv += f"#{url_quote(anchor)}" 

340 return rv 

341 

342 

343def get_template_attribute(template_name: str, attribute: str) -> t.Any: 

344 """Loads a macro (or variable) a template exports. This can be used to 

345 invoke a macro from within Python code. If you for example have a 

346 template named :file:`_cider.html` with the following contents: 

347 

348 .. sourcecode:: html+jinja 

349 

350 {% macro hello(name) %}Hello {{ name }}!{% endmacro %} 

351 

352 You can access this from Python code like this:: 

353 

354 hello = get_template_attribute('_cider.html', 'hello') 

355 return hello('World') 

356 

357 .. versionadded:: 0.2 

358 

359 :param template_name: the name of the template 

360 :param attribute: the name of the variable of macro to access 

361 """ 

362 return getattr(current_app.jinja_env.get_template(template_name).module, attribute) 

363 

364 

365def flash(message: str, category: str = "message") -> None: 

366 """Flashes a message to the next request. In order to remove the 

367 flashed message from the session and to display it to the user, 

368 the template has to call :func:`get_flashed_messages`. 

369 

370 .. versionchanged:: 0.3 

371 `category` parameter added. 

372 

373 :param message: the message to be flashed. 

374 :param category: the category for the message. The following values 

375 are recommended: ``'message'`` for any kind of message, 

376 ``'error'`` for errors, ``'info'`` for information 

377 messages and ``'warning'`` for warnings. However any 

378 kind of string can be used as category. 

379 """ 

380 # Original implementation: 

381 # 

382 # session.setdefault('_flashes', []).append((category, message)) 

383 # 

384 # This assumed that changes made to mutable structures in the session are 

385 # always in sync with the session object, which is not true for session 

386 # implementations that use external storage for keeping their keys/values. 

387 flashes = session.get("_flashes", []) 

388 flashes.append((category, message)) 

389 session["_flashes"] = flashes 

390 message_flashed.send( 

391 current_app._get_current_object(), # type: ignore 

392 message=message, 

393 category=category, 

394 ) 

395 

396 

397def get_flashed_messages( 

398 with_categories: bool = False, category_filter: t.Iterable[str] = () 

399) -> t.Union[t.List[str], t.List[t.Tuple[str, str]]]: 

400 """Pulls all flashed messages from the session and returns them. 

401 Further calls in the same request to the function will return 

402 the same messages. By default just the messages are returned, 

403 but when `with_categories` is set to ``True``, the return value will 

404 be a list of tuples in the form ``(category, message)`` instead. 

405 

406 Filter the flashed messages to one or more categories by providing those 

407 categories in `category_filter`. This allows rendering categories in 

408 separate html blocks. The `with_categories` and `category_filter` 

409 arguments are distinct: 

410 

411 * `with_categories` controls whether categories are returned with message 

412 text (``True`` gives a tuple, where ``False`` gives just the message text). 

413 * `category_filter` filters the messages down to only those matching the 

414 provided categories. 

415 

416 See :doc:`/patterns/flashing` for examples. 

417 

418 .. versionchanged:: 0.3 

419 `with_categories` parameter added. 

420 

421 .. versionchanged:: 0.9 

422 `category_filter` parameter added. 

423 

424 :param with_categories: set to ``True`` to also receive categories. 

425 :param category_filter: filter of categories to limit return values. Only 

426 categories in the list will be returned. 

427 """ 

428 flashes = _request_ctx_stack.top.flashes 

429 if flashes is None: 

430 _request_ctx_stack.top.flashes = flashes = ( 

431 session.pop("_flashes") if "_flashes" in session else [] 

432 ) 

433 if category_filter: 

434 flashes = list(filter(lambda f: f[0] in category_filter, flashes)) 

435 if not with_categories: 

436 return [x[1] for x in flashes] 

437 return flashes 

438 

439 

440def _prepare_send_file_kwargs( 

441 download_name: t.Optional[str] = None, 

442 attachment_filename: t.Optional[str] = None, 

443 etag: t.Optional[t.Union[bool, str]] = None, 

444 add_etags: t.Optional[t.Union[bool]] = None, 

445 max_age: t.Optional[ 

446 t.Union[int, t.Callable[[t.Optional[str]], t.Optional[int]]] 

447 ] = None, 

448 cache_timeout: t.Optional[int] = None, 

449 **kwargs: t.Any, 

450) -> t.Dict[str, t.Any]: 

451 if attachment_filename is not None: 

452 warnings.warn( 

453 "The 'attachment_filename' parameter has been renamed to" 

454 " 'download_name'. The old name will be removed in Flask" 

455 " 2.2.", 

456 DeprecationWarning, 

457 stacklevel=3, 

458 ) 

459 download_name = attachment_filename 

460 

461 if cache_timeout is not None: 

462 warnings.warn( 

463 "The 'cache_timeout' parameter has been renamed to" 

464 " 'max_age'. The old name will be removed in Flask 2.2.", 

465 DeprecationWarning, 

466 stacklevel=3, 

467 ) 

468 max_age = cache_timeout 

469 

470 if add_etags is not None: 

471 warnings.warn( 

472 "The 'add_etags' parameter has been renamed to 'etag'. The" 

473 " old name will be removed in Flask 2.2.", 

474 DeprecationWarning, 

475 stacklevel=3, 

476 ) 

477 etag = add_etags 

478 

479 if max_age is None: 

480 max_age = current_app.get_send_file_max_age 

481 

482 kwargs.update( 

483 environ=request.environ, 

484 download_name=download_name, 

485 etag=etag, 

486 max_age=max_age, 

487 use_x_sendfile=current_app.use_x_sendfile, 

488 response_class=current_app.response_class, 

489 _root_path=current_app.root_path, # type: ignore 

490 ) 

491 return kwargs 

492 

493 

494def send_file( 

495 path_or_file: t.Union[os.PathLike, str, t.BinaryIO], 

496 mimetype: t.Optional[str] = None, 

497 as_attachment: bool = False, 

498 download_name: t.Optional[str] = None, 

499 attachment_filename: t.Optional[str] = None, 

500 conditional: bool = True, 

501 etag: t.Union[bool, str] = True, 

502 add_etags: t.Optional[bool] = None, 

503 last_modified: t.Optional[t.Union[datetime, int, float]] = None, 

504 max_age: t.Optional[ 

505 t.Union[int, t.Callable[[t.Optional[str]], t.Optional[int]]] 

506 ] = None, 

507 cache_timeout: t.Optional[int] = None, 

508): 

509 """Send the contents of a file to the client. 

510 

511 The first argument can be a file path or a file-like object. Paths 

512 are preferred in most cases because Werkzeug can manage the file and 

513 get extra information from the path. Passing a file-like object 

514 requires that the file is opened in binary mode, and is mostly 

515 useful when building a file in memory with :class:`io.BytesIO`. 

516 

517 Never pass file paths provided by a user. The path is assumed to be 

518 trusted, so a user could craft a path to access a file you didn't 

519 intend. Use :func:`send_from_directory` to safely serve 

520 user-requested paths from within a directory. 

521 

522 If the WSGI server sets a ``file_wrapper`` in ``environ``, it is 

523 used, otherwise Werkzeug's built-in wrapper is used. Alternatively, 

524 if the HTTP server supports ``X-Sendfile``, configuring Flask with 

525 ``USE_X_SENDFILE = True`` will tell the server to send the given 

526 path, which is much more efficient than reading it in Python. 

527 

528 :param path_or_file: The path to the file to send, relative to the 

529 current working directory if a relative path is given. 

530 Alternatively, a file-like object opened in binary mode. Make 

531 sure the file pointer is seeked to the start of the data. 

532 :param mimetype: The MIME type to send for the file. If not 

533 provided, it will try to detect it from the file name. 

534 :param as_attachment: Indicate to a browser that it should offer to 

535 save the file instead of displaying it. 

536 :param download_name: The default name browsers will use when saving 

537 the file. Defaults to the passed file name. 

538 :param conditional: Enable conditional and range responses based on 

539 request headers. Requires passing a file path and ``environ``. 

540 :param etag: Calculate an ETag for the file, which requires passing 

541 a file path. Can also be a string to use instead. 

542 :param last_modified: The last modified time to send for the file, 

543 in seconds. If not provided, it will try to detect it from the 

544 file path. 

545 :param max_age: How long the client should cache the file, in 

546 seconds. If set, ``Cache-Control`` will be ``public``, otherwise 

547 it will be ``no-cache`` to prefer conditional caching. 

548 

549 .. versionchanged:: 2.0 

550 ``download_name`` replaces the ``attachment_filename`` 

551 parameter. If ``as_attachment=False``, it is passed with 

552 ``Content-Disposition: inline`` instead. 

553 

554 .. versionchanged:: 2.0 

555 ``max_age`` replaces the ``cache_timeout`` parameter. 

556 ``conditional`` is enabled and ``max_age`` is not set by 

557 default. 

558 

559 .. versionchanged:: 2.0 

560 ``etag`` replaces the ``add_etags`` parameter. It can be a 

561 string to use instead of generating one. 

562 

563 .. versionchanged:: 2.0 

564 Passing a file-like object that inherits from 

565 :class:`~io.TextIOBase` will raise a :exc:`ValueError` rather 

566 than sending an empty file. 

567 

568 .. versionadded:: 2.0 

569 Moved the implementation to Werkzeug. This is now a wrapper to 

570 pass some Flask-specific arguments. 

571 

572 .. versionchanged:: 1.1 

573 ``filename`` may be a :class:`~os.PathLike` object. 

574 

575 .. versionchanged:: 1.1 

576 Passing a :class:`~io.BytesIO` object supports range requests. 

577 

578 .. versionchanged:: 1.0.3 

579 Filenames are encoded with ASCII instead of Latin-1 for broader 

580 compatibility with WSGI servers. 

581 

582 .. versionchanged:: 1.0 

583 UTF-8 filenames as specified in :rfc:`2231` are supported. 

584 

585 .. versionchanged:: 0.12 

586 The filename is no longer automatically inferred from file 

587 objects. If you want to use automatic MIME and etag support, 

588 pass a filename via ``filename_or_fp`` or 

589 ``attachment_filename``. 

590 

591 .. versionchanged:: 0.12 

592 ``attachment_filename`` is preferred over ``filename`` for MIME 

593 detection. 

594 

595 .. versionchanged:: 0.9 

596 ``cache_timeout`` defaults to 

597 :meth:`Flask.get_send_file_max_age`. 

598 

599 .. versionchanged:: 0.7 

600 MIME guessing and etag support for file-like objects was 

601 deprecated because it was unreliable. Pass a filename if you are 

602 able to, otherwise attach an etag yourself. 

603 

604 .. versionchanged:: 0.5 

605 The ``add_etags``, ``cache_timeout`` and ``conditional`` 

606 parameters were added. The default behavior is to add etags. 

607 

608 .. versionadded:: 0.2 

609 """ 

610 return werkzeug.utils.send_file( 

611 **_prepare_send_file_kwargs( 

612 path_or_file=path_or_file, 

613 environ=request.environ, 

614 mimetype=mimetype, 

615 as_attachment=as_attachment, 

616 download_name=download_name, 

617 attachment_filename=attachment_filename, 

618 conditional=conditional, 

619 etag=etag, 

620 add_etags=add_etags, 

621 last_modified=last_modified, 

622 max_age=max_age, 

623 cache_timeout=cache_timeout, 

624 ) 

625 ) 

626 

627 

628def send_from_directory( 

629 directory: t.Union[os.PathLike, str], 

630 path: t.Union[os.PathLike, str], 

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

632 **kwargs: t.Any, 

633) -> "Response": 

634 """Send a file from within a directory using :func:`send_file`. 

635 

636 .. code-block:: python 

637 

638 @app.route("/uploads/<path:name>") 

639 def download_file(name): 

640 return send_from_directory( 

641 app.config['UPLOAD_FOLDER'], name, as_attachment=True 

642 ) 

643 

644 This is a secure way to serve files from a folder, such as static 

645 files or uploads. Uses :func:`~werkzeug.security.safe_join` to 

646 ensure the path coming from the client is not maliciously crafted to 

647 point outside the specified directory. 

648 

649 If the final path does not point to an existing regular file, 

650 raises a 404 :exc:`~werkzeug.exceptions.NotFound` error. 

651 

652 :param directory: The directory that ``path`` must be located under, 

653 relative to the current application's root path. 

654 :param path: The path to the file to send, relative to 

655 ``directory``. 

656 :param kwargs: Arguments to pass to :func:`send_file`. 

657 

658 .. versionchanged:: 2.0 

659 ``path`` replaces the ``filename`` parameter. 

660 

661 .. versionadded:: 2.0 

662 Moved the implementation to Werkzeug. This is now a wrapper to 

663 pass some Flask-specific arguments. 

664 

665 .. versionadded:: 0.5 

666 """ 

667 if filename is not None: 

668 warnings.warn( 

669 "The 'filename' parameter has been renamed to 'path'. The" 

670 " old name will be removed in Flask 2.2.", 

671 DeprecationWarning, 

672 stacklevel=2, 

673 ) 

674 path = filename 

675 

676 return werkzeug.utils.send_from_directory( # type: ignore 

677 directory, path, **_prepare_send_file_kwargs(**kwargs) 

678 ) 

679 

680 

681def get_root_path(import_name: str) -> str: 

682 """Find the root path of a package, or the path that contains a 

683 module. If it cannot be found, returns the current working 

684 directory. 

685 

686 Not to be confused with the value returned by :func:`find_package`. 

687 

688 :meta private: 

689 """ 

690 # Module already imported and has a file attribute. Use that first. 

691 mod = sys.modules.get(import_name) 

692 

693 if mod is not None and hasattr(mod, "__file__") and mod.__file__ is not None: 

694 return os.path.dirname(os.path.abspath(mod.__file__)) 

695 

696 # Next attempt: check the loader. 

697 loader = pkgutil.get_loader(import_name) 

698 

699 # Loader does not exist or we're referring to an unloaded main 

700 # module or a main module without path (interactive sessions), go 

701 # with the current working directory. 

702 if loader is None or import_name == "__main__": 

703 return os.getcwd() 

704 

705 if hasattr(loader, "get_filename"): 

706 filepath = loader.get_filename(import_name) # type: ignore 

707 else: 

708 # Fall back to imports. 

709 __import__(import_name) 

710 mod = sys.modules[import_name] 

711 filepath = getattr(mod, "__file__", None) 

712 

713 # If we don't have a file path it might be because it is a 

714 # namespace package. In this case pick the root path from the 

715 # first module that is contained in the package. 

716 if filepath is None: 

717 raise RuntimeError( 

718 "No root path can be found for the provided module" 

719 f" {import_name!r}. This can happen because the module" 

720 " came from an import hook that does not provide file" 

721 " name information or because it's a namespace package." 

722 " In this case the root path needs to be explicitly" 

723 " provided." 

724 ) 

725 

726 # filepath is import_name.py for a module, or __init__.py for a package. 

727 return os.path.dirname(os.path.abspath(filepath)) 

728 

729 

730class locked_cached_property(werkzeug.utils.cached_property): 

731 """A :func:`property` that is only evaluated once. Like 

732 :class:`werkzeug.utils.cached_property` except access uses a lock 

733 for thread safety. 

734 

735 .. versionchanged:: 2.0 

736 Inherits from Werkzeug's ``cached_property`` (and ``property``). 

737 """ 

738 

739 def __init__( 

740 self, 

741 fget: t.Callable[[t.Any], t.Any], 

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

743 doc: t.Optional[str] = None, 

744 ) -> None: 

745 super().__init__(fget, name=name, doc=doc) 

746 self.lock = RLock() 

747 

748 def __get__(self, obj: object, type: type = None) -> t.Any: # type: ignore 

749 if obj is None: 

750 return self 

751 

752 with self.lock: 

753 return super().__get__(obj, type=type) 

754 

755 def __set__(self, obj: object, value: t.Any) -> None: 

756 with self.lock: 

757 super().__set__(obj, value) 

758 

759 def __delete__(self, obj: object) -> None: 

760 with self.lock: 

761 super().__delete__(obj) 

762 

763 

764def is_ip(value: str) -> bool: 

765 """Determine if the given string is an IP address. 

766 

767 :param value: value to check 

768 :type value: str 

769 

770 :return: True if string is an IP address 

771 :rtype: bool 

772 """ 

773 for family in (socket.AF_INET, socket.AF_INET6): 

774 try: 

775 socket.inet_pton(family, value) 

776 except OSError: 

777 pass 

778 else: 

779 return True 

780 

781 return False 

782 

783 

784@lru_cache(maxsize=None) 

785def _split_blueprint_path(name: str) -> t.List[str]: 

786 out: t.List[str] = [name] 

787 

788 if "." in name: 

789 out.extend(_split_blueprint_path(name.rpartition(".")[0])) 

790 

791 return out