convoke.bases
Decentralized module dependency declaration and initialization
Bases provide a similar concept to Django's AppConfig, and act as a central place for each module to register important things like signal handlers and template context processors, without needing a global central object.
A single HQ acts as the coordinator for a suite of Bases. At runtime,
an application instantiates an HQ, providing a list of dependencies
(dotted strings, similar to Django's INSTALLED_APPS
setting). Each
dependency is a dotted string to a module or package containing a Base
subclass named Main
. Bases may also declare their own dependencies.
This system allows us to avoid module-level code dependencies that depend overly on import order and global state, and allows a better separation of initialization and execution.
convoke.bases.HQ
The HQ is the special root Base.
The HQ is directly instantiated by a client code entrypoint, rather than discovered by the dependency loader.
hq = HQ(config=MyConfig(), dependencies=['foo'])
Source code in src/convoke/bases.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
connect_signal_receiver(signal_class, receiver)
Connect a receiver function to the given Signal subclass.
All connections are local to this HQ instance. Mostly used
internally via the Base.responds(SignalSubclass)
decorator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signal_class |
Type[Signal]
|
The Signal subclass to connect to. |
required |
receiver |
Receiver
|
a Callable that accepts a message of the type defined on the Signal subclass. |
required |
Source code in src/convoke/bases.py
90 91 92 93 94 95 96 97 98 99 100 101 |
|
disconnect_signal_receiver(signal_class, receiver)
Disconnect a receiver function previously connected to the given Signal subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signal_class |
Type[Signal]
|
The Signal subclass to disconnect from. |
required |
receiver |
Receiver
|
a previously-connected Callable. |
required |
Source code in src/convoke/bases.py
103 104 105 106 107 108 109 |
|
get_current()
classmethod
Return the instance of HQ for the current context.
Source code in src/convoke/bases.py
63 64 65 66 |
|
load_dependencies(dependencies)
Load peripheral Base dependencies.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dependencies |
Sequence[str]
|
a list of dotted paths to modules/packages that contain a Base subclass named |
required |
Source code in src/convoke/bases.py
79 80 81 82 83 84 85 86 87 88 |
|
reset()
Reset this HQ and its associated Bases.
Primarily, this re-establishes this instance as the current HQ instance, and re-initializes bases.
Source code in src/convoke/bases.py
68 69 70 71 72 73 74 75 76 77 |
|
send_signal(signal_class, msg)
async
Send a Message to all receivers of the given Signal subclass.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signal_class |
Type[Signal]
|
The Signal subclass to send |
required |
msg |
Any
|
An instance of signal_class.Message |
required |
Source code in src/convoke/bases.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
convoke.bases.Base
A Base organizes an app within a Convoke project.
For base discovery, an app should provide a subclass named Main
in the app's primary module:
class Main(Base):
config_class: MyConfig = MyConfig
# Dependencies are dotted paths to modules containing a
# Main subclass:
dependencies = ['foo', 'foo.bar']
Base is similar in concept to Django's AppConfig
.
Source code in src/convoke/bases.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
|
get_current()
classmethod
Return the current instance of this Base for the current context.
Source code in src/convoke/bases.py
233 234 235 236 |
|
on_init()
Subclass-overridable method to call at the end of initialization
Source code in src/convoke/bases.py
225 226 227 |
|
on_ready()
Subclass-overridable method to call after all Bases ready
Source code in src/convoke/bases.py
229 230 231 |
|
ready()
Make the base ready for action.
Source code in src/convoke/bases.py
211 212 213 |
|
reset()
Reset the base, reloading configuration and initialization.
Source code in src/convoke/bases.py
218 219 220 221 222 223 |
|