|
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) |
0 commit comments