Skip to content

Commit

Permalink
try beartype
Browse files Browse the repository at this point in the history
  • Loading branch information
YajJackson committed Apr 13, 2024
1 parent 773f2aa commit 3800175
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
15 changes: 7 additions & 8 deletions godot_parser/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Optional,
Sequence,
Type,
TypeVar,
Union,
cast,
)
Expand Down Expand Up @@ -39,9 +38,6 @@
]


GDFileType = TypeVar("GDFileType", bound="GDFile")


class GodotFileException(Exception):
"""Thrown when there are errors in a Godot file"""

Expand Down Expand Up @@ -304,12 +300,12 @@ def get_node(self, path: str = ".") -> Optional[GDNodeSection]:
return node.section if node is not None else None

@classmethod
def parse(cls: Type[GDFileType], contents: str):
def parse(cls, contents: str):
"""Parse the contents of a Godot file"""
return cls.from_parser(scene_file.parse_string(contents, parseAll=True))

@classmethod
def load(cls: Type[GDFileType], filepath: str):
def load(cls, filepath: str):
with open(filepath, "r", encoding="utf-8") as ifile:
try:
file = cls.parse(ifile.read())
Expand All @@ -322,7 +318,7 @@ def load(cls: Type[GDFileType], filepath: str):
return file

@classmethod
def from_parser(cls: Type[GDFileType], parse_result):
def from_parser(cls, parse_result):
first_section = parse_result[0]
if first_section.header.name == "gd_scene":
scene = GDScene.__new__(GDScene)
Expand All @@ -334,7 +330,6 @@ def from_parser(cls: Type[GDFileType], parse_result):
return resource

return cls(*parse_result)
raise GodotFileException("Unknown GDFileType %s" % first_section.header.name)

def write(self, filename: str):
"""Writes this to a file"""
Expand Down Expand Up @@ -386,6 +381,7 @@ def remove_at(self, index: int):
section = self._sections.pop(index)
if section.header.name in ["ext_resource", "sub_resource"]:
self.load_steps -= 1
return section

def remove_unused_resources(self):
self._remove_unused_resources(self.get_ext_resources(), ExtResource)
Expand Down Expand Up @@ -460,3 +456,6 @@ def __init__(self, *sections: GDSection) -> None:
class GDResource(GDCommonFile):
def __init__(self, *sections: GDSection) -> None:
super().__init__("gd_resource", *sections)


GDFileType = Union[GDFile, GDScene, GDResource]
1 change: 1 addition & 0 deletions requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ black
isort
mypy
pylint==2.17.7
beartype
25 changes: 13 additions & 12 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import os
from typing import Optional, Union
from typing import Optional
import unittest

from pyparsing import ParseException

from beartype.door import is_bearable

from godot_parser import GDFile, GDObject, GDSection, GDSectionHeader, Vector2, parse
from godot_parser.files import GDResource, GDScene
from godot_parser.files import GDFileType

HERE = os.path.dirname(__file__)

TEST_CASES = [

TEST_CASES: list[tuple[str, GDFileType]] = [
(
"[gd_scene load_steps=5 format=2]",
GDFile(GDSection(GDSectionHeader("gd_scene", load_steps=5, format=2))),
Expand Down Expand Up @@ -51,7 +54,7 @@
GDSectionHeader("sub_resource", type="RectangleShape2D", id=1),
extents=Vector2(12.7855, 17.0634),
other=None,
**{"with spaces": 1}
**{"with spaces": 1},
)
),
),
Expand All @@ -71,7 +74,7 @@
"update": 0,
"values": [Vector2(0, 0), Vector2(1, 0)],
}
}
},
)
),
),
Expand Down Expand Up @@ -108,28 +111,26 @@
"0:0/0": 0,
"0:0/0/physics_layer_0/linear_velocity": Vector2(0, 0),
"0:0/0/physics_layer_0/angular_velocity": 0.0,
}
},
)
),
),
]


class TestParser(unittest.TestCase):
""" """

def _run_test(self, string: str, expected):
def _run_test(self, string: str, expected: GDFileType):
"""Run a set of tests"""
parse_result: Optional[Union[GDScene, GDResource]] = None
parse_result: Optional[GDFileType] = None
try:
parse_result = parse(string)
if expected == "error":
assert False, "Parsing '%s' should have failed.\nGot: %s" % (
string,
parse_result,
)
else:
self.assertEqual(parse_result, expected)
condition = is_bearable(parse_result, GDFileType)
assert condition, f"Expected {type(expected)}, got {type(parse_result)}"
except ParseException as e:
if expected != "error":
print(string)
Expand Down

0 comments on commit 3800175

Please sign in to comment.