-
Notifications
You must be signed in to change notification settings - Fork 9
WebCore Conventions
-
Use, but optimize away debug level logging.
Debug-level logging statements are encouraged, however, these should be wrapped inif __debug__:
conditions to ensure that the logging statement itself is removed from production code. (When Python is run with the-O
flag or in an environment withPYTHONOPTIMIZE
set.) -
Provide optimizations.
Theif __debug__:
condition should additionally be utilized to mark costly (but optional) validation tests and other diagnostic statements, especially if there are loops involved. A few of the extension callbacks are executed within loops, and will be noted below. -
Provide logging metadata.
Logging statements are encouraged to containextras
dictionary keyword argument. Be careful to avoid accidentally overwriting a standard attribute oflogging.Log
objects. Individual guidelines below may mention specific keys that should be included at different points of execution. The values should be encodable as JSON, or, optionally (with an appropriate logger configured), MongoDB Extended JSON, or some other format as supported. For maximum portability, stick to basic Python built-in types.
-
Does it need to be a WebCore Extension?
There are many hooks provided through other mechanisms, such as object dispatch for request endpoint discovery, the serialization extension to adapt outgoing data transformers like JSON encoders, etc. -
Are you adapting one of a class of tools?
For example, instead of having aSQLiteExtension
and aMongoDBExtension
, there is the database extension providing its own extension interface. If your own efforts involve integrating one specific tool in a class of tools, please consider instead creating an extension exposing an interface that can be provided by several, even if you only initially write an interface for one.
-
All extension classes should end with
Extension
. -
Be expressive, but keep it short. For example,
LocaleExtension
is preferable toLocalizationExtension
. One word is usually sufficient–synonyms are your friend. -
Short abbreviations–two or fewer characters–should be avoided. For example,
DatabaseExtension
is preferable toDBExtension
.
Several metadata attributes are provided to allow execution order to be controlled through dependency graphing, as well as to advertise features the extension implements to the application, and provide introspective documentation. You can see these documented in the extension example doc-strings.
-
Mean what you say.
Do not define a metadata attribute if you wish to omit it, i.e. do not assignNone
, empty lists or tuples, etc. -
Sets are awesome.
Most list-like attributes (such asuses
, orprovides
) should be defined as sets, typically as set literals. -
Metadata is configurable.
The only time it is permitted to modify an extension's metadata is within its constructor. For example, it is perfectly permissible to customize your extension'suses
orneeds
(evenprovides
) in response to specific configuration.
-
__call__
– Wrapping WSGI middleware.-
Avoid WSGI middleware where possible.
There are many fine-grained hooks provided that replicate the "do work before", "capture result", and "do work after" pattern offered by middleware. There are two occasions to utilize middleware: to adapt an awesome existing package that just must be middleware, or when you must capture bubbled exceptions including their stack trace. There are better ways to "just detect an error" when using extension callbacks.
-
Avoid WSGI middleware where possible.