From c03eda65bd3298a92c6a6a712d482e26325a17ea Mon Sep 17 00:00:00 2001 From: Jonah Jeleniewski Date: Tue, 19 Dec 2023 01:04:23 +1100 Subject: [PATCH] Rename `WalkPlayerServerPacket.Direction` field to `direction` --- CHANGELOG.md | 5 ++ eo-protocol | 2 +- mkdocs.yml | 1 + .../generate/field_code_generator.py | 51 +++++++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e46e441..ec48f28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- `WalkPlayerServerPacket.direction` field. + ### Changed - Remove trailing break from `ArenaSpecServerPacket`. - Remove trailing break from `ArenaAcceptServerPacket`. +- Deprecate `WalkPlayerServerPacket.Direction` field. ## [1.0.0] - 2023-11-07 diff --git a/eo-protocol b/eo-protocol index 255cd27..4324c3c 160000 --- a/eo-protocol +++ b/eo-protocol @@ -1 +1 @@ -Subproject commit 255cd2774f27b6f1b6a5dbe69b42a38d6255b47e +Subproject commit 4324c3c009ab76985375a007bc1e7e6aa8e47de5 diff --git a/mkdocs.yml b/mkdocs.yml index f8d2f27..eb494ef 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -54,4 +54,5 @@ plugins: summary: true filters: ["!^_[^_]", "!^__repr__$", "!^__all__$"] markdown_extensions: + - admonition - attr_list \ No newline at end of file diff --git a/protocol_code_generator/generate/field_code_generator.py b/protocol_code_generator/generate/field_code_generator.py index d53cded..aa3bebf 100644 --- a/protocol_code_generator/generate/field_code_generator.py +++ b/protocol_code_generator/generate/field_code_generator.py @@ -1,3 +1,4 @@ +from collections import namedtuple from protocol_code_generator.generate.code_block import CodeBlock from protocol_code_generator.generate.object_code_generator import FieldData from protocol_code_generator.type.basic_type import BasicType @@ -13,6 +14,19 @@ from protocol_code_generator.util.docstring_utils import generate_docstring from protocol_code_generator.util.number_utils import try_parse_int +DeprecatedField = namedtuple( + "DeprecatedField", ["type_name", "old_field_name", "new_field_name", "since"] +) + +DEPRECATED_FIELDS = [DeprecatedField("WalkPlayerServerPacket", "Direction", "direction", "1.1.0")] + + +def get_deprecated_field(type_name, field_name): + for field in DEPRECATED_FIELDS: + if field.type_name == type_name and field.new_field_name == field_name: + return field + return None + class FieldCodeGenerator: def __init__( @@ -233,6 +247,43 @@ def generate_field(self): setter.unindent() self._data.add_method(setter) + deprecated = get_deprecated_field(self._data.class_name, self._name) + if deprecated is not None: + old_name = deprecated.old_field_name + deprecated_docstring = ( + CodeBlock() + .add_line('"""') + .add_line('!!! warning "Deprecated"') + .add_line() + .add_line(f" Use `{self._name}` instead. (Deprecated since v{deprecated.since})") + .add_line('"""') + ) + deprecation_warning = ( + f"'{self._data.class_name}.{deprecated.old_field_name}' is deprecated as of " + f"{deprecated.since}, use '{self._name}' instead." + ) + self._data.add_method( + CodeBlock() + .add_line('@property') + .add_line(f'def {old_name}(self) -> {python_type_name}:') + .indent() + .add_code_block(deprecated_docstring) + .add_line(f'warn("{deprecation_warning}", DeprecationWarning, stacklevel=2)') + .add_line(f'return self.{self._name}') + .unindent() + .add_import("warn", "warnings") + ) + if self._hardcoded_value is None: + self._data.add_method( + CodeBlock() + .add_line(f'@{old_name}.setter') + .add_line(f'def {old_name}(self, {self._name}: {python_type_name}) -> None:') + .indent() + .add_code_block(deprecated_docstring) + .add_line(f'self.{self._name} = {self._name}') + .unindent() + ) + def generate_serialize(self): self._generate_serialize_missing_optional_guard() self._generate_serialize_none_not_allowed_error()