Skip to main content

Runtime configuration

Project description

thinking-runtime

CI PyPI version

Part of thinking family.

This will configure the very basics of the runtime before you run more code.

The point is to run some configurations before you proceed with anything else - logging anything, running any meaningul code, etc. It is based on dunder config files like __project__ or __log__, where you can configure meta-aspects of your app. This lib will load them and configure the runtime for you.

Those dunder files should not be packaged and should reside in the root of your repo. If you're writing an app, you can provide custom config files in your working directory. In case of libraries, you can have defaults for CI and local development, without impacting consumers configs.

Requires python 3.12. Is properly typed.

Usage

Admittedly, docs need enhancements.

Base abstraction is the BootstrapAction. It represents an action that needs to run when bootstrapping the runtime. It must provide perform(self) -> None method that encapsulates the action itself. It can also have prepare(self) -> None method and expose requirements for dunder config files by implementing requirements(self) -> list[ConfigurationRequirement]. Each requirement is constructed from list of dunder name and required flag (False by default, actions should usually provide default configs and dunder config files should be used for customization).

todo describe disabling actions

todo talk about how you can do anything from any config, but there is only guarantee that config will be loaded before an action that requires it

Entrypoint to the framework is bootstrap() method. It will register initial actions and execute them. It should be called before anything else - notably before any call to logging.getLogger (so that the framework can configure logging first).

This is a subject to change in the future. It's pretty early in the lifecycle of this project. We will retrieve all existing loggers and reconfigure them, at some point, but not at MVP.

For each action the framework will retrieve requirements and iterate over them. For each requirement it will iterate over dunder names and try to import them until import succeeds or it runs out of names. If no import succeeded and required is True, there will be a failure.

First action that runs is SetupBootstrapping. It will try to read any of __bootstrap__, __setup__, __project__, __structure__. That file should use register_action(action: ActionType, *, name: ActionName=None, enabled: bool=True) to register any custom actions. Before the setup action is executed, following actions are already registered.

...but can be disabled; document it.

First non-setup action is RecogniseRuntime. It will analyse what is going on in the runtime to figure out if you're running the app or tests and if there are any facets (think "Spring profiles") enabled. You can configure custom facets with register_facet(facet: RuntimeFacetDefinition) or customize what is the package structure of your code and its tests with project_structure() -> PackagesStructure. Config files used by that actions are the same as for SetupBootstrapping or __runtime__.

Another default step is ConfigureLogging. Use __log__, __logs__, __logging__ to make calls to thinking_runtime.default.logging_config.logging_config: LoggingConfig and configure logging aspects like log format, handlers, etc. There is already a default config that will log to stderr with a sane format, and in case of non-test environment, will als dump logs to a file (with name based on session timestamp).

Sweet part of this setup is that when ConfigureLogging is running (so, loading the dunder configs), there is a guarantee that RecogniseRuntime has already executed, so you can safely make statements like

if current_runtime().mode == RuntimeMode.APP:
    logging_config.handlers.streams["STDOUT"].enabled = False

#or
if current_runtime().mode == RuntimeMode.TEST:
    logging_config.format = "..."

fluent_container and logging configuration

This package allows for creation of lists on steroids, meant to simplify creation of configuration DSLs.

It exposes Container base class that can hold 'Identifiable's (anything that exposes identifier property). It won't allow duplicates (considering only the identifiers) and will only accept the type that you specify (raising, when you try to append something that doesn't match). Besides that it is ordered like a list.

It can also expose views ("closures") that filter out anything that doesn't match view criteria (these usually being "of given type").

Both Container and ...Closures overload append(...) so that you can either pass a concrete instance of Identifiable or simply constructor arguments.

See Handlers for example usage. Its a container of logging handler definition. As the framework supports 3 main types of handlers: StreamHandler (routing logs to any stream, usually stdout or stderr), FileHandler (dumping logs to file or rotating file) or RawHandler (wrapper over a raw logging.Handler and its identifier), Handlers expose 3 properties: streams, files and raw, being closures that match those specific types. Thanks to that you can use:

handlers.streams.append(NamedTextIO("name", some_stream), "DEBUG")
handlers.files.all #or simply handlers.files, since it supports __iter__ 
handlers.raw["specific_id"]

instead of

handlers.append(StreamHandler(NamedTextIO("name", some_stream), "DEBUG"))
[ x for x in handlers if isinstance(FileHandler) ]
{ x.identifier: x for x in handlers if isinstance(RawHandler) }["specific_id"]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thinking_runtime-0.0.3.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

thinking_runtime-0.0.3-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file thinking_runtime-0.0.3.tar.gz.

File metadata

  • Download URL: thinking_runtime-0.0.3.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for thinking_runtime-0.0.3.tar.gz
Algorithm Hash digest
SHA256 8a7252b905089262b5844ecf87728596a8893b0cb0b4efc4ddc889d04a31f76d
MD5 aefa3e769d6c18cbd7068845716c7782
BLAKE2b-256 d03bbb97cca8aece913fa6b77d8872629ba4fc1f77cbda7b90010833703d86a1

See more details on using hashes here.

File details

Details for the file thinking_runtime-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for thinking_runtime-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b156333fda5d70306274077d8cf918dd2eb24e089a66619ac243e7d61be19aaf
MD5 046c2b1171cf54ff6a47e606a37f55ef
BLAKE2b-256 92fae25d226f66accafcdf5f3b8d9a2cfce1fbe40501cb56da005d53096085f4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page