Skip to content

Commit e6ffe6c

Browse files
committed
Expose TileSet parsing more directly (#67)
1 parent def5deb commit e6ffe6c

File tree

3 files changed

+90
-50
lines changed

3 files changed

+90
-50
lines changed

pytiled_parser/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .common_types import Color, OrderedPair, Size
1515
from .exception import UnknownFormat
1616
from .layer import Chunk, ImageLayer, Layer, LayerGroup, ObjectLayer, TileLayer
17-
from .parser import parse_map, parse_world
17+
from .parser import parse_map, parse_world, parse_tileset
1818
from .properties import Properties, Property
1919
from .tiled_map import TiledMap
2020
from .tileset import Frame, Grid, Tile, Tileset, Transformations

pytiled_parser/parser.py

Lines changed: 77 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,77 @@
1-
from pathlib import Path
2-
3-
from pytiled_parser import UnknownFormat
4-
from pytiled_parser.parsers.json.tiled_map import parse as json_map_parse
5-
from pytiled_parser.parsers.tmx.tiled_map import parse as tmx_map_parse
6-
from pytiled_parser.tiled_map import TiledMap
7-
from pytiled_parser.util import check_format
8-
from pytiled_parser.world import World
9-
from pytiled_parser.world import parse_world as _parse_world
10-
11-
12-
def parse_map(file: Path) -> TiledMap:
13-
"""Parse the raw Tiled map into a pytiled_parser type
14-
15-
Args:
16-
file: Path to the map file
17-
18-
Returns:
19-
TiledMap: A parsed and typed TiledMap
20-
"""
21-
parser = check_format(file)
22-
23-
# The type ignores are because mypy for some reason thinks those functions return Any
24-
if parser == "tmx":
25-
return tmx_map_parse(file) # type: ignore
26-
else:
27-
try:
28-
return json_map_parse(file) # type: ignore
29-
except ValueError:
30-
raise UnknownFormat(
31-
"Unknown Map Format, please use either the TMX or JSON format. "
32-
"This message could also mean your map file is invalid or corrupted."
33-
)
34-
35-
36-
def parse_world(file: Path) -> World:
37-
"""Parse the raw world file into a pytiled_parser type
38-
39-
Args:
40-
file: Path to the world file
41-
42-
Returns:
43-
World: A parsed and typed World
44-
"""
45-
return _parse_world(file)
1+
import json
2+
import xml.etree.ElementTree as etree
3+
from pathlib import Path
4+
5+
from pytiled_parser import UnknownFormat
6+
from pytiled_parser.parsers.json.tiled_map import parse as json_map_parse
7+
from pytiled_parser.parsers.json.tileset import parse as json_tileset_parse
8+
from pytiled_parser.parsers.tmx.tiled_map import parse as tmx_map_parse
9+
from pytiled_parser.parsers.tmx.tileset import parse as tmx_tileset_parse
10+
from pytiled_parser.tiled_map import TiledMap
11+
from pytiled_parser.tileset import Tileset
12+
from pytiled_parser.util import check_format
13+
from pytiled_parser.world import World
14+
from pytiled_parser.world import parse_world as _parse_world
15+
16+
17+
def parse_map(file: Path) -> TiledMap:
18+
"""Parse the raw Tiled map into a pytiled_parser type
19+
20+
Args:
21+
file: Path to the map file
22+
23+
Returns:
24+
TiledMap: A parsed and typed TiledMap
25+
"""
26+
parser = check_format(file)
27+
28+
# The type ignores are because mypy for some reason thinks those functions return Any
29+
if parser == "tmx":
30+
return tmx_map_parse(file) # type: ignore
31+
else:
32+
try:
33+
return json_map_parse(file) # type: ignore
34+
except ValueError:
35+
raise UnknownFormat(
36+
"Unknown Map Format, please use either the TMX or JSON format. "
37+
"This message could also mean your map file is invalid or corrupted."
38+
)
39+
40+
41+
def parse_tileset(file: Path) -> Tileset:
42+
"""Parse the raw Tiled Tileset into a pytiled_parser type
43+
44+
Args:
45+
file: Path to the map file
46+
47+
Returns:
48+
Tileset: A parsed and typed Tileset
49+
"""
50+
parser = check_format(file)
51+
52+
if parser == "tmx":
53+
with open(file) as map_file:
54+
raw_tileset = etree.parse(map_file).getroot()
55+
return tmx_tileset_parse(raw_tileset, 1)
56+
else:
57+
try:
58+
with open(file) as my_file:
59+
raw_tileset = json.load(my_file)
60+
return json_tileset_parse(raw_tileset, 1)
61+
except ValueError:
62+
raise UnknownFormat(
63+
"Unknowm Tileset Format, please use either the TSX or JSON format. "
64+
"This message could also mean your tileset file is invalid or corrupted."
65+
)
66+
67+
68+
def parse_world(file: Path) -> World:
69+
"""Parse the raw world file into a pytiled_parser type
70+
71+
Args:
72+
file: Path to the world file
73+
74+
Returns:
75+
World: A parsed and typed World
76+
"""
77+
return _parse_world(file)

tests/test_tileset.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import pytest
99

10+
from pytiled_parser import parse_tileset
1011
from pytiled_parser.common_types import OrderedPair, Size
1112
from pytiled_parser.parsers.json.tileset import parse as parse_json
1213
from pytiled_parser.parsers.tmx.tileset import parse as parse_tmx
@@ -59,12 +60,19 @@ def test_tilesets_integration(parser_type, tileset_dir):
5960

6061
if parser_type == "json":
6162
raw_tileset_path = tileset_dir / "tileset.json"
62-
with open(raw_tileset_path) as raw_tileset:
63-
tileset_ = parse_json(json.loads(raw_tileset.read()), 1)
6463
elif parser_type == "tmx":
6564
raw_tileset_path = tileset_dir / "tileset.tsx"
66-
with open(raw_tileset_path) as raw_tileset:
67-
tileset_ = parse_tmx(etree.parse(raw_tileset).getroot(), 1)
65+
66+
tileset_ = parse_tileset(raw_tileset_path)
67+
68+
# if parser_type == "json":
69+
# raw_tileset_path = tileset_dir / "tileset.json"
70+
# with open(raw_tileset_path) as raw_tileset:
71+
# tileset_ = parse_json(json.loads(raw_tileset.read()), 1)
72+
# elif parser_type == "tmx":
73+
# raw_tileset_path = tileset_dir / "tileset.tsx"
74+
# with open(raw_tileset_path) as raw_tileset:
75+
# tileset_ = parse_tmx(etree.parse(raw_tileset).getroot(), 1)
6876

6977
fix_tileset(tileset_)
7078
fix_tileset(expected.EXPECTED)

0 commit comments

Comments
 (0)