-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# Writing a `LibraryPlugin` | ||
|
||
We'll go step by step through all the files you need to create and what they'll do. | ||
Writing a `LibraryPlugin` differs significantly from implementing any of the other plugin types. It requires the implementation of all functions defined by the `NendoLibraryPlugin`, which can be found in the [API Reference](https://okio.ai/docs/reference/schema/plugin/#nendo.schema.plugin.NendoLibraryPlugin). The following is a preliminary introduction and will be heavily extended in the future. | ||
|
||
Remember the directory structure from before: | ||
The plugin's directory structure is exactly the same as with any other kind of plugin: | ||
|
||
```shell | ||
├── README.md | ||
|
@@ -15,19 +15,37 @@ Remember the directory structure from before: | |
│ └── plugin.py | ||
``` | ||
|
||
Let's look into the different files now. | ||
|
||
## Files | ||
|
||
### plugin.py | ||
|
||
The most important thing in your plugin. Here is where all your magic happens. | ||
Below, you'll find a simple implementation that just implements the library initializationa method. | ||
We'll go through it step by step. | ||
Below, you'll find a simple implementation that just implements the library initialization method and the `play()` method. | ||
|
||
- To implement a new library plugin, you have two options: | ||
|
||
1. Inherit from the `SqlAlchemyNendoLibrary`, if your taget DBMS is compatible with SQLAlchemy, i.e. an SQLAlchemy driver exists for it. In this case, you only have to implement the initialization of the library as shown above. | ||
1. Inherit from the `NendoLibraryPlugin`, and implement a general library plugin that does not use SQLAlchemy to connect to the DBMS backend. In this case, you have to implement/override all public methods defined in the `NendoLibraryPlugin`. Refer to the [API Reference](https://okio.ai/docs/reference/schema/plugin/#nendo.schema.plugin.NendoLibraryPlugin) to see the full list of functions that have to be implemented. | ||
|
||
Below, we show how to implement a nendo library that uses the first approach, inheriting from the `SqlAlchemyNendoLibrary` plugin and implementing a `MongoDB` backend. | ||
|
||
```python | ||
from logging import Logger | ||
from nendo import Nendo, NendoConfig, SqlAlchemyNendoLibrary, NendoStorageLocalFS, NendoTrack | ||
from nendo import ( | ||
NendoConfig, | ||
NendoStorage, | ||
NendoStorageLocalFS, | ||
NendoUser, | ||
SqlAlchemyNendoLibrary, | ||
) | ||
from sqlalchemy import Engine, MetaData, create_engine | ||
from sqlalchemy.orm import declarative_base | ||
|
||
from .config import MongoDBConfig | ||
|
||
plugin_package = metadata.metadata(__package__ or __name__) | ||
plugin_config = MongoDBConfig() | ||
Base = declarative_base(metadata=MetaData()) | ||
logger = logging.getLogger("nendo") | ||
|
||
|
||
class MongoDBLibrary(SqlAlchemyNendoLibrary): | ||
|
@@ -71,41 +89,27 @@ class MongoDBLibrary(SqlAlchemyNendoLibrary): | |
Base.metadata.create_all(bind=self.db) | ||
self.user = self.default_user | ||
return None | ||
|
||
def play(self, track: schema.NendoTrack) -> None: | ||
"""Preview an audio track on mac & linux. | ||
Args: | ||
track (NendoTrack): The track to play. | ||
""" | ||
play_signal(track.signal, track.sr) | ||
|
||
``` | ||
|
||
The basics are very simple: | ||
|
||
- To implement a new library plugin, you have two options:a | ||
1. Inherit from the `SqlAlchemyNendoLibrary`, if your taget DBMS is compatible with SQLAlchemy, i.e. an SQLAlchemy driver exists for it. In this case, you only have to implement the initialization of the library as shown bove. | ||
1. Inherit from the `NendoLibraryPlugin`, and implement a general library plugin that does not use SQLAlchemy to connect to the DBMS backend. In this case, you have to implement/override all public methods defined in the `NendoLibraryPlugin`. Refer to the [API Reference](https://okio.ai/docs/reference/schema/plugin/#nendo.schema.plugin.NendoLibraryPlugin) to see the full list of functions that have to be implemented.a | ||
- Use nendo's `NendoStorageLocalFS` storage driver or implement your own. | ||
- Overwrite any methods whose behavior you want to change | ||
- Make sure that your implementation of the nendo library passes the library tests defined in `tests/test_library.py`. | ||
- Use nendo's `NendoStorageLocalFS` storage driver or implement your own. Refer to the [API reference](https://okio.ai/docs/reference/schema/core/#nendo.schema.core.NendoStorage) to see which methods you need to implement for a `StorageDriver` to work. | ||
- Overwrite any methods whose behavior you want to change but make sure that your implementation of the nendo library passes the library tests defined in `tests/test_library.py`. | ||
|
||
### config.py | ||
|
||
```python | ||
from nendo import NendoConfig | ||
|
||
|
||
class MongoDBLibraryConfig(NendoConfig): | ||
class MongoDBConfig(NendoConfig): | ||
"""Configuration defaults for the mongodb library plugin.""" | ||
model_config = ConfigDict(arbitrary_types_allowed=True) | ||
|
||
my_default_param: bool = False | ||
``` | ||
|
||
This class extends the base `NendoConfig` and allows you to define some default and overridable parameters for your | ||
plugin. | ||
This class extends the base `NendoConfig` and allows you to define some default and overridable parameters for your plugin. | ||
It behaves just like `NendoConfig`, read up more on basic `nendo` configuration [here](../usage/config.md). | ||
|
||
### setup.py | ||
|
@@ -117,13 +121,13 @@ if __name__ == "__main__": | |
setup( | ||
name="nendo-plugin-library-mongodb", | ||
version="0.1.0", | ||
description="Nendo mongodb librarya plugin", | ||
description="Nendo mongodb library plugin", | ||
author="Felix Lorenz <[email protected]>", | ||
) | ||
``` | ||
|
||
This is a standard `setup.py` file. | ||
You can read up more on how tao configure it [here](https://packaging.python.org/tutorials/packaging-projects/). | ||
You can read up more on how to configure it [here](https://packaging.python.org/tutorials/packaging-projects/). | ||
You just need to define some basics like the name of your plugin, a version number and a description. | ||
|
||
### pyproject.toml | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters