|
| 1 | +# Settings in production |
| 2 | + |
| 3 | +To pass production settings to your component, you have two options: |
| 4 | + |
| 5 | +- Use the **%settings** parameter |
| 6 | +- Create your custom settings |
| 7 | + |
| 8 | +## Context |
| 9 | + |
| 10 | +In production when you select a component, you can configure it by passing settings. |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +Those settings can be passed to your python code. |
| 15 | + |
| 16 | +## Use the %settings parameter |
| 17 | + |
| 18 | +All the settings passed to **%settings** are available in string format into your class as a root attribute. |
| 19 | + |
| 20 | +Each line of the **%settings** parameter is a key-value pair separated by a the equal sign. |
| 21 | + |
| 22 | +Key will be the name of the attribute and value will be the value of the attribute. |
| 23 | + |
| 24 | +For example, if you have the following settings: |
| 25 | + |
| 26 | +```text |
| 27 | +foo=bar |
| 28 | +my_number=42 |
| 29 | +``` |
| 30 | + |
| 31 | +You can access those settings in your class like this: |
| 32 | + |
| 33 | +```python |
| 34 | +from iop import BusinessOperation |
| 35 | + |
| 36 | +class MyBusinessOperation(BusinessOperation): |
| 37 | + |
| 38 | + def on_init(self): |
| 39 | + self.log_info("[Python] MyBusinessOperation:on_init() is called") |
| 40 | + self.log_info("[Python] foo: " + self.foo) |
| 41 | + self.log_info("[Python] my_number: " + self.my_number) |
| 42 | + return |
| 43 | +``` |
| 44 | + |
| 45 | +As **%settings** is a free text field, you can pass any settings you want. |
| 46 | + |
| 47 | +Meaning you should verify if the attribute exists before using it. |
| 48 | + |
| 49 | +```python |
| 50 | +from iop import BusinessOperation |
| 51 | + |
| 52 | +class MyBusinessOperation(BusinessOperation): |
| 53 | + |
| 54 | + def on_init(self): |
| 55 | + self.log_info("[Python] MyBusinessOperation:on_init() is called") |
| 56 | + if hasattr(self, 'foo'): |
| 57 | + self.log_info("[Python] foo: " + self.foo) |
| 58 | + if hasattr(self, 'my_number'): |
| 59 | + self.log_info("[Python] my_number: " + self.my_number) |
| 60 | + return |
| 61 | +``` |
| 62 | + |
| 63 | +## Create your custom settings |
| 64 | + |
| 65 | +If you want to have a more structured way to pass settings, you can create your custom settings. |
| 66 | + |
| 67 | +To create a custom settings, you create an attribute in your class. |
| 68 | + |
| 69 | +This attribute must : |
| 70 | + |
| 71 | +- have an default value. |
| 72 | +- don't start with an underscore. |
| 73 | +- be untyped or have the following types: `str`, `int`, `float`, `bool`. |
| 74 | + |
| 75 | +Otherwise, it will not be available in the managment portal. |
| 76 | + |
| 77 | +```python |
| 78 | +from iop import BusinessOperation |
| 79 | + |
| 80 | +class MyBusinessOperation(BusinessOperation): |
| 81 | + |
| 82 | + # This setting will be available in the managment portal |
| 83 | + foo: str = "default" |
| 84 | + my_number: int = 42 |
| 85 | + untyped_setting = None |
| 86 | + |
| 87 | + # This setting will not be available in the managment portal |
| 88 | + _my_internal_setting: str = "default" |
| 89 | + no_aviable_setting |
| 90 | + |
| 91 | + def on_init(self): |
| 92 | + self.log_info("[Python] MyBusinessOperation:on_init() is called") |
| 93 | + self.log_info("[Python] foo: " + self.foo) |
| 94 | + self.log_info("[Python] my_number: " + str(self.my_number)) |
| 95 | + return |
| 96 | +``` |
| 97 | + |
| 98 | +They will be available in the managment portal as the following: |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +If you overwrite the default value in the managment portal, the new value will be passed to your class. |
0 commit comments