From 91188a6e8f9d6bf7aa8de989ef7a6ab224e7210c Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Mon, 21 Apr 2025 16:17:51 +1000 Subject: [PATCH] Refactor magic numbers into CslaKnownTypes --- csla_binary/binary_reader.py | 49 ++++++++++++++++++------------------ csla_binary/known_types.py | 43 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 24 deletions(-) create mode 100644 csla_binary/known_types.py diff --git a/csla_binary/binary_reader.py b/csla_binary/binary_reader.py index 20005a5..0406a96 100644 --- a/csla_binary/binary_reader.py +++ b/csla_binary/binary_reader.py @@ -17,6 +17,7 @@ import io import struct +from .known_types import CslaKnownTypes from .serialization_info import ChildData, FieldData, SerializationInfo class CslaBinaryReader: @@ -109,79 +110,79 @@ class CslaBinaryReader: # CslaBinaryReader.ReadObject known_type = self.stream.read(1)[0] - if known_type == 1: # CslaKnownTypes.Boolean + if known_type == CslaKnownTypes.Boolean.value: raise NotImplementedError() - if known_type == 2: # CslaKnownTypes.Char + if known_type == CslaKnownTypes.Char.value: raise NotImplementedError() - if known_type == 3: # CslaKnownTypes.SByte + if known_type == CslaKnownTypes.SByte.value: raise NotImplementedError() - if known_type == 4: # CslaKnownTypes.Byte + if known_type == CslaKnownTypes.Byte.value: raise NotImplementedError() - if known_type == 5: # CslaKnownTypes.Int16 + if known_type == CslaKnownTypes.Int16.value: raise NotImplementedError() - if known_type == 6: # CslaKnownTypes.UInt16 + if known_type == CslaKnownTypes.UInt16.value: raise NotImplementedError() - if known_type == 7: # CslaKnownTypes.Int32 + if known_type == CslaKnownTypes.Int32.value: raise NotImplementedError() - if known_type == 8: # CslaKnownTypes.UInt32 + if known_type == CslaKnownTypes.UInt32.value: raise NotImplementedError() - if known_type == 9: # CslaKnownTypes.Int64 + if known_type == CslaKnownTypes.Int64.value: raise NotImplementedError() - if known_type == 10: # CslaKnownTypes.UInt64 + if known_type == CslaKnownTypes.UInt16.value: raise NotImplementedError() - if known_type == 11: # CslaKnownTypes.Single + if known_type == CslaKnownTypes.Single.value: raise NotImplementedError() - if known_type == 12: # CslaKnownTypes.Double + if known_type == CslaKnownTypes.Double.value: raise NotImplementedError() - if known_type == 13: # CslaKnownTypes.Decimal + if known_type == CslaKnownTypes.Decimal.value: raise NotImplementedError() - if known_type == 14: # CslaKnownTypes.DateTime + if known_type == CslaKnownTypes.DateTime.value: raise NotImplementedError() - if known_type == 15: # CslaKnownTypes.String + if known_type == CslaKnownTypes.String.value: return self.read_string() - if known_type == 16: # CslaKnownTypes.TimeSpan + if known_type == CslaKnownTypes.TimeSpan.value: raise NotImplementedError() - if known_type == 17: # CslaKnownTypes.DateTimeOffset + if known_type == CslaKnownTypes.DateTimeOffset.value: raise NotImplementedError() - if known_type == 18: # CslaKnownTypes.Guid + if known_type == CslaKnownTypes.Guid.value: raise NotImplementedError() - if known_type == 19: # CslaKnownTypes.ByteArray + if known_type == CslaKnownTypes.ByteArray.value: raise NotImplementedError() - if known_type == 20: # CslaKnownTypes.CharArray + if known_type == CslaKnownTypes.CharArray.value: raise NotImplementedError() - if known_type == 21: # CslaKnownTypes.ListOfInt + if known_type == CslaKnownTypes.ListOfInt.value: raise NotImplementedError() - if known_type == 22: # CslaKnownTypes.Null + if known_type == CslaKnownTypes.Null.value: raise NotImplementedError() - if known_type == 23: # CslaKnownTypes.StringWithDictionaryKey + if known_type == CslaKnownTypes.StringWithDictionaryKey.value: system_string = self.read_string() dictionary_key = self.read_int32() self.keywords_dictionary[dictionary_key] = system_string return system_string - if known_type == 24: # CslaKnownTypes.StringDictionaryKey + if known_type == CslaKnownTypes.StringDictionaryKey.value: raise NotImplementedError() raise ValueError('Unexpected object tag {}'.format(known_type)) diff --git a/csla_binary/known_types.py b/csla_binary/known_types.py new file mode 100644 index 0000000..3222d03 --- /dev/null +++ b/csla_binary/known_types.py @@ -0,0 +1,43 @@ +# pycsla-binary: Python implementation of CSLA .NET binary serialisation +# Copyright (C) 2025 Lee Yingtong Li (RunasSudo) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from enum import Enum + +class CslaKnownTypes(Enum): + Boolean = 1 + Char = 2 + SByte = 3 + Byte = 4 + Int16 = 5 + UInt16 = 6 + Int32 = 7 + UInt32 = 8 + Int64 = 9 + UInt64 = 10 + Single = 11 + Double = 12 + Decimal = 13 + DateTime = 14 + String = 15 + TimeSpan = 16 + DateTimeOffset = 17 + Guid = 18 + ByteArray = 19 + CharArray = 20 + ListOfInt = 21 + Null = 22 + StringWithDictionaryKey = 23 + StringDictionaryKey = 24