|
| 1 | + |
| 2 | +=================== |
| 3 | +Create your backend |
| 4 | +=================== |
| 5 | + |
| 6 | +.. include:: ../_github.rst |
| 7 | + |
| 8 | +Every backend is based on the common ground of some elements provided by the |
| 9 | +netjsonconfig library. The `BaseBackend`, `BaseConverter`, `BaseParser` and |
| 10 | +`BaseRenderer` are a battle proven set of tools that can be extended when |
| 11 | +creating you backend. |
| 12 | + |
| 13 | +But the netjsonconfig package is not a playground to experiment, your contributions |
| 14 | +to a new backend should start elsewhere, a different package, where you are in control |
| 15 | +and can make errors and experiment more. |
| 16 | + |
| 17 | +Netjsonconfig can now discover packages that provides a custom backend using |
| 18 | +a feature available in the Python packaging ecosystem which is called `entry_points`. |
| 19 | + |
| 20 | +To create a new backend start from scratch with a new folder and add this file to your |
| 21 | +project root directory. |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + # setup.py |
| 26 | + from setuptools import setup, find_packages |
| 27 | + |
| 28 | + setup( |
| 29 | + name='example_backend', |
| 30 | + version='0.0.0', |
| 31 | + description='an example to illustrate a netjsonconfig backend as an external module', |
| 32 | + packages=find_packages(), |
| 33 | + entry_points={ |
| 34 | + 'netjsonconfig.backends': [ |
| 35 | + 'example=example_backend.__init__:ExampleBackend', |
| 36 | + ] |
| 37 | + } |
| 38 | + ) |
| 39 | +
|
| 40 | +this file can be used to create a package that can be installed using pip or other tools |
| 41 | +in the python ecosystem. You can find more information about Python packaging |
| 42 | +`at packaging.python.org <https://packaging.python.org/>`_ |
| 43 | +and `at the hitchhikers guide to packaging <https://the-hitchhikers-guide-to-packaging.readthedocs.io/en/latest/>`_. |
| 44 | + |
| 45 | +The most important part is to give your package a good name, a well thought description and |
| 46 | +to add the `entry_points` keyword argument with the following code |
| 47 | + |
| 48 | +.. code-block:: python |
| 49 | +
|
| 50 | + { |
| 51 | + # this is used by netjsonconfig |
| 52 | + # to find your backend |
| 53 | + 'netjsonconfig.backends': [ |
| 54 | + ... |
| 55 | + ] |
| 56 | + } |
| 57 | +
|
| 58 | +Now your package will be in the list of backends that netjsonconfig can use! |
| 59 | + |
| 60 | +But we still have to give us a name to be unique! Netjsonconfig already |
| 61 | +defined the names `openwisp`, `openwrt` and `openvpn` but you can choose |
| 62 | +whatever you like most. |
| 63 | + |
| 64 | +The name `netjsonconfig.backends` will be associated with a list of classes |
| 65 | +from your package that will be presented to netjconfig at runtime. To specify |
| 66 | +which classes you want to expose write the triple `name`, `path` and `class_name` |
| 67 | +using the format `name=path:class_name` as in the example below. |
| 68 | + |
| 69 | +The `path` part is simply the path to the file that contains the class |
| 70 | +you want to expose and the `class_name` is the name of the class. |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | + { |
| 75 | + 'netjsonconfig.backends': [ |
| 76 | + # name=path:class_name |
| 77 | + 'example=example_backend.__init__:ExampleBackend', |
| 78 | + ] |
| 79 | + } |
| 80 | +
|
| 81 | +The previous example can be used with the following class definition |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + # example_backend/__init__.py |
| 86 | + from netjsonconfig.backends.base.backend import BaseBackend |
| 87 | + from netjsonconfig.backends.base.renderer import BaseRenderer |
| 88 | + from netjsonconfig.backends.base.parser import BaseParser |
| 89 | + |
| 90 | + from netjsonconfig.schema import schema as default_schema |
| 91 | + |
| 92 | + class ExampleBackend(BaseBackend): |
| 93 | + schema = default_schema |
| 94 | + converter = [] |
| 95 | + parser = BaseParser |
| 96 | + renderer = BaseRenderer |
0 commit comments