You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by hu...@apache.org on 2023/05/07 05:32:32 UTC

[plc4x] 01/03: feat(plc4py): Implement the WriteBuffer interface

This is an automated email from the ASF dual-hosted git repository.

hutcheb pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 9af6b0e69bc5536d626ee509fc8e99537e8e4228
Author: Ben Hutcheson <be...@gmail.com>
AuthorDate: Sun May 7 07:04:36 2023 +0200

    feat(plc4py): Implement the WriteBuffer interface
---
 .../plc4py/plc4py/spi/generation/WriteBuffer.py    | 98 ++++++++++++++++++++++
 sandbox/plc4py/plc4py/spi/generation/__init__.py   | 17 ++++
 sandbox/plc4py/plc4py/spi/values/common.py         | 20 +++++
 sandbox/plc4py/plc4py/utils/GenericTypes.py        | 11 +++
 4 files changed, 146 insertions(+)

diff --git a/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py b/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py
new file mode 100644
index 0000000000..f58c25d2dd
--- /dev/null
+++ b/sandbox/plc4py/plc4py/spi/generation/WriteBuffer.py
@@ -0,0 +1,98 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+
+from ctypes import *
+from dataclasses import dataclass
+from typing import List
+
+from plc4py.spi.values.common import Serializable
+from plc4py.utils.GenericTypes import ByteOrder, ByteOrderAware
+
+
+class PositionAware:
+
+    def get_pos(self) -> int:
+        raise NotImplementedError
+
+
+@dataclass
+class WriteBuffer(ByteOrderAware, PositionAware):
+    byte_order: ByteOrder
+
+    def get_pos(self) -> int:
+        raise NotImplementedError
+
+    def push_context(self, logical_name: str, **kwargs) -> None:
+        raise NotImplementedError
+
+    def pop_context(self, logical_name: str, **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_bit(self, value: c_bool, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_byte(self, value: c_byte, logical_name: str = "", **kwargs) -> None:
+        self.write_signed_byte(value, logical_name, kwargs)
+
+    def write_byte_array(self, value: List[c_byte], logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_unsigned_byte(self, value: c_ubyte, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_unsigned_short(self, value: c_uint16, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_unsigned_int(self, value: c_uint32, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_unsigned_long(self, value: c_uint64, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_signed_byte(self, value: c_byte, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_short(self, value: c_int16, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_int(self, value: c_int32, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_long(self, value: c_int64, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_float(self, value: c_float, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_double(self, value: c_double, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_str(self, value: str, logical_name: str = "", bit_length: int = -1, **kwargs) -> None:
+        raise NotImplementedError
+
+    def write_virtual(self, value: str, logical_name: str = "", **kwargs) -> None:
+        raise NotImplementedError
+
+
+    #
+    # This method can be used to influence serializing (e.g. intercept whole types and render them in a simplified form)
+    #
+    # @param value the value to be serialized
+    # @throws SerializationException if something goes wrong
+    #
+    def write_serializable(self, value: Serializable) -> None:
+        value.serialize(self)
diff --git a/sandbox/plc4py/plc4py/spi/generation/__init__.py b/sandbox/plc4py/plc4py/spi/generation/__init__.py
new file mode 100644
index 0000000000..c3434ab60d
--- /dev/null
+++ b/sandbox/plc4py/plc4py/spi/generation/__init__.py
@@ -0,0 +1,17 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+
diff --git a/sandbox/plc4py/plc4py/spi/values/common.py b/sandbox/plc4py/plc4py/spi/values/common.py
new file mode 100644
index 0000000000..2c76fbc8b8
--- /dev/null
+++ b/sandbox/plc4py/plc4py/spi/values/common.py
@@ -0,0 +1,20 @@
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+
+
+class Serializable:
+    pass
diff --git a/sandbox/plc4py/plc4py/utils/GenericTypes.py b/sandbox/plc4py/plc4py/utils/GenericTypes.py
index 863931f55b..9b46d976dc 100644
--- a/sandbox/plc4py/plc4py/utils/GenericTypes.py
+++ b/sandbox/plc4py/plc4py/utils/GenericTypes.py
@@ -16,6 +16,7 @@
 # specific language governing permissions and limitations
 # under the License.
 #
+from dataclasses import dataclass
 from enum import Enum, auto
 from typing import Generator
 
@@ -42,3 +43,13 @@ class ByteOrder(Enum):
 
     LITTLE_ENDIAN = auto()
     BIG_ENDIAN = auto()
+
+    def __new__(cls, value):
+        obj = object.__new__(cls)
+        obj._value_ = value
+        return obj
+
+
+@dataclass
+class ByteOrderAware:
+    byte_order: ByteOrder