plain 0.1.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.
Files changed (169) hide show
  1. plain/README.md +33 -0
  2. plain/__main__.py +5 -0
  3. plain/assets/README.md +56 -0
  4. plain/assets/__init__.py +6 -0
  5. plain/assets/finders.py +233 -0
  6. plain/assets/preflight.py +14 -0
  7. plain/assets/storage.py +916 -0
  8. plain/assets/utils.py +52 -0
  9. plain/assets/whitenoise/__init__.py +5 -0
  10. plain/assets/whitenoise/base.py +259 -0
  11. plain/assets/whitenoise/compress.py +189 -0
  12. plain/assets/whitenoise/media_types.py +137 -0
  13. plain/assets/whitenoise/middleware.py +197 -0
  14. plain/assets/whitenoise/responders.py +286 -0
  15. plain/assets/whitenoise/storage.py +178 -0
  16. plain/assets/whitenoise/string_utils.py +13 -0
  17. plain/cli/README.md +123 -0
  18. plain/cli/__init__.py +3 -0
  19. plain/cli/cli.py +439 -0
  20. plain/cli/formatting.py +61 -0
  21. plain/cli/packages.py +73 -0
  22. plain/cli/print.py +9 -0
  23. plain/cli/startup.py +33 -0
  24. plain/csrf/README.md +3 -0
  25. plain/csrf/middleware.py +466 -0
  26. plain/csrf/views.py +10 -0
  27. plain/debug.py +23 -0
  28. plain/exceptions.py +242 -0
  29. plain/forms/README.md +14 -0
  30. plain/forms/__init__.py +8 -0
  31. plain/forms/boundfield.py +58 -0
  32. plain/forms/exceptions.py +11 -0
  33. plain/forms/fields.py +1030 -0
  34. plain/forms/forms.py +297 -0
  35. plain/http/README.md +1 -0
  36. plain/http/__init__.py +51 -0
  37. plain/http/cookie.py +20 -0
  38. plain/http/multipartparser.py +743 -0
  39. plain/http/request.py +754 -0
  40. plain/http/response.py +719 -0
  41. plain/internal/__init__.py +0 -0
  42. plain/internal/files/README.md +3 -0
  43. plain/internal/files/__init__.py +3 -0
  44. plain/internal/files/base.py +161 -0
  45. plain/internal/files/locks.py +127 -0
  46. plain/internal/files/move.py +102 -0
  47. plain/internal/files/temp.py +79 -0
  48. plain/internal/files/uploadedfile.py +150 -0
  49. plain/internal/files/uploadhandler.py +254 -0
  50. plain/internal/files/utils.py +78 -0
  51. plain/internal/handlers/__init__.py +0 -0
  52. plain/internal/handlers/base.py +133 -0
  53. plain/internal/handlers/exception.py +145 -0
  54. plain/internal/handlers/wsgi.py +216 -0
  55. plain/internal/legacy/__init__.py +0 -0
  56. plain/internal/legacy/__main__.py +12 -0
  57. plain/internal/legacy/management/__init__.py +414 -0
  58. plain/internal/legacy/management/base.py +692 -0
  59. plain/internal/legacy/management/color.py +113 -0
  60. plain/internal/legacy/management/commands/__init__.py +0 -0
  61. plain/internal/legacy/management/commands/collectstatic.py +297 -0
  62. plain/internal/legacy/management/sql.py +67 -0
  63. plain/internal/legacy/management/utils.py +175 -0
  64. plain/json.py +40 -0
  65. plain/logs/README.md +24 -0
  66. plain/logs/__init__.py +5 -0
  67. plain/logs/configure.py +39 -0
  68. plain/logs/loggers.py +74 -0
  69. plain/logs/utils.py +46 -0
  70. plain/middleware/README.md +3 -0
  71. plain/middleware/__init__.py +0 -0
  72. plain/middleware/clickjacking.py +52 -0
  73. plain/middleware/common.py +87 -0
  74. plain/middleware/gzip.py +64 -0
  75. plain/middleware/security.py +64 -0
  76. plain/packages/README.md +41 -0
  77. plain/packages/__init__.py +4 -0
  78. plain/packages/config.py +259 -0
  79. plain/packages/registry.py +438 -0
  80. plain/paginator.py +187 -0
  81. plain/preflight/README.md +3 -0
  82. plain/preflight/__init__.py +38 -0
  83. plain/preflight/compatibility/__init__.py +0 -0
  84. plain/preflight/compatibility/django_4_0.py +20 -0
  85. plain/preflight/files.py +19 -0
  86. plain/preflight/messages.py +88 -0
  87. plain/preflight/registry.py +72 -0
  88. plain/preflight/security/__init__.py +0 -0
  89. plain/preflight/security/base.py +268 -0
  90. plain/preflight/security/csrf.py +40 -0
  91. plain/preflight/urls.py +117 -0
  92. plain/runtime/README.md +75 -0
  93. plain/runtime/__init__.py +61 -0
  94. plain/runtime/global_settings.py +199 -0
  95. plain/runtime/user_settings.py +353 -0
  96. plain/signals/README.md +14 -0
  97. plain/signals/__init__.py +5 -0
  98. plain/signals/dispatch/__init__.py +9 -0
  99. plain/signals/dispatch/dispatcher.py +320 -0
  100. plain/signals/dispatch/license.txt +35 -0
  101. plain/signing.py +299 -0
  102. plain/templates/README.md +20 -0
  103. plain/templates/__init__.py +6 -0
  104. plain/templates/core.py +24 -0
  105. plain/templates/jinja/README.md +227 -0
  106. plain/templates/jinja/__init__.py +22 -0
  107. plain/templates/jinja/defaults.py +119 -0
  108. plain/templates/jinja/extensions.py +39 -0
  109. plain/templates/jinja/filters.py +28 -0
  110. plain/templates/jinja/globals.py +19 -0
  111. plain/test/README.md +3 -0
  112. plain/test/__init__.py +16 -0
  113. plain/test/client.py +985 -0
  114. plain/test/utils.py +255 -0
  115. plain/urls/README.md +3 -0
  116. plain/urls/__init__.py +40 -0
  117. plain/urls/base.py +118 -0
  118. plain/urls/conf.py +94 -0
  119. plain/urls/converters.py +66 -0
  120. plain/urls/exceptions.py +9 -0
  121. plain/urls/resolvers.py +731 -0
  122. plain/utils/README.md +3 -0
  123. plain/utils/__init__.py +0 -0
  124. plain/utils/_os.py +52 -0
  125. plain/utils/cache.py +327 -0
  126. plain/utils/connection.py +84 -0
  127. plain/utils/crypto.py +76 -0
  128. plain/utils/datastructures.py +345 -0
  129. plain/utils/dateformat.py +329 -0
  130. plain/utils/dateparse.py +154 -0
  131. plain/utils/dates.py +76 -0
  132. plain/utils/deconstruct.py +54 -0
  133. plain/utils/decorators.py +90 -0
  134. plain/utils/deprecation.py +6 -0
  135. plain/utils/duration.py +44 -0
  136. plain/utils/email.py +12 -0
  137. plain/utils/encoding.py +235 -0
  138. plain/utils/functional.py +456 -0
  139. plain/utils/hashable.py +26 -0
  140. plain/utils/html.py +401 -0
  141. plain/utils/http.py +374 -0
  142. plain/utils/inspect.py +73 -0
  143. plain/utils/ipv6.py +46 -0
  144. plain/utils/itercompat.py +8 -0
  145. plain/utils/module_loading.py +69 -0
  146. plain/utils/regex_helper.py +353 -0
  147. plain/utils/safestring.py +72 -0
  148. plain/utils/termcolors.py +221 -0
  149. plain/utils/text.py +518 -0
  150. plain/utils/timesince.py +138 -0
  151. plain/utils/timezone.py +244 -0
  152. plain/utils/tree.py +126 -0
  153. plain/validators.py +603 -0
  154. plain/views/README.md +268 -0
  155. plain/views/__init__.py +18 -0
  156. plain/views/base.py +107 -0
  157. plain/views/csrf.py +24 -0
  158. plain/views/errors.py +25 -0
  159. plain/views/exceptions.py +4 -0
  160. plain/views/forms.py +76 -0
  161. plain/views/objects.py +229 -0
  162. plain/views/redirect.py +72 -0
  163. plain/views/templates.py +66 -0
  164. plain/wsgi.py +11 -0
  165. plain-0.1.0.dist-info/LICENSE +85 -0
  166. plain-0.1.0.dist-info/METADATA +51 -0
  167. plain-0.1.0.dist-info/RECORD +169 -0
  168. plain-0.1.0.dist-info/WHEEL +4 -0
  169. plain-0.1.0.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,244 @@
1
+ """
2
+ Timezone-related classes and functions.
3
+ """
4
+
5
+ import functools
6
+ import zoneinfo
7
+ from contextlib import ContextDecorator
8
+ from datetime import datetime, timedelta, timezone, tzinfo
9
+ from threading import local
10
+
11
+ from plain.runtime import settings
12
+
13
+ __all__ = [
14
+ "get_fixed_timezone",
15
+ "get_default_timezone",
16
+ "get_default_timezone_name",
17
+ "get_current_timezone",
18
+ "get_current_timezone_name",
19
+ "activate",
20
+ "deactivate",
21
+ "override",
22
+ "localtime",
23
+ "now",
24
+ "is_aware",
25
+ "is_naive",
26
+ "make_aware",
27
+ "make_naive",
28
+ ]
29
+
30
+
31
+ def get_fixed_timezone(offset):
32
+ """Return a tzinfo instance with a fixed offset from UTC."""
33
+ if isinstance(offset, timedelta):
34
+ offset = offset.total_seconds() // 60
35
+ sign = "-" if offset < 0 else "+"
36
+ hhmm = "%02d%02d" % divmod(abs(offset), 60)
37
+ name = sign + hhmm
38
+ return timezone(timedelta(minutes=offset), name)
39
+
40
+
41
+ # In order to avoid accessing settings at compile time,
42
+ # wrap the logic in a function and cache the result.
43
+ @functools.lru_cache
44
+ def get_default_timezone():
45
+ """
46
+ Return the default time zone as a tzinfo instance.
47
+
48
+ This is the time zone defined by settings.TIME_ZONE.
49
+ """
50
+ return zoneinfo.ZoneInfo(settings.TIME_ZONE)
51
+
52
+
53
+ # This function exists for consistency with get_current_timezone_name
54
+ def get_default_timezone_name():
55
+ """Return the name of the default time zone."""
56
+ return _get_timezone_name(get_default_timezone())
57
+
58
+
59
+ _active = local()
60
+
61
+
62
+ def get_current_timezone():
63
+ """Return the currently active time zone as a tzinfo instance."""
64
+ return getattr(_active, "value", get_default_timezone())
65
+
66
+
67
+ def get_current_timezone_name():
68
+ """Return the name of the currently active time zone."""
69
+ return _get_timezone_name(get_current_timezone())
70
+
71
+
72
+ def _get_timezone_name(timezone):
73
+ """
74
+ Return the offset for fixed offset timezones, or the name of timezone if
75
+ not set.
76
+ """
77
+ return timezone.tzname(None) or str(timezone)
78
+
79
+
80
+ # Timezone selection functions.
81
+
82
+ # These functions don't change os.environ['TZ'] and call time.tzset()
83
+ # because it isn't thread safe.
84
+
85
+
86
+ def activate(timezone):
87
+ """
88
+ Set the time zone for the current thread.
89
+
90
+ The ``timezone`` argument must be an instance of a tzinfo subclass or a
91
+ time zone name.
92
+ """
93
+ if isinstance(timezone, tzinfo):
94
+ _active.value = timezone
95
+ elif isinstance(timezone, str):
96
+ _active.value = zoneinfo.ZoneInfo(timezone)
97
+ else:
98
+ raise ValueError("Invalid timezone: %r" % timezone)
99
+
100
+
101
+ def deactivate():
102
+ """
103
+ Unset the time zone for the current thread.
104
+
105
+ Plain will then use the time zone defined by settings.TIME_ZONE.
106
+ """
107
+ if hasattr(_active, "value"):
108
+ del _active.value
109
+
110
+
111
+ class override(ContextDecorator):
112
+ """
113
+ Temporarily set the time zone for the current thread.
114
+
115
+ This is a context manager that uses plain.utils.timezone.activate()
116
+ to set the timezone on entry and restores the previously active timezone
117
+ on exit.
118
+
119
+ The ``timezone`` argument must be an instance of a ``tzinfo`` subclass, a
120
+ time zone name, or ``None``. If it is ``None``, Plain enables the default
121
+ time zone.
122
+ """
123
+
124
+ def __init__(self, timezone):
125
+ self.timezone = timezone
126
+
127
+ def __enter__(self):
128
+ self.old_timezone = getattr(_active, "value", None)
129
+ if self.timezone is None:
130
+ deactivate()
131
+ else:
132
+ activate(self.timezone)
133
+
134
+ def __exit__(self, exc_type, exc_value, traceback):
135
+ if self.old_timezone is None:
136
+ deactivate()
137
+ else:
138
+ _active.value = self.old_timezone
139
+
140
+
141
+ # Templates
142
+
143
+
144
+ def template_localtime(value, use_tz=None):
145
+ """
146
+ Check if value is a datetime and converts it to local time if necessary.
147
+
148
+ If use_tz is provided and is not None, that will force the value to
149
+ be converted (or not), overriding the value of settings.USE_TZ.
150
+
151
+ This function is designed for use by the template engine.
152
+ """
153
+ should_convert = (
154
+ isinstance(value, datetime)
155
+ and (settings.USE_TZ if use_tz is None else use_tz)
156
+ and not is_naive(value)
157
+ and getattr(value, "convert_to_local_time", True)
158
+ )
159
+ return localtime(value) if should_convert else value
160
+
161
+
162
+ # Utilities
163
+
164
+
165
+ def localtime(value=None, timezone=None):
166
+ """
167
+ Convert an aware datetime.datetime to local time.
168
+
169
+ Only aware datetimes are allowed. When value is omitted, it defaults to
170
+ now().
171
+
172
+ Local time is defined by the current time zone, unless another time zone
173
+ is specified.
174
+ """
175
+ if value is None:
176
+ value = now()
177
+ if timezone is None:
178
+ timezone = get_current_timezone()
179
+ # Emulate the behavior of astimezone() on Python < 3.6.
180
+ if is_naive(value):
181
+ raise ValueError("localtime() cannot be applied to a naive datetime")
182
+ return value.astimezone(timezone)
183
+
184
+
185
+ def now():
186
+ """
187
+ Return an aware or naive datetime.datetime, depending on settings.USE_TZ.
188
+ """
189
+ return datetime.now(tz=timezone.utc if settings.USE_TZ else None)
190
+
191
+
192
+ # By design, these four functions don't perform any checks on their arguments.
193
+ # The caller should ensure that they don't receive an invalid value like None.
194
+
195
+
196
+ def is_aware(value):
197
+ """
198
+ Determine if a given datetime.datetime is aware.
199
+
200
+ The concept is defined in Python's docs:
201
+ https://docs.python.org/library/datetime.html#datetime.tzinfo
202
+
203
+ Assuming value.tzinfo is either None or a proper datetime.tzinfo,
204
+ value.utcoffset() implements the appropriate logic.
205
+ """
206
+ return value.utcoffset() is not None
207
+
208
+
209
+ def is_naive(value):
210
+ """
211
+ Determine if a given datetime.datetime is naive.
212
+
213
+ The concept is defined in Python's docs:
214
+ https://docs.python.org/library/datetime.html#datetime.tzinfo
215
+
216
+ Assuming value.tzinfo is either None or a proper datetime.tzinfo,
217
+ value.utcoffset() implements the appropriate logic.
218
+ """
219
+ return value.utcoffset() is None
220
+
221
+
222
+ def make_aware(value, timezone=None):
223
+ """Make a naive datetime.datetime in a given time zone aware."""
224
+ if timezone is None:
225
+ timezone = get_current_timezone()
226
+ # Check that we won't overwrite the timezone of an aware datetime.
227
+ if is_aware(value):
228
+ raise ValueError("make_aware expects a naive datetime, got %s" % value)
229
+ # This may be wrong around DST changes!
230
+ return value.replace(tzinfo=timezone)
231
+
232
+
233
+ def make_naive(value, timezone=None):
234
+ """Make an aware datetime.datetime naive in a given time zone."""
235
+ if timezone is None:
236
+ timezone = get_current_timezone()
237
+ # Emulate the behavior of astimezone() on Python < 3.6.
238
+ if is_naive(value):
239
+ raise ValueError("make_naive() cannot be applied to a naive datetime")
240
+ return value.astimezone(timezone).replace(tzinfo=None)
241
+
242
+
243
+ def _datetime_ambiguous_or_imaginary(dt, tz):
244
+ return tz.utcoffset(dt.replace(fold=not dt.fold)) != tz.utcoffset(dt)
plain/utils/tree.py ADDED
@@ -0,0 +1,126 @@
1
+ """
2
+ A class for storing a tree graph. Primarily used for filter constructs in the
3
+ ORM.
4
+ """
5
+
6
+ import copy
7
+
8
+ from plain.utils.hashable import make_hashable
9
+
10
+
11
+ class Node:
12
+ """
13
+ A single internal node in the tree graph. A Node should be viewed as a
14
+ connection (the root) with the children being either leaf nodes or other
15
+ Node instances.
16
+ """
17
+
18
+ # Standard connector type. Clients usually won't use this at all and
19
+ # subclasses will usually override the value.
20
+ default = "DEFAULT"
21
+
22
+ def __init__(self, children=None, connector=None, negated=False):
23
+ """Construct a new Node. If no connector is given, use the default."""
24
+ self.children = children[:] if children else []
25
+ self.connector = connector or self.default
26
+ self.negated = negated
27
+
28
+ @classmethod
29
+ def create(cls, children=None, connector=None, negated=False):
30
+ """
31
+ Create a new instance using Node() instead of __init__() as some
32
+ subclasses, e.g. plain.models.query_utils.Q, may implement a custom
33
+ __init__() with a signature that conflicts with the one defined in
34
+ Node.__init__().
35
+ """
36
+ obj = Node(children, connector or cls.default, negated)
37
+ obj.__class__ = cls
38
+ return obj
39
+
40
+ def __str__(self):
41
+ template = "(NOT (%s: %s))" if self.negated else "(%s: %s)"
42
+ return template % (self.connector, ", ".join(str(c) for c in self.children))
43
+
44
+ def __repr__(self):
45
+ return f"<{self.__class__.__name__}: {self}>"
46
+
47
+ def __copy__(self):
48
+ obj = self.create(connector=self.connector, negated=self.negated)
49
+ obj.children = self.children # Don't [:] as .__init__() via .create() does.
50
+ return obj
51
+
52
+ copy = __copy__
53
+
54
+ def __deepcopy__(self, memodict):
55
+ obj = self.create(connector=self.connector, negated=self.negated)
56
+ obj.children = copy.deepcopy(self.children, memodict)
57
+ return obj
58
+
59
+ def __len__(self):
60
+ """Return the number of children this node has."""
61
+ return len(self.children)
62
+
63
+ def __bool__(self):
64
+ """Return whether or not this node has children."""
65
+ return bool(self.children)
66
+
67
+ def __contains__(self, other):
68
+ """Return True if 'other' is a direct child of this instance."""
69
+ return other in self.children
70
+
71
+ def __eq__(self, other):
72
+ return (
73
+ self.__class__ == other.__class__
74
+ and self.connector == other.connector
75
+ and self.negated == other.negated
76
+ and self.children == other.children
77
+ )
78
+
79
+ def __hash__(self):
80
+ return hash(
81
+ (
82
+ self.__class__,
83
+ self.connector,
84
+ self.negated,
85
+ *make_hashable(self.children),
86
+ )
87
+ )
88
+
89
+ def add(self, data, conn_type):
90
+ """
91
+ Combine this tree and the data represented by data using the
92
+ connector conn_type. The combine is done by squashing the node other
93
+ away if possible.
94
+
95
+ This tree (self) will never be pushed to a child node of the
96
+ combined tree, nor will the connector or negated properties change.
97
+
98
+ Return a node which can be used in place of data regardless if the
99
+ node other got squashed or not.
100
+ """
101
+ if self.connector != conn_type:
102
+ obj = self.copy()
103
+ self.connector = conn_type
104
+ self.children = [obj, data]
105
+ return data
106
+ elif (
107
+ isinstance(data, Node)
108
+ and not data.negated
109
+ and (data.connector == conn_type or len(data) == 1)
110
+ ):
111
+ # We can squash the other node's children directly into this node.
112
+ # We are just doing (AB)(CD) == (ABCD) here, with the addition that
113
+ # if the length of the other node is 1 the connector doesn't
114
+ # matter. However, for the len(self) == 1 case we don't want to do
115
+ # the squashing, as it would alter self.connector.
116
+ self.children.extend(data.children)
117
+ return self
118
+ else:
119
+ # We could use perhaps additional logic here to see if some
120
+ # children could be used for pushdown here.
121
+ self.children.append(data)
122
+ return data
123
+
124
+ def negate(self):
125
+ """Negate the sense of the root connector."""
126
+ self.negated = not self.negated