Importing blueprints can get messy fast if you have too many. Yet blueprints are a great way to build something and having many of them can happen quickly. So, let's clean that up.
So, welcome to the blueprint manager. This script/class allows you to directly let all blueprints within a certain directory to be added.
Using this is quite easy.
from quart import Quart
from manager.blueprintmanager import BlueprintManager
app = Quart(__name__)
BlueprintManager(app)
if __name__ == "__main__":
app.run()We can import the class using:
from manager.blueprintmanager import BlueprintManagerNow we need to create an instance of the BlueprintManager
bpm = BlueprintManager(app=app, default_dir='blueprints', debugging = True)| Parameter | Description | Default |
|---|---|---|
app |
Instance of Quart() |
Required |
default_dir |
Directory where blueprints are stored | "blueprints" |
debugging |
Enables detailed logging | False |
Once the instance is created all blueprints inside the directory are automatically registered. No additional configuration or setup required
If the blueprints are not loaded, it is helpful to understand how the Blueprint Manager works internally.
Continue with section 1.2 for more inforamtion on how it works. For error fixing go to section 1.4
The most important thing is always to know how something works. So, here is a quick overview.
- The blueprint directory is checked
- If the directory does not exist, it will be created
- The manager scans all
.pyfiles inside the directory - Blueprint instances are detected
- All detected blueprints are registered to the Quart app
If no default_dir is provided, the manager uses
blueprints/
If the folder already exists, it will not be recreated.
The BlueprintManager includes a failsafe system.
It scans Python files and searches for Blueprint instances. If a file inside the blueprint directory does not contain a blueprint, it will simply be ignored.
This means you can safely place:
- helper scripts
- utilities
- configuration files
- documentation files inside the blueprint directory without breaking anything.
Only valid Blueprint objects will be registered.
Debugging mode helps identify problems during blueprint discovery and registration.
When debugging is enabled, the manager prints structured log messages with error codes.
This makes it easier to:
- detect missing blueprints
- find import errors
- locate parsing issues
- understand failures
- report bugs
| Code | Meaning | Error |
|---|---|---|
| [AA00] | The directory was not found. Creating a new one | |
| [AB01] | Somehow creating a new directory failed. | ✔ |
| [AC02] | Tried to fetch all blueprints as a list. An error occured | ✔ |
| [AD03] | The given app-value is not an instance of the class Quart |
✔ |
| [AE04] | There were no blueprints in the folder | |
| [AF05] | There has been an error importing one blueprint | ✔ |
| [AH07] | An unknown error occured whilst trying to apply the blueprints | ✔ |
| [AI08] | Somehow we extracted an instance that is not a blueprint, failsave prevented issues | |
| [AJ09] | An error occured whilst trying to parse a certain file | ✔ |
| [AK10] | Registering one blueprint was successful |
If an error occurs:
- copy the full log message
- include the error code
- include the stack trace
- open an issue in the repository
This helps identify and fix problems quickly.
So, it is important to look at your folder structure. For the setup to work it should look like this:
project/
│
├── app.py
├── manager/
│ └── blueprintmanager.py
│
└── blueprints/
├── bp_api.py
├── bp_auth.py
└── bp_smth.py
The name of your blueprint-files is not really important. There are some rules to follow though:
- Do not use spaces: The name
blueprint super good.pyis not allowed and will result in import issues. - Do not use '-': If the name is
blueprint-super-good.pyit will too result in import issues. - Do not use special characters: Why ever you should feel the urge to name the file
blueprint?super#good.pyit will too result in import issues.
A recommended naming convention is:
bp_[role].pywhere role describes the functionality of the blueprint.
blueprints/
├── bp_api.py
├── bp_auth.py
├── bp_projects.py
├── bp_chat.py
└── bp_user.py