pboot 3.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pboot/cli.py ADDED
@@ -0,0 +1,1126 @@
1
+ from __future__ import annotations
2
+
3
+ import ast
4
+ import collections.abc as cabc
5
+ import importlib.metadata
6
+ import inspect
7
+ import os
8
+ import platform
9
+ import re
10
+ import sys
11
+ import traceback
12
+ import typing as t
13
+ from functools import update_wrapper
14
+ from operator import itemgetter
15
+ from types import ModuleType
16
+
17
+ import click
18
+ from click.core import ParameterSource
19
+ from werkzeug import run_simple
20
+ from werkzeug.serving import is_running_from_reloader
21
+ from werkzeug.utils import import_string
22
+
23
+ from .globals import current_app
24
+ from .helpers import get_debug_flag
25
+ from .helpers import get_load_dotenv
26
+
27
+ if t.TYPE_CHECKING:
28
+ import ssl
29
+
30
+ from _typeshed.wsgi import StartResponse
31
+ from _typeshed.wsgi import WSGIApplication
32
+ from _typeshed.wsgi import WSGIEnvironment
33
+
34
+ from .app import Pboot
35
+
36
+
37
+ class NoAppException(click.UsageError):
38
+ """Raised if an application cannot be found or loaded."""
39
+
40
+
41
+ def find_best_app(module: ModuleType) -> Pboot:
42
+ """Given a module instance this tries to find the best possible
43
+ application in the module or raises an exception.
44
+ """
45
+ from . import Pboot
46
+
47
+ # Search for the most common names first.
48
+ for attr_name in ("app", "application"):
49
+ app = getattr(module, attr_name, None)
50
+
51
+ if isinstance(app, Pboot):
52
+ return app
53
+
54
+ # Otherwise find the only object that is a Pboot instance.
55
+ matches = [v for v in module.__dict__.values() if isinstance(v, Pboot)]
56
+
57
+ if len(matches) == 1:
58
+ return matches[0]
59
+ elif len(matches) > 1:
60
+ raise NoAppException(
61
+ "Detected multiple Pboot applications in module"
62
+ f" '{module.__name__}'. Use '{module.__name__}:name'"
63
+ " to specify the correct one."
64
+ )
65
+
66
+ # Search for app factory functions.
67
+ for attr_name in ("create_app", "make_app"):
68
+ app_factory = getattr(module, attr_name, None)
69
+
70
+ if inspect.isfunction(app_factory):
71
+ try:
72
+ app = app_factory()
73
+
74
+ if isinstance(app, Pboot):
75
+ return app
76
+ except TypeError as e:
77
+ if not _called_with_wrong_args(app_factory):
78
+ raise
79
+
80
+ raise NoAppException(
81
+ f"Detected factory '{attr_name}' in module '{module.__name__}',"
82
+ " but could not call it without arguments. Use"
83
+ f" '{module.__name__}:{attr_name}(args)'"
84
+ " to specify arguments."
85
+ ) from e
86
+
87
+ raise NoAppException(
88
+ "Failed to find Pboot application or factory in module"
89
+ f" '{module.__name__}'. Use '{module.__name__}:name'"
90
+ " to specify one."
91
+ )
92
+
93
+
94
+ def _called_with_wrong_args(f: t.Callable[..., Pboot]) -> bool:
95
+ """Check whether calling a function raised a ``TypeError`` because
96
+ the call failed or because something in the factory raised the
97
+ error.
98
+
99
+ :param f: The function that was called.
100
+ :return: ``True`` if the call failed.
101
+ """
102
+ tb = sys.exc_info()[2]
103
+
104
+ try:
105
+ while tb is not None:
106
+ if tb.tb_frame.f_code is f.__code__:
107
+ # In the function, it was called successfully.
108
+ return False
109
+
110
+ tb = tb.tb_next
111
+
112
+ # Didn't reach the function.
113
+ return True
114
+ finally:
115
+ # Delete tb to break a circular reference.
116
+ # https://docs.python.org/2/library/sys.html#sys.exc_info
117
+ del tb
118
+
119
+
120
+ def find_app_by_string(module: ModuleType, app_name: str) -> Pboot:
121
+ """Check if the given string is a variable name or a function. Call
122
+ a function to get the app instance, or return the variable directly.
123
+ """
124
+ from . import Pboot
125
+
126
+ # Parse app_name as a single expression to determine if it's a valid
127
+ # attribute name or function call.
128
+ try:
129
+ expr = ast.parse(app_name.strip(), mode="eval").body
130
+ except SyntaxError:
131
+ raise NoAppException(
132
+ f"Failed to parse {app_name!r} as an attribute name or function call."
133
+ ) from None
134
+
135
+ if isinstance(expr, ast.Name):
136
+ name = expr.id
137
+ args = []
138
+ kwargs = {}
139
+ elif isinstance(expr, ast.Call):
140
+ # Ensure the function name is an attribute name only.
141
+ if not isinstance(expr.func, ast.Name):
142
+ raise NoAppException(
143
+ f"Function reference must be a simple name: {app_name!r}."
144
+ )
145
+
146
+ name = expr.func.id
147
+
148
+ # Parse the positional and keyword arguments as literals.
149
+ try:
150
+ args = [ast.literal_eval(arg) for arg in expr.args]
151
+ kwargs = {
152
+ kw.arg: ast.literal_eval(kw.value)
153
+ for kw in expr.keywords
154
+ if kw.arg is not None
155
+ }
156
+ except ValueError:
157
+ # literal_eval gives cryptic error messages, show a generic
158
+ # message with the full expression instead.
159
+ raise NoAppException(
160
+ f"Failed to parse arguments as literal values: {app_name!r}."
161
+ ) from None
162
+ else:
163
+ raise NoAppException(
164
+ f"Failed to parse {app_name!r} as an attribute name or function call."
165
+ )
166
+
167
+ try:
168
+ attr = getattr(module, name)
169
+ except AttributeError as e:
170
+ raise NoAppException(
171
+ f"Failed to find attribute {name!r} in {module.__name__!r}."
172
+ ) from e
173
+
174
+ # If the attribute is a function, call it with any args and kwargs
175
+ # to get the real application.
176
+ if inspect.isfunction(attr):
177
+ try:
178
+ app = attr(*args, **kwargs)
179
+ except TypeError as e:
180
+ if not _called_with_wrong_args(attr):
181
+ raise
182
+
183
+ raise NoAppException(
184
+ f"The factory {app_name!r} in module"
185
+ f" {module.__name__!r} could not be called with the"
186
+ " specified arguments."
187
+ ) from e
188
+ else:
189
+ app = attr
190
+
191
+ if isinstance(app, Pboot):
192
+ return app
193
+
194
+ raise NoAppException(
195
+ "A valid Pboot application was not obtained from"
196
+ f" '{module.__name__}:{app_name}'."
197
+ )
198
+
199
+
200
+ def prepare_import(path: str) -> str:
201
+ """Given a filename this will try to calculate the python path, add it
202
+ to the search path and return the actual module name that is expected.
203
+ """
204
+ path = os.path.realpath(path)
205
+
206
+ fname, ext = os.path.splitext(path)
207
+ if ext == ".py":
208
+ path = fname
209
+
210
+ if os.path.basename(path) == "__init__":
211
+ path = os.path.dirname(path)
212
+
213
+ module_name = []
214
+
215
+ # move up until outside package structure (no __init__.py)
216
+ while True:
217
+ path, name = os.path.split(path)
218
+ module_name.append(name)
219
+
220
+ if not os.path.exists(os.path.join(path, "__init__.py")):
221
+ break
222
+
223
+ if sys.path[0] != path:
224
+ sys.path.insert(0, path)
225
+
226
+ return ".".join(module_name[::-1])
227
+
228
+
229
+ @t.overload
230
+ def locate_app(
231
+ module_name: str, app_name: str | None, raise_if_not_found: t.Literal[True] = True
232
+ ) -> Pboot: ...
233
+
234
+
235
+ @t.overload
236
+ def locate_app(
237
+ module_name: str, app_name: str | None, raise_if_not_found: t.Literal[False] = ...
238
+ ) -> Pboot | None: ...
239
+
240
+
241
+ def locate_app(
242
+ module_name: str, app_name: str | None, raise_if_not_found: bool = True
243
+ ) -> Pboot | None:
244
+ try:
245
+ __import__(module_name)
246
+ except ImportError:
247
+ # Reraise the ImportError if it occurred within the imported module.
248
+ # Determine this by checking whether the trace has a depth > 1.
249
+ if sys.exc_info()[2].tb_next: # type: ignore[union-attr]
250
+ raise NoAppException(
251
+ f"While importing {module_name!r}, an ImportError was"
252
+ f" raised:\n\n{traceback.format_exc()}"
253
+ ) from None
254
+ elif raise_if_not_found:
255
+ raise NoAppException(f"Could not import {module_name!r}.") from None
256
+ else:
257
+ return None
258
+
259
+ module = sys.modules[module_name]
260
+
261
+ if app_name is None:
262
+ return find_best_app(module)
263
+ else:
264
+ return find_app_by_string(module, app_name)
265
+
266
+
267
+ def get_version(ctx: click.Context, param: click.Parameter, value: t.Any) -> None:
268
+ if not value or ctx.resilient_parsing:
269
+ return
270
+
271
+ pboot_version = importlib.metadata.version("pboot")
272
+ werkzeug_version = importlib.metadata.version("werkzeug")
273
+
274
+ click.echo(
275
+ f"Python {platform.python_version()}\n"
276
+ f"Pboot {pboot_version}\n"
277
+ f"Werkzeug {werkzeug_version}",
278
+ color=ctx.color,
279
+ )
280
+ ctx.exit()
281
+
282
+
283
+ version_option = click.Option(
284
+ ["--version"],
285
+ help="Show the Pboot version.",
286
+ expose_value=False,
287
+ callback=get_version,
288
+ is_flag=True,
289
+ is_eager=True,
290
+ )
291
+
292
+
293
+ class ScriptInfo:
294
+ """Helper object to deal with Pboot applications. This is usually not
295
+ necessary to interface with as it's used internally in the dispatching
296
+ to click. In future versions of Pboot this object will most likely play
297
+ a bigger role. Typically it's created automatically by the
298
+ :class:`PbootGroup` but you can also manually create it and pass it
299
+ onwards as click object.
300
+
301
+ .. versionchanged:: 3.1
302
+ Added the ``load_dotenv_defaults`` parameter and attribute.
303
+ """
304
+
305
+ def __init__(
306
+ self,
307
+ app_import_path: str | None = None,
308
+ create_app: t.Callable[..., Pboot] | None = None,
309
+ set_debug_flag: bool = True,
310
+ load_dotenv_defaults: bool = True,
311
+ ) -> None:
312
+ #: Optionally the import path for the Pboot application.
313
+ self.app_import_path = app_import_path
314
+ #: Optionally a function that is passed the script info to create
315
+ #: the instance of the application.
316
+ self.create_app = create_app
317
+ #: A dictionary with arbitrary data that can be associated with
318
+ #: this script info.
319
+ self.data: dict[t.Any, t.Any] = {}
320
+ self.set_debug_flag = set_debug_flag
321
+
322
+ self.load_dotenv_defaults = get_load_dotenv(load_dotenv_defaults)
323
+ """Whether default ``.pbootenv`` and ``.env`` files should be loaded.
324
+
325
+ ``ScriptInfo`` doesn't load anything, this is for reference when doing
326
+ the load elsewhere during processing.
327
+
328
+ .. versionadded:: 3.1
329
+ """
330
+
331
+ self._loaded_app: Pboot | None = None
332
+
333
+ def load_app(self) -> Pboot:
334
+ """Loads the Pboot app (if not yet loaded) and returns it. Calling
335
+ this multiple times will just result in the already loaded app to
336
+ be returned.
337
+ """
338
+ if self._loaded_app is not None:
339
+ return self._loaded_app
340
+ app: Pboot | None = None
341
+ if self.create_app is not None:
342
+ app = self.create_app()
343
+ else:
344
+ if self.app_import_path:
345
+ path, name = (
346
+ re.split(r":(?![\\/])", self.app_import_path, maxsplit=1) + [None]
347
+ )[:2]
348
+ import_name = prepare_import(path)
349
+ app = locate_app(import_name, name)
350
+ else:
351
+ for path in ("wsgi.py", "app.py"):
352
+ import_name = prepare_import(path)
353
+ app = locate_app(import_name, None, raise_if_not_found=False)
354
+
355
+ if app is not None:
356
+ break
357
+
358
+ if app is None:
359
+ raise NoAppException(
360
+ "Could not locate a Pboot application. Use the"
361
+ " 'pboot --app' option, 'PBOOT_APP' environment"
362
+ " variable, or a 'wsgi.py' or 'app.py' file in the"
363
+ " current directory."
364
+ )
365
+
366
+ if self.set_debug_flag:
367
+ # Update the app's debug flag through the descriptor so that
368
+ # other values repopulate as well.
369
+ app.debug = get_debug_flag()
370
+
371
+ self._loaded_app = app
372
+ return app
373
+
374
+
375
+ pass_script_info = click.make_pass_decorator(ScriptInfo, ensure=True)
376
+
377
+ F = t.TypeVar("F", bound=t.Callable[..., t.Any])
378
+
379
+
380
+ def with_appcontext(f: F) -> F:
381
+ """Wraps a callback so that it's guaranteed to be executed with the
382
+ script's application context.
383
+
384
+ Custom commands (and their options) registered under ``app.cli`` or
385
+ ``blueprint.cli`` will always have an app context available, this
386
+ decorator is not required in that case.
387
+
388
+ .. versionchanged:: 2.2
389
+ The app context is active for subcommands as well as the
390
+ decorated callback. The app context is always available to
391
+ ``app.cli`` command and parameter callbacks.
392
+ """
393
+
394
+ @click.pass_context
395
+ def decorator(ctx: click.Context, /, *args: t.Any, **kwargs: t.Any) -> t.Any:
396
+ if not current_app:
397
+ app = ctx.ensure_object(ScriptInfo).load_app()
398
+ ctx.with_resource(app.app_context())
399
+
400
+ return ctx.invoke(f, *args, **kwargs)
401
+
402
+ return update_wrapper(decorator, f) # type: ignore[return-value]
403
+
404
+
405
+ class AppGroup(click.Group):
406
+ """This works similar to a regular click :class:`~click.Group` but it
407
+ changes the behavior of the :meth:`command` decorator so that it
408
+ automatically wraps the functions in :func:`with_appcontext`.
409
+
410
+ Not to be confused with :class:`PbootGroup`.
411
+ """
412
+
413
+ def command( # type: ignore[override]
414
+ self, *args: t.Any, **kwargs: t.Any
415
+ ) -> t.Callable[[t.Callable[..., t.Any]], click.Command]:
416
+ """This works exactly like the method of the same name on a regular
417
+ :class:`click.Group` but it wraps callbacks in :func:`with_appcontext`
418
+ unless it's disabled by passing ``with_appcontext=False``.
419
+ """
420
+ wrap_for_ctx = kwargs.pop("with_appcontext", True)
421
+
422
+ def decorator(f: t.Callable[..., t.Any]) -> click.Command:
423
+ if wrap_for_ctx:
424
+ f = with_appcontext(f)
425
+ return super(AppGroup, self).command(*args, **kwargs)(f) # type: ignore[no-any-return]
426
+
427
+ return decorator
428
+
429
+ def group( # type: ignore[override]
430
+ self, *args: t.Any, **kwargs: t.Any
431
+ ) -> t.Callable[[t.Callable[..., t.Any]], click.Group]:
432
+ """This works exactly like the method of the same name on a regular
433
+ :class:`click.Group` but it defaults the group class to
434
+ :class:`AppGroup`.
435
+ """
436
+ kwargs.setdefault("cls", AppGroup)
437
+ return super().group(*args, **kwargs) # type: ignore[no-any-return]
438
+
439
+
440
+ def _set_app(ctx: click.Context, param: click.Option, value: str | None) -> str | None:
441
+ if value is None:
442
+ return None
443
+
444
+ info = ctx.ensure_object(ScriptInfo)
445
+ info.app_import_path = value
446
+ return value
447
+
448
+
449
+ # This option is eager so the app will be available if --help is given.
450
+ # --help is also eager, so --app must be before it in the param list.
451
+ # no_args_is_help bypasses eager processing, so this option must be
452
+ # processed manually in that case to ensure PBOOT_APP gets picked up.
453
+ _app_option = click.Option(
454
+ ["-A", "--app"],
455
+ metavar="IMPORT",
456
+ help=(
457
+ "The Pboot application or factory function to load, in the form 'module:name'."
458
+ " Module can be a dotted import or file path. Name is not required if it is"
459
+ " 'app', 'application', 'create_app', or 'make_app', and can be 'name(args)' to"
460
+ " pass arguments."
461
+ ),
462
+ is_eager=True,
463
+ expose_value=False,
464
+ callback=_set_app,
465
+ )
466
+
467
+
468
+ def _set_debug(ctx: click.Context, param: click.Option, value: bool) -> bool | None:
469
+ # If the flag isn't provided, it will default to False. Don't use
470
+ # that, let debug be set by env in that case.
471
+ source = ctx.get_parameter_source(param.name)
472
+
473
+ if source is not None and source in (
474
+ ParameterSource.DEFAULT,
475
+ ParameterSource.DEFAULT_MAP,
476
+ ):
477
+ return None
478
+
479
+ # Set with env var instead of ScriptInfo.load so that it can be
480
+ # accessed early during a factory function.
481
+ os.environ["PBOOT_DEBUG"] = "1" if value else "0"
482
+ return value
483
+
484
+
485
+ _debug_option = click.Option(
486
+ ["--debug/--no-debug"],
487
+ help="Set debug mode.",
488
+ expose_value=False,
489
+ callback=_set_debug,
490
+ )
491
+
492
+
493
+ def _env_file_callback(
494
+ ctx: click.Context, param: click.Option, value: str | None
495
+ ) -> str | None:
496
+ try:
497
+ import dotenv # noqa: F401
498
+ except ImportError:
499
+ # Only show an error if a value was passed, otherwise we still want to
500
+ # call load_dotenv and show a message without exiting.
501
+ if value is not None:
502
+ raise click.BadParameter(
503
+ "python-dotenv must be installed to load an env file.",
504
+ ctx=ctx,
505
+ param=param,
506
+ ) from None
507
+
508
+ # Load if a value was passed, or we want to load default files, or both.
509
+ if value is not None or ctx.obj.load_dotenv_defaults:
510
+ load_dotenv(value, load_defaults=ctx.obj.load_dotenv_defaults)
511
+
512
+ return value
513
+
514
+
515
+ # This option is eager so env vars are loaded as early as possible to be
516
+ # used by other options.
517
+ _env_file_option = click.Option(
518
+ ["-e", "--env-file"],
519
+ type=click.Path(exists=True, dir_okay=False),
520
+ help=(
521
+ "Load environment variables from this file, taking precedence over"
522
+ " those set by '.env' and '.pbootenv'. Variables set directly in the"
523
+ " environment take highest precedence. python-dotenv must be installed."
524
+ ),
525
+ is_eager=True,
526
+ expose_value=False,
527
+ callback=_env_file_callback,
528
+ )
529
+
530
+
531
+ class PbootGroup(AppGroup):
532
+ """Special subclass of the :class:`AppGroup` group that supports
533
+ loading more commands from the configured Pboot app. Normally a
534
+ developer does not have to interface with this class but there are
535
+ some very advanced use cases for which it makes sense to create an
536
+ instance of this. see :ref:`custom-scripts`.
537
+
538
+ :param add_default_commands: if this is True then the default run and
539
+ shell commands will be added.
540
+ :param add_version_option: adds the ``--version`` option.
541
+ :param create_app: an optional callback that is passed the script info and
542
+ returns the loaded app.
543
+ :param load_dotenv: Load the nearest :file:`.env` and :file:`.pbootenv`
544
+ files to set environment variables. Will also change the working
545
+ directory to the directory containing the first file found.
546
+ :param set_debug_flag: Set the app's debug flag.
547
+
548
+ .. versionchanged:: 3.1
549
+ ``-e path`` takes precedence over default ``.env`` and ``.pbootenv`` files.
550
+
551
+ .. versionchanged:: 2.2
552
+ Added the ``-A/--app``, ``--debug/--no-debug``, ``-e/--env-file`` options.
553
+
554
+ .. versionchanged:: 2.2
555
+ An app context is pushed when running ``app.cli`` commands, so
556
+ ``@with_appcontext`` is no longer required for those commands.
557
+
558
+ .. versionchanged:: 1.0
559
+ If installed, python-dotenv will be used to load environment variables
560
+ from :file:`.env` and :file:`.pbootenv` files.
561
+ """
562
+
563
+ def __init__(
564
+ self,
565
+ add_default_commands: bool = True,
566
+ create_app: t.Callable[..., Pboot] | None = None,
567
+ add_version_option: bool = True,
568
+ load_dotenv: bool = True,
569
+ set_debug_flag: bool = True,
570
+ **extra: t.Any,
571
+ ) -> None:
572
+ params: list[click.Parameter] = list(extra.pop("params", None) or ())
573
+ # Processing is done with option callbacks instead of a group
574
+ # callback. This allows users to make a custom group callback
575
+ # without losing the behavior. --env-file must come first so
576
+ # that it is eagerly evaluated before --app.
577
+ params.extend((_env_file_option, _app_option, _debug_option))
578
+
579
+ if add_version_option:
580
+ params.append(version_option)
581
+
582
+ if "context_settings" not in extra:
583
+ extra["context_settings"] = {}
584
+
585
+ extra["context_settings"].setdefault("auto_envvar_prefix", "PBOOT")
586
+
587
+ super().__init__(params=params, **extra)
588
+
589
+ self.create_app = create_app
590
+ self.load_dotenv = load_dotenv
591
+ self.set_debug_flag = set_debug_flag
592
+
593
+ if add_default_commands:
594
+ self.add_command(run_command)
595
+ self.add_command(shell_command)
596
+ self.add_command(routes_command)
597
+
598
+ self._loaded_plugin_commands = False
599
+
600
+ def _load_plugin_commands(self) -> None:
601
+ if self._loaded_plugin_commands:
602
+ return
603
+
604
+ for ep in importlib.metadata.entry_points(group="pboot.commands"):
605
+ self.add_command(ep.load(), ep.name)
606
+
607
+ self._loaded_plugin_commands = True
608
+
609
+ def get_command(self, ctx: click.Context, name: str) -> click.Command | None:
610
+ self._load_plugin_commands()
611
+ # Look up built-in and plugin commands, which should be
612
+ # available even if the app fails to load.
613
+ rv = super().get_command(ctx, name)
614
+
615
+ if rv is not None:
616
+ return rv
617
+
618
+ info = ctx.ensure_object(ScriptInfo)
619
+
620
+ # Look up commands provided by the app, showing an error and
621
+ # continuing if the app couldn't be loaded.
622
+ try:
623
+ app = info.load_app()
624
+ except NoAppException as e:
625
+ click.secho(f"Error: {e.format_message()}\n", err=True, fg="red")
626
+ return None
627
+
628
+ # Push an app context for the loaded app unless it is already
629
+ # active somehow. This makes the context available to parameter
630
+ # and command callbacks without needing @with_appcontext.
631
+ if not current_app or current_app._get_current_object() is not app:
632
+ ctx.with_resource(app.app_context())
633
+
634
+ return app.cli.get_command(ctx, name)
635
+
636
+ def list_commands(self, ctx: click.Context) -> list[str]:
637
+ self._load_plugin_commands()
638
+ # Start with the built-in and plugin commands.
639
+ rv = set(super().list_commands(ctx))
640
+ info = ctx.ensure_object(ScriptInfo)
641
+
642
+ # Add commands provided by the app, showing an error and
643
+ # continuing if the app couldn't be loaded.
644
+ try:
645
+ rv.update(info.load_app().cli.list_commands(ctx))
646
+ except NoAppException as e:
647
+ # When an app couldn't be loaded, show the error message
648
+ # without the traceback.
649
+ click.secho(f"Error: {e.format_message()}\n", err=True, fg="red")
650
+ except Exception:
651
+ # When any other errors occurred during loading, show the
652
+ # full traceback.
653
+ click.secho(f"{traceback.format_exc()}\n", err=True, fg="red")
654
+
655
+ return sorted(rv)
656
+
657
+ def make_context(
658
+ self,
659
+ info_name: str | None,
660
+ args: list[str],
661
+ parent: click.Context | None = None,
662
+ **extra: t.Any,
663
+ ) -> click.Context:
664
+ # Set a flag to tell app.run to become a no-op. If app.run was
665
+ # not in a __name__ == __main__ guard, it would start the server
666
+ # when importing, blocking whatever command is being called.
667
+ os.environ["PBOOT_RUN_FROM_CLI"] = "true"
668
+
669
+ if "obj" not in extra and "obj" not in self.context_settings:
670
+ extra["obj"] = ScriptInfo(
671
+ create_app=self.create_app,
672
+ set_debug_flag=self.set_debug_flag,
673
+ load_dotenv_defaults=self.load_dotenv,
674
+ )
675
+
676
+ return super().make_context(info_name, args, parent=parent, **extra)
677
+
678
+ def parse_args(self, ctx: click.Context, args: list[str]) -> list[str]:
679
+ if (not args and self.no_args_is_help) or (
680
+ len(args) == 1 and args[0] in self.get_help_option_names(ctx)
681
+ ):
682
+ # Attempt to load --env-file and --app early in case they
683
+ # were given as env vars. Otherwise no_args_is_help will not
684
+ # see commands from app.cli.
685
+ _env_file_option.handle_parse_result(ctx, {}, [])
686
+ _app_option.handle_parse_result(ctx, {}, [])
687
+
688
+ return super().parse_args(ctx, args)
689
+
690
+
691
+ def _path_is_ancestor(path: str, other: str) -> bool:
692
+ """Take ``other`` and remove the length of ``path`` from it. Then join it
693
+ to ``path``. If it is the original value, ``path`` is an ancestor of
694
+ ``other``."""
695
+ return os.path.join(path, other[len(path) :].lstrip(os.sep)) == other
696
+
697
+
698
+ def load_dotenv(
699
+ path: str | os.PathLike[str] | None = None, load_defaults: bool = True
700
+ ) -> bool:
701
+ """Load "dotenv" files to set environment variables. A given path takes
702
+ precedence over ``.env``, which takes precedence over ``.pbootenv``. After
703
+ loading and combining these files, values are only set if the key is not
704
+ already set in ``os.environ``.
705
+
706
+ This is a no-op if `python-dotenv`_ is not installed.
707
+
708
+ .. _python-dotenv: https://github.com/theskumar/python-dotenv#readme
709
+
710
+ :param path: Load the file at this location.
711
+ :param load_defaults: Search for and load the default ``.pbootenv`` and
712
+ ``.env`` files.
713
+ :return: ``True`` if at least one env var was loaded.
714
+
715
+ .. versionchanged:: 3.1
716
+ Added the ``load_defaults`` parameter. A given path takes precedence
717
+ over default files.
718
+
719
+ .. versionchanged:: 2.0
720
+ The current directory is not changed to the location of the
721
+ loaded file.
722
+
723
+ .. versionchanged:: 2.0
724
+ When loading the env files, set the default encoding to UTF-8.
725
+
726
+ .. versionchanged:: 1.1.0
727
+ Returns ``False`` when python-dotenv is not installed, or when
728
+ the given path isn't a file.
729
+
730
+ .. versionadded:: 1.0
731
+ """
732
+ try:
733
+ import dotenv
734
+ except ImportError:
735
+ if path or os.path.isfile(".env") or os.path.isfile(".pbootenv"):
736
+ click.secho(
737
+ " * Tip: There are .env files present. Install python-dotenv"
738
+ " to use them.",
739
+ fg="yellow",
740
+ err=True,
741
+ )
742
+
743
+ return False
744
+
745
+ data: dict[str, str | None] = {}
746
+
747
+ if load_defaults:
748
+ for default_name in (".pbootenv", ".env"):
749
+ if not (default_path := dotenv.find_dotenv(default_name, usecwd=True)):
750
+ continue
751
+
752
+ data |= dotenv.dotenv_values(default_path, encoding="utf-8")
753
+
754
+ if path is not None and os.path.isfile(path):
755
+ data |= dotenv.dotenv_values(path, encoding="utf-8")
756
+
757
+ for key, value in data.items():
758
+ if key in os.environ or value is None:
759
+ continue
760
+
761
+ os.environ[key] = value
762
+
763
+ return bool(data) # True if at least one env var was loaded.
764
+
765
+
766
+ def show_server_banner(debug: bool, app_import_path: str | None) -> None:
767
+ """Show extra startup messages the first time the server is run,
768
+ ignoring the reloader.
769
+ """
770
+ if is_running_from_reloader():
771
+ return
772
+
773
+ if app_import_path is not None:
774
+ click.echo(f" * Serving Pboot app '{app_import_path}'")
775
+
776
+ if debug is not None:
777
+ click.echo(f" * Debug mode: {'on' if debug else 'off'}")
778
+
779
+
780
+ class CertParamType(click.ParamType): # type: ignore[type-arg]
781
+ """Click option type for the ``--cert`` option. Allows either an
782
+ existing file, the string ``'adhoc'``, or an import for a
783
+ :class:`~ssl.SSLContext` object.
784
+ """
785
+
786
+ name = "path"
787
+
788
+ def __init__(self) -> None:
789
+ self.path_type = click.Path(exists=True, dir_okay=False, resolve_path=True)
790
+
791
+ def convert(
792
+ self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None
793
+ ) -> t.Any:
794
+ try:
795
+ import ssl
796
+ except ImportError:
797
+ raise click.BadParameter(
798
+ 'Using "--cert" requires Python to be compiled with SSL support.',
799
+ ctx,
800
+ param,
801
+ ) from None
802
+
803
+ try:
804
+ return self.path_type(value, param, ctx)
805
+ except click.BadParameter:
806
+ value = click.STRING(value, param, ctx).lower() # type: ignore[union-attr]
807
+
808
+ if value == "adhoc":
809
+ try:
810
+ import cryptography # noqa: F401
811
+ except ImportError:
812
+ raise click.BadParameter(
813
+ "Using ad-hoc certificates requires the cryptography library.",
814
+ ctx,
815
+ param,
816
+ ) from None
817
+
818
+ return value
819
+
820
+ obj = import_string(value, silent=True)
821
+
822
+ if isinstance(obj, ssl.SSLContext):
823
+ return obj
824
+
825
+ raise
826
+
827
+
828
+ def _validate_key(ctx: click.Context, param: click.Parameter, value: t.Any) -> t.Any:
829
+ """The ``--key`` option must be specified when ``--cert`` is a file.
830
+ Modifies the ``cert`` param to be a ``(cert, key)`` pair if needed.
831
+ """
832
+ cert = ctx.params.get("cert")
833
+ is_adhoc = cert == "adhoc"
834
+
835
+ try:
836
+ import ssl
837
+ except ImportError:
838
+ is_context = False
839
+ else:
840
+ is_context = isinstance(cert, ssl.SSLContext)
841
+
842
+ if value is not None:
843
+ if is_adhoc:
844
+ raise click.BadParameter(
845
+ 'When "--cert" is "adhoc", "--key" is not used.', ctx, param
846
+ )
847
+
848
+ if is_context:
849
+ raise click.BadParameter(
850
+ 'When "--cert" is an SSLContext object, "--key" is not used.',
851
+ ctx,
852
+ param,
853
+ )
854
+
855
+ if not cert:
856
+ raise click.BadParameter('"--cert" must also be specified.', ctx, param)
857
+
858
+ ctx.params["cert"] = cert, value
859
+
860
+ else:
861
+ if cert and not (is_adhoc or is_context):
862
+ raise click.BadParameter('Required when using "--cert".', ctx, param)
863
+
864
+ return value
865
+
866
+
867
+ class SeparatedPathType(click.Path):
868
+ """Click option type that accepts a list of values separated by the
869
+ OS's path separator (``:``, ``;`` on Windows). Each value is
870
+ validated as a :class:`click.Path` type.
871
+ """
872
+
873
+ def convert(
874
+ self, value: t.Any, param: click.Parameter | None, ctx: click.Context | None
875
+ ) -> t.Any:
876
+ items = self.split_envvar_value(value)
877
+ super_convert = super().convert
878
+ return [super_convert(item, param, ctx) for item in items]
879
+
880
+
881
+ @click.command("run", short_help="Run a development server.")
882
+ @click.option("--host", "-h", default="127.0.0.1", help="The interface to bind to.")
883
+ @click.option("--port", "-p", default=5000, help="The port to bind to.")
884
+ @click.option(
885
+ "--cert",
886
+ type=CertParamType(),
887
+ help="Specify a certificate file to use HTTPS.",
888
+ is_eager=True,
889
+ )
890
+ @click.option(
891
+ "--key",
892
+ type=click.Path(exists=True, dir_okay=False, resolve_path=True),
893
+ callback=_validate_key,
894
+ expose_value=False,
895
+ help="The key file to use when specifying a certificate.",
896
+ )
897
+ @click.option(
898
+ "--reload/--no-reload",
899
+ default=None,
900
+ help="Enable or disable the reloader. By default the reloader "
901
+ "is active if debug is enabled.",
902
+ )
903
+ @click.option(
904
+ "--debugger/--no-debugger",
905
+ default=None,
906
+ help="Enable or disable the debugger. By default the debugger "
907
+ "is active if debug is enabled.",
908
+ )
909
+ @click.option(
910
+ "--with-threads/--without-threads",
911
+ default=True,
912
+ help="Enable or disable multithreading.",
913
+ )
914
+ @click.option(
915
+ "--extra-files",
916
+ default=None,
917
+ type=SeparatedPathType(),
918
+ help=(
919
+ "Extra files that trigger a reload on change. Multiple paths"
920
+ f" are separated by {os.path.pathsep!r}."
921
+ ),
922
+ )
923
+ @click.option(
924
+ "--exclude-patterns",
925
+ default=None,
926
+ type=SeparatedPathType(),
927
+ help=(
928
+ "Files matching these fnmatch patterns will not trigger a reload"
929
+ " on change. Multiple patterns are separated by"
930
+ f" {os.path.pathsep!r}."
931
+ ),
932
+ )
933
+ @pass_script_info
934
+ def run_command(
935
+ info: ScriptInfo,
936
+ host: str,
937
+ port: int,
938
+ reload: bool,
939
+ debugger: bool,
940
+ with_threads: bool,
941
+ cert: ssl.SSLContext | tuple[str, str | None] | t.Literal["adhoc"] | None,
942
+ extra_files: list[str] | None,
943
+ exclude_patterns: list[str] | None,
944
+ ) -> None:
945
+ """Run a local development server.
946
+
947
+ This server is for development purposes only. It does not provide
948
+ the stability, security, or performance of production WSGI servers.
949
+
950
+ The reloader and debugger are enabled by default with the '--debug'
951
+ option.
952
+ """
953
+ try:
954
+ app: WSGIApplication = info.load_app() # pyright: ignore
955
+ except Exception as e:
956
+ if is_running_from_reloader():
957
+ # When reloading, print out the error immediately, but raise
958
+ # it later so the debugger or server can handle it.
959
+ traceback.print_exc()
960
+ err = e
961
+
962
+ def app(
963
+ environ: WSGIEnvironment, start_response: StartResponse
964
+ ) -> cabc.Iterable[bytes]:
965
+ raise err from None
966
+
967
+ else:
968
+ # When not reloading, raise the error immediately so the
969
+ # command fails.
970
+ raise e from None
971
+
972
+ debug = get_debug_flag()
973
+
974
+ if reload is None:
975
+ reload = debug
976
+
977
+ if debugger is None:
978
+ debugger = debug
979
+
980
+ show_server_banner(debug, info.app_import_path)
981
+
982
+ run_simple(
983
+ host,
984
+ port,
985
+ app,
986
+ use_reloader=reload,
987
+ use_debugger=debugger,
988
+ threaded=with_threads,
989
+ ssl_context=cert,
990
+ extra_files=extra_files,
991
+ exclude_patterns=exclude_patterns,
992
+ )
993
+
994
+
995
+ run_command.params.insert(0, _debug_option)
996
+
997
+
998
+ @click.command("shell", short_help="Run a shell in the app context.")
999
+ @with_appcontext
1000
+ def shell_command() -> None:
1001
+ """Run an interactive Python shell in the context of a given
1002
+ Pboot application. The application will populate the default
1003
+ namespace of this shell according to its configuration.
1004
+
1005
+ This is useful for executing small snippets of management code
1006
+ without having to manually configure the application.
1007
+ """
1008
+ import code
1009
+
1010
+ banner = (
1011
+ f"Python {sys.version} on {sys.platform}\n"
1012
+ f"App: {current_app.import_name}\n"
1013
+ f"Instance: {current_app.instance_path}"
1014
+ )
1015
+ ctx: dict[str, t.Any] = {}
1016
+
1017
+ # Support the regular Python interpreter startup script if someone
1018
+ # is using it.
1019
+ startup = os.environ.get("PYTHONSTARTUP")
1020
+ if startup and os.path.isfile(startup):
1021
+ with open(startup) as f:
1022
+ eval(compile(f.read(), startup, "exec"), ctx)
1023
+
1024
+ ctx.update(current_app.make_shell_context())
1025
+
1026
+ # Site, customize, or startup script can set a hook to call when
1027
+ # entering interactive mode. The default one sets up readline with
1028
+ # tab and history completion.
1029
+ interactive_hook = getattr(sys, "__interactivehook__", None)
1030
+
1031
+ if interactive_hook is not None:
1032
+ try:
1033
+ import readline
1034
+ from rlcompleter import Completer
1035
+ except ImportError:
1036
+ pass
1037
+ else:
1038
+ # rlcompleter uses __main__.__dict__ by default, which is
1039
+ # pboot.__main__. Use the shell context instead.
1040
+ readline.set_completer(Completer(ctx).complete)
1041
+
1042
+ interactive_hook()
1043
+
1044
+ code.interact(banner=banner, local=ctx)
1045
+
1046
+
1047
+ @click.command("routes", short_help="Show the routes for the app.")
1048
+ @click.option(
1049
+ "--sort",
1050
+ "-s",
1051
+ type=click.Choice(("endpoint", "methods", "domain", "rule", "match")),
1052
+ default="endpoint",
1053
+ help=(
1054
+ "Method to sort routes by. 'match' is the order that Pboot will match routes"
1055
+ " when dispatching a request."
1056
+ ),
1057
+ )
1058
+ @click.option("--all-methods", is_flag=True, help="Show HEAD and OPTIONS methods.")
1059
+ @with_appcontext
1060
+ def routes_command(sort: str, all_methods: bool) -> None:
1061
+ """Show all registered routes with endpoints and methods."""
1062
+ rules = list(current_app.url_map.iter_rules())
1063
+
1064
+ if not rules:
1065
+ click.echo("No routes were registered.")
1066
+ return
1067
+
1068
+ ignored_methods = set() if all_methods else {"HEAD", "OPTIONS"}
1069
+ host_matching = current_app.url_map.host_matching
1070
+ has_domain = any(rule.host if host_matching else rule.subdomain for rule in rules)
1071
+ rows = []
1072
+
1073
+ for rule in rules:
1074
+ row = [
1075
+ rule.endpoint,
1076
+ ", ".join(sorted((rule.methods or set()) - ignored_methods)),
1077
+ ]
1078
+
1079
+ if has_domain:
1080
+ row.append((rule.host if host_matching else rule.subdomain) or "")
1081
+
1082
+ row.append(rule.rule)
1083
+ rows.append(row)
1084
+
1085
+ headers = ["Endpoint", "Methods"]
1086
+ sorts = ["endpoint", "methods"]
1087
+
1088
+ if has_domain:
1089
+ headers.append("Host" if host_matching else "Subdomain")
1090
+ sorts.append("domain")
1091
+
1092
+ headers.append("Rule")
1093
+ sorts.append("rule")
1094
+
1095
+ try:
1096
+ rows.sort(key=itemgetter(sorts.index(sort)))
1097
+ except ValueError:
1098
+ pass
1099
+
1100
+ rows.insert(0, headers)
1101
+ widths = [max(len(row[i]) for row in rows) for i in range(len(headers))]
1102
+ rows.insert(1, ["-" * w for w in widths])
1103
+ template = " ".join(f"{{{i}:<{w}}}" for i, w in enumerate(widths))
1104
+
1105
+ for row in rows:
1106
+ click.echo(template.format(*row))
1107
+
1108
+
1109
+ cli = PbootGroup(
1110
+ name="pboot",
1111
+ help="""\
1112
+ A general utility script for Pboot applications.
1113
+
1114
+ An application to load must be given with the '--app' option,
1115
+ 'PBOOT_APP' environment variable, or with a 'wsgi.py' or 'app.py' file
1116
+ in the current directory.
1117
+ """,
1118
+ )
1119
+
1120
+
1121
+ def main() -> None:
1122
+ cli.main()
1123
+
1124
+
1125
+ if __name__ == "__main__":
1126
+ main()