plain 0.38.0__py3-none-any.whl → 0.39.1__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.
plain/chores/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Chores
2
+
3
+ **Routine maintenance tasks.**
4
+
5
+ Chores are registered functions that can be run at any time to keep an app in a desirable state.
6
+
7
+ ![](https://assets.plainframework.com/docs/plain-chores-run.png)
8
+
9
+ A good example is the clearing of expired sessions in [`plain.sessions`](/plain-sessions/plain/sessions/chores.py) — since the sessions are stored in the database, occasionally you will want to delete any sessions that are expired and no longer in use.
10
+
11
+ ```python
12
+ # plain/sessions/chores.py
13
+ from plain.chores import register_chore
14
+ from plain.utils import timezone
15
+
16
+ from .models import Session
17
+
18
+
19
+ @register_chore("sessions")
20
+ def clear_expired():
21
+ """
22
+ Delete sessions that have expired.
23
+ """
24
+ result = Session.objects.filter(expires_at__lt=timezone.now()).delete()
25
+ return f"{result[0]} expired sessions deleted"
26
+ ```
27
+
28
+ ## Running chores
29
+
30
+ The `plain chores run` command will execute all registered chores. When and how to run this is up to the user, but running them hourly is a safe assumption in most cases (assuming you have any chores — `plain chores list`).
31
+
32
+ There are several ways you can run chores depending on your needs:
33
+
34
+ - on deploy
35
+ - as a [`plain.worker` scheduled job](/plain-worker/plain/worker/README.md#scheduled-jobs)
36
+ - as a cron job (using any cron-like system where your app is hosted)
37
+ - manually as needed
38
+
39
+ ## Writing chores
40
+
41
+ A chore is a function decorated with `@register_chore(chore_group_name)`. It can write a description as a docstring, and it can return a value that will be printed when the chore is run.
42
+
43
+ ```python
44
+ # app/chores.py
45
+ from plain.chores import register_chore
46
+
47
+
48
+ @register_chore("app")
49
+ def chore_name():
50
+ """
51
+ A chore description can go here
52
+ """
53
+ # Do a thing!
54
+ return "We did it!"
55
+ ```
56
+
57
+ A good chore is:
58
+
59
+ - Fast
60
+ - Idempotent
61
+ - Recurring
62
+ - Stateless
63
+
64
+ If chores are written in `app/chores.py` or `{pkg}/chores.py`, then they will be imported automatically and registered.
@@ -0,0 +1,3 @@
1
+ from .registry import register_chore
2
+
3
+ __all__ = ["register_chore"]
@@ -0,0 +1,79 @@
1
+ from importlib import import_module
2
+ from importlib.util import find_spec
3
+
4
+ from plain.packages import packages_registry
5
+
6
+
7
+ class Chore:
8
+ def __init__(self, *, group, func):
9
+ self.group = group
10
+ self.func = func
11
+ self.name = f"{group}.{func.__name__}"
12
+ self.description = func.__doc__.strip() if func.__doc__ else ""
13
+
14
+ def __str__(self):
15
+ return self.name
16
+
17
+ def run(self):
18
+ """
19
+ Run the chore.
20
+ """
21
+ return self.func()
22
+
23
+
24
+ class ChoresRegistry:
25
+ def __init__(self):
26
+ self._chores = {}
27
+
28
+ def register_chore(self, chore):
29
+ """
30
+ Register a chore with the specified name.
31
+ """
32
+ self._chores[chore.func] = chore
33
+
34
+ def import_modules(self):
35
+ """
36
+ Import modules from installed packages and app to trigger registration.
37
+ """
38
+ # Import from installed packages
39
+ for package_config in packages_registry.get_package_configs():
40
+ import_name = f"{package_config.name}.chores"
41
+ try:
42
+ import_module(import_name)
43
+ except ModuleNotFoundError:
44
+ pass
45
+
46
+ # Import from app
47
+ import_name = "app.chores"
48
+ if find_spec(import_name):
49
+ try:
50
+ import_module(import_name)
51
+ except ModuleNotFoundError:
52
+ pass
53
+
54
+ def get_chores(self):
55
+ """
56
+ Get all registered chores.
57
+ """
58
+ return list(self._chores.values())
59
+
60
+
61
+ chores_registry = ChoresRegistry()
62
+
63
+
64
+ def register_chore(group):
65
+ """
66
+ Register a chore with a given group.
67
+
68
+ Usage:
69
+ @register_chore("clear_expired")
70
+ def clear_expired():
71
+ pass
72
+ """
73
+
74
+ def wrapper(func):
75
+ chore = Chore(group=group, func=func)
76
+ chores_registry.register_chore(chore)
77
+ return func
78
+
79
+ return wrapper
plain/cli/chores.py ADDED
@@ -0,0 +1,91 @@
1
+ import logging
2
+ import sys
3
+
4
+ import click
5
+
6
+ logger = logging.getLogger("plain.chores")
7
+
8
+
9
+ @click.group()
10
+ def chores():
11
+ """Routine maintenance tasks"""
12
+ pass
13
+
14
+
15
+ @chores.command("list")
16
+ @click.option("--group", default=None, type=str, help="Group to run", multiple=True)
17
+ @click.option(
18
+ "--name", default=None, type=str, help="Name of the chore to run", multiple=True
19
+ )
20
+ def list_chores(group, name):
21
+ """
22
+ List all registered chores.
23
+ """
24
+ from plain.chores.registry import chores_registry
25
+
26
+ chores_registry.import_modules()
27
+
28
+ if group or name:
29
+ chores = [
30
+ chore
31
+ for chore in chores_registry.get_chores()
32
+ if (chore.group in group or not group) and (chore.name in name or not name)
33
+ ]
34
+ else:
35
+ chores = chores_registry.get_chores()
36
+
37
+ for chore in chores:
38
+ click.secho(f"{chore}", bold=True, nl=False)
39
+ if chore.description:
40
+ click.echo(f": {chore.description}")
41
+ else:
42
+ click.echo("")
43
+
44
+
45
+ @chores.command("run")
46
+ @click.option("--group", default=None, type=str, help="Group to run", multiple=True)
47
+ @click.option(
48
+ "--name", default=None, type=str, help="Name of the chore to run", multiple=True
49
+ )
50
+ @click.option(
51
+ "--dry-run", is_flag=True, help="Show what would be done without executing"
52
+ )
53
+ def run_chores(group, name, dry_run):
54
+ """
55
+ Run the specified chores.
56
+ """
57
+ from plain.chores.registry import chores_registry
58
+
59
+ chores_registry.import_modules()
60
+
61
+ if group or name:
62
+ chores = [
63
+ chore
64
+ for chore in chores_registry.get_chores()
65
+ if (chore.group in group or not group) and (chore.name in name or not name)
66
+ ]
67
+ else:
68
+ chores = chores_registry.get_chores()
69
+
70
+ chores_failed = []
71
+
72
+ for chore in chores:
73
+ click.echo(f"{chore.name}:", nl=False)
74
+ if dry_run:
75
+ click.echo(" (dry run)", fg="yellow")
76
+ else:
77
+ try:
78
+ result = chore.run()
79
+ except Exception:
80
+ click.secho(" Failed", fg="red")
81
+ chores_failed.append(chore)
82
+ logger.exception(f"Error running chore {chore.name}")
83
+ continue
84
+
85
+ if result is None:
86
+ click.secho(" Done", fg="green")
87
+ else:
88
+ click.secho(f" {result}", fg="green")
89
+
90
+ if chores_failed:
91
+ sys.exit(1)
plain/cli/core.py CHANGED
@@ -7,6 +7,7 @@ import plain.runtime
7
7
  from plain.exceptions import ImproperlyConfigured
8
8
 
9
9
  from .build import build
10
+ from .chores import chores
10
11
  from .docs import docs
11
12
  from .formatting import PlainContext
12
13
  from .preflight import preflight_checks
@@ -26,6 +27,7 @@ def plain_cli():
26
27
  plain_cli.add_command(docs)
27
28
  plain_cli.add_command(preflight_checks)
28
29
  plain_cli.add_command(create)
30
+ plain_cli.add_command(chores)
29
31
  plain_cli.add_command(build)
30
32
  plain_cli.add_command(utils)
31
33
  plain_cli.add_command(urls)
plain/views/base.py CHANGED
@@ -1,4 +1,5 @@
1
1
  import logging
2
+ from http import HTTPMethod
2
3
 
3
4
  from plain.http import (
4
5
  HttpRequest,
@@ -100,14 +101,4 @@ class View:
100
101
  return response
101
102
 
102
103
  def _allowed_methods(self) -> list[str]:
103
- known_http_methods = [
104
- "get",
105
- "post",
106
- "put",
107
- "patch",
108
- "delete",
109
- "head",
110
- "options",
111
- "trace",
112
- ]
113
- return [m.upper() for m in known_http_methods if hasattr(self, m)]
104
+ return [m.upper() for m in HTTPMethod if hasattr(self, m.lower())]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: plain
3
- Version: 0.38.0
3
+ Version: 0.39.1
4
4
  Summary: A web framework for building products with Python.
5
5
  Author-email: Dave Gaeddert <dave.gaeddert@dropseed.dev>
6
6
  License-File: LICENSE
@@ -14,10 +14,14 @@ plain/assets/finders.py,sha256=2k8QZAbfUbc1LykxbzdazTSB6xNxJZnsZaGhWbSFZZs,1452
14
14
  plain/assets/fingerprints.py,sha256=2LPHLUkoITMseLDmemTpBtMRDWCR2H5GAHjC6AN4gz0,1367
15
15
  plain/assets/urls.py,sha256=zQUA8bAlh9qVqskPJJrqWd9DjvetOi5jPSqy4vUX0J4,1161
16
16
  plain/assets/views.py,sha256=T_0Qh6v9qBerEBYbhToigwOzsij-x1z_R-1zETQcIh0,9447
17
+ plain/chores/README.md,sha256=Da8Nw8ZF7PlAE_iVb0AqJGbLOu0F6HC0cj1K451KBak,1946
18
+ plain/chores/__init__.py,sha256=r9TXtQCH-VbvfnIJ5F8FxgQC35GRWFOfmMZN3q9niLg,67
19
+ plain/chores/registry.py,sha256=V3WjuekRI22LFvJbqSkUXQtiOtuE2ZK8gKV1TRvxRUI,1866
17
20
  plain/cli/README.md,sha256=ompPcgzY2Fdpm579ITmCpFIaZIsiXYbfD61mqkq312M,1860
18
21
  plain/cli/__init__.py,sha256=6w9T7K2WrPwh6DcaMb2oNt_CWU6Bc57nUTO2Bt1p38Y,63
19
22
  plain/cli/build.py,sha256=dKUYBNegvb6QNckR7XZ7CJJtINwZcmDvbUdv2dWwjf8,3226
20
- plain/cli/core.py,sha256=N7xUO8Ox2xz8wJyWZ0fSyCfqVFmKz9czH9gulRyQuM4,2809
23
+ plain/cli/chores.py,sha256=xXSSFvr8T5jWfLWqe6E8YVMw1BkQxyOHHVuY0x9RH0A,2412
24
+ plain/cli/core.py,sha256=sy5otyN5jNOHkcy1EXISB7iMr1itp0rzmBSnEXYGe2k,2866
21
25
  plain/cli/docs.py,sha256=2CpTv5k-TNWf593tPiglvUVXWBGdfPmbGf8vl5AfJwU,8995
22
26
  plain/cli/formatting.py,sha256=1hZH13y1qwHcU2K2_Na388nw9uvoeQH8LrWL-O9h8Yc,2207
23
27
  plain/cli/preflight.py,sha256=NKyYjcoDjigzfJIDhf7A7degYadaUI05Bw7U9OQ73vs,4170
@@ -134,7 +138,7 @@ plain/utils/timezone.py,sha256=6u0sE-9RVp0_OCe0Y1KiYYQoq5THWLokZFQYY8jf78g,6221
134
138
  plain/utils/tree.py,sha256=wdWzmfsgc26YDF2wxhAY3yVxXTixQYqYDKE9mL3L3ZY,4383
135
139
  plain/views/README.md,sha256=_jR_8_eccE1Qwc9sbUhD_hpZGGf0r-HY4W-al6kqtGs,6762
136
140
  plain/views/__init__.py,sha256=a-N1nkklVohJTtz0yD1MMaS0g66HviEjsKydNVVjvVc,392
137
- plain/views/base.py,sha256=bFSSZABqANn3DPrUa97p6XxzXh0My-FSJSWjvLoFljQ,3240
141
+ plain/views/base.py,sha256=bF_yBHrBfOdq3OcA_CTGCs7hNI1O6LpDVLM2Pkzyfjs,3062
138
142
  plain/views/csrf.py,sha256=7q6l5xzLWhRnMY64aNj0hR6G-3pxI2yhRwG6k_5j00E,144
139
143
  plain/views/errors.py,sha256=jbNCJIzowwCsEvqyJ3opMeZpPDqTyhtrbqb0VnAm2HE,1263
140
144
  plain/views/exceptions.py,sha256=b4euI49ZUKS9O8AGAcFfiDpstzkRAuuj_uYQXzWNHME,138
@@ -142,8 +146,8 @@ plain/views/forms.py,sha256=ESZOXuo6IeYixp1RZvPb94KplkowRiwO2eGJCM6zJI0,2400
142
146
  plain/views/objects.py,sha256=GGbcfg_9fPZ-PiaBwIHG2e__8GfWDR7JQtQ15wTyiHg,5970
143
147
  plain/views/redirect.py,sha256=vMXx8430FtyKcT0V0gyY92SkLtyULBX52KhX4eu4gEA,1985
144
148
  plain/views/templates.py,sha256=kMcHKkKNvucF91SFGkaq-ugjrCwn4zJBpFV1JkwA544,2027
145
- plain-0.38.0.dist-info/METADATA,sha256=UXCQr1j9RRhGH4fDnS65T0j-DSZIGHOVjt8XeWq5GT0,4297
146
- plain-0.38.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
147
- plain-0.38.0.dist-info/entry_points.txt,sha256=1Ys2lsSeMepD1vz8RSrJopna0RQfUd951vYvCRsvl6A,45
148
- plain-0.38.0.dist-info/licenses/LICENSE,sha256=m0D5O7QoH9l5Vz_rrX_9r-C8d9UNr_ciK6Qwac7o6yo,3175
149
- plain-0.38.0.dist-info/RECORD,,
149
+ plain-0.39.1.dist-info/METADATA,sha256=e8tEYf656PoEDJU624eCgDYMAT9_Jc1ewukJa45-_7g,4297
150
+ plain-0.39.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
151
+ plain-0.39.1.dist-info/entry_points.txt,sha256=1Ys2lsSeMepD1vz8RSrJopna0RQfUd951vYvCRsvl6A,45
152
+ plain-0.39.1.dist-info/licenses/LICENSE,sha256=m0D5O7QoH9l5Vz_rrX_9r-C8d9UNr_ciK6Qwac7o6yo,3175
153
+ plain-0.39.1.dist-info/RECORD,,
File without changes