From d4aa970003b5e5a012dceaa33d1905a170cb461b Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 12 Jun 2024 18:57:47 +0200 Subject: [PATCH] refactor(ocpp-server): format code with black MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- tests/ocpp-server/.flake8 | 2 + tests/ocpp-server/poetry.lock | 183 ++++++++++++++++++++++++++++++- tests/ocpp-server/pyproject.toml | 9 +- tests/ocpp-server/server.py | 67 ++++++----- 4 files changed, 231 insertions(+), 30 deletions(-) create mode 100644 tests/ocpp-server/.flake8 diff --git a/tests/ocpp-server/.flake8 b/tests/ocpp-server/.flake8 new file mode 100644 index 00000000..7da1f960 --- /dev/null +++ b/tests/ocpp-server/.flake8 @@ -0,0 +1,2 @@ +[flake8] +max-line-length = 100 diff --git a/tests/ocpp-server/poetry.lock b/tests/ocpp-server/poetry.lock index 3bcabcc1..a7ff07e3 100644 --- a/tests/ocpp-server/poetry.lock +++ b/tests/ocpp-server/poetry.lock @@ -19,6 +19,105 @@ tests = ["attrs[tests-no-zope]", "zope-interface"] tests-mypy = ["mypy (>=1.6)", "pytest-mypy-plugins"] tests-no-zope = ["attrs[tests-mypy]", "cloudpickle", "hypothesis", "pympler", "pytest (>=4.3.0)", "pytest-xdist[psutil]"] +[[package]] +name = "black" +version = "24.4.2" +description = "The uncompromising code formatter." +optional = false +python-versions = ">=3.8" +files = [ + {file = "black-24.4.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:dd1b5a14e417189db4c7b64a6540f31730713d173f0b63e55fabd52d61d8fdce"}, + {file = "black-24.4.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:8e537d281831ad0e71007dcdcbe50a71470b978c453fa41ce77186bbe0ed6021"}, + {file = "black-24.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:eaea3008c281f1038edb473c1aa8ed8143a5535ff18f978a318f10302b254063"}, + {file = "black-24.4.2-cp310-cp310-win_amd64.whl", hash = "sha256:7768a0dbf16a39aa5e9a3ded568bb545c8c2727396d063bbaf847df05b08cd96"}, + {file = "black-24.4.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:257d724c2c9b1660f353b36c802ccece186a30accc7742c176d29c146df6e474"}, + {file = "black-24.4.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:bdde6f877a18f24844e381d45e9947a49e97933573ac9d4345399be37621e26c"}, + {file = "black-24.4.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e151054aa00bad1f4e1f04919542885f89f5f7d086b8a59e5000e6c616896ffb"}, + {file = "black-24.4.2-cp311-cp311-win_amd64.whl", hash = "sha256:7e122b1c4fb252fd85df3ca93578732b4749d9be076593076ef4d07a0233c3e1"}, + {file = "black-24.4.2-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:accf49e151c8ed2c0cdc528691838afd217c50412534e876a19270fea1e28e2d"}, + {file = "black-24.4.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:88c57dc656038f1ab9f92b3eb5335ee9b021412feaa46330d5eba4e51fe49b04"}, + {file = "black-24.4.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:be8bef99eb46d5021bf053114442914baeb3649a89dc5f3a555c88737e5e98fc"}, + {file = "black-24.4.2-cp312-cp312-win_amd64.whl", hash = "sha256:415e686e87dbbe6f4cd5ef0fbf764af7b89f9057b97c908742b6008cc554b9c0"}, + {file = "black-24.4.2-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:bf10f7310db693bb62692609b397e8d67257c55f949abde4c67f9cc574492cc7"}, + {file = "black-24.4.2-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:98e123f1d5cfd42f886624d84464f7756f60ff6eab89ae845210631714f6db94"}, + {file = "black-24.4.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:48a85f2cb5e6799a9ef05347b476cce6c182d6c71ee36925a6c194d074336ef8"}, + {file = "black-24.4.2-cp38-cp38-win_amd64.whl", hash = "sha256:b1530ae42e9d6d5b670a34db49a94115a64596bc77710b1d05e9801e62ca0a7c"}, + {file = "black-24.4.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:37aae07b029fa0174d39daf02748b379399b909652a806e5708199bd93899da1"}, + {file = "black-24.4.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:da33a1a5e49c4122ccdfd56cd021ff1ebc4a1ec4e2d01594fef9b6f267a9e741"}, + {file = "black-24.4.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ef703f83fc32e131e9bcc0a5094cfe85599e7109f896fe8bc96cc402f3eb4b6e"}, + {file = "black-24.4.2-cp39-cp39-win_amd64.whl", hash = "sha256:b9176b9832e84308818a99a561e90aa479e73c523b3f77afd07913380ae2eab7"}, + {file = "black-24.4.2-py3-none-any.whl", hash = "sha256:d36ed1124bb81b32f8614555b34cc4259c3fbc7eec17870e8ff8ded335b58d8c"}, + {file = "black-24.4.2.tar.gz", hash = "sha256:c872b53057f000085da66a19c55d68f6f8ddcac2642392ad3a355878406fbd4d"}, +] + +[package.dependencies] +click = ">=8.0.0" +mypy-extensions = ">=0.4.3" +packaging = ">=22.0" +pathspec = ">=0.9.0" +platformdirs = ">=2" + +[package.extras] +colorama = ["colorama (>=0.4.3)"] +d = ["aiohttp (>=3.7.4)", "aiohttp (>=3.7.4,!=3.9.0)"] +jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] +uvloop = ["uvloop (>=0.15.2)"] + +[[package]] +name = "click" +version = "8.1.7" +description = "Composable command line interface toolkit" +optional = false +python-versions = ">=3.7" +files = [ + {file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"}, + {file = "click-8.1.7.tar.gz", hash = "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de"}, +] + +[package.dependencies] +colorama = {version = "*", markers = "platform_system == \"Windows\""} + +[[package]] +name = "colorama" +version = "0.4.6" +description = "Cross-platform colored terminal text." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,!=3.6.*,>=2.7" +files = [ + {file = "colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6"}, + {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, +] + +[[package]] +name = "flake8" +version = "7.0.0" +description = "the modular source code checker: pep8 pyflakes and co" +optional = false +python-versions = ">=3.8.1" +files = [ + {file = "flake8-7.0.0-py2.py3-none-any.whl", hash = "sha256:a6dfbb75e03252917f2473ea9653f7cd799c3064e54d4c8140044c5c065f53c3"}, + {file = "flake8-7.0.0.tar.gz", hash = "sha256:33f96621059e65eec474169085dc92bf26e7b2d47366b70be2f67ab80dc25132"}, +] + +[package.dependencies] +mccabe = ">=0.7.0,<0.8.0" +pycodestyle = ">=2.11.0,<2.12.0" +pyflakes = ">=3.2.0,<3.3.0" + +[[package]] +name = "isort" +version = "5.13.2" +description = "A Python utility / library to sort Python imports." +optional = false +python-versions = ">=3.8.0" +files = [ + {file = "isort-5.13.2-py3-none-any.whl", hash = "sha256:8ca5e72a8d85860d5a3fa69b8745237f2939afe12dbf656afbcb47fe72d947a6"}, + {file = "isort-5.13.2.tar.gz", hash = "sha256:48fdfcb9face5d58a4f6dde2e72a1fb8dcaf8ab26f95ab49fab84c2ddefb0109"}, +] + +[package.extras] +colors = ["colorama (>=0.4.6)"] + [[package]] name = "jsonschema" version = "4.22.0" @@ -54,6 +153,28 @@ files = [ [package.dependencies] referencing = ">=0.31.0" +[[package]] +name = "mccabe" +version = "0.7.0" +description = "McCabe checker, plugin for flake8" +optional = false +python-versions = ">=3.6" +files = [ + {file = "mccabe-0.7.0-py2.py3-none-any.whl", hash = "sha256:6c2d30ab6be0e4a46919781807b4f0d834ebdd6c6e3dca0bda5a15f863427b6e"}, + {file = "mccabe-0.7.0.tar.gz", hash = "sha256:348e0240c33b60bbdf4e523192ef919f28cb2c3d7d5c7794f74009290f236325"}, +] + +[[package]] +name = "mypy-extensions" +version = "1.0.0" +description = "Type system extensions for programs checked with the mypy type checker." +optional = false +python-versions = ">=3.5" +files = [ + {file = "mypy_extensions-1.0.0-py3-none-any.whl", hash = "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d"}, + {file = "mypy_extensions-1.0.0.tar.gz", hash = "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782"}, +] + [[package]] name = "ocpp" version = "2.0.0rc1" @@ -68,6 +189,66 @@ files = [ [package.dependencies] jsonschema = ">=4.4.0,<5.0.0" +[[package]] +name = "packaging" +version = "24.1" +description = "Core utilities for Python packages" +optional = false +python-versions = ">=3.8" +files = [ + {file = "packaging-24.1-py3-none-any.whl", hash = "sha256:5b8f2217dbdbd2f7f384c41c628544e6d52f2d0f53c6d0c3ea61aa5d1d7ff124"}, + {file = "packaging-24.1.tar.gz", hash = "sha256:026ed72c8ed3fcce5bf8950572258698927fd1dbda10a5e981cdf0ac37f4f002"}, +] + +[[package]] +name = "pathspec" +version = "0.12.1" +description = "Utility library for gitignore style pattern matching of file paths." +optional = false +python-versions = ">=3.8" +files = [ + {file = "pathspec-0.12.1-py3-none-any.whl", hash = "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08"}, + {file = "pathspec-0.12.1.tar.gz", hash = "sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712"}, +] + +[[package]] +name = "platformdirs" +version = "4.2.2" +description = "A small Python package for determining appropriate platform-specific dirs, e.g. a `user data dir`." +optional = false +python-versions = ">=3.8" +files = [ + {file = "platformdirs-4.2.2-py3-none-any.whl", hash = "sha256:2d7a1657e36a80ea911db832a8a6ece5ee53d8de21edd5cc5879af6530b1bfee"}, + {file = "platformdirs-4.2.2.tar.gz", hash = "sha256:38b7b51f512eed9e84a22788b4bce1de17c0adb134d6becb09836e37d8654cd3"}, +] + +[package.extras] +docs = ["furo (>=2023.9.10)", "proselint (>=0.13)", "sphinx (>=7.2.6)", "sphinx-autodoc-typehints (>=1.25.2)"] +test = ["appdirs (==1.4.4)", "covdefaults (>=2.3)", "pytest (>=7.4.3)", "pytest-cov (>=4.1)", "pytest-mock (>=3.12)"] +type = ["mypy (>=1.8)"] + +[[package]] +name = "pycodestyle" +version = "2.11.1" +description = "Python style guide checker" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pycodestyle-2.11.1-py2.py3-none-any.whl", hash = "sha256:44fe31000b2d866f2e41841b18528a505fbd7fef9017b04eff4e2648a0fadc67"}, + {file = "pycodestyle-2.11.1.tar.gz", hash = "sha256:41ba0e7afc9752dfb53ced5489e89f8186be00e599e712660695b7a75ff2663f"}, +] + +[[package]] +name = "pyflakes" +version = "3.2.0" +description = "passive checker of Python programs" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pyflakes-3.2.0-py2.py3-none-any.whl", hash = "sha256:84b5be138a2dfbb40689ca07e2152deb896a65c3a3e24c251c5c62489568074a"}, + {file = "pyflakes-3.2.0.tar.gz", hash = "sha256:1c61603ff154621fb2a9172037d84dca3500def8c8b630657d1701f026f8af3f"}, +] + [[package]] name = "referencing" version = "0.35.1" @@ -275,4 +456,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "e76eceb912dc1457ae116b3ecc0f0fc5a58ae23d2c2652dcd69b4cd9df207099" +content-hash = "1967ea6a8bdd559c8d930e2948996f1cd701e9c6f83b4fa17b86a05972b01817" diff --git a/tests/ocpp-server/pyproject.toml b/tests/ocpp-server/pyproject.toml index e1a30e5b..9b8a4e2d 100644 --- a/tests/ocpp-server/pyproject.toml +++ b/tests/ocpp-server/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "ocpp-server" version = "0.1.0" -description = "" +description = "OCPP2 mock server" authors = ["Jérôme Benoit "] readme = "README.md" @@ -10,6 +10,13 @@ python = "^3.12" websockets = "^12.0" ocpp = "^2.0.0rc1" +[tool.poetry.group.development.dependencies] +isort = "^5" +black = "^24" +flake8 = "^7" + +[tool.isort] +profile = 'black' [build-system] requires = ["poetry-core"] diff --git a/tests/ocpp-server/server.py b/tests/ocpp-server/server.py index 8c1c8dfb..15229a1d 100644 --- a/tests/ocpp-server/server.py +++ b/tests/ocpp-server/server.py @@ -6,16 +6,20 @@ from threading import Timer import ocpp.v201 import websockets from ocpp.routing import on -from ocpp.v201.enums import RegistrationStatusType, ClearCacheStatusType, AuthorizationStatusType, \ - TransactionEventType, \ - Action +from ocpp.v201.enums import ( + Action, + AuthorizationStatusType, + ClearCacheStatusType, + RegistrationStatusType, + TransactionEventType, +) # Setting up the logging configuration to display debug level messages. logging.basicConfig(level=logging.DEBUG) class RepeatTimer(Timer): - """ Class that inherits from the Timer class. It will run a + """Class that inherits from the Timer class. It will run a function at regular intervals.""" def run(self): @@ -34,18 +38,20 @@ class ChargePoint(ocpp.v201.ChargePoint): return ocpp.v201.call_result.BootNotification( current_time=datetime.now(timezone.utc).isoformat(), interval=60, - status=RegistrationStatusType.accepted + status=RegistrationStatusType.accepted, ) @on(Action.Heartbeat) async def on_heartbeat(self, **kwargs): logging.info("Received Heartbeat") - return ocpp.v201.call_result.Heartbeat(current_time=datetime.now(timezone.utc).isoformat()) + return ocpp.v201.call_result.Heartbeat( + current_time=datetime.now(timezone.utc).isoformat() + ) @on(Action.StatusNotification) - async def on_status_notification(self, timestamp, evse_id: int, connector_id: int, - connector_status, - **kwargs): + async def on_status_notification( + self, timestamp, evse_id: int, connector_id: int, connector_status, **kwargs + ): logging.info("Received StatusNotification") return ocpp.v201.call_result.StatusNotification() @@ -53,24 +59,28 @@ class ChargePoint(ocpp.v201.ChargePoint): async def on_authorize(self, id_token, **kwargs): logging.info("Received Authorize") return ocpp.v201.call_result.Authorize( - id_token_info={'status': AuthorizationStatusType.accepted} + id_token_info={"status": AuthorizationStatusType.accepted} ) @on(Action.TransactionEvent) - async def on_transaction_event(self, event_type: TransactionEventType, timestamp, - trigger_reason, seq_no: int, - transaction_info, **kwargs): + async def on_transaction_event( + self, + event_type: TransactionEventType, + timestamp, + trigger_reason, + seq_no: int, + transaction_info, + **kwargs, + ): match event_type: case TransactionEventType.started: logging.info("Received TransactionEvent Started") return ocpp.v201.call_result.TransactionEvent( - id_token_info={'status': AuthorizationStatusType.accepted} + id_token_info={"status": AuthorizationStatusType.accepted} ) case TransactionEventType.updated: logging.info("Received TransactionEvent Updated") - return ocpp.v201.call_result.TransactionEvent( - total_cost=10 - ) + return ocpp.v201.call_result.TransactionEvent(total_cost=10) case TransactionEventType.ended: logging.info("Received TransactionEvent Ended") return ocpp.v201.call_result.TransactionEvent() @@ -93,10 +103,10 @@ class ChargePoint(ocpp.v201.ChargePoint): # Function to handle new WebSocket connections. async def on_connect(websocket, path): - """ For every new charge point that connects, create a ChargePoint instance and start + """For every new charge point that connects, create a ChargePoint instance and start listening for messages.""" try: - requested_protocols = websocket.request_headers['Sec-WebSocket-Protocol'] + requested_protocols = websocket.request_headers["Sec-WebSocket-Protocol"] except KeyError: logging.info("Client hasn't requested any Subprotocol. Closing Connection") return await websocket.close() @@ -104,14 +114,15 @@ async def on_connect(websocket, path): if websocket.subprotocol: logging.info("Protocols Matched: %s", websocket.subprotocol) else: - logging.warning('Protocols Mismatched | Expected Subprotocols: %s,' - ' but client supports %s | Closing connection', - websocket.available_subprotocols, - requested_protocols - ) + logging.warning( + "Protocols Mismatched | Expected Subprotocols: %s," + " but client supports %s | Closing connection", + websocket.available_subprotocols, + requested_protocols, + ) return await websocket.close() - charge_point_id = path.strip('/') + charge_point_id = path.strip("/") cp = ChargePoint(charge_point_id, websocket) # Start the ChargePoint instance to listen for incoming messages. @@ -123,9 +134,9 @@ async def main(): # Create the WebSocket server and specify the handler for new connections. server = await websockets.serve( on_connect, - '127.0.0.1', # Listen on loopback. + "127.0.0.1", # Listen on loopback. 9000, # Port number. - subprotocols=['ocpp2.0', 'ocpp2.0.1'] # Specify OCPP 2.0.1 subprotocols. + subprotocols=["ocpp2.0", "ocpp2.0.1"], # Specify OCPP 2.0.1 subprotocols. ) logging.info("WebSocket Server Started") # Wait for the server to close (runs indefinitely). @@ -133,6 +144,6 @@ async def main(): # Entry point of the script. -if __name__ == '__main__': +if __name__ == "__main__": # Run the main function to start the server. asyncio.run(main()) -- 2.34.1