|
| 1 | +# Copyright (c) Quectel Wireless Solution, Co., Ltd.All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +@file : routing.py |
| 17 | +@author : Jack Sun (jack.sun@quectel.com) |
| 18 | +@brief : <Description> |
| 19 | +@version : v1.0.0 |
| 20 | +@date : 2024-04-21 14:23:03 |
| 21 | +@copyright : Copyright (c) 2024 |
| 22 | +""" |
| 23 | + |
| 24 | +routables = [] |
| 25 | + |
| 26 | + |
| 27 | +class InnerBase: |
| 28 | + |
| 29 | + def __init__(self, func): |
| 30 | + self.func = func |
| 31 | + self.func_name = repr(self.func).split(" ")[1] |
| 32 | + self.parent = None |
| 33 | + |
| 34 | + def __call__(self, *args, **kwargs): |
| 35 | + return self.func(self.parent, *args, **kwargs) |
| 36 | + |
| 37 | + |
| 38 | +def on(action, skip_schema_validation=False, call_unique_id_required=False): |
| 39 | + """ |
| 40 | + Function decorator to mark function as handler for specific action. The |
| 41 | + wrapped function may be async or sync. |
| 42 | +
|
| 43 | + The handler function will receive keyword arguments derived from the |
| 44 | + payload of the specific action. It's recommended you use `**kwargs` in your |
| 45 | + definition to ignore any extra arguments that may be added in the future. |
| 46 | +
|
| 47 | + The handler function should return a relevant payload to be returned to the |
| 48 | + Charge Point. |
| 49 | +
|
| 50 | + It can be used like so: |
| 51 | +
|
| 52 | + ``` |
| 53 | + class MyChargePoint(cp): |
| 54 | + @on(Action.BootNotification): |
| 55 | + async def on_boot_notification( |
| 56 | + self, |
| 57 | + charge_point_model, |
| 58 | + charge_point_vendor, |
| 59 | + **kwargs, |
| 60 | + ): |
| 61 | + print(f'{charge_point_model} from {charge_point_vendor} booted.') |
| 62 | +
|
| 63 | + now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S') + "Z" |
| 64 | + return call_result.BootNotificationPayload( |
| 65 | + current_time=now, |
| 66 | + interval=30, |
| 67 | + status="Accepted", |
| 68 | + ) |
| 69 | + ``` |
| 70 | +
|
| 71 | + The decorator takes an optional argument `skip_schema_validation` which |
| 72 | + defaults to False. Setting this argument to `True` will disable schema |
| 73 | + validation of the request and the response of the specific route. |
| 74 | +
|
| 75 | + """ |
| 76 | + |
| 77 | + def decorator(func): |
| 78 | + inner = InnerBase(func) |
| 79 | + inner._on_action = action |
| 80 | + inner._skip_schema_validation = skip_schema_validation |
| 81 | + inner._call_unique_id_required = call_unique_id_required |
| 82 | + |
| 83 | + if inner.func_name not in routables: |
| 84 | + routables.append(inner.func_name) |
| 85 | + return inner |
| 86 | + |
| 87 | + return decorator |
| 88 | + |
| 89 | + |
| 90 | +def after(action, call_unique_id_required=False): |
| 91 | + """Function decorator to mark function as hook to post-request hook. |
| 92 | +
|
| 93 | + This hook's arguments are the data that is in the payload for the specific |
| 94 | + action. |
| 95 | +
|
| 96 | + It can be used like so: |
| 97 | +
|
| 98 | + @after(Action.BootNotification): |
| 99 | + def after_boot_notification(): |
| 100 | + pass |
| 101 | +
|
| 102 | + """ |
| 103 | + |
| 104 | + def decorator(func): |
| 105 | + inner = InnerBase(func) |
| 106 | + inner._after_action = action |
| 107 | + inner._call_unique_id_required = call_unique_id_required |
| 108 | + if inner.func_name not in routables: |
| 109 | + routables.append(inner.func_name) |
| 110 | + return inner |
| 111 | + |
| 112 | + return decorator |
| 113 | + |
| 114 | + |
| 115 | +def create_route_map(obj): |
| 116 | + """ |
| 117 | + Iterates of all attributes of the class looking for attributes which |
| 118 | + have been decorated by the @on() decorator It returns a dictionary where |
| 119 | + the action name are the keys and the decorated functions are the values. |
| 120 | +
|
| 121 | + To illustrate this with an example, consider the following function: |
| 122 | +
|
| 123 | + class ChargePoint: |
| 124 | +
|
| 125 | + @on(Action.BootNotification) |
| 126 | + def on_boot_notification(self, *args, **kwargs): |
| 127 | + pass |
| 128 | +
|
| 129 | + @after(Action.BootNotification) |
| 130 | + def after_boot_notification(self, *args, **kwargs): |
| 131 | + pass |
| 132 | +
|
| 133 | +
|
| 134 | + In this case this returns: |
| 135 | +
|
| 136 | + { |
| 137 | + Action.BootNotification: { |
| 138 | + '_on_action': <reference to 'on_boot_notification'>, |
| 139 | + '_after_action': <reference to 'after_boot_notification'>, |
| 140 | + '_skip_schema_validation': False, |
| 141 | + }, |
| 142 | + } |
| 143 | +
|
| 144 | + """ |
| 145 | + routes = {} |
| 146 | + |
| 147 | + for attr_name in routables: |
| 148 | + for option in ["_on_action", "_after_action"]: |
| 149 | + try: |
| 150 | + attr = getattr(obj, attr_name) |
| 151 | + action = getattr(attr, option) |
| 152 | + attr.parent = obj |
| 153 | + |
| 154 | + if action not in routes: |
| 155 | + routes[action] = {} |
| 156 | + |
| 157 | + # Routes decorated with the `@on()` decorator can be configured |
| 158 | + # to skip validation of the input and output. For more info see |
| 159 | + # the docstring of `on()`. |
| 160 | + if option == "_on_action": |
| 161 | + routes[action]["_skip_schema_validation"] = getattr( |
| 162 | + attr, "_skip_schema_validation", False |
| 163 | + ) |
| 164 | + |
| 165 | + routes[action][option] = attr |
| 166 | + |
| 167 | + except AttributeError: |
| 168 | + continue |
| 169 | + |
| 170 | + return routes |
0 commit comments